Skip to content

Commit 89ef4cc

Browse files
committed
Add utils_compare_exchange_u8()
1 parent 46fafe4 commit 89ef4cc

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/utils/utils_concurrency.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,19 @@ static inline bool utils_compare_exchange_u64(uint64_t *ptr, uint64_t *expected,
171171
return false;
172172
}
173173

174+
static inline bool utils_compare_exchange_u8(uint8_t *ptr, uint8_t *expected,
175+
uint8_t *desired) {
176+
char out = _InterlockedCompareExchange8(
177+
(char volatile *)ptr, *(char *)desired, *(char *)expected);
178+
if (out == *(char *)expected) {
179+
return true;
180+
}
181+
182+
// else
183+
*expected = out;
184+
return false;
185+
}
186+
174187
#else // !defined(_WIN32)
175188

176189
static inline void utils_atomic_load_acquire_u64(uint64_t *ptr, uint64_t *out) {
@@ -241,6 +254,13 @@ static inline bool utils_compare_exchange_u64(uint64_t *ptr, uint64_t *expected,
241254
memory_order_relaxed);
242255
}
243256

257+
static inline bool utils_compare_exchange_u8(uint8_t *ptr, uint8_t *expected,
258+
uint8_t *desired) {
259+
return __atomic_compare_exchange(ptr, expected, desired, 0 /* strong */,
260+
memory_order_acq_rel,
261+
memory_order_relaxed);
262+
}
263+
244264
#endif // !defined(_WIN32)
245265

246266
static inline void utils_atomic_load_acquire_size_t(size_t *ptr, size_t *out) {

0 commit comments

Comments
 (0)