Skip to content

Commit 841c43c

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 aff94a2 commit 841c43c

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
@@ -36,7 +36,7 @@ void util_mutex_destroy_not_free(os_mutex_t *m);
3636
int util_mutex_lock(os_mutex_t *mutex);
3737
int util_mutex_unlock(os_mutex_t *mutex);
3838

39-
#if defined(_WIN32)
39+
#if defined(_WIN32) /* Windows part starts here */
4040
static __inline unsigned char util_lssb_index(long long value) {
4141
unsigned long ret;
4242
_BitScanForward64(&ret, value);
@@ -58,7 +58,14 @@ static __inline unsigned char util_mssb_index(long long value) {
5858
InterlockedExchange64((LONG64 volatile *)object, (LONG64)desired)
5959
#define util_atomic_increment(object) \
6060
InterlockedIncrement64((LONG64 volatile *)object)
61-
#else
61+
#define util_atomic_decrement(object) \
62+
InterlockedDecrement64((LONG64 volatile *)object)
63+
#define util_atomic_bool_compare_exchange(object, oldval, newval) \
64+
(InterlockedCompareExchange64((LONG64 volatile *)object, (LONG64)newval, \
65+
(LONG64)oldval) == (LONG64)oldval)
66+
67+
#else /* NON-Windows part starts here */
68+
6269
#define util_lssb_index(x) ((unsigned char)__builtin_ctzll(x))
6370
#define util_mssb_index(x) ((unsigned char)(63 - __builtin_clzll(x)))
6471
#define util_atomic_load_acquire(object, dest) \
@@ -67,6 +74,10 @@ static __inline unsigned char util_mssb_index(long long value) {
6774
__atomic_store_n(object, desired, memory_order_release)
6875
#define util_atomic_increment(object) \
6976
__atomic_add_fetch(object, 1, __ATOMIC_ACQ_REL)
77+
#define util_atomic_decrement(object) \
78+
__atomic_sub_fetch(object, 1, __ATOMIC_ACQ_REL)
79+
#define util_atomic_bool_compare_exchange(object, oldval, newval) \
80+
__sync_bool_compare_and_swap(object, oldval, newval)
7081
#endif
7182

7283
#ifdef __cplusplus

0 commit comments

Comments
 (0)