ObjFW
atomic_osatomic.h
1 /*
2  * Copyright (c) 2008-2021 Jonathan Schleifer <js@nil.im>
3  *
4  * All rights reserved.
5  *
6  * This file is part of ObjFW. It may be distributed under the terms of the
7  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
8  * the packaging of this file.
9  *
10  * Alternatively, it may be distributed under the terms of the GNU General
11  * Public License, either version 2 or 3, which can be found in the file
12  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
13  * file.
14  */
15 
16 #include <libkern/OSAtomic.h>
17 
18 static OF_INLINE int
19 of_atomic_int_add(volatile int *_Nonnull p, int i)
20 {
21  return OSAtomicAdd32(i, p);
22 }
23 
24 static OF_INLINE int32_t
25 of_atomic_int32_add(volatile int32_t *_Nonnull p, int32_t i)
26 {
27  return OSAtomicAdd32(i, p);
28 }
29 
30 static OF_INLINE void *_Nullable
31 of_atomic_ptr_add(void *volatile _Nullable *_Nonnull p, intptr_t i)
32 {
33 #ifdef __LP64__
34  return (void *)OSAtomicAdd64(i, (int64_t *)p);
35 #else
36  return (void *)OSAtomicAdd32(i, (int32_t *)p);
37 #endif
38 }
39 
40 static OF_INLINE int
41 of_atomic_int_sub(volatile int *_Nonnull p, int i)
42 {
43  return OSAtomicAdd32(-i, p);
44 }
45 
46 static OF_INLINE int32_t
47 of_atomic_int32_sub(volatile int32_t *_Nonnull p, int32_t i)
48 {
49  return OSAtomicAdd32(-i, p);
50 }
51 
52 static OF_INLINE void *_Nullable
53 of_atomic_ptr_sub(void *volatile _Nullable *_Nonnull p, intptr_t i)
54 {
55 #ifdef __LP64__
56  return (void *)OSAtomicAdd64(-i, (int64_t *)p);
57 #else
58  return (void *)OSAtomicAdd32(-i, (int32_t *)p);
59 #endif
60 }
61 
62 static OF_INLINE int
63 of_atomic_int_inc(volatile int *_Nonnull p)
64 {
65  return OSAtomicIncrement32(p);
66 }
67 
68 static OF_INLINE int32_t
69 of_atomic_int32_inc(volatile int32_t *_Nonnull p)
70 {
71  return OSAtomicIncrement32(p);
72 }
73 
74 static OF_INLINE int
75 of_atomic_int_dec(volatile int *_Nonnull p)
76 {
77  return OSAtomicDecrement32(p);
78 }
79 
80 static OF_INLINE int32_t
81 of_atomic_int32_dec(volatile int32_t *_Nonnull p)
82 {
83  return OSAtomicDecrement32(p);
84 }
85 
86 static OF_INLINE unsigned int
87 of_atomic_int_or(volatile unsigned int *_Nonnull p, unsigned int i)
88 {
89  return OSAtomicOr32(i, p);
90 }
91 
92 static OF_INLINE uint32_t
93 of_atomic_int32_or(volatile uint32_t *_Nonnull p, uint32_t i)
94 {
95  return OSAtomicOr32(i, p);
96 }
97 
98 static OF_INLINE unsigned int
99 of_atomic_int_and(volatile unsigned int *_Nonnull p, unsigned int i)
100 {
101  return OSAtomicAnd32(i, p);
102 }
103 
104 static OF_INLINE uint32_t
105 of_atomic_int32_and(volatile uint32_t *_Nonnull p, uint32_t i)
106 {
107  return OSAtomicAnd32(i, p);
108 }
109 
110 static OF_INLINE unsigned int
111 of_atomic_int_xor(volatile unsigned int *_Nonnull p, unsigned int i)
112 {
113  return OSAtomicXor32(i, p);
114 }
115 
116 static OF_INLINE uint32_t
117 of_atomic_int32_xor(volatile uint32_t *_Nonnull p, uint32_t i)
118 {
119  return OSAtomicXor32(i, p);
120 }
121 
122 static OF_INLINE bool
123 of_atomic_int_cmpswap(volatile int *_Nonnull p, int o, int n)
124 {
125  return OSAtomicCompareAndSwapInt(o, n, p);
126 }
127 
128 static OF_INLINE bool
129 of_atomic_int32_cmpswap(volatile int32_t *_Nonnull p, int32_t o, int32_t n)
130 {
131  return OSAtomicCompareAndSwap32(o, n, p);
132 }
133 
134 static OF_INLINE bool
135 of_atomic_ptr_cmpswap(void *volatile _Nullable *_Nonnull p,
136  void *_Nullable o, void *_Nullable n)
137 {
138  return OSAtomicCompareAndSwapPtr(o, n, p);
139 }
140 
141 static OF_INLINE void
142 of_memory_barrier(void)
143 {
144  OSMemoryBarrier();
145 }
146 
147 static OF_INLINE void
148 of_memory_barrier_acquire(void)
149 {
150  OSMemoryBarrier();
151 }
152 
153 static OF_INLINE void
154 of_memory_barrier_release(void)
155 {
156  OSMemoryBarrier();
157 }