xref: /minix3/sys/external/bsd/compiler_rt/dist/lib/builtins/atomic.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*===-- atomic.c - Implement support functions for atomic operations.------===
2*0a6a1f1dSLionel Sambuc  *
3*0a6a1f1dSLionel Sambuc  *                     The LLVM Compiler Infrastructure
4*0a6a1f1dSLionel Sambuc  *
5*0a6a1f1dSLionel Sambuc  * This file is dual licensed under the MIT and the University of Illinois Open
6*0a6a1f1dSLionel Sambuc  * Source Licenses. See LICENSE.TXT for details.
7*0a6a1f1dSLionel Sambuc  *
8*0a6a1f1dSLionel Sambuc  *===----------------------------------------------------------------------===
9*0a6a1f1dSLionel Sambuc  *
10*0a6a1f1dSLionel Sambuc  *  atomic.c defines a set of functions for performing atomic accesses on
11*0a6a1f1dSLionel Sambuc  *  arbitrary-sized memory locations.  This design uses locks that should
12*0a6a1f1dSLionel Sambuc  *  be fast in the uncontended case, for two reasons:
13*0a6a1f1dSLionel Sambuc  *
14*0a6a1f1dSLionel Sambuc  *  1) This code must work with C programs that do not link to anything
15*0a6a1f1dSLionel Sambuc  *     (including pthreads) and so it should not depend on any pthread
16*0a6a1f1dSLionel Sambuc  *     functions.
17*0a6a1f1dSLionel Sambuc  *  2) Atomic operations, rather than explicit mutexes, are most commonly used
18*0a6a1f1dSLionel Sambuc  *     on code where contended operations are rate.
19*0a6a1f1dSLionel Sambuc  *
20*0a6a1f1dSLionel Sambuc  *  To avoid needing a per-object lock, this code allocates an array of
21*0a6a1f1dSLionel Sambuc  *  locks and hashes the object pointers to find the one that it should use.
22*0a6a1f1dSLionel Sambuc  *  For operations that must be atomic on two locations, the lower lock is
23*0a6a1f1dSLionel Sambuc  *  always acquired first, to avoid deadlock.
24*0a6a1f1dSLionel Sambuc  *
25*0a6a1f1dSLionel Sambuc  *===----------------------------------------------------------------------===
26*0a6a1f1dSLionel Sambuc  */
27*0a6a1f1dSLionel Sambuc 
28*0a6a1f1dSLionel Sambuc #include <stdint.h>
29*0a6a1f1dSLionel Sambuc #include <string.h>
30*0a6a1f1dSLionel Sambuc 
31*0a6a1f1dSLionel Sambuc // Clang objects if you redefine a builtin.  This little hack allows us to
32*0a6a1f1dSLionel Sambuc // define a function with the same name as an intrinsic.
33*0a6a1f1dSLionel Sambuc #if __APPLE__
34*0a6a1f1dSLionel Sambuc // mach-o has extra leading underscore
35*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_load_c ___atomic_load
36*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_store_c ___atomic_store
37*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_exchange_c ___atomic_exchange
38*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_compare_exchange_c ___atomic_compare_exchange
39*0a6a1f1dSLionel Sambuc #else
40*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_load_c __atomic_load
41*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_store_c __atomic_store
42*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_exchange_c __atomic_exchange
43*0a6a1f1dSLionel Sambuc #pragma redefine_extname __atomic_compare_exchange_c __atomic_compare_exchange
44*0a6a1f1dSLionel Sambuc #endif
45*0a6a1f1dSLionel Sambuc 
46*0a6a1f1dSLionel Sambuc /// Number of locks.  This allocates one page on 32-bit platforms, two on
47*0a6a1f1dSLionel Sambuc /// 64-bit.  This can be specified externally if a different trade between
48*0a6a1f1dSLionel Sambuc /// memory usage and contention probability is required for a given platform.
49*0a6a1f1dSLionel Sambuc #ifndef SPINLOCK_COUNT
50*0a6a1f1dSLionel Sambuc #define SPINLOCK_COUNT (1<<10)
51*0a6a1f1dSLionel Sambuc #endif
52*0a6a1f1dSLionel Sambuc static const long SPINLOCK_MASK = SPINLOCK_COUNT - 1;
53*0a6a1f1dSLionel Sambuc 
54*0a6a1f1dSLionel Sambuc ////////////////////////////////////////////////////////////////////////////////
55*0a6a1f1dSLionel Sambuc // Platform-specific lock implementation.  Falls back to spinlocks if none is
56*0a6a1f1dSLionel Sambuc // defined.  Each platform should define the Lock type, and corresponding
57*0a6a1f1dSLionel Sambuc // lock() and unlock() functions.
58*0a6a1f1dSLionel Sambuc ////////////////////////////////////////////////////////////////////////////////
59*0a6a1f1dSLionel Sambuc #ifdef __FreeBSD__
60*0a6a1f1dSLionel Sambuc #include <errno.h>
61*0a6a1f1dSLionel Sambuc #include <sys/types.h>
62*0a6a1f1dSLionel Sambuc #include <machine/atomic.h>
63*0a6a1f1dSLionel Sambuc #include <sys/umtx.h>
64*0a6a1f1dSLionel Sambuc typedef struct _usem Lock;
unlock(Lock * l)65*0a6a1f1dSLionel Sambuc inline static void unlock(Lock *l) {
66*0a6a1f1dSLionel Sambuc   __c11_atomic_store((_Atomic(uint32_t)*)&l->_count, 1, __ATOMIC_RELEASE);
67*0a6a1f1dSLionel Sambuc   __c11_atomic_thread_fence(__ATOMIC_SEQ_CST);
68*0a6a1f1dSLionel Sambuc   if (l->_has_waiters)
69*0a6a1f1dSLionel Sambuc       _umtx_op(l, UMTX_OP_SEM_WAKE, 1, 0, 0);
70*0a6a1f1dSLionel Sambuc }
lock(Lock * l)71*0a6a1f1dSLionel Sambuc inline static void lock(Lock *l) {
72*0a6a1f1dSLionel Sambuc   uint32_t old = 1;
73*0a6a1f1dSLionel Sambuc   while (!__c11_atomic_compare_exchange_weak((_Atomic(uint32_t)*)&l->_count, &old,
74*0a6a1f1dSLionel Sambuc         0, __ATOMIC_ACQUIRE, __ATOMIC_RELAXED)) {
75*0a6a1f1dSLionel Sambuc     _umtx_op(l, UMTX_OP_SEM_WAIT, 0, 0, 0);
76*0a6a1f1dSLionel Sambuc     old = 1;
77*0a6a1f1dSLionel Sambuc   }
78*0a6a1f1dSLionel Sambuc }
79*0a6a1f1dSLionel Sambuc /// locks for atomic operations
80*0a6a1f1dSLionel Sambuc static Lock locks[SPINLOCK_COUNT] = { [0 ...  SPINLOCK_COUNT-1] = {0,1,0} };
81*0a6a1f1dSLionel Sambuc 
82*0a6a1f1dSLionel Sambuc #elif defined(__APPLE__)
83*0a6a1f1dSLionel Sambuc #include <libkern/OSAtomic.h>
84*0a6a1f1dSLionel Sambuc typedef OSSpinLock Lock;
unlock(Lock * l)85*0a6a1f1dSLionel Sambuc inline static void unlock(Lock *l) {
86*0a6a1f1dSLionel Sambuc   OSSpinLockUnlock(l);
87*0a6a1f1dSLionel Sambuc }
88*0a6a1f1dSLionel Sambuc /// Locks a lock.  In the current implementation, this is potentially
89*0a6a1f1dSLionel Sambuc /// unbounded in the contended case.
lock(Lock * l)90*0a6a1f1dSLionel Sambuc inline static void lock(Lock *l) {
91*0a6a1f1dSLionel Sambuc   OSSpinLockLock(l);
92*0a6a1f1dSLionel Sambuc }
93*0a6a1f1dSLionel Sambuc static Lock locks[SPINLOCK_COUNT]; // initialized to OS_SPINLOCK_INIT which is 0
94*0a6a1f1dSLionel Sambuc 
95*0a6a1f1dSLionel Sambuc #else
96*0a6a1f1dSLionel Sambuc typedef _Atomic(uintptr_t) Lock;
97*0a6a1f1dSLionel Sambuc /// Unlock a lock.  This is a release operation.
unlock(Lock * l)98*0a6a1f1dSLionel Sambuc inline static void unlock(Lock *l) {
99*0a6a1f1dSLionel Sambuc   __c11_atomic_store(l, 0, __ATOMIC_RELEASE);
100*0a6a1f1dSLionel Sambuc }
101*0a6a1f1dSLionel Sambuc /// Locks a lock.  In the current implementation, this is potentially
102*0a6a1f1dSLionel Sambuc /// unbounded in the contended case.
lock(Lock * l)103*0a6a1f1dSLionel Sambuc inline static void lock(Lock *l) {
104*0a6a1f1dSLionel Sambuc   uintptr_t old = 0;
105*0a6a1f1dSLionel Sambuc   while (!__c11_atomic_compare_exchange_weak(l, &old, 1, __ATOMIC_ACQUIRE,
106*0a6a1f1dSLionel Sambuc         __ATOMIC_RELAXED))
107*0a6a1f1dSLionel Sambuc     old = 0;
108*0a6a1f1dSLionel Sambuc }
109*0a6a1f1dSLionel Sambuc /// locks for atomic operations
110*0a6a1f1dSLionel Sambuc static Lock locks[SPINLOCK_COUNT];
111*0a6a1f1dSLionel Sambuc #endif
112*0a6a1f1dSLionel Sambuc 
113*0a6a1f1dSLionel Sambuc 
114*0a6a1f1dSLionel Sambuc /// Returns a lock to use for a given pointer.
lock_for_pointer(void * ptr)115*0a6a1f1dSLionel Sambuc static inline Lock *lock_for_pointer(void *ptr) {
116*0a6a1f1dSLionel Sambuc   intptr_t hash = (intptr_t)ptr;
117*0a6a1f1dSLionel Sambuc   // Disregard the lowest 4 bits.  We want all values that may be part of the
118*0a6a1f1dSLionel Sambuc   // same memory operation to hash to the same value and therefore use the same
119*0a6a1f1dSLionel Sambuc   // lock.
120*0a6a1f1dSLionel Sambuc   hash >>= 4;
121*0a6a1f1dSLionel Sambuc   // Use the next bits as the basis for the hash
122*0a6a1f1dSLionel Sambuc   intptr_t low = hash & SPINLOCK_MASK;
123*0a6a1f1dSLionel Sambuc   // Now use the high(er) set of bits to perturb the hash, so that we don't
124*0a6a1f1dSLionel Sambuc   // get collisions from atomic fields in a single object
125*0a6a1f1dSLionel Sambuc   hash >>= 16;
126*0a6a1f1dSLionel Sambuc   hash ^= low;
127*0a6a1f1dSLionel Sambuc   // Return a pointer to the word to use
128*0a6a1f1dSLionel Sambuc   return locks + (hash & SPINLOCK_MASK);
129*0a6a1f1dSLionel Sambuc }
130*0a6a1f1dSLionel Sambuc 
131*0a6a1f1dSLionel Sambuc /// Macros for determining whether a size is lock free.  Clang can not yet
132*0a6a1f1dSLionel Sambuc /// codegen __atomic_is_lock_free(16), so for now we assume 16-byte values are
133*0a6a1f1dSLionel Sambuc /// not lock free.
134*0a6a1f1dSLionel Sambuc #define IS_LOCK_FREE_1 __c11_atomic_is_lock_free(1)
135*0a6a1f1dSLionel Sambuc #define IS_LOCK_FREE_2 __c11_atomic_is_lock_free(2)
136*0a6a1f1dSLionel Sambuc #define IS_LOCK_FREE_4 __c11_atomic_is_lock_free(4)
137*0a6a1f1dSLionel Sambuc #define IS_LOCK_FREE_8 __c11_atomic_is_lock_free(8)
138*0a6a1f1dSLionel Sambuc #define IS_LOCK_FREE_16 0
139*0a6a1f1dSLionel Sambuc 
140*0a6a1f1dSLionel Sambuc /// Macro that calls the compiler-generated lock-free versions of functions
141*0a6a1f1dSLionel Sambuc /// when they exist.
142*0a6a1f1dSLionel Sambuc #define LOCK_FREE_CASES() \
143*0a6a1f1dSLionel Sambuc   do {\
144*0a6a1f1dSLionel Sambuc   switch (size) {\
145*0a6a1f1dSLionel Sambuc     case 2:\
146*0a6a1f1dSLionel Sambuc       if (IS_LOCK_FREE_2) {\
147*0a6a1f1dSLionel Sambuc         LOCK_FREE_ACTION(uint16_t);\
148*0a6a1f1dSLionel Sambuc       }\
149*0a6a1f1dSLionel Sambuc     case 4:\
150*0a6a1f1dSLionel Sambuc       if (IS_LOCK_FREE_4) {\
151*0a6a1f1dSLionel Sambuc         LOCK_FREE_ACTION(uint32_t);\
152*0a6a1f1dSLionel Sambuc       }\
153*0a6a1f1dSLionel Sambuc     case 8:\
154*0a6a1f1dSLionel Sambuc       if (IS_LOCK_FREE_8) {\
155*0a6a1f1dSLionel Sambuc         LOCK_FREE_ACTION(uint64_t);\
156*0a6a1f1dSLionel Sambuc       }\
157*0a6a1f1dSLionel Sambuc     case 16:\
158*0a6a1f1dSLionel Sambuc       if (IS_LOCK_FREE_16) {\
159*0a6a1f1dSLionel Sambuc         /* FIXME: __uint128_t isn't available on 32 bit platforms.
160*0a6a1f1dSLionel Sambuc         LOCK_FREE_ACTION(__uint128_t);*/\
161*0a6a1f1dSLionel Sambuc       }\
162*0a6a1f1dSLionel Sambuc   }\
163*0a6a1f1dSLionel Sambuc   } while (0)
164*0a6a1f1dSLionel Sambuc 
165*0a6a1f1dSLionel Sambuc 
166*0a6a1f1dSLionel Sambuc /// An atomic load operation.  This is atomic with respect to the source
167*0a6a1f1dSLionel Sambuc /// pointer only.
__atomic_load_c(int size,void * src,void * dest,int model)168*0a6a1f1dSLionel Sambuc void __atomic_load_c(int size, void *src, void *dest, int model) {
169*0a6a1f1dSLionel Sambuc #define LOCK_FREE_ACTION(type) \
170*0a6a1f1dSLionel Sambuc     *((type*)dest) = __c11_atomic_load((_Atomic(type)*)src, model);\
171*0a6a1f1dSLionel Sambuc     return;
172*0a6a1f1dSLionel Sambuc   LOCK_FREE_CASES();
173*0a6a1f1dSLionel Sambuc #undef LOCK_FREE_ACTION
174*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(src);
175*0a6a1f1dSLionel Sambuc   lock(l);
176*0a6a1f1dSLionel Sambuc   memcpy(dest, src, size);
177*0a6a1f1dSLionel Sambuc   unlock(l);
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc 
180*0a6a1f1dSLionel Sambuc /// An atomic store operation.  This is atomic with respect to the destination
181*0a6a1f1dSLionel Sambuc /// pointer only.
__atomic_store_c(int size,void * dest,void * src,int model)182*0a6a1f1dSLionel Sambuc void __atomic_store_c(int size, void *dest, void *src, int model) {
183*0a6a1f1dSLionel Sambuc #define LOCK_FREE_ACTION(type) \
184*0a6a1f1dSLionel Sambuc     __c11_atomic_store((_Atomic(type)*)dest, *(type*)dest, model);\
185*0a6a1f1dSLionel Sambuc     return;
186*0a6a1f1dSLionel Sambuc   LOCK_FREE_CASES();
187*0a6a1f1dSLionel Sambuc #undef LOCK_FREE_ACTION
188*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(dest);
189*0a6a1f1dSLionel Sambuc   lock(l);
190*0a6a1f1dSLionel Sambuc   memcpy(dest, src, size);
191*0a6a1f1dSLionel Sambuc   unlock(l);
192*0a6a1f1dSLionel Sambuc }
193*0a6a1f1dSLionel Sambuc 
194*0a6a1f1dSLionel Sambuc /// Atomic compare and exchange operation.  If the value at *ptr is identical
195*0a6a1f1dSLionel Sambuc /// to the value at *expected, then this copies value at *desired to *ptr.  If
196*0a6a1f1dSLionel Sambuc /// they  are not, then this stores the current value from *ptr in *expected.
197*0a6a1f1dSLionel Sambuc ///
198*0a6a1f1dSLionel Sambuc /// This function returns 1 if the exchange takes place or 0 if it fails.
__atomic_compare_exchange_c(int size,void * ptr,void * expected,void * desired,int success,int failure)199*0a6a1f1dSLionel Sambuc int __atomic_compare_exchange_c(int size, void *ptr, void *expected,
200*0a6a1f1dSLionel Sambuc     void *desired, int success, int failure) {
201*0a6a1f1dSLionel Sambuc #define LOCK_FREE_ACTION(type) \
202*0a6a1f1dSLionel Sambuc   return __c11_atomic_compare_exchange_strong((_Atomic(type)*)ptr, (type*)expected,\
203*0a6a1f1dSLionel Sambuc       *(type*)desired, success, failure)
204*0a6a1f1dSLionel Sambuc   LOCK_FREE_CASES();
205*0a6a1f1dSLionel Sambuc #undef LOCK_FREE_ACTION
206*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(ptr);
207*0a6a1f1dSLionel Sambuc   lock(l);
208*0a6a1f1dSLionel Sambuc   if (memcmp(ptr, expected, size) == 0) {
209*0a6a1f1dSLionel Sambuc     memcpy(ptr, desired, size);
210*0a6a1f1dSLionel Sambuc     unlock(l);
211*0a6a1f1dSLionel Sambuc     return 1;
212*0a6a1f1dSLionel Sambuc   }
213*0a6a1f1dSLionel Sambuc   memcpy(expected, ptr, size);
214*0a6a1f1dSLionel Sambuc   unlock(l);
215*0a6a1f1dSLionel Sambuc   return 0;
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc 
218*0a6a1f1dSLionel Sambuc /// Performs an atomic exchange operation between two pointers.  This is atomic
219*0a6a1f1dSLionel Sambuc /// with respect to the target address.
__atomic_exchange_c(int size,void * ptr,void * val,void * old,int model)220*0a6a1f1dSLionel Sambuc void __atomic_exchange_c(int size, void *ptr, void *val, void *old, int model) {
221*0a6a1f1dSLionel Sambuc #define LOCK_FREE_ACTION(type) \
222*0a6a1f1dSLionel Sambuc     *(type*)old = __c11_atomic_exchange((_Atomic(type)*)ptr, *(type*)val,\
223*0a6a1f1dSLionel Sambuc         model);\
224*0a6a1f1dSLionel Sambuc     return;
225*0a6a1f1dSLionel Sambuc   LOCK_FREE_CASES();
226*0a6a1f1dSLionel Sambuc #undef LOCK_FREE_ACTION
227*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(ptr);
228*0a6a1f1dSLionel Sambuc   lock(l);
229*0a6a1f1dSLionel Sambuc   memcpy(old, ptr, size);
230*0a6a1f1dSLionel Sambuc   memcpy(ptr, val, size);
231*0a6a1f1dSLionel Sambuc   unlock(l);
232*0a6a1f1dSLionel Sambuc }
233*0a6a1f1dSLionel Sambuc 
234*0a6a1f1dSLionel Sambuc ////////////////////////////////////////////////////////////////////////////////
235*0a6a1f1dSLionel Sambuc // Where the size is known at compile time, the compiler may emit calls to
236*0a6a1f1dSLionel Sambuc // specialised versions of the above functions.
237*0a6a1f1dSLionel Sambuc ////////////////////////////////////////////////////////////////////////////////
238*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASES\
239*0a6a1f1dSLionel Sambuc   OPTIMISED_CASE(1, IS_LOCK_FREE_1, uint8_t)\
240*0a6a1f1dSLionel Sambuc   OPTIMISED_CASE(2, IS_LOCK_FREE_2, uint16_t)\
241*0a6a1f1dSLionel Sambuc   OPTIMISED_CASE(4, IS_LOCK_FREE_4, uint32_t)\
242*0a6a1f1dSLionel Sambuc   OPTIMISED_CASE(8, IS_LOCK_FREE_8, uint64_t)\
243*0a6a1f1dSLionel Sambuc   /* FIXME: __uint128_t isn't available on 32 bit platforms.
244*0a6a1f1dSLionel Sambuc   OPTIMISED_CASE(16, IS_LOCK_FREE_16, __uint128_t)*/\
245*0a6a1f1dSLionel Sambuc 
246*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type)\
247*0a6a1f1dSLionel Sambuc type __atomic_load_##n(type *src, int model) {\
248*0a6a1f1dSLionel Sambuc   if (lockfree)\
249*0a6a1f1dSLionel Sambuc     return __c11_atomic_load((_Atomic(type)*)src, model);\
250*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(src);\
251*0a6a1f1dSLionel Sambuc   lock(l);\
252*0a6a1f1dSLionel Sambuc   type val = *src;\
253*0a6a1f1dSLionel Sambuc   unlock(l);\
254*0a6a1f1dSLionel Sambuc   return val;\
255*0a6a1f1dSLionel Sambuc }
256*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
257*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
258*0a6a1f1dSLionel Sambuc 
259*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type)\
260*0a6a1f1dSLionel Sambuc void  __atomic_store_##n(type *dest, type val, int model) {\
261*0a6a1f1dSLionel Sambuc   if (lockfree) {\
262*0a6a1f1dSLionel Sambuc     __c11_atomic_store((_Atomic(type)*)dest, val, model);\
263*0a6a1f1dSLionel Sambuc     return;\
264*0a6a1f1dSLionel Sambuc   }\
265*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(dest);\
266*0a6a1f1dSLionel Sambuc   lock(l);\
267*0a6a1f1dSLionel Sambuc   *dest = val;\
268*0a6a1f1dSLionel Sambuc   unlock(l);\
269*0a6a1f1dSLionel Sambuc   return;\
270*0a6a1f1dSLionel Sambuc }
271*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
272*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
273*0a6a1f1dSLionel Sambuc 
274*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type)\
275*0a6a1f1dSLionel Sambuc type __atomic_exchange_##n(type *dest, type val, int model) {\
276*0a6a1f1dSLionel Sambuc   if (lockfree)\
277*0a6a1f1dSLionel Sambuc     return __c11_atomic_exchange((_Atomic(type)*)dest, val, model);\
278*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(dest);\
279*0a6a1f1dSLionel Sambuc   lock(l);\
280*0a6a1f1dSLionel Sambuc   type tmp = *dest;\
281*0a6a1f1dSLionel Sambuc   *dest = val;\
282*0a6a1f1dSLionel Sambuc   unlock(l);\
283*0a6a1f1dSLionel Sambuc   return tmp;\
284*0a6a1f1dSLionel Sambuc }
285*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
286*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
287*0a6a1f1dSLionel Sambuc 
288*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type)\
289*0a6a1f1dSLionel Sambuc int __atomic_compare_exchange_##n(type *ptr, type *expected, type desired,\
290*0a6a1f1dSLionel Sambuc     int success, int failure) {\
291*0a6a1f1dSLionel Sambuc   if (lockfree)\
292*0a6a1f1dSLionel Sambuc     return __c11_atomic_compare_exchange_strong((_Atomic(type)*)ptr, expected, desired,\
293*0a6a1f1dSLionel Sambuc         success, failure);\
294*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(ptr);\
295*0a6a1f1dSLionel Sambuc   lock(l);\
296*0a6a1f1dSLionel Sambuc   if (*ptr == *expected) {\
297*0a6a1f1dSLionel Sambuc     *ptr = desired;\
298*0a6a1f1dSLionel Sambuc     unlock(l);\
299*0a6a1f1dSLionel Sambuc     return 1;\
300*0a6a1f1dSLionel Sambuc   }\
301*0a6a1f1dSLionel Sambuc   *expected = *ptr;\
302*0a6a1f1dSLionel Sambuc   unlock(l);\
303*0a6a1f1dSLionel Sambuc   return 0;\
304*0a6a1f1dSLionel Sambuc }
305*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
306*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
307*0a6a1f1dSLionel Sambuc 
308*0a6a1f1dSLionel Sambuc ////////////////////////////////////////////////////////////////////////////////
309*0a6a1f1dSLionel Sambuc // Atomic read-modify-write operations for integers of various sizes.
310*0a6a1f1dSLionel Sambuc ////////////////////////////////////////////////////////////////////////////////
311*0a6a1f1dSLionel Sambuc #define ATOMIC_RMW(n, lockfree, type, opname, op) \
312*0a6a1f1dSLionel Sambuc type __atomic_fetch_##opname##_##n(type *ptr, type val, int model) {\
313*0a6a1f1dSLionel Sambuc   if (lockfree) \
314*0a6a1f1dSLionel Sambuc     return __c11_atomic_fetch_##opname((_Atomic(type)*)ptr, val, model);\
315*0a6a1f1dSLionel Sambuc   Lock *l = lock_for_pointer(ptr);\
316*0a6a1f1dSLionel Sambuc   lock(l);\
317*0a6a1f1dSLionel Sambuc   type tmp = *ptr;\
318*0a6a1f1dSLionel Sambuc   *ptr = tmp op val;\
319*0a6a1f1dSLionel Sambuc   unlock(l);\
320*0a6a1f1dSLionel Sambuc   return tmp;\
321*0a6a1f1dSLionel Sambuc }
322*0a6a1f1dSLionel Sambuc 
323*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, add, +)
324*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
325*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
326*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, sub, -)
327*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
328*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
329*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, and, &)
330*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
331*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
332*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, or, |)
333*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
334*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
335*0a6a1f1dSLionel Sambuc #define OPTIMISED_CASE(n, lockfree, type) ATOMIC_RMW(n, lockfree, type, xor, ^)
336*0a6a1f1dSLionel Sambuc OPTIMISED_CASES
337*0a6a1f1dSLionel Sambuc #undef OPTIMISED_CASE
338