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