ObjFW
atomic_sync_builtins.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 static OF_INLINE int
17 of_atomic_int_add(volatile int *_Nonnull p, int i)
18 {
19  return __sync_add_and_fetch(p, i);
20 }
21 
22 static OF_INLINE int32_t
23 of_atomic_int32_add(volatile int32_t *_Nonnull p, int32_t i)
24 {
25  return __sync_add_and_fetch(p, i);
26 }
27 
28 static OF_INLINE void *_Nullable
29 of_atomic_ptr_add(void *volatile _Nullable *_Nonnull p, intptr_t i)
30 {
31  return __sync_add_and_fetch(p, (void *)i);
32 }
33 
34 static OF_INLINE int
35 of_atomic_int_sub(volatile int *_Nonnull p, int i)
36 {
37  return __sync_sub_and_fetch(p, i);
38 }
39 
40 static OF_INLINE int32_t
41 of_atomic_int32_sub(volatile int32_t *_Nonnull p, int32_t i)
42 {
43  return __sync_sub_and_fetch(p, i);
44 }
45 
46 static OF_INLINE void *_Nullable
47 of_atomic_ptr_sub(void *volatile _Nullable *_Nonnull p, intptr_t i)
48 {
49  return __sync_sub_and_fetch(p, (void *)i);
50 }
51 
52 static OF_INLINE int
53 of_atomic_int_inc(volatile int *_Nonnull p)
54 {
55  return __sync_add_and_fetch(p, 1);
56 }
57 
58 static OF_INLINE int32_t
59 of_atomic_int32_inc(volatile int32_t *_Nonnull p)
60 {
61  return __sync_add_and_fetch(p, 1);
62 }
63 
64 static OF_INLINE int
65 of_atomic_int_dec(volatile int *_Nonnull p)
66 {
67  return __sync_sub_and_fetch(p, 1);
68 }
69 
70 static OF_INLINE int32_t
71 of_atomic_int32_dec(volatile int32_t *_Nonnull p)
72 {
73  return __sync_sub_and_fetch(p, 1);
74 }
75 
76 static OF_INLINE unsigned int
77 of_atomic_int_or(volatile unsigned int *_Nonnull p, unsigned int i)
78 {
79  return __sync_or_and_fetch(p, i);
80 }
81 
82 static OF_INLINE uint32_t
83 of_atomic_int32_or(volatile uint32_t *_Nonnull p, uint32_t i)
84 {
85  return __sync_or_and_fetch(p, i);
86 }
87 
88 static OF_INLINE unsigned int
89 of_atomic_int_and(volatile unsigned int *_Nonnull p, unsigned int i)
90 {
91  return __sync_and_and_fetch(p, i);
92 }
93 
94 static OF_INLINE uint32_t
95 of_atomic_int32_and(volatile uint32_t *_Nonnull p, uint32_t i)
96 {
97  return __sync_and_and_fetch(p, i);
98 }
99 
100 static OF_INLINE unsigned int
101 of_atomic_int_xor(volatile unsigned int *_Nonnull p, unsigned int i)
102 {
103  return __sync_xor_and_fetch(p, i);
104 }
105 
106 static OF_INLINE uint32_t
107 of_atomic_int32_xor(volatile uint32_t *_Nonnull p, uint32_t i)
108 {
109  return __sync_xor_and_fetch(p, i);
110 }
111 
112 static OF_INLINE bool
113 of_atomic_int_cmpswap(volatile int *_Nonnull p, int o, int n)
114 {
115  return __sync_bool_compare_and_swap(p, o, n);
116 }
117 
118 static OF_INLINE bool
119 of_atomic_int32_cmpswap(volatile int32_t *_Nonnull p, int32_t o, int32_t n)
120 {
121  return __sync_bool_compare_and_swap(p, o, n);
122 }
123 
124 static OF_INLINE bool
125 of_atomic_ptr_cmpswap(void *volatile _Nullable *_Nonnull p,
126  void *_Nullable o, void *_Nullable n)
127 {
128  return __sync_bool_compare_and_swap(p, o, n);
129 }
130 
131 static OF_INLINE void
132 of_memory_barrier(void)
133 {
134  __sync_synchronize();
135 }
136 
137 static OF_INLINE void
138 of_memory_barrier_acquire(void)
139 {
140  __sync_synchronize();
141 }
142 
143 static OF_INLINE void
144 of_memory_barrier_release(void)
145 {
146  __sync_synchronize();
147 }