21 #ifdef OF_HAVE_OSATOMIC
22 # include <libkern/OSAtomic.h>
26 of_atomic_add_int(
volatile int *p,
int i)
28 #if !defined(OF_THREADS)
30 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
40 else if (
sizeof(
int) == 8)
53 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
54 return __sync_add_and_fetch(p, i);
55 #elif defined(OF_HAVE_OSATOMIC)
57 return OSAtomicAdd32Barrier(i, p);
58 # ifdef OF_HAVE_OSATOMIC_64
59 else if (
sizeof(
int) == 8)
60 return OSAtomicAdd64Barrier(i, p);
65 # error No atomic operations available!
69 static OF_INLINE int32_t
70 of_atomic_add_32(
volatile int32_t *p, int32_t i)
72 #if !defined(OF_THREADS)
74 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
84 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
85 return __sync_add_and_fetch(p, i);
86 #elif defined(OF_HAVE_OSATOMIC)
87 return OSAtomicAdd32Barrier(i, p);
89 # error No atomic operations available!
93 static OF_INLINE
void*
94 of_atomic_add_ptr(
void*
volatile *p, intptr_t i)
96 #if !defined(OF_THREADS)
97 return (*(
char*
volatile*)p += i);
98 #elif defined(OF_X86_ASM)
108 #elif defined(OF_AMD64_ASM)
118 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
119 return __sync_add_and_fetch(p, (
void*)i);
120 #elif defined(OF_HAVE_OSATOMIC)
121 if (
sizeof(
void*) == 4)
122 return (
void*)OSAtomicAdd32Barrier(i, (int32_t*)p);
123 # ifdef OF_HAVE_OSATOMIC_64
124 else if (
sizeof(
void*) == 8)
125 return (
void*)OSAtomicAdd64Barrier(i, (int64_t*)p);
130 # error No atomic operations available!
135 of_atomic_sub_int(
volatile int *p,
int i)
137 #if !defined(OF_THREADS)
139 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
140 if (
sizeof(
int) == 4)
150 else if (
sizeof(
int) == 8)
164 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
165 return __sync_sub_and_fetch(p, i);
166 #elif defined(OF_HAVE_OSATOMIC)
167 if (
sizeof(
int) == 4)
168 return OSAtomicAdd32Barrier(-i, p);
169 # ifdef OF_HAVE_OSATOMIC_64
170 else if (
sizeof(
int) == 8)
171 return OSAtomicAdd64Barrier(-i, p);
176 # error No atomic operations available!
180 static OF_INLINE int32_t
181 of_atomic_sub_32(
volatile int32_t *p, int32_t i)
183 #if !defined(OF_THREADS)
185 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
196 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
197 return __sync_sub_and_fetch(p, i);
198 #elif defined(OF_HAVE_OSATOMIC)
199 return OSAtomicAdd32Barrier(-i, p);
201 # error No atomic operations available!
205 static OF_INLINE
void*
206 of_atomic_sub_ptr(
void*
volatile *p, intptr_t i)
208 #if !defined(OF_THREADS)
209 return (*(
char*
volatile*)p -= i);
210 #elif defined(OF_X86_ASM)
221 #elif defined(OF_AMD64_ASM)
232 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
233 return __sync_sub_and_fetch(p, (
void*)i);
234 #elif defined(OF_HAVE_OSATOMIC)
235 if (
sizeof(
void*) == 4)
236 return (
void*)OSAtomicAdd32Barrier(-i, (int32_t*)p);
237 # ifdef OF_HAVE_OSATOMIC_64
238 else if (
sizeof(
void*) == 8)
239 return (
void*)OSAtomicAdd64Barrier(-i, (int64_t*)p);
244 # error No atomic operations available!
249 of_atomic_inc_int(
volatile int *p)
251 #if !defined(OF_THREADS)
253 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
256 if (
sizeof(
int) == 4)
267 else if (
sizeof(
int) == 8)
282 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
283 return __sync_add_and_fetch(p, 1);
284 #elif defined(OF_HAVE_OSATOMIC)
285 if (
sizeof(
int) == 4)
286 return OSAtomicIncrement32Barrier(p);
287 # ifdef OF_HAVE_OSATOMIC_64
288 else if (
sizeof(
int) == 8)
289 return OSAtomicDecrement64Barrier(p);
294 # error No atomic operations available!
298 static OF_INLINE int32_t
299 of_atomic_inc_32(
volatile int32_t *p)
301 #if !defined(OF_THREADS)
303 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
317 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
318 return __sync_add_and_fetch(p, 1);
319 #elif defined(OF_HAVE_OSATOMIC)
320 return OSAtomicIncrement32Barrier(p);
322 # error No atomic operations available!
327 of_atomic_dec_int(
volatile int *p)
329 #if !defined(OF_THREADS)
331 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
334 if (
sizeof(
int) == 4)
345 else if (
sizeof(
int) == 8)
360 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
361 return __sync_sub_and_fetch(p, 1);
362 #elif defined(OF_HAVE_OSATOMIC)
363 if (
sizeof(
int) == 4)
364 return OSAtomicDecrement32Barrier(p);
365 # ifdef OF_HAVE_OSATOMIC_64
366 else if (
sizeof(
int) == 8)
367 return OSAtomicDecrement64Barrier(p);
372 # error No atomic operations available!
376 static OF_INLINE int32_t
377 of_atomic_dec_32(
volatile int32_t *p)
379 #if !defined(OF_THREADS)
381 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
395 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
396 return __sync_sub_and_fetch(p, 1);
397 #elif defined(OF_HAVE_OSATOMIC)
398 return OSAtomicDecrement32Barrier(p);
400 # error No atomic operations available!
404 static OF_INLINE
unsigned int
405 of_atomic_or_int(
volatile unsigned int *p,
unsigned int i)
407 #if !defined(OF_THREADS)
409 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
410 if (
sizeof(
int) == 4)
424 if (
sizeof(
int) == 8)
442 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
443 return __sync_or_and_fetch(p, i);
444 #elif defined(OF_HAVE_OSATOMIC)
445 if (
sizeof(
int) == 4)
446 return OSAtomicOr32Barrier(i, p);
447 # ifdef OF_HAVE_OSATOMIC_64
448 else if (
sizeof(
int) == 8)
449 return OSAtomicOr64Barrier(i, p);
454 # error No atomic operations available!
458 static OF_INLINE uint32_t
459 of_atomic_or_32(
volatile uint32_t *p, uint32_t i)
461 #if !defined(OF_THREADS)
463 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
478 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
479 return __sync_or_and_fetch(p, i);
480 #elif defined(OF_HAVE_OSATOMIC)
481 return OSAtomicOr32Barrier(i, p);
483 # error No atomic operations available!
487 static OF_INLINE
unsigned int
488 of_atomic_and_int(
volatile unsigned int *p,
unsigned int i)
490 #if !defined(OF_THREADS)
492 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
493 if (
sizeof(
int) == 4)
507 if (
sizeof(
int) == 8)
525 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
526 return __sync_and_and_fetch(p, i);
527 #elif defined(OF_HAVE_OSATOMIC)
528 if (
sizeof(
int) == 4)
529 return OSAtomicAnd32Barrier(i, p);
530 # ifdef OF_HAVE_OSATOMIC_64
531 else if (
sizeof(
int) == 8)
532 return OSAtomicAnd64Barrier(i, p);
537 # error No atomic operations available!
541 static OF_INLINE uint32_t
542 of_atomic_and_32(
volatile uint32_t *p, uint32_t i)
544 #if !defined(OF_THREADS)
546 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
561 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
562 return __sync_and_and_fetch(p, i);
563 #elif defined(OF_HAVE_OSATOMIC)
564 return OSAtomicAnd32Barrier(i, p);
566 # error No atomic operations available!
570 static OF_INLINE
unsigned int
571 of_atomic_xor_int(
volatile unsigned int *p,
unsigned int i)
573 #if !defined(OF_THREADS)
575 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
576 if (
sizeof(
int) == 4)
590 if (
sizeof(
int) == 8)
608 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
609 return __sync_xor_and_fetch(p, i);
610 #elif defined(OF_HAVE_OSATOMIC)
611 if (
sizeof(
int) == 4)
612 return OSAtomicXor32Barrier(i, p);
613 # ifdef OF_HAVE_OSATOMIC_64
614 else (
sizeof(
int) == 8)
615 return OSAtomicXor64Barrier(i, p);
620 # error No atomic operations available!
624 static OF_INLINE uint32_t
625 of_atomic_xor_32(
volatile uint32_t *p, uint32_t i)
627 #if !defined(OF_THREADS)
629 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
636 "cmpxchgl %0, %2\n\t"
644 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
645 return __sync_xor_and_fetch(p, i);
646 #elif defined(OF_HAVE_OSATOMIC)
647 return OSAtomicXor32Barrier(i, p);
649 # error No atomic operations available!
653 static OF_INLINE BOOL
654 of_atomic_cmpswap_int(
volatile int *p,
int o,
int n)
656 #if !defined(OF_THREADS)
663 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
673 :
"a"(o),
"r"(n),
"m"(*p)
678 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
679 return __sync_bool_compare_and_swap(p, o, n);
680 #elif defined(OF_HAVE_OSATOMIC)
681 return OSAtomicCompareAndSwapIntBarrier(o, n, p);
683 # error No atomic operations available!
687 static OF_INLINE BOOL
688 of_atomic_cmpswap_32(
volatile int32_t *p, int32_t o, int32_t n)
690 #if !defined(OF_THREADS)
697 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
707 :
"a"(o),
"r"(n),
"m"(*p)
712 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
713 return __sync_bool_compare_and_swap(p, o, n);
714 #elif defined(OF_HAVE_OSATOMIC)
715 return OSAtomicCompareAndSwap32Barrier(o, n, p);
717 # error No atomic operations available!
721 static OF_INLINE BOOL
722 of_atomic_cmpswap_ptr(
void*
volatile *p,
void *o,
void *n)
724 #if !defined(OF_THREADS)
731 #elif defined(OF_X86_ASM) || defined(OF_AMD64_ASM)
741 :
"a"(o),
"r"(n),
"m"(*p)
746 #elif defined(OF_HAVE_GCC_ATOMIC_OPS)
747 return __sync_bool_compare_and_swap(p, o, n);
748 #elif defined(OF_HAVE_OSATOMIC)
749 return OSAtomicCompareAndSwapPtrBarrier(o, n, p);
751 # error No atomic operations available!