ObjFW
atomic_no_threads.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 (*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 (*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 (*(char *volatile *)p += i);
32 }
33 
34 static OF_INLINE int
35 of_atomic_int_sub(volatile int *_Nonnull p, int i)
36 {
37  return (*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 (*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 (*(char *volatile *)p -= i);
50 }
51 
52 static OF_INLINE int
53 of_atomic_int_inc(volatile int *_Nonnull p)
54 {
55  return ++*p;
56 }
57 
58 static OF_INLINE int32_t
59 of_atomic_int32_inc(volatile int32_t *_Nonnull p)
60 {
61  return ++*p;
62 }
63 
64 static OF_INLINE int
65 of_atomic_int_dec(volatile int *_Nonnull p)
66 {
67  return --*p;
68 }
69 
70 static OF_INLINE int32_t
71 of_atomic_int32_dec(volatile int32_t *_Nonnull p)
72 {
73  return --*p;
74 }
75 
76 static OF_INLINE unsigned int
77 of_atomic_int_or(volatile unsigned int *_Nonnull p, unsigned int i)
78 {
79  return (*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 (*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 (*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 (*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 (*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 (*p ^= i);
110 }
111 
112 static OF_INLINE bool
113 of_atomic_int_cmpswap(volatile int *_Nonnull p, int o, int n)
114 {
115  if (*p == o) {
116  *p = n;
117  return true;
118  }
119 
120  return false;
121 }
122 
123 static OF_INLINE bool
124 of_atomic_int32_cmpswap(volatile int32_t *_Nonnull p, int32_t o, int32_t n)
125 {
126  if (*p == o) {
127  *p = n;
128  return true;
129  }
130 
131  return false;
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  if (*p == o) {
139  *p = n;
140  return true;
141  }
142 
143  return false;
144 }
145 
146 static OF_INLINE void
147 of_memory_barrier(void)
148 {
149  /* nop */
150 }
151 
152 static OF_INLINE void
153 of_memory_barrier_acquire(void)
154 {
155  /* nop */
156 }
157 
158 static OF_INLINE void
159 of_memory_barrier_release(void)
160 {
161  /* nop */
162 }