Skip to content

Commit 786a5f2

Browse files
committed
Add util_atomic_decrement and util_atomic_bool_compare_exchange
Add util_atomic_decrement and util_atomic_bool_compare_exchange to utils_concurrency.h. Signed-off-by: Lukasz Dorau <[email protected]>
1 parent deaf8d5 commit 786a5f2

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

src/utils/utils_concurrency.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ void util_mutex_destroy(os_mutex_t *mutex);
3838
int util_mutex_lock(os_mutex_t *mutex);
3939
int util_mutex_unlock(os_mutex_t *mutex);
4040

41-
#if defined(_WIN32)
41+
#if defined(_WIN32) /* Windows part starts here */
4242
static __inline unsigned char util_lssb_index(long long value) {
4343
unsigned long ret;
4444
_BitScanForward64(&ret, value);
@@ -60,7 +60,14 @@ static __inline unsigned char util_mssb_index(long long value) {
6060
InterlockedExchange64((LONG64 volatile *)object, (LONG64)desired)
6161
#define util_atomic_increment(object) \
6262
InterlockedIncrement64((LONG64 volatile *)object)
63-
#else
63+
#define util_atomic_decrement(object) \
64+
InterlockedDecrement64((LONG64 volatile *)object)
65+
#define util_atomic_bool_compare_exchange(object, oldval, newval) \
66+
(InterlockedCompareExchange64((LONG64 volatile *)object, (LONG64)newval, \
67+
(LONG64)oldval) == (LONG64)oldval)
68+
69+
#else /* NON-Windows part starts here */
70+
6471
#define util_lssb_index(x) ((unsigned char)__builtin_ctzll(x))
6572
#define util_mssb_index(x) ((unsigned char)(63 - __builtin_clzll(x)))
6673
#define util_atomic_load_acquire(object, dest) \
@@ -69,6 +76,10 @@ static __inline unsigned char util_mssb_index(long long value) {
6976
__atomic_store_n(object, desired, memory_order_release)
7077
#define util_atomic_increment(object) \
7178
__atomic_add_fetch(object, 1, __ATOMIC_ACQ_REL)
79+
#define util_atomic_decrement(object) \
80+
__atomic_sub_fetch(object, 1, __ATOMIC_ACQ_REL)
81+
#define util_atomic_bool_compare_exchange(object, oldval, newval) \
82+
__sync_bool_compare_and_swap(object, oldval, newval)
7283
#endif
7384

7485
#ifdef __cplusplus

0 commit comments

Comments
 (0)