Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
4 | | All rights reserved. | | ||
5 | | | | ||
6 | | Redistribution and use in source and binary forms, with or without | | ||
7 | | modification, are permitted provided that the following conditions are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | |||
29 | #include "gate/atomics.hpp" | ||
30 | #include "gate/memalloc.h" | ||
31 | |||
32 | namespace gate | ||
33 | { | ||
34 | 2 | AtomicFlag::AtomicFlag(bool init) noexcept | |
35 | { | ||
36 | 2 | impl.value = 0; | |
37 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (init) |
38 | { | ||
39 | 1 | this->set(); | |
40 | } | ||
41 | 2 | } | |
42 | 2 | AtomicFlag::~AtomicFlag() noexcept | |
43 | { | ||
44 | 2 | } | |
45 | |||
46 | 1 | void AtomicFlag::clear() noexcept | |
47 | { | ||
48 | 1 | gate_atomic_flag_clear(const_cast<gate_atomic_flag_t*>(&this->impl)); | |
49 | 1 | } | |
50 | 8 | bool_t AtomicFlag::set() noexcept | |
51 | { | ||
52 | 8 | return gate_atomic_flag_set(&this->impl); | |
53 | } | ||
54 | |||
55 | |||
56 | |||
57 | |||
58 | |||
59 | |||
60 | |||
61 | 11 | AtomicInt::AtomicInt(int32_t init) noexcept | |
62 | { | ||
63 | 11 | gate_atomic_int_init(&this->impl, init); | |
64 | 11 | } | |
65 | 1 | AtomicInt::AtomicInt(AtomicInt const& src) noexcept | |
66 | { | ||
67 | 1 | gate_atomic_int_init(&this->impl, src.get()); | |
68 | 1 | } | |
69 | 1 | AtomicInt& AtomicInt::operator=(AtomicInt const& src) noexcept | |
70 | { | ||
71 | 1 | gate_atomic_int_set(&this->impl, src.get()); | |
72 | 1 | return *this; | |
73 | } | ||
74 | 12 | AtomicInt::~AtomicInt() noexcept | |
75 | { | ||
76 | 12 | } | |
77 | |||
78 | 27 | int32_t AtomicInt::get() const noexcept | |
79 | { | ||
80 | 27 | return gate_atomic_int_get(const_cast<gate_atomic_int_t*>(&this->impl)); | |
81 | } | ||
82 | 7 | int32_t AtomicInt::set(int32_t newvalue) noexcept | |
83 | { | ||
84 | 7 | return gate_atomic_int_set(&this->impl, newvalue); | |
85 | } | ||
86 | 4 | int32_t AtomicInt::changeIf(int32_t comparand, int32_t newvalue) noexcept | |
87 | { | ||
88 | 4 | return gate_atomic_int_xchg_if(&this->impl, comparand, newvalue); | |
89 | } | ||
90 | 3 | bool_t AtomicInt::updateIf(int32_t fromvalue, int32_t tovalue) noexcept | |
91 | { | ||
92 | 3 | return gate_atomic_int_update_if(&this->impl, fromvalue, tovalue); | |
93 | } | ||
94 | |||
95 | 17 | int32_t AtomicInt::operator++() noexcept | |
96 | { | ||
97 | 17 | return gate_atomic_int_inc(&this->impl); | |
98 | } | ||
99 | 1 | int32_t AtomicInt::operator++(int) noexcept | |
100 | { | ||
101 | 1 | return gate_atomic_int_inc(&this->impl) - 1; | |
102 | } | ||
103 | 16 | int32_t AtomicInt::operator--() noexcept | |
104 | { | ||
105 | 16 | return gate_atomic_int_dec(&this->impl); | |
106 | } | ||
107 | 1 | int32_t AtomicInt::operator--(int) noexcept | |
108 | { | ||
109 | 1 | return gate_atomic_int_dec(&this->impl) + 1; | |
110 | } | ||
111 | |||
112 | 1 | int32_t AtomicInt::operator+=(int32_t value) noexcept | |
113 | { | ||
114 | 1 | return gate_atomic_int_add(&this->impl, value); | |
115 | } | ||
116 | 1 | int32_t AtomicInt::operator-=(int32_t value) noexcept | |
117 | { | ||
118 | 1 | return gate_atomic_int_add(&this->impl, -value); | |
119 | } | ||
120 | |||
121 | |||
122 | |||
123 | |||
124 | |||
125 | 1 | AtomicInt64::AtomicInt64(int64_t init) noexcept | |
126 | { | ||
127 | 1 | gate_atomic_int64_init(&this->impl, init); | |
128 | 1 | } | |
129 | 1 | AtomicInt64::AtomicInt64(AtomicInt64 const& src) noexcept | |
130 | { | ||
131 | 1 | gate_atomic_int64_init(&this->impl, src.get()); | |
132 | 1 | } | |
133 | 1 | AtomicInt64& AtomicInt64::operator=(AtomicInt64 const& src) noexcept | |
134 | { | ||
135 | 1 | gate_atomic_int64_set(&this->impl, src.get()); | |
136 | 1 | return *this; | |
137 | } | ||
138 | 2 | AtomicInt64::~AtomicInt64() noexcept | |
139 | { | ||
140 | 2 | } | |
141 | |||
142 | 8 | int64_t AtomicInt64::get() const noexcept | |
143 | { | ||
144 | 8 | return gate_atomic_int64_get(const_cast<gate_atomic_int64_t*>(&this->impl)); | |
145 | } | ||
146 | 2 | int64_t AtomicInt64::set(int64_t newvalue) noexcept | |
147 | { | ||
148 | 2 | return gate_atomic_int64_set(&this->impl, newvalue); | |
149 | } | ||
150 | 2 | int64_t AtomicInt64::changeIf(int64_t comparand, int64_t newvalue) noexcept | |
151 | { | ||
152 | 2 | return gate_atomic_int64_xchg_if(&this->impl, comparand, newvalue); | |
153 | } | ||
154 | |||
155 | 1 | int64_t AtomicInt64::operator++() noexcept | |
156 | { | ||
157 | 1 | return gate_atomic_int64_inc(&this->impl); | |
158 | } | ||
159 | 1 | int64_t AtomicInt64::operator++(int) noexcept | |
160 | { | ||
161 | 1 | return gate_atomic_int64_inc(&this->impl) - 1; | |
162 | } | ||
163 | 1 | int64_t AtomicInt64::operator--() noexcept | |
164 | { | ||
165 | 1 | return gate_atomic_int64_dec(&this->impl); | |
166 | } | ||
167 | 1 | int64_t AtomicInt64::operator--(int) noexcept | |
168 | { | ||
169 | 1 | return gate_atomic_int64_dec(&this->impl) + 1; | |
170 | } | ||
171 | |||
172 | 1 | int64_t AtomicInt64::operator+=(int64_t value) noexcept | |
173 | { | ||
174 | 1 | return gate_atomic_int64_add(&this->impl, value); | |
175 | } | ||
176 | 1 | int64_t AtomicInt64::operator-=(int64_t value) noexcept | |
177 | { | ||
178 | 1 | return gate_atomic_int64_add(&this->impl, -value); | |
179 | } | ||
180 | |||
181 | |||
182 | |||
183 | |||
184 | |||
185 | 3 | AtomicPointer::AtomicPointer(void* init) noexcept | |
186 | { | ||
187 | 3 | gate_atomic_ptr_init(&this->impl, init); | |
188 | 3 | } | |
189 | 1 | AtomicPointer::AtomicPointer(AtomicPointer const& src) noexcept | |
190 | { | ||
191 | 1 | gate_atomic_ptr_init(&this->impl, src.get()); | |
192 | 1 | } | |
193 | 1 | AtomicPointer& AtomicPointer::operator=(AtomicPointer const& src) noexcept | |
194 | { | ||
195 | 1 | this->set(src.get()); | |
196 | 1 | return *this; | |
197 | } | ||
198 | 4 | AtomicPointer::~AtomicPointer() noexcept | |
199 | { | ||
200 | 4 | } | |
201 | |||
202 | 12 | void* AtomicPointer::get() const noexcept | |
203 | { | ||
204 | 12 | return gate_atomic_ptr_get(const_cast<gate_atomic_ptr_t*>(&this->impl)); | |
205 | } | ||
206 | 6 | void* AtomicPointer::set(void* newvalue) noexcept | |
207 | { | ||
208 | 6 | return gate_atomic_ptr_set(&this->impl, newvalue); | |
209 | } | ||
210 | 4 | void* AtomicPointer::changeIf(void* comparand, void* newvalue) noexcept | |
211 | { | ||
212 | 4 | return gate_atomic_ptr_xchg_if(&this->impl, comparand, newvalue); | |
213 | } | ||
214 | |||
215 | |||
216 | |||
217 | |||
218 | |||
219 | 1 | AtomicMutex::AtomicMutex() noexcept | |
220 | { | ||
221 | 1 | gate_mem_clear(&this->impl, sizeof(this->impl)); | |
222 | //this->impl = GATE_ATOMIC_LOCK_INIT; | ||
223 | 1 | } | |
224 | 1 | AtomicMutex::~AtomicMutex() noexcept | |
225 | { | ||
226 | 1 | } | |
227 | 1 | gate_atomic_lock_t* AtomicMutex::c_impl() noexcept | |
228 | { | ||
229 | 1 | return &this->impl; | |
230 | } | ||
231 | |||
232 | 1 | void AtomicMutex::lock() noexcept | |
233 | { | ||
234 | 1 | gate_atomic_lock_acquire(&this->impl); | |
235 | 1 | } | |
236 | 2 | bool AtomicMutex::lock(uint32_t spinCount) noexcept | |
237 | { | ||
238 | 2 | return gate_atomic_lock_acquire_spin(&this->impl, spinCount); | |
239 | } | ||
240 | 2 | void AtomicMutex::unlock() noexcept | |
241 | { | ||
242 | 2 | gate_atomic_lock_release(&this->impl); | |
243 | 2 | } | |
244 | |||
245 | |||
246 | 1 | AtomicLock::AtomicLock(AtomicMutex& mtx) noexcept | |
247 | 1 | : mutex(mtx.c_impl()) | |
248 | { | ||
249 | 1 | gate_atomic_lock_acquire(this->mutex); | |
250 | 1 | } | |
251 | 1 | AtomicLock::AtomicLock(gate_atomic_lock_t& ref_mtx) noexcept | |
252 | 1 | : mutex(&ref_mtx) | |
253 | { | ||
254 | 1 | gate_atomic_lock_acquire(this->mutex); | |
255 | 1 | } | |
256 | 4 | AtomicLock::~AtomicLock() noexcept | |
257 | { | ||
258 | 2 | gate_atomic_lock_release(this->mutex); | |
259 | 2 | } | |
260 | |||
261 | |||
262 | 1 | AtomicRecursiveMutex::AtomicRecursiveMutex() noexcept | |
263 | { | ||
264 | 1 | gate_mem_clear(&this->impl, sizeof(this->impl)); | |
265 | 1 | } | |
266 | 1 | AtomicRecursiveMutex::~AtomicRecursiveMutex() noexcept | |
267 | { | ||
268 | 1 | } | |
269 | 2 | gate_atomic_rlock_t* AtomicRecursiveMutex::c_impl() noexcept | |
270 | { | ||
271 | 2 | return &this->impl; | |
272 | } | ||
273 | |||
274 | 1 | void AtomicRecursiveMutex::lock() noexcept | |
275 | { | ||
276 | 1 | gate_atomic_rlock_acquire(&this->impl); | |
277 | 1 | } | |
278 | 2 | bool AtomicRecursiveMutex::lock(uint32_t spinCount) noexcept | |
279 | { | ||
280 | 2 | return gate_atomic_rlock_acquire_spin(&this->impl, spinCount); | |
281 | } | ||
282 | 3 | void AtomicRecursiveMutex::unlock() noexcept | |
283 | { | ||
284 | 3 | gate_atomic_rlock_release(&this->impl); | |
285 | 3 | } | |
286 | |||
287 | |||
288 | 2 | AtomicRecursiveLock::AtomicRecursiveLock(AtomicRecursiveMutex& mtx) noexcept | |
289 | 2 | : mutex(mtx.c_impl()) | |
290 | { | ||
291 | 2 | gate_atomic_rlock_acquire(this->mutex); | |
292 | 2 | } | |
293 | 1 | AtomicRecursiveLock::AtomicRecursiveLock(gate_atomic_rlock_t& ref_mtx) noexcept | |
294 | 1 | : mutex(&ref_mtx) | |
295 | { | ||
296 | 1 | gate_atomic_rlock_acquire(this->mutex); | |
297 | 1 | } | |
298 | 6 | AtomicRecursiveLock::~AtomicRecursiveLock() noexcept | |
299 | { | ||
300 | 3 | gate_atomic_rlock_release(this->mutex); | |
301 | 3 | } | |
302 | |||
303 | |||
304 | 1 | AtomicCallOnce::AtomicCallOnce() noexcept | |
305 | { | ||
306 | 1 | this->state.value = 0; | |
307 | 1 | } | |
308 | 1 | AtomicCallOnce::~AtomicCallOnce() noexcept | |
309 | { | ||
310 | 1 | } | |
311 | 3 | void AtomicCallOnce::operator()(void(*func)(void)) | |
312 | { | ||
313 | 3 | gate_atomic_call_once(&this->state, func); | |
314 | 3 | } | |
315 | |||
316 | |||
317 | 1 | AtomicCallOnceEx::AtomicCallOnceEx() noexcept | |
318 | { | ||
319 | 1 | this->state.value = 0; | |
320 | 1 | } | |
321 | 1 | AtomicCallOnceEx::~AtomicCallOnceEx() noexcept | |
322 | { | ||
323 | 1 | } | |
324 | 3 | void AtomicCallOnceEx::operator()(void(*func)(void)) | |
325 | { | ||
326 | 3 | gate_atomic_call_once_exclusive(&this->state, func); | |
327 | 3 | } | |
328 | |||
329 | } // end of namespace gate | ||
330 |