18typedef atomic_t atomic_val_t;
39static inline int atomic_cas(atomic_t *target, atomic_val_t old_value,
40 atomic_val_t new_value)
42 return __atomic_compare_exchange_n(target, &old_value, new_value,
58static inline atomic_val_t atomic_add(atomic_t *target, atomic_val_t value)
60 return __atomic_fetch_add(target, value, __ATOMIC_SEQ_CST);
75static inline atomic_val_t atomic_sub(atomic_t *target, atomic_val_t value)
77 return __atomic_fetch_sub(target, value, __ATOMIC_SEQ_CST);
91static inline atomic_val_t atomic_inc(atomic_t *target)
93 return atomic_add(target, 1);
107static inline atomic_val_t atomic_dec(atomic_t *target)
109 return atomic_sub(target, 1);
123static inline atomic_val_t atomic_get(
const atomic_t *target)
125 return __atomic_load_n(target, __ATOMIC_SEQ_CST);
141static inline atomic_val_t atomic_set(atomic_t *target, atomic_val_t value)
147 return __atomic_exchange_n(target, value, __ATOMIC_SEQ_CST);
162static inline atomic_val_t atomic_clear(atomic_t *target)
164 return atomic_set(target, 0);
180static inline atomic_val_t atomic_or(atomic_t *target, atomic_val_t value)
182 return __atomic_fetch_or(target, value, __ATOMIC_SEQ_CST);
198static inline atomic_val_t atomic_xor(atomic_t *target, atomic_val_t value)
200 return __atomic_fetch_xor(target, value, __ATOMIC_SEQ_CST);
216static inline atomic_val_t atomic_and(atomic_t *target, atomic_val_t value)
218 return __atomic_fetch_and(target, value, __ATOMIC_SEQ_CST);
234static inline atomic_val_t atomic_nand(atomic_t *target, atomic_val_t value)
236 return __atomic_fetch_nand(target, value, __ATOMIC_SEQ_CST);
247#define ATOMIC_INIT(i) (i)
253#define ATOMIC_BITS (sizeof(atomic_val_t) * 8)
254#define ATOMIC_MASK(bit) (1 << ((bit) & (ATOMIC_BITS - 1)))
255#define ATOMIC_ELEM(addr, bit) ((addr) + ((bit) / ATOMIC_BITS))
274#define ATOMIC_DEFINE(name, num_bits) \
275 atomic_t name[1 + ((num_bits) - 1) / ATOMIC_BITS]
289 atomic_test_bit(
const atomic_t *target,
int bit)
291 atomic_val_t val = atomic_get(ATOMIC_ELEM(target, bit));
293 return (1 & (val >> (bit & (ATOMIC_BITS - 1))));
308 atomic_test_and_clear_bit(atomic_t *target,
int bit)
310 atomic_val_t mask = ATOMIC_MASK(bit);
313 old = atomic_and(ATOMIC_ELEM(target, bit), ~mask);
315 return (old & mask) != 0;
330 atomic_test_and_set_bit(atomic_t *target,
int bit)
332 atomic_val_t mask = ATOMIC_MASK(bit);
335 old = atomic_or(ATOMIC_ELEM(target, bit), mask);
337 return (old & mask) != 0;
352 atomic_clear_bit(atomic_t *target,
int bit)
354 atomic_val_t mask = ATOMIC_MASK(bit);
356 atomic_and(ATOMIC_ELEM(target, bit), ~mask);
371 atomic_set_bit(atomic_t *target,
int bit)
373 atomic_val_t mask = ATOMIC_MASK(bit);
375 atomic_or(ATOMIC_ELEM(target, bit), mask);
390static inline void atomic_set_bit_to(atomic_t *target,
int bit,
bool val)
392 atomic_val_t mask = ATOMIC_MASK(bit);
395 (void)atomic_or(ATOMIC_ELEM(target, bit), mask);
397 (void)atomic_and(ATOMIC_ELEM(target, bit), ~mask);