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