3 // Copyright (C) 2008, 2009
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file bits/atomic_0.h
27 * This is an internal header file, included by other library headers.
28 * You should not attempt to use it directly.
31 #ifndef _GLIBCXX_ATOMIC_0_H
32 #define _GLIBCXX_ATOMIC_0_H 1
34 #pragma GCC system_header
36 // _GLIBCXX_BEGIN_NAMESPACE(std)
38 // 0 == __atomic0 == Never lock-free
43 // Implementation specific defines.
44 #define _ATOMIC_LOAD_(__a, __x) \
45 ({__typeof__ _ATOMIC_MEMBER_* __p = &_ATOMIC_MEMBER_; \
46 __atomic_flag_base* __g = __atomic_flag_for_address(__p); \
47 __atomic_flag_wait_explicit(__g, __x); \
48 __typeof__ _ATOMIC_MEMBER_ __r = *__p; \
49 atomic_flag_clear_explicit(__g, __x); \
52 #define _ATOMIC_STORE_(__a, __m, __x) \
53 ({__typeof__ _ATOMIC_MEMBER_* __p = &_ATOMIC_MEMBER_; \
54 __typeof__(__m) __v = (__m); \
55 __atomic_flag_base* __g = __atomic_flag_for_address(__p); \
56 __atomic_flag_wait_explicit(__g, __x); \
58 atomic_flag_clear_explicit(__g, __x); \
61 #define _ATOMIC_MODIFY_(__a, __o, __m, __x) \
62 ({__typeof__ _ATOMIC_MEMBER_* __p = &_ATOMIC_MEMBER_; \
63 __typeof__(__m) __v = (__m); \
64 __atomic_flag_base* __g = __atomic_flag_for_address(__p); \
65 __atomic_flag_wait_explicit(__g, __x); \
66 __typeof__ _ATOMIC_MEMBER_ __r = *__p; \
68 atomic_flag_clear_explicit(__g, __x); \
71 #define _ATOMIC_CMPEXCHNG_(__a, __e, __m, __x) \
72 ({__typeof__ _ATOMIC_MEMBER_* __p = &_ATOMIC_MEMBER_; \
73 __typeof__(__e) __q = (__e); \
74 __typeof__(__m) __v = (__m); \
76 __atomic_flag_base* __g = __atomic_flag_for_address(__p); \
77 __atomic_flag_wait_explicit(__g, __x); \
78 __typeof__ _ATOMIC_MEMBER_ __t__ = *__p; \
79 if (__t__ == *__q) { *__p = __v; __r = true; } \
80 else { *__q = __t__; __r = false; } \
81 atomic_flag_clear_explicit(__g, __x); \
85 struct atomic_flag : public __atomic_flag_base
87 atomic_flag() = default;
88 ~atomic_flag() = default;
89 atomic_flag(const atomic_flag&) = delete;
90 atomic_flag& operator=(const atomic_flag&) volatile = delete;
92 // Conversion to ATOMIC_FLAG_INIT.
93 atomic_flag(bool __i): __atomic_flag_base({ __i }) { }
96 test_and_set(memory_order __m = memory_order_seq_cst);
99 clear(memory_order __m = memory_order_seq_cst);
102 /// 29.4.2, address types
103 struct atomic_address
109 atomic_address() = default;
110 ~atomic_address() = default;
111 atomic_address(const atomic_address&) = delete;
112 atomic_address& operator=(const atomic_address&) volatile = delete;
114 atomic_address(void* __v) { _M_i = __v; }
121 store(void* __v, memory_order __m = memory_order_seq_cst)
123 __glibcxx_assert(__m != memory_order_acquire);
124 __glibcxx_assert(__m != memory_order_acq_rel);
125 __glibcxx_assert(__m != memory_order_consume);
126 _ATOMIC_STORE_(this, __v, __m);
130 load(memory_order __m = memory_order_seq_cst) const
132 __glibcxx_assert(__m != memory_order_release);
133 __glibcxx_assert(__m != memory_order_acq_rel);
134 return _ATOMIC_LOAD_(this, __m);
138 exchange(void* __v, memory_order __m = memory_order_seq_cst)
139 { return _ATOMIC_MODIFY_(this, =, __v, __m); }
142 compare_exchange_weak(void*& __v1, void* __v2, memory_order __m1,
145 __glibcxx_assert(__m2 != memory_order_release);
146 __glibcxx_assert(__m2 != memory_order_acq_rel);
147 __glibcxx_assert(__m2 <= __m1);
148 return _ATOMIC_CMPEXCHNG_(this, &__v1, __v2, __m1);
152 compare_exchange_weak(void*& __v1, void* __v2,
153 memory_order __m = memory_order_seq_cst)
155 return compare_exchange_weak(__v1, __v2, __m,
156 __calculate_memory_order(__m));
160 compare_exchange_strong(void*& __v1, void* __v2, memory_order __m1,
163 __glibcxx_assert(__m2 != memory_order_release);
164 __glibcxx_assert(__m2 != memory_order_acq_rel);
165 __glibcxx_assert(__m2 <= __m1);
166 return _ATOMIC_CMPEXCHNG_(this, &__v1, __v2, __m1);
170 compare_exchange_strong(void*& __v1, void* __v2,
171 memory_order __m = memory_order_seq_cst)
173 return compare_exchange_strong(__v1, __v2, __m,
174 __calculate_memory_order(__m));
178 fetch_add(ptrdiff_t __d, memory_order __m = memory_order_seq_cst)
180 void** __p = &(_M_i);
181 __atomic_flag_base* __g = __atomic_flag_for_address(__p);
182 __atomic_flag_wait_explicit(__g, __m);
184 *__p = (void*)((char*)(*__p) + __d);
185 atomic_flag_clear_explicit(__g, __m);
190 fetch_sub(ptrdiff_t __d, memory_order __m = memory_order_seq_cst)
192 void** __p = &(_M_i);
193 __atomic_flag_base* __g = __atomic_flag_for_address(__p);
194 __atomic_flag_wait_explicit(__g, __m);
196 *__p = (void*)((char*)(*__p) - __d);
197 atomic_flag_clear_explicit(__g, __m);
201 operator void*() const
212 operator+=(ptrdiff_t __d)
213 { return fetch_add(__d) + __d; }
216 operator-=(ptrdiff_t __d)
217 { return fetch_sub(__d) - __d; }
221 // 29.3.1 atomic integral types
222 // For each of the integral types, define atomic_[integral type] struct
226 // atomic_schar signed char
227 // atomic_uchar unsigned char
228 // atomic_short short
229 // atomic_ushort unsigned short
231 // atomic_uint unsigned int
233 // atomic_ulong unsigned long
234 // atomic_llong long long
235 // atomic_ullong unsigned long long
236 // atomic_char16_t char16_t
237 // atomic_char32_t char32_t
238 // atomic_wchar_t wchar_t
241 // NB: Assuming _ITp is an integral scalar type that is 1, 2, 4, or 8 bytes,
242 // since that is what GCC built-in functions for atomic memory access work on.
243 template<typename _ITp>
247 typedef _ITp __integral_type;
249 __integral_type _M_i;
252 __atomic_base() = default;
253 ~__atomic_base() = default;
254 __atomic_base(const __atomic_base&) = delete;
255 __atomic_base& operator=(const __atomic_base&) volatile = delete;
257 // Requires __integral_type convertible to _M_base._M_i.
258 __atomic_base(__integral_type __i) { _M_i = __i; }
260 operator __integral_type() const
264 operator=(__integral_type __i)
272 { return fetch_add(1); }
276 { return fetch_sub(1); }
280 { return fetch_add(1) + 1; }
284 { return fetch_sub(1) - 1; }
287 operator+=(__integral_type __i)
288 { return fetch_add(__i) + __i; }
291 operator-=(__integral_type __i)
292 { return fetch_sub(__i) - __i; }
295 operator&=(__integral_type __i)
296 { return fetch_and(__i) & __i; }
299 operator|=(__integral_type __i)
300 { return fetch_or(__i) | __i; }
303 operator^=(__integral_type __i)
304 { return fetch_xor(__i) ^ __i; }
311 store(__integral_type __i, memory_order __m = memory_order_seq_cst)
313 __glibcxx_assert(__m != memory_order_acquire);
314 __glibcxx_assert(__m != memory_order_acq_rel);
315 __glibcxx_assert(__m != memory_order_consume);
316 _ATOMIC_STORE_(this, __i, __m);
320 load(memory_order __m = memory_order_seq_cst) const
322 __glibcxx_assert(__m != memory_order_release);
323 __glibcxx_assert(__m != memory_order_acq_rel);
324 return _ATOMIC_LOAD_(this, __m);
328 exchange(__integral_type __i, memory_order __m = memory_order_seq_cst)
329 { return _ATOMIC_MODIFY_(this, =, __i, __m); }
332 compare_exchange_weak(__integral_type& __i1, __integral_type __i2,
333 memory_order __m1, memory_order __m2)
335 __glibcxx_assert(__m2 != memory_order_release);
336 __glibcxx_assert(__m2 != memory_order_acq_rel);
337 __glibcxx_assert(__m2 <= __m1);
338 return _ATOMIC_CMPEXCHNG_(this, &__i1, __i2, __m1);
342 compare_exchange_weak(__integral_type& __i1, __integral_type __i2,
343 memory_order __m = memory_order_seq_cst)
345 return compare_exchange_weak(__i1, __i2, __m,
346 __calculate_memory_order(__m));
350 compare_exchange_strong(__integral_type& __i1, __integral_type __i2,
351 memory_order __m1, memory_order __m2)
353 __glibcxx_assert(__m2 != memory_order_release);
354 __glibcxx_assert(__m2 != memory_order_acq_rel);
355 __glibcxx_assert(__m2 <= __m1);
356 return _ATOMIC_CMPEXCHNG_(this, &__i1, __i2, __m1);
360 compare_exchange_strong(__integral_type& __i1, __integral_type __i2,
361 memory_order __m = memory_order_seq_cst)
363 return compare_exchange_strong(__i1, __i2, __m,
364 __calculate_memory_order(__m));
368 fetch_add(__integral_type __i, memory_order __m = memory_order_seq_cst)
369 { return _ATOMIC_MODIFY_(this, +=, __i, __m); }
372 fetch_sub(__integral_type __i, memory_order __m = memory_order_seq_cst)
373 { return _ATOMIC_MODIFY_(this, -=, __i, __m); }
376 fetch_and(__integral_type __i, memory_order __m = memory_order_seq_cst)
377 { return _ATOMIC_MODIFY_(this, &=, __i, __m); }
380 fetch_or(__integral_type __i, memory_order __m = memory_order_seq_cst)
381 { return _ATOMIC_MODIFY_(this, |=, __i, __m); }
384 fetch_xor(__integral_type __i, memory_order __m = memory_order_seq_cst)
385 { return _ATOMIC_MODIFY_(this, ^=, __i, __m); }
390 // NB: No operators or fetch-operations for this type.
394 __atomic_base<bool> _M_base;
397 atomic_bool() = default;
398 ~atomic_bool() = default;
399 atomic_bool(const atomic_bool&) = delete;
400 atomic_bool& operator=(const atomic_bool&) volatile = delete;
402 atomic_bool(bool __i) : _M_base(__i) { }
406 { return _M_base.operator=(__i); }
408 operator bool() const
409 { return _M_base.load(); }
413 { return _M_base.is_lock_free(); }
416 store(bool __i, memory_order __m = memory_order_seq_cst)
417 { _M_base.store(__i, __m); }
420 load(memory_order __m = memory_order_seq_cst) const
421 { return _M_base.load(__m); }
424 exchange(bool __i, memory_order __m = memory_order_seq_cst)
425 { return _M_base.exchange(__i, __m); }
428 compare_exchange_weak(bool& __i1, bool __i2, memory_order __m1,
430 { return _M_base.compare_exchange_weak(__i1, __i2, __m1, __m2); }
433 compare_exchange_weak(bool& __i1, bool __i2,
434 memory_order __m = memory_order_seq_cst)
435 { return _M_base.compare_exchange_weak(__i1, __i2, __m); }
438 compare_exchange_strong(bool& __i1, bool __i2, memory_order __m1,
440 { return _M_base.compare_exchange_strong(__i1, __i2, __m1, __m2); }
444 compare_exchange_strong(bool& __i1, bool __i2,
445 memory_order __m = memory_order_seq_cst)
446 { return _M_base.compare_exchange_strong(__i1, __i2, __m); }
450 #undef _ATOMIC_STORE_
451 #undef _ATOMIC_MODIFY_
452 #undef _ATOMIC_CMPEXCHNG_
453 } // namespace __atomic0
455 // _GLIBCXX_END_NAMESPACE