Skip to content

Commit d525125

Browse files
bratpiorkarbanka1
authored andcommitted
add utils_compare_exchange function
1 parent 21aa44e commit d525125

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

src/utils/utils_concurrency.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ int utils_mutex_unlock(utils_mutex_t *mutex);
6161
void utils_init_once(UTIL_ONCE_FLAG *flag, void (*onceCb)(void));
6262

6363
#if defined(_WIN32)
64+
6465
static __inline unsigned char utils_lssb_index(long long value) {
6566
unsigned long ret;
6667
_BitScanForward64(&ret, value);
6768
return (unsigned char)ret;
6869
}
70+
6971
static __inline unsigned char utils_mssb_index(long long value) {
7072
unsigned long ret;
7173
_BitScanReverse64(&ret, value);
@@ -81,15 +83,25 @@ static __inline unsigned char utils_mssb_index(long long value) {
8183

8284
#define utils_atomic_store_release(object, desired) \
8385
InterlockedExchange64((LONG64 volatile *)object, (LONG64)desired)
86+
8487
#define utils_atomic_increment(object) \
8588
InterlockedIncrement64((LONG64 volatile *)object)
89+
8690
#define utils_atomic_decrement(object) \
8791
InterlockedDecrement64((LONG64 volatile *)object)
92+
8893
#define utils_fetch_and_add64(ptr, value) \
8994
InterlockedExchangeAdd64((LONG64 *)(ptr), value)
90-
#else
95+
96+
// NOTE: windows version have different order of args
97+
#define utils_compare_exchange(object, desired, expected) \
98+
InterlockedCompareExchange64((LONG64 volatile *)object, *expected, *desired)
99+
100+
#else // !defined(_WIN32)
101+
91102
#define utils_lssb_index(x) ((unsigned char)__builtin_ctzll(x))
92103
#define utils_mssb_index(x) ((unsigned char)(63 - __builtin_clzll(x)))
104+
93105
#define utils_atomic_load_acquire(object, dest) \
94106
do { \
95107
utils_annotate_acquire((void *)object); \
@@ -103,12 +115,19 @@ static __inline unsigned char utils_mssb_index(long long value) {
103115
} while (0)
104116

105117
#define utils_atomic_increment(object) \
106-
__atomic_add_fetch(object, 1, __ATOMIC_ACQ_REL)
118+
__atomic_add_fetch(object, 1, memory_order_acq_rel)
119+
107120
#define utils_atomic_decrement(object) \
108-
__atomic_sub_fetch(object, 1, __ATOMIC_ACQ_REL)
109-
#define utils_fetch_and_add64 __sync_fetch_and_add
121+
__atomic_sub_fetch(object, 1, memory_order_acq_rel)
110122

111-
#endif
123+
#define utils_fetch_and_add64(object, value) \
124+
__atomic_fetch_add(object, value, memory_order_acq_rel)
125+
126+
#define utils_compare_exchange(object, expected, desired) \
127+
__atomic_compare_exchange(object, expected, desired, 0 /* strong */, \
128+
memory_order_acq_rel, memory_order_relaxed)
129+
130+
#endif // !defined(_WIN32)
112131

113132
#ifdef __cplusplus
114133
}

0 commit comments

Comments
 (0)