blob: 8c0a06901887e6f5718cccc4a9e6a8aadfe7b08d [file] [log] [blame]
Howard Hinnant8f73c632010-09-27 21:17:38 +00001// -*- C++ -*-
2//===--------------------------- atomic -----------------------------------===//
3//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_ATOMIC
12#define _LIBCPP_ATOMIC
13
14/*
15 atomic synopsis
16
17namespace std
18{
19
20// order and consistency
21
22typedef enum memory_order
23{
Howard Hinnantd1176e22010-09-28 17:13:38 +000024 memory_order_relaxed,
25 memory_order_consume, // load-consume
26 memory_order_acquire, // load-acquire
27 memory_order_release, // store-release
28 memory_order_acq_rel, // store-release load-acquire
29 memory_order_seq_cst // store-release load-acquire
Howard Hinnant8f73c632010-09-27 21:17:38 +000030} memory_order;
31
32template <class T> T kill_dependency(T y);
33
34// lock-free property
35
36#define ATOMIC_CHAR_LOCK_FREE unspecified
37#define ATOMIC_CHAR16_T_LOCK_FREE unspecified
38#define ATOMIC_CHAR32_T_LOCK_FREE unspecified
39#define ATOMIC_WCHAR_T_LOCK_FREE unspecified
40#define ATOMIC_SHORT_LOCK_FREE unspecified
41#define ATOMIC_INT_LOCK_FREE unspecified
42#define ATOMIC_LONG_LOCK_FREE unspecified
43#define ATOMIC_LLONG_LOCK_FREE unspecified
Howard Hinnant8f73c632010-09-27 21:17:38 +000044
Howard Hinnant8f73c632010-09-27 21:17:38 +000045// flag type and operations
46
47typedef struct atomic_flag
48{
Howard Hinnant4777bf22010-12-06 23:10:08 +000049 bool test_and_set(memory_order m = memory_order_seq_cst) volatile;
50 bool test_and_set(memory_order m = memory_order_seq_cst);
51 void clear(memory_order m = memory_order_seq_cst) volatile;
52 void clear(memory_order m = memory_order_seq_cst);
Howard Hinnant8f73c632010-09-27 21:17:38 +000053 atomic_flag() = default;
54 atomic_flag(const atomic_flag&) = delete;
55 atomic_flag& operator=(const atomic_flag&) = delete;
56 atomic_flag& operator=(const atomic_flag&) volatile = delete;
57} atomic_flag;
58
Howard Hinnant4777bf22010-12-06 23:10:08 +000059bool
60 atomic_flag_test_and_set(volatile atomic_flag* obj);
61
62bool
63 atomic_flag_test_and_set(atomic_flag* obj);
64
65bool
66 atomic_flag_test_and_set_explicit(volatile atomic_flag* obj,
67 memory_order m);
68
69bool
70 atomic_flag_test_and_set_explicit(atomic_flag* obj, memory_order m);
71
72void
73 atomic_flag_clear(volatile atomic_flag* obj);
74
75void
76 atomic_flag_clear(atomic_flag* obj);
77
78void
79 atomic_flag_clear_explicit(volatile atomic_flag* obj, memory_order m);
80
81void
82 atomic_flag_clear_explicit(atomic_flag* obj, memory_order m);
Howard Hinnant8f73c632010-09-27 21:17:38 +000083
84#define ATOMIC_FLAG_INIT see below
Howard Hinnante7385012010-10-19 16:51:18 +000085#define ATOMIC_VAR_INIT(value) see below
Howard Hinnant8f73c632010-09-27 21:17:38 +000086
Howard Hinnant8f73c632010-09-27 21:17:38 +000087template <class T>
88struct atomic
89{
90 bool is_lock_free() const volatile;
91 bool is_lock_free() const;
Howard Hinnant4777bf22010-12-06 23:10:08 +000092 void store(T desr, memory_order m = memory_order_seq_cst) volatile;
93 void store(T desr, memory_order m = memory_order_seq_cst);
94 T load(memory_order m = memory_order_seq_cst) const volatile;
95 T load(memory_order m = memory_order_seq_cst) const;
Howard Hinnant8f73c632010-09-27 21:17:38 +000096 operator T() const volatile;
97 operator T() const;
Howard Hinnant4777bf22010-12-06 23:10:08 +000098 T exchange(T desr, memory_order m = memory_order_seq_cst) volatile;
99 T exchange(T desr, memory_order m = memory_order_seq_cst);
100 bool compare_exchange_weak(T& expc, T desr,
101 memory_order s, memory_order f) volatile;
102 bool compare_exchange_weak(T& expc, T desr, memory_order s, memory_order f);
103 bool compare_exchange_strong(T& expc, T desr,
104 memory_order s, memory_order f) volatile;
105 bool compare_exchange_strong(T& expc, T desr,
106 memory_order s, memory_order f);
107 bool compare_exchange_weak(T& expc, T desr,
108 memory_order m = memory_order_seq_cst) volatile;
109 bool compare_exchange_weak(T& expc, T desr,
110 memory_order m = memory_order_seq_cst);
111 bool compare_exchange_strong(T& expc, T desr,
112 memory_order m = memory_order_seq_cst) volatile;
113 bool compare_exchange_strong(T& expc, T desr,
114 memory_order m = memory_order_seq_cst);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000115
116 atomic() = default;
Howard Hinnant4777bf22010-12-06 23:10:08 +0000117 constexpr atomic(T desr);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000118 atomic(const atomic&) = delete;
119 atomic& operator=(const atomic&) = delete;
120 atomic& operator=(const atomic&) volatile = delete;
121 T operator=(T) volatile;
122 T operator=(T);
123};
124
125template <>
Howard Hinnant4777bf22010-12-06 23:10:08 +0000126struct atomic<integral>
Howard Hinnant8f73c632010-09-27 21:17:38 +0000127{
Howard Hinnant4777bf22010-12-06 23:10:08 +0000128 bool is_lock_free() const volatile;
129 bool is_lock_free() const;
130 void store(integral desr, memory_order m = memory_order_seq_cst) volatile;
131 void store(integral desr, memory_order m = memory_order_seq_cst);
132 integral load(memory_order m = memory_order_seq_cst) const volatile;
133 integral load(memory_order m = memory_order_seq_cst) const;
134 operator integral() const volatile;
135 operator integral() const;
136 integral exchange(integral desr,
137 memory_order m = memory_order_seq_cst) volatile;
138 integral exchange(integral desr, memory_order m = memory_order_seq_cst);
139 bool compare_exchange_weak(integral& expc, integral desr,
140 memory_order s, memory_order f) volatile;
141 bool compare_exchange_weak(integral& expc, integral desr,
142 memory_order s, memory_order f);
143 bool compare_exchange_strong(integral& expc, integral desr,
144 memory_order s, memory_order f) volatile;
145 bool compare_exchange_strong(integral& expc, integral desr,
146 memory_order s, memory_order f);
147 bool compare_exchange_weak(integral& expc, integral desr,
148 memory_order m = memory_order_seq_cst) volatile;
149 bool compare_exchange_weak(integral& expc, integral desr,
150 memory_order m = memory_order_seq_cst);
151 bool compare_exchange_strong(integral& expc, integral desr,
152 memory_order m = memory_order_seq_cst) volatile;
153 bool compare_exchange_strong(integral& expc, integral desr,
154 memory_order m = memory_order_seq_cst);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000155
Howard Hinnant4777bf22010-12-06 23:10:08 +0000156 integral
157 fetch_add(integral op, memory_order m = memory_order_seq_cst) volatile;
158 integral fetch_add(integral op, memory_order m = memory_order_seq_cst);
159 integral
160 fetch_sub(integral op, memory_order m = memory_order_seq_cst) volatile;
161 integral fetch_sub(integral op, memory_order m = memory_order_seq_cst);
162 integral
163 fetch_and(integral op, memory_order m = memory_order_seq_cst) volatile;
164 integral fetch_and(integral op, memory_order m = memory_order_seq_cst);
165 integral
166 fetch_or(integral op, memory_order m = memory_order_seq_cst) volatile;
167 integral fetch_or(integral op, memory_order m = memory_order_seq_cst);
168 integral
169 fetch_xor(integral op, memory_order m = memory_order_seq_cst) volatile;
170 integral fetch_xor(integral op, memory_order m = memory_order_seq_cst);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000171
Howard Hinnant8f73c632010-09-27 21:17:38 +0000172 atomic() = default;
Howard Hinnant4777bf22010-12-06 23:10:08 +0000173 constexpr atomic(integral desr);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000174 atomic(const atomic&) = delete;
175 atomic& operator=(const atomic&) = delete;
176 atomic& operator=(const atomic&) volatile = delete;
Howard Hinnant4777bf22010-12-06 23:10:08 +0000177 integral operator=(integral desr) volatile;
178 integral operator=(integral desr);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000179
Howard Hinnant4777bf22010-12-06 23:10:08 +0000180 integral operator++(int) volatile;
181 integral operator++(int);
182 integral operator--(int) volatile;
183 integral operator--(int);
184 integral operator++() volatile;
185 integral operator++();
186 integral operator--() volatile;
187 integral operator--();
188 integral operator+=(integral op) volatile;
189 integral operator+=(integral op);
190 integral operator-=(integral op) volatile;
191 integral operator-=(integral op);
192 integral operator&=(integral op) volatile;
193 integral operator&=(integral op);
194 integral operator|=(integral op) volatile;
195 integral operator|=(integral op);
Howard Hinnant91e2f262010-12-07 20:46:14 +0000196 integral operator^=(integral op) volatile;
197 integral operator^=(integral op);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000198};
199
200template <class T>
201struct atomic<T*>
Howard Hinnant8f73c632010-09-27 21:17:38 +0000202{
Howard Hinnant4777bf22010-12-06 23:10:08 +0000203 bool is_lock_free() const volatile;
204 bool is_lock_free() const;
205 void store(T* desr, memory_order m = memory_order_seq_cst) volatile;
206 void store(T* desr, memory_order m = memory_order_seq_cst);
207 T* load(memory_order m = memory_order_seq_cst) const volatile;
208 T* load(memory_order m = memory_order_seq_cst) const;
Howard Hinnant8f73c632010-09-27 21:17:38 +0000209 operator T*() const volatile;
210 operator T*() const;
Howard Hinnant4777bf22010-12-06 23:10:08 +0000211 T* exchange(T* desr, memory_order m = memory_order_seq_cst) volatile;
212 T* exchange(T* desr, memory_order m = memory_order_seq_cst);
213 bool compare_exchange_weak(T*& expc, T* desr,
214 memory_order s, memory_order f) volatile;
215 bool compare_exchange_weak(T*& expc, T* desr,
216 memory_order s, memory_order f);
217 bool compare_exchange_strong(T*& expc, T* desr,
218 memory_order s, memory_order f) volatile;
219 bool compare_exchange_strong(T*& expc, T* desr,
220 memory_order s, memory_order f);
221 bool compare_exchange_weak(T*& expc, T* desr,
222 memory_order m = memory_order_seq_cst) volatile;
223 bool compare_exchange_weak(T*& expc, T* desr,
224 memory_order m = memory_order_seq_cst);
225 bool compare_exchange_strong(T*& expc, T* desr,
226 memory_order m = memory_order_seq_cst) volatile;
227 bool compare_exchange_strong(T*& expc, T* desr,
228 memory_order m = memory_order_seq_cst);
229 T* fetch_add(ptrdiff_t op, memory_order m = memory_order_seq_cst) volatile;
230 T* fetch_add(ptrdiff_t op, memory_order m = memory_order_seq_cst);
231 T* fetch_sub(ptrdiff_t op, memory_order m = memory_order_seq_cst) volatile;
232 T* fetch_sub(ptrdiff_t op, memory_order m = memory_order_seq_cst);
233
Howard Hinnant8f73c632010-09-27 21:17:38 +0000234 atomic() = default;
Howard Hinnant4777bf22010-12-06 23:10:08 +0000235 constexpr atomic(T* desr);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000236 atomic(const atomic&) = delete;
237 atomic& operator=(const atomic&) = delete;
238 atomic& operator=(const atomic&) volatile = delete;
Howard Hinnant4777bf22010-12-06 23:10:08 +0000239
Howard Hinnant8f73c632010-09-27 21:17:38 +0000240 T* operator=(T*) volatile;
241 T* operator=(T*);
242 T* operator++(int) volatile;
243 T* operator++(int);
244 T* operator--(int) volatile;
245 T* operator--(int);
246 T* operator++() volatile;
247 T* operator++();
248 T* operator--() volatile;
249 T* operator--();
Howard Hinnant4777bf22010-12-06 23:10:08 +0000250 T* operator+=(ptrdiff_t op) volatile;
251 T* operator+=(ptrdiff_t op);
252 T* operator-=(ptrdiff_t op) volatile;
253 T* operator-=(ptrdiff_t op);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000254};
255
Howard Hinnant4777bf22010-12-06 23:10:08 +0000256
257template <class T>
258 bool
259 atomic_is_lock_free(const volatile atomic<T>* obj);
260
261template <class T>
262 bool
263 atomic_is_lock_free(const atomic<T>* obj);
264
265template <class T>
266 void
267 atomic_init(volatile atomic<T>* obj, T desr);
268
269template <class T>
270 void
271 atomic_init(atomic<T>* obj, T desr);
272
273template <class T>
274 void
275 atomic_store(volatile atomic<T>* obj, T desr);
276
277template <class T>
278 void
279 atomic_store(atomic<T>* obj, T desr);
280
281template <class T>
282 void
283 atomic_store_explicit(volatile atomic<T>* obj, T desr, memory_order m);
284
285template <class T>
286 void
287 atomic_store_explicit(atomic<T>* obj, T desr, memory_order m);
288
289template <class T>
290 T
291 atomic_load(const volatile atomic<T>* obj);
292
293template <class T>
294 T
295 atomic_load(const atomic<T>* obj);
296
297template <class T>
298 T
299 atomic_load_explicit(const volatile atomic<T>* obj, memory_order m);
300
301template <class T>
302 T
303 atomic_load_explicit(const atomic<T>* obj, memory_order m);
304
305template <class T>
306 T
307 atomic_exchange(volatile atomic<T>* obj, T desr);
308
309template <class T>
310 T
311 atomic_exchange(atomic<T>* obj, T desr);
312
313template <class T>
314 T
315 atomic_exchange_explicit(volatile atomic<T>* obj, T desr, memory_order m);
316
317template <class T>
318 T
319 atomic_exchange_explicit(atomic<T>* obj, T desr, memory_order m);
320
321template <class T>
322 bool
323 atomic_compare_exchange_weak(volatile atomic<T>* obj, T* expc, T desr);
324
325template <class T>
326 bool
327 atomic_compare_exchange_weak(atomic<T>* obj, T* expc, T desr);
328
329template <class T>
330 bool
331 atomic_compare_exchange_strong(volatile atomic<T>* obj, T* expc, T desr);
332
333template <class T>
334 bool
335 atomic_compare_exchange_strong(atomic<T>* obj, T* expc, T desr);
336
337template <class T>
338 bool
339 atomic_compare_exchange_weak_explicit(volatile atomic<T>* obj, T* expc,
340 T desr,
341 memory_order s, memory_order f);
342
343template <class T>
344 bool
345 atomic_compare_exchange_weak_explicit(atomic<T>* obj, T* expc, T desr,
346 memory_order s, memory_order f);
347
348template <class T>
349 bool
350 atomic_compare_exchange_strong_explicit(volatile atomic<T>* obj,
351 T* expc, T desr,
352 memory_order s, memory_order f);
353
354template <class T>
355 bool
356 atomic_compare_exchange_strong_explicit(atomic<T>* obj, T* expc,
357 T desr,
358 memory_order s, memory_order f);
359
360template <class Integral>
361 Integral
362 atomic_fetch_add(volatile atomic<Integral>* obj, Integral op);
363
364template <class Integral>
365 Integral
366 atomic_fetch_add(atomic<Integral>* obj, Integral op);
367
368template <class Integral>
369 Integral
370 atomic_fetch_add_explicit(volatile atomic<Integral>* obj, Integral op,
371 memory_order m);
372template <class Integral>
373 Integral
374 atomic_fetch_add_explicit(atomic<Integral>* obj, Integral op,
375 memory_order m);
376template <class Integral>
377 Integral
378 atomic_fetch_sub(volatile atomic<Integral>* obj, Integral op);
379
380template <class Integral>
381 Integral
382 atomic_fetch_sub(atomic<Integral>* obj, Integral op);
383
384template <class Integral>
385 Integral
386 atomic_fetch_sub_explicit(volatile atomic<Integral>* obj, Integral op,
387 memory_order m);
388template <class Integral>
389 Integral
390 atomic_fetch_sub_explicit(atomic<Integral>* obj, Integral op,
391 memory_order m);
392template <class Integral>
393 Integral
394 atomic_fetch_and(volatile atomic<Integral>* obj, Integral op);
395
396template <class Integral>
397 Integral
398 atomic_fetch_and(atomic<Integral>* obj, Integral op);
399
400template <class Integral>
401 Integral
402 atomic_fetch_and_explicit(volatile atomic<Integral>* obj, Integral op,
403 memory_order m);
404template <class Integral>
405 Integral
406 atomic_fetch_and_explicit(atomic<Integral>* obj, Integral op,
407 memory_order m);
408template <class Integral>
409 Integral
410 atomic_fetch_or(volatile atomic<Integral>* obj, Integral op);
411
412template <class Integral>
413 Integral
414 atomic_fetch_or(atomic<Integral>* obj, Integral op);
415
416template <class Integral>
417 Integral
418 atomic_fetch_or_explicit(volatile atomic<Integral>* obj, Integral op,
419 memory_order m);
420template <class Integral>
421 Integral
422 atomic_fetch_or_explicit(atomic<Integral>* obj, Integral op,
423 memory_order m);
424template <class Integral>
425 Integral
426 atomic_fetch_xor(volatile atomic<Integral>* obj, Integral op);
427
428template <class Integral>
429 Integral
430 atomic_fetch_xor(atomic<Integral>* obj, Integral op);
431
432template <class Integral>
433 Integral
434 atomic_fetch_xor_explicit(volatile atomic<Integral>* obj, Integral op,
435 memory_order m);
436template <class Integral>
437 Integral
438 atomic_fetch_xor_explicit(atomic<Integral>* obj, Integral op,
439 memory_order m);
440
441template <class T>
442 T*
443 atomic_fetch_add(volatile atomic<T*>* obj, ptrdiff_t op);
444
445template <class T>
446 T*
447 atomic_fetch_add(atomic<T*>* obj, ptrdiff_t op);
448
449template <class T>
450 T*
451 atomic_fetch_add_explicit(volatile atomic<T*>* obj, ptrdiff_t op,
452 memory_order m);
453template <class T>
454 T*
455 atomic_fetch_add_explicit(atomic<T*>* obj, ptrdiff_t op, memory_order m);
456
457template <class T>
458 T*
459 atomic_fetch_sub(volatile atomic<T*>* obj, ptrdiff_t op);
460
461template <class T>
462 T*
463 atomic_fetch_sub(atomic<T*>* obj, ptrdiff_t op);
464
465template <class T>
466 T*
467 atomic_fetch_sub_explicit(volatile atomic<T*>* obj, ptrdiff_t op,
468 memory_order m);
469template <class T>
470 T*
471 atomic_fetch_sub_explicit(atomic<T*>* obj, ptrdiff_t op, memory_order m);
472
473// Atomics for standard typedef types
474
475typedef atomic<char> atomic_char;
476typedef atomic<signed char> atomic_schar;
477typedef atomic<unsigned char> atomic_uchar;
478typedef atomic<short> atomic_short;
479typedef atomic<unsigned short> atomic_ushort;
480typedef atomic<int> atomic_int;
481typedef atomic<unsigned int> atomic_uint;
482typedef atomic<long> atomic_long;
483typedef atomic<unsigned long> atomic_ulong;
484typedef atomic<long long> atomic_llong;
485typedef atomic<unsigned long long> atomic_ullong;
486typedef atomic<char16_t> atomic_char16_t;
487typedef atomic<char32_t> atomic_char32_t;
488typedef atomic<wchar_t> atomic_wchar_t;
489
490typedef atomic<int_least8_t> atomic_int_least8_t;
491typedef atomic<uint_least8_t> atomic_uint_least8_t;
492typedef atomic<int_least16_t> atomic_int_least16_t;
493typedef atomic<uint_least16_t> atomic_uint_least16_t;
494typedef atomic<int_least32_t> atomic_int_least32_t;
495typedef atomic<uint_least32_t> atomic_uint_least32_t;
496typedef atomic<int_least64_t> atomic_int_least64_t;
497typedef atomic<uint_least64_t> atomic_uint_least64_t;
498
499typedef atomic<int_fast8_t> atomic_int_fast8_t;
500typedef atomic<uint_fast8_t> atomic_uint_fast8_t;
501typedef atomic<int_fast16_t> atomic_int_fast16_t;
502typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
503typedef atomic<int_fast32_t> atomic_int_fast32_t;
504typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
505typedef atomic<int_fast64_t> atomic_int_fast64_t;
506typedef atomic<uint_fast64_t> atomic_uint_fast64_t;
507
508typedef atomic<intptr_t> atomic_intptr_t;
509typedef atomic<uintptr_t> atomic_uintptr_t;
510typedef atomic<size_t> atomic_size_t;
511typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
512typedef atomic<intmax_t> atomic_intmax_t;
513typedef atomic<uintmax_t> atomic_uintmax_t;
514
Howard Hinnant8f73c632010-09-27 21:17:38 +0000515// fences
516
Howard Hinnant4777bf22010-12-06 23:10:08 +0000517void atomic_thread_fence(memory_order m);
518void atomic_signal_fence(memory_order m);
Howard Hinnant8f73c632010-09-27 21:17:38 +0000519
520} // std
521
522*/
523
524#include <__config>
Howard Hinnant4777bf22010-12-06 23:10:08 +0000525#include <cstddef>
526#include <cstdint>
527#include <type_traits>
Howard Hinnant8f73c632010-09-27 21:17:38 +0000528
529#pragma GCC system_header
530
Howard Hinnant4777bf22010-12-06 23:10:08 +0000531//// Begin Temporary Intrinsics ////
532
533template <class _Tp>
534inline _LIBCPP_INLINE_VISIBILITY
535bool
536__atomic_is_lock_free(_Tp)
537{
538 return false;
539}
540
541template <class _Tp>
542inline _LIBCPP_INLINE_VISIBILITY
543_Tp
544__atomic_load(const volatile _Tp* __t, int)
545{
546 return *__t;
547}
548
549template <class _Tp>
550inline _LIBCPP_INLINE_VISIBILITY
551void
552__atomic_store(volatile _Tp* __t, _Tp __d, int)
553{
554 *__t = __d;
555}
556
557template <class _Tp>
558inline _LIBCPP_INLINE_VISIBILITY
559_Tp
560__atomic_exchange(volatile _Tp* __t, _Tp __d, int)
561{
562 _Tp __tmp = *__t;
563 *__t = __d;
564 return __tmp;
565}
566
567template <class _Tp>
568inline _LIBCPP_INLINE_VISIBILITY
569bool
570__atomic_compare_exchange_strong(volatile _Tp* __o, _Tp* __e, _Tp __d, int, int)
571{
572 if (const_cast<_Tp&>(*__o) == *__e)
573 {
574 *__o = __d;
575 return true;
576 }
577 *__e = __d;
578 return false;
579}
580
581template <class _Tp>
582inline _LIBCPP_INLINE_VISIBILITY
583bool
584__atomic_compare_exchange_weak(volatile _Tp* __o, _Tp* __e, _Tp __d, int, int)
585{
586 if (const_cast<_Tp&>(*__o) == *__e)
587 {
588 *__o = __d;
589 return true;
590 }
591 *__e = __d;
592 return false;
593}
594
Howard Hinnant91e2f262010-12-07 20:46:14 +0000595template <class _Tp>
596inline _LIBCPP_INLINE_VISIBILITY
597_Tp
598__atomic_fetch_add(volatile _Tp* __o, _Tp __op, int)
599{
600 _Tp __tmp = *__o;
601 *__o += __op;
602 return __tmp;
603}
604
605template <class _Tp>
606inline _LIBCPP_INLINE_VISIBILITY
607_Tp*
608__atomic_fetch_add(_Tp* volatile* __o, ptrdiff_t __op, int)
609{
610 _Tp* __tmp = *__o;
611 *__o += __op;
612 return __tmp;
613}
614
615template <class _Tp>
616inline _LIBCPP_INLINE_VISIBILITY
617_Tp
618__atomic_fetch_sub(volatile _Tp* __o, _Tp __op, int)
619{
620 _Tp __tmp = *__o;
621 *__o -= __op;
622 return __tmp;
623}
624
625template <class _Tp>
626inline _LIBCPP_INLINE_VISIBILITY
627_Tp*
628__atomic_fetch_sub(_Tp* volatile* __o, ptrdiff_t __op, int)
629{
630 _Tp* __tmp = *__o;
631 *__o -= __op;
632 return __tmp;
633}
634
635template <class _Tp>
636inline _LIBCPP_INLINE_VISIBILITY
637_Tp
638__atomic_fetch_and(volatile _Tp* __o, _Tp __op, int)
639{
640 _Tp __tmp = *__o;
641 *__o &= __op;
642 return __tmp;
643}
644
645template <class _Tp>
646inline _LIBCPP_INLINE_VISIBILITY
647_Tp
648__atomic_fetch_or(volatile _Tp* __o, _Tp __op, int)
649{
650 _Tp __tmp = *__o;
651 *__o |= __op;
652 return __tmp;
653}
654
655template <class _Tp>
656inline _LIBCPP_INLINE_VISIBILITY
657_Tp
658__atomic_fetch_xor(volatile _Tp* __o, _Tp __op, int)
659{
660 _Tp __tmp = *__o;
661 *__o ^= __op;
662 return __tmp;
663}
664
Howard Hinnant4777bf22010-12-06 23:10:08 +0000665//// End Temporary Intrinsics ////
666
Howard Hinnant8f73c632010-09-27 21:17:38 +0000667_LIBCPP_BEGIN_NAMESPACE_STD
668
Howard Hinnantd1176e22010-09-28 17:13:38 +0000669typedef enum memory_order
670{
671 memory_order_relaxed, memory_order_consume, memory_order_acquire,
672 memory_order_release, memory_order_acq_rel, memory_order_seq_cst
673} memory_order;
674
675template <class _Tp>
676inline _LIBCPP_INLINE_VISIBILITY
677_Tp
678kill_dependency(_Tp __y)
679{
680 return __y;
681}
Howard Hinnant8f73c632010-09-27 21:17:38 +0000682
Howard Hinnant91e2f262010-12-07 20:46:14 +0000683// general atomic<T>
684
685template <class _Tp, bool = is_integral<_Tp>::value && !is_same<_Tp, bool>::value>
686struct __atomic_base // false
687{
688 _Tp __a_;
689
690 _LIBCPP_INLINE_VISIBILITY
691 bool is_lock_free() const volatile
692 {return __atomic_is_lock_free(_Tp());}
693 _LIBCPP_INLINE_VISIBILITY
694 bool is_lock_free() const
695 {return __atomic_is_lock_free(_Tp());}
696 _LIBCPP_INLINE_VISIBILITY
697 void store(_Tp __d, memory_order __m = memory_order_seq_cst) volatile
698 {__atomic_store(&__a_, __d, __m);}
699 _LIBCPP_INLINE_VISIBILITY
700 void store(_Tp __d, memory_order __m = memory_order_seq_cst)
701 {__atomic_store(&__a_, __d, __m);}
702 _LIBCPP_INLINE_VISIBILITY
703 _Tp load(memory_order __m = memory_order_seq_cst) const volatile
704 {return __atomic_load(&__a_, __m);}
705 _LIBCPP_INLINE_VISIBILITY
706 _Tp load(memory_order __m = memory_order_seq_cst) const
707 {return __atomic_load(&__a_, __m);}
708 _LIBCPP_INLINE_VISIBILITY
709 operator _Tp() const volatile {return load();}
710 _LIBCPP_INLINE_VISIBILITY
711 operator _Tp() const {return load();}
712 _LIBCPP_INLINE_VISIBILITY
713 _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst) volatile
714 {return __atomic_exchange(&__a_, __d, __m);}
715 _LIBCPP_INLINE_VISIBILITY
716 _Tp exchange(_Tp __d, memory_order __m = memory_order_seq_cst)
717 {return __atomic_exchange(&__a_, __d, __m);}
718 _LIBCPP_INLINE_VISIBILITY
719 bool compare_exchange_weak(_Tp& __e, _Tp __d,
720 memory_order __s, memory_order __f) volatile
721 {return __atomic_compare_exchange_weak(&__a_, &__e, __d, __s, __f);}
722 _LIBCPP_INLINE_VISIBILITY
723 bool compare_exchange_weak(_Tp& __e, _Tp __d,
724 memory_order __s, memory_order __f)
725 {return __atomic_compare_exchange_weak(&__a_, &__e, __d, __s, __f);}
726 _LIBCPP_INLINE_VISIBILITY
727 bool compare_exchange_strong(_Tp& __e, _Tp __d,
728 memory_order __s, memory_order __f) volatile
729 {return __atomic_compare_exchange_strong(&__a_, &__e, __d, __s, __f);}
730 _LIBCPP_INLINE_VISIBILITY
731 bool compare_exchange_strong(_Tp& __e, _Tp __d,
732 memory_order __s, memory_order __f)
733 {return __atomic_compare_exchange_strong(&__a_, &__e, __d, __s, __f);}
734 _LIBCPP_INLINE_VISIBILITY
735 bool compare_exchange_weak(_Tp& __e, _Tp __d,
736 memory_order __m = memory_order_seq_cst) volatile
737 {return __atomic_compare_exchange_weak(&__a_, &__e, __d, __m, __m);}
738 _LIBCPP_INLINE_VISIBILITY
739 bool compare_exchange_weak(_Tp& __e, _Tp __d,
740 memory_order __m = memory_order_seq_cst)
741 {return __atomic_compare_exchange_weak(&__a_, &__e, __d, __m, __m);}
742 _LIBCPP_INLINE_VISIBILITY
743 bool compare_exchange_strong(_Tp& __e, _Tp __d,
744 memory_order __m = memory_order_seq_cst) volatile
745 {return __atomic_compare_exchange_strong(&__a_, &__e, __d, __m, __m);}
746 _LIBCPP_INLINE_VISIBILITY
747 bool compare_exchange_strong(_Tp& __e, _Tp __d,
748 memory_order __m = memory_order_seq_cst)
749 {return __atomic_compare_exchange_strong(&__a_, &__e, __d, __m, __m);}
750
751 _LIBCPP_INLINE_VISIBILITY
752 __atomic_base() {} // = default;
753 _LIBCPP_INLINE_VISIBILITY
754 /*constexpr*/ __atomic_base(_Tp __d) : __a_(__d) {}
755 __atomic_base(const __atomic_base&) = delete;
756 __atomic_base& operator=(const __atomic_base&) = delete;
757 __atomic_base& operator=(const __atomic_base&) volatile = delete;
758 _LIBCPP_INLINE_VISIBILITY
759 _Tp operator=(_Tp __d) volatile
760 {store(__d); return __d;}
761 _LIBCPP_INLINE_VISIBILITY
762 _Tp operator=(_Tp __d)
763 {store(__d); return __d;}
764};
765
766// atomic<Integral>
767
768template <class _Tp>
769struct __atomic_base<_Tp, true>
770 : public __atomic_base<_Tp, false>
771{
772 typedef __atomic_base<_Tp, false> __base;
773 _LIBCPP_INLINE_VISIBILITY
774 __atomic_base() {} // = default;
775 _LIBCPP_INLINE_VISIBILITY
776 /*constexpr*/ __atomic_base(_Tp __d) : __base(__d) {}
777
778 _LIBCPP_INLINE_VISIBILITY
779 _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst) volatile
780 {return __atomic_fetch_add(&this->__a_, __op, __m);}
781 _LIBCPP_INLINE_VISIBILITY
782 _Tp fetch_add(_Tp __op, memory_order __m = memory_order_seq_cst)
783 {return __atomic_fetch_add(&this->__a_, __op, __m);}
784 _LIBCPP_INLINE_VISIBILITY
785 _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst) volatile
786 {return __atomic_fetch_sub(&this->__a_, __op, __m);}
787 _LIBCPP_INLINE_VISIBILITY
788 _Tp fetch_sub(_Tp __op, memory_order __m = memory_order_seq_cst)
789 {return __atomic_fetch_sub(&this->__a_, __op, __m);}
790 _LIBCPP_INLINE_VISIBILITY
791 _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst) volatile
792 {return __atomic_fetch_and(&this->__a_, __op, __m);}
793 _LIBCPP_INLINE_VISIBILITY
794 _Tp fetch_and(_Tp __op, memory_order __m = memory_order_seq_cst)
795 {return __atomic_fetch_and(&this->__a_, __op, __m);}
796 _LIBCPP_INLINE_VISIBILITY
797 _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst) volatile
798 {return __atomic_fetch_or(&this->__a_, __op, __m);}
799 _LIBCPP_INLINE_VISIBILITY
800 _Tp fetch_or(_Tp __op, memory_order __m = memory_order_seq_cst)
801 {return __atomic_fetch_or(&this->__a_, __op, __m);}
802 _LIBCPP_INLINE_VISIBILITY
803 _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst) volatile
804 {return __atomic_fetch_xor(&this->__a_, __op, __m);}
805 _LIBCPP_INLINE_VISIBILITY
806 _Tp fetch_xor(_Tp __op, memory_order __m = memory_order_seq_cst)
807 {return __atomic_fetch_xor(&this->__a_, __op, __m);}
808
809 _LIBCPP_INLINE_VISIBILITY
810 _Tp operator++(int) volatile {return fetch_add(_Tp(1));}
811 _LIBCPP_INLINE_VISIBILITY
812 _Tp operator++(int) {return fetch_add(_Tp(1));}
813 _LIBCPP_INLINE_VISIBILITY
814 _Tp operator--(int) volatile {return fetch_sub(_Tp(1));}
815 _LIBCPP_INLINE_VISIBILITY
816 _Tp operator--(int) {return fetch_sub(_Tp(1));}
817 _LIBCPP_INLINE_VISIBILITY
818 _Tp operator++() volatile {return fetch_add(_Tp(1)) + _Tp(1);}
819 _LIBCPP_INLINE_VISIBILITY
820 _Tp operator++() {return fetch_add(_Tp(1)) + _Tp(1);}
821 _LIBCPP_INLINE_VISIBILITY
822 _Tp operator--() volatile {return fetch_sub(_Tp(1)) - _Tp(1);}
823 _LIBCPP_INLINE_VISIBILITY
824 _Tp operator--() {return fetch_sub(_Tp(1)) - _Tp(1);}
825 _LIBCPP_INLINE_VISIBILITY
826 _Tp operator+=(_Tp __op) volatile {return fetch_add(__op) + __op;}
827 _LIBCPP_INLINE_VISIBILITY
828 _Tp operator+=(_Tp __op) {return fetch_add(__op) + __op;}
829 _LIBCPP_INLINE_VISIBILITY
830 _Tp operator-=(_Tp __op) volatile {return fetch_sub(__op) - __op;}
831 _LIBCPP_INLINE_VISIBILITY
832 _Tp operator-=(_Tp __op) {return fetch_sub(__op) - __op;}
833 _LIBCPP_INLINE_VISIBILITY
834 _Tp operator&=(_Tp __op) volatile {return fetch_and(__op) & __op;}
835 _LIBCPP_INLINE_VISIBILITY
836 _Tp operator&=(_Tp __op) {return fetch_and(__op) & __op;}
837 _LIBCPP_INLINE_VISIBILITY
838 _Tp operator|=(_Tp __op) volatile {return fetch_or(__op) | __op;}
839 _LIBCPP_INLINE_VISIBILITY
840 _Tp operator|=(_Tp __op) {return fetch_or(__op) | __op;}
841 _LIBCPP_INLINE_VISIBILITY
842 _Tp operator^=(_Tp __op) volatile {return fetch_xor(__op) ^ __op;}
843 _LIBCPP_INLINE_VISIBILITY
844 _Tp operator^=(_Tp __op) {return fetch_xor(__op) ^ __op;}
845};
846
847// atomic<T>
848
849template <class _Tp>
850struct atomic
851 : public __atomic_base<_Tp>
852{
853 typedef __atomic_base<_Tp> __base;
854 _LIBCPP_INLINE_VISIBILITY
855 atomic() {} // = default;
856 _LIBCPP_INLINE_VISIBILITY
857 /*constexpr*/ atomic(_Tp __d) : __base(__d) {}
858};
859
860// atomic<T*>
861
862template <class _Tp>
863struct atomic<_Tp*>
864 : public __atomic_base<_Tp*>
865{
866 typedef __atomic_base<_Tp> __base;
867 _LIBCPP_INLINE_VISIBILITY
868 atomic() {} // = default;
869 _LIBCPP_INLINE_VISIBILITY
870 /*constexpr*/ atomic(_Tp* __d) : __base(__d) {}
871
872 _LIBCPP_INLINE_VISIBILITY
873 _Tp* fetch_add(ptrdiff_t __op, memory_order __m = memory_order_seq_cst)
874 volatile
875 {return __atomic_fetch_add(&this->__a_, __op, __m);}
876 _LIBCPP_INLINE_VISIBILITY
877 _Tp* fetch_add(ptrdiff_t __op, memory_order __m = memory_order_seq_cst)
878 {return __atomic_fetch_add(&this->__a_, __op, __m);}
879 _LIBCPP_INLINE_VISIBILITY
880 _Tp* fetch_sub(ptrdiff_t __op, memory_order __m = memory_order_seq_cst)
881 volatile
882 {return __atomic_fetch_sub(&this->__a_, __op, __m);}
883 _LIBCPP_INLINE_VISIBILITY
884 _Tp* fetch_sub(ptrdiff_t __op, memory_order __m = memory_order_seq_cst)
885 {return __atomic_fetch_sub(&this->__a_, __op, __m);}
886
887 _LIBCPP_INLINE_VISIBILITY
888 _Tp* operator++(int) volatile {return fetch_add(1);}
889 _LIBCPP_INLINE_VISIBILITY
890 _Tp* operator++(int) {return fetch_add(1);}
891 _LIBCPP_INLINE_VISIBILITY
892 _Tp* operator--(int) volatile {return fetch_sub(1);}
893 _LIBCPP_INLINE_VISIBILITY
894 _Tp* operator--(int) {return fetch_sub(1);}
895 _LIBCPP_INLINE_VISIBILITY
896 _Tp* operator++() volatile {return fetch_add(1) + 1;}
897 _LIBCPP_INLINE_VISIBILITY
898 _Tp* operator++() {return fetch_add(1) + 1;}
899 _LIBCPP_INLINE_VISIBILITY
900 _Tp* operator--() volatile {return fetch_sub(1) - 1;}
901 _LIBCPP_INLINE_VISIBILITY
902 _Tp* operator--() {return fetch_sub(1) - 1;}
903 _LIBCPP_INLINE_VISIBILITY
904 _Tp* operator+=(ptrdiff_t __op) volatile {return fetch_add(__op) + __op;}
905 _LIBCPP_INLINE_VISIBILITY
906 _Tp* operator+=(ptrdiff_t __op) {return fetch_add(__op) + __op;}
907 _LIBCPP_INLINE_VISIBILITY
908 _Tp* operator-=(ptrdiff_t __op) volatile {return fetch_sub(__op) - __op;}
909 _LIBCPP_INLINE_VISIBILITY
910 _Tp* operator-=(ptrdiff_t __op) {return fetch_sub(__op) - __op;}
911};
Howard Hinnant4777bf22010-12-06 23:10:08 +0000912
913// atomic_is_lock_free
914
915template <class _Tp>
916inline _LIBCPP_INLINE_VISIBILITY
917bool
Howard Hinnant91e2f262010-12-07 20:46:14 +0000918atomic_is_lock_free(const volatile atomic<_Tp>* __o)
Howard Hinnant4777bf22010-12-06 23:10:08 +0000919{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000920 return __o->is_lock_free();
Howard Hinnant4777bf22010-12-06 23:10:08 +0000921}
922
923template <class _Tp>
924inline _LIBCPP_INLINE_VISIBILITY
925bool
Howard Hinnant91e2f262010-12-07 20:46:14 +0000926atomic_is_lock_free(const atomic<_Tp>* __o)
Howard Hinnant4777bf22010-12-06 23:10:08 +0000927{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000928 return __o->is_lock_free();
Howard Hinnant4777bf22010-12-06 23:10:08 +0000929}
930
931// atomic_init
932
933template <class _Tp>
934inline _LIBCPP_INLINE_VISIBILITY
935void
936atomic_init(volatile atomic<_Tp>* __o, _Tp __d)
937{
938 __o->__a_ = __d;
939}
940
941template <class _Tp>
942inline _LIBCPP_INLINE_VISIBILITY
943void
944atomic_init(atomic<_Tp>* __o, _Tp __d)
945{
946 __o->__a_ = __d;
947}
948
949// atomic_store
950
951template <class _Tp>
952inline _LIBCPP_INLINE_VISIBILITY
953void
954atomic_store(volatile atomic<_Tp>* __o, _Tp __d)
955{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000956 __o->store(__d);
Howard Hinnant4777bf22010-12-06 23:10:08 +0000957}
958
959template <class _Tp>
960inline _LIBCPP_INLINE_VISIBILITY
961void
962atomic_store(atomic<_Tp>* __o, _Tp __d)
963{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000964 __o->store(__d);
Howard Hinnant4777bf22010-12-06 23:10:08 +0000965}
966
967// atomic_store_explicit
968
969template <class _Tp>
970inline _LIBCPP_INLINE_VISIBILITY
971void
972atomic_store_explicit(volatile atomic<_Tp>* __o, _Tp __d, memory_order __m)
973{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000974 __o->store(__d, __m);
Howard Hinnant4777bf22010-12-06 23:10:08 +0000975}
976
977template <class _Tp>
978inline _LIBCPP_INLINE_VISIBILITY
979void
980atomic_store_explicit(atomic<_Tp>* __o, _Tp __d, memory_order __m)
981{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000982 __o->store(__d, __m);
Howard Hinnant4777bf22010-12-06 23:10:08 +0000983}
984
985// atomic_load
986
987template <class _Tp>
988inline _LIBCPP_INLINE_VISIBILITY
989_Tp
990atomic_load(const volatile atomic<_Tp>* __o)
991{
Howard Hinnant91e2f262010-12-07 20:46:14 +0000992 return __o->load();
Howard Hinnant4777bf22010-12-06 23:10:08 +0000993}
994
995template <class _Tp>
996inline _LIBCPP_INLINE_VISIBILITY
997_Tp
998atomic_load(const atomic<_Tp>* __o)
999{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001000 return __o->load();
Howard Hinnant4777bf22010-12-06 23:10:08 +00001001}
1002
1003// atomic_load_explicit
1004
1005template <class _Tp>
1006inline _LIBCPP_INLINE_VISIBILITY
1007_Tp
1008atomic_load_explicit(const volatile atomic<_Tp>* __o, memory_order __m)
1009{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001010 return __o->load(__m);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001011}
1012
1013template <class _Tp>
1014inline _LIBCPP_INLINE_VISIBILITY
1015_Tp
1016atomic_load_explicit(const atomic<_Tp>* __o, memory_order __m)
1017{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001018 return __o->load(__m);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001019}
1020
1021// atomic_exchange
1022
1023template <class _Tp>
1024inline _LIBCPP_INLINE_VISIBILITY
1025_Tp
1026atomic_exchange(volatile atomic<_Tp>* __o, _Tp __d)
1027{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001028 return __o->exchange(__d);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001029}
1030
1031template <class _Tp>
1032inline _LIBCPP_INLINE_VISIBILITY
1033_Tp
1034atomic_exchange(atomic<_Tp>* __o, _Tp __d)
1035{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001036 return __o->exchange(__d);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001037}
1038
1039// atomic_exchange_explicit
1040
1041template <class _Tp>
1042inline _LIBCPP_INLINE_VISIBILITY
1043_Tp
1044atomic_exchange_explicit(volatile atomic<_Tp>* __o, _Tp __d, memory_order __m)
1045{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001046 return __o->exchange(__d, __m);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001047}
1048
1049template <class _Tp>
1050inline _LIBCPP_INLINE_VISIBILITY
1051_Tp
1052atomic_exchange_explicit(atomic<_Tp>* __o, _Tp __d, memory_order __m)
1053{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001054 return __o->exchange(__d, __m);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001055}
1056
1057// atomic_compare_exchange_weak
1058
1059template <class _Tp>
1060inline _LIBCPP_INLINE_VISIBILITY
1061bool
1062atomic_compare_exchange_weak(volatile atomic<_Tp>* __o, _Tp* __e, _Tp __d)
1063{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001064 return __o->compare_exchange_weak(*__e, __d);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001065}
1066
1067template <class _Tp>
1068inline _LIBCPP_INLINE_VISIBILITY
1069bool
1070atomic_compare_exchange_weak(atomic<_Tp>* __o, _Tp* __e, _Tp __d)
1071{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001072 return __o->compare_exchange_weak(*__e, __d);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001073}
1074
1075// atomic_compare_exchange_strong
1076
1077template <class _Tp>
1078inline _LIBCPP_INLINE_VISIBILITY
1079bool
1080atomic_compare_exchange_strong(volatile atomic<_Tp>* __o, _Tp* __e, _Tp __d)
1081{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001082 return __o->compare_exchange_strong(*__e, __d);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001083}
1084
1085template <class _Tp>
1086inline _LIBCPP_INLINE_VISIBILITY
1087bool
1088atomic_compare_exchange_strong(atomic<_Tp>* __o, _Tp* __e, _Tp __d)
1089{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001090 return __o->compare_exchange_strong(*__e, __d);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001091}
1092
1093// atomic_compare_exchange_weak_explicit
1094
1095template <class _Tp>
1096inline _LIBCPP_INLINE_VISIBILITY
1097bool
1098atomic_compare_exchange_weak_explicit(volatile atomic<_Tp>* __o, _Tp* __e,
1099 _Tp __d,
1100 memory_order __s, memory_order __f)
1101{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001102 return __o->compare_exchange_weak(*__e, __d, __s, __f);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001103}
1104
1105template <class _Tp>
1106inline _LIBCPP_INLINE_VISIBILITY
1107bool
1108atomic_compare_exchange_weak_explicit(atomic<_Tp>* __o, _Tp* __e, _Tp __d,
1109 memory_order __s, memory_order __f)
1110{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001111 return __o->compare_exchange_weak(*__e, __d, __s, __f);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001112}
1113
1114// atomic_compare_exchange_strong_explicit
1115
1116template <class _Tp>
1117inline _LIBCPP_INLINE_VISIBILITY
1118bool
1119atomic_compare_exchange_strong_explicit(volatile atomic<_Tp>* __o,
1120 _Tp* __e, _Tp __d,
1121 memory_order __s, memory_order __f)
1122{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001123 return __o->compare_exchange_strong(*__e, __d, __s, __f);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001124}
1125
1126template <class _Tp>
1127inline _LIBCPP_INLINE_VISIBILITY
1128bool
1129atomic_compare_exchange_strong_explicit(atomic<_Tp>* __o, _Tp* __e,
1130 _Tp __d,
1131 memory_order __s, memory_order __f)
1132{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001133 return __o->compare_exchange_strong(*__e, __d, __s, __f);
Howard Hinnant4777bf22010-12-06 23:10:08 +00001134}
1135
Howard Hinnant91e2f262010-12-07 20:46:14 +00001136// atomic_fetch_add
Howard Hinnant4777bf22010-12-06 23:10:08 +00001137
1138template <class _Tp>
Howard Hinnant91e2f262010-12-07 20:46:14 +00001139inline _LIBCPP_INLINE_VISIBILITY
1140typename enable_if
1141<
1142 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1143 _Tp
1144>::type
1145atomic_fetch_add(volatile atomic<_Tp>* __o, _Tp __op)
Howard Hinnant4777bf22010-12-06 23:10:08 +00001146{
Howard Hinnant91e2f262010-12-07 20:46:14 +00001147 return __o->fetch_add(__op);
1148}
Howard Hinnant4777bf22010-12-06 23:10:08 +00001149
Howard Hinnant91e2f262010-12-07 20:46:14 +00001150template <class _Tp>
1151inline _LIBCPP_INLINE_VISIBILITY
1152typename enable_if
1153<
1154 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1155 _Tp
1156>::type
1157atomic_fetch_add(atomic<_Tp>* __o, _Tp __op)
1158{
1159 return __o->fetch_add(__op);
1160}
Howard Hinnant4777bf22010-12-06 23:10:08 +00001161
Howard Hinnant91e2f262010-12-07 20:46:14 +00001162template <class _Tp>
1163inline _LIBCPP_INLINE_VISIBILITY
1164_Tp*
1165atomic_fetch_add(volatile atomic<_Tp*>* __o, ptrdiff_t __op)
1166{
1167 return __o->fetch_add(__op);
1168}
1169
1170template <class _Tp>
1171inline _LIBCPP_INLINE_VISIBILITY
1172_Tp*
1173atomic_fetch_add(atomic<_Tp*>* __o, ptrdiff_t __op)
1174{
1175 return __o->fetch_add(__op);
1176}
1177
1178// atomic_fetch_add_explicit
1179
1180template <class _Tp>
1181inline _LIBCPP_INLINE_VISIBILITY
1182typename enable_if
1183<
1184 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1185 _Tp
1186>::type
1187atomic_fetch_add_explicit(volatile atomic<_Tp>* __o, _Tp __op, memory_order __m)
1188{
1189 return __o->fetch_add(__op, __m);
1190}
1191
1192template <class _Tp>
1193inline _LIBCPP_INLINE_VISIBILITY
1194typename enable_if
1195<
1196 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1197 _Tp
1198>::type
1199atomic_fetch_add_explicit(atomic<_Tp>* __o, _Tp __op, memory_order __m)
1200{
1201 return __o->fetch_add(__op, __m);
1202}
1203
1204template <class _Tp>
1205inline _LIBCPP_INLINE_VISIBILITY
1206_Tp*
1207atomic_fetch_add_explicit(volatile atomic<_Tp*>* __o, ptrdiff_t __op,
1208 memory_order __m)
1209{
1210 return __o->fetch_add(__op, __m);
1211}
1212
1213template <class _Tp>
1214inline _LIBCPP_INLINE_VISIBILITY
1215_Tp*
1216atomic_fetch_add_explicit(atomic<_Tp*>* __o, ptrdiff_t __op, memory_order __m)
1217{
1218 return __o->fetch_add(__op, __m);
1219}
1220
1221// atomic_fetch_sub
1222
1223template <class _Tp>
1224inline _LIBCPP_INLINE_VISIBILITY
1225typename enable_if
1226<
1227 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1228 _Tp
1229>::type
1230atomic_fetch_sub(volatile atomic<_Tp>* __o, _Tp __op)
1231{
1232 return __o->fetch_sub(__op);
1233}
1234
1235template <class _Tp>
1236inline _LIBCPP_INLINE_VISIBILITY
1237typename enable_if
1238<
1239 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1240 _Tp
1241>::type
1242atomic_fetch_sub(atomic<_Tp>* __o, _Tp __op)
1243{
1244 return __o->fetch_sub(__op);
1245}
1246
1247template <class _Tp>
1248inline _LIBCPP_INLINE_VISIBILITY
1249_Tp*
1250atomic_fetch_sub(volatile atomic<_Tp*>* __o, ptrdiff_t __op)
1251{
1252 return __o->fetch_sub(__op);
1253}
1254
1255template <class _Tp>
1256inline _LIBCPP_INLINE_VISIBILITY
1257_Tp*
1258atomic_fetch_sub(atomic<_Tp*>* __o, ptrdiff_t __op)
1259{
1260 return __o->fetch_sub(__op);
1261}
1262
1263// atomic_fetch_sub_explicit
1264
1265template <class _Tp>
1266inline _LIBCPP_INLINE_VISIBILITY
1267typename enable_if
1268<
1269 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1270 _Tp
1271>::type
1272atomic_fetch_sub_explicit(volatile atomic<_Tp>* __o, _Tp __op, memory_order __m)
1273{
1274 return __o->fetch_sub(__op, __m);
1275}
1276
1277template <class _Tp>
1278inline _LIBCPP_INLINE_VISIBILITY
1279typename enable_if
1280<
1281 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1282 _Tp
1283>::type
1284atomic_fetch_sub_explicit(atomic<_Tp>* __o, _Tp __op, memory_order __m)
1285{
1286 return __o->fetch_sub(__op, __m);
1287}
1288
1289template <class _Tp>
1290inline _LIBCPP_INLINE_VISIBILITY
1291_Tp*
1292atomic_fetch_sub_explicit(volatile atomic<_Tp*>* __o, ptrdiff_t __op,
1293 memory_order __m)
1294{
1295 return __o->fetch_sub(__op, __m);
1296}
1297
1298template <class _Tp>
1299inline _LIBCPP_INLINE_VISIBILITY
1300_Tp*
1301atomic_fetch_sub_explicit(atomic<_Tp*>* __o, ptrdiff_t __op, memory_order __m)
1302{
1303 return __o->fetch_sub(__op, __m);
1304}
1305
1306// atomic_fetch_and
1307
1308template <class _Tp>
1309inline _LIBCPP_INLINE_VISIBILITY
1310typename enable_if
1311<
1312 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1313 _Tp
1314>::type
1315atomic_fetch_and(volatile atomic<_Tp>* __o, _Tp __op)
1316{
1317 return __o->fetch_and(__op);
1318}
1319
1320template <class _Tp>
1321inline _LIBCPP_INLINE_VISIBILITY
1322typename enable_if
1323<
1324 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1325 _Tp
1326>::type
1327atomic_fetch_and(atomic<_Tp>* __o, _Tp __op)
1328{
1329 return __o->fetch_and(__op);
1330}
1331
1332// atomic_fetch_and_explicit
1333
1334template <class _Tp>
1335inline _LIBCPP_INLINE_VISIBILITY
1336typename enable_if
1337<
1338 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1339 _Tp
1340>::type
1341atomic_fetch_and_explicit(volatile atomic<_Tp>* __o, _Tp __op, memory_order __m)
1342{
1343 return __o->fetch_and(__op, __m);
1344}
1345
1346template <class _Tp>
1347inline _LIBCPP_INLINE_VISIBILITY
1348typename enable_if
1349<
1350 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1351 _Tp
1352>::type
1353atomic_fetch_and_explicit(atomic<_Tp>* __o, _Tp __op, memory_order __m)
1354{
1355 return __o->fetch_and(__op, __m);
1356}
1357
1358// atomic_fetch_or
1359
1360template <class _Tp>
1361inline _LIBCPP_INLINE_VISIBILITY
1362typename enable_if
1363<
1364 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1365 _Tp
1366>::type
1367atomic_fetch_or(volatile atomic<_Tp>* __o, _Tp __op)
1368{
1369 return __o->fetch_or(__op);
1370}
1371
1372template <class _Tp>
1373inline _LIBCPP_INLINE_VISIBILITY
1374typename enable_if
1375<
1376 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1377 _Tp
1378>::type
1379atomic_fetch_or(atomic<_Tp>* __o, _Tp __op)
1380{
1381 return __o->fetch_or(__op);
1382}
1383
1384// atomic_fetch_or_explicit
1385
1386template <class _Tp>
1387inline _LIBCPP_INLINE_VISIBILITY
1388typename enable_if
1389<
1390 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1391 _Tp
1392>::type
1393atomic_fetch_or_explicit(volatile atomic<_Tp>* __o, _Tp __op, memory_order __m)
1394{
1395 return __o->fetch_or(__op, __m);
1396}
1397
1398template <class _Tp>
1399inline _LIBCPP_INLINE_VISIBILITY
1400typename enable_if
1401<
1402 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1403 _Tp
1404>::type
1405atomic_fetch_or_explicit(atomic<_Tp>* __o, _Tp __op, memory_order __m)
1406{
1407 return __o->fetch_or(__op, __m);
1408}
1409
1410// atomic_fetch_xor
1411
1412template <class _Tp>
1413inline _LIBCPP_INLINE_VISIBILITY
1414typename enable_if
1415<
1416 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1417 _Tp
1418>::type
1419atomic_fetch_xor(volatile atomic<_Tp>* __o, _Tp __op)
1420{
1421 return __o->fetch_xor(__op);
1422}
1423
1424template <class _Tp>
1425inline _LIBCPP_INLINE_VISIBILITY
1426typename enable_if
1427<
1428 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1429 _Tp
1430>::type
1431atomic_fetch_xor(atomic<_Tp>* __o, _Tp __op)
1432{
1433 return __o->fetch_xor(__op);
1434}
1435
1436// atomic_fetch_xor_explicit
1437
1438template <class _Tp>
1439inline _LIBCPP_INLINE_VISIBILITY
1440typename enable_if
1441<
1442 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1443 _Tp
1444>::type
1445atomic_fetch_xor_explicit(volatile atomic<_Tp>* __o, _Tp __op, memory_order __m)
1446{
1447 return __o->fetch_xor(__op, __m);
1448}
1449
1450template <class _Tp>
1451inline _LIBCPP_INLINE_VISIBILITY
1452typename enable_if
1453<
1454 is_integral<_Tp>::value && !is_same<_Tp, bool>::value,
1455 _Tp
1456>::type
1457atomic_fetch_xor_explicit(atomic<_Tp>* __o, _Tp __op, memory_order __m)
1458{
1459 return __o->fetch_xor(__op, __m);
1460}
Howard Hinnant4777bf22010-12-06 23:10:08 +00001461
1462/*
Howard Hinnanted760f42010-09-29 18:13:54 +00001463// flag type and operations
1464
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001465typedef bool __atomic_flag__;
Howard Hinnant6cac2c22010-10-05 14:02:23 +00001466
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001467struct atomic_flag;
Howard Hinnanted760f42010-09-29 18:13:54 +00001468
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001469bool atomic_flag_test_and_set(volatile atomic_flag*);
1470bool atomic_flag_test_and_set(atomic_flag*);
1471bool atomic_flag_test_and_set_explicit(volatile atomic_flag*, memory_order);
1472bool atomic_flag_test_and_set_explicit(atomic_flag*, memory_order);
1473void atomic_flag_clear(volatile atomic_flag*);
1474void atomic_flag_clear(atomic_flag*);
1475void atomic_flag_clear_explicit(volatile atomic_flag*, memory_order);
1476void atomic_flag_clear_explicit(atomic_flag*, memory_order);
1477
1478typedef struct _LIBCPP_VISIBLE atomic_flag
Howard Hinnanted760f42010-09-29 18:13:54 +00001479{
Howard Hinnant6cac2c22010-10-05 14:02:23 +00001480 __atomic_flag__ __flg_;
Howard Hinnanted760f42010-09-29 18:13:54 +00001481
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001482 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001483 bool test_and_set(memory_order __o = memory_order_seq_cst) volatile
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001484 {return atomic_flag_test_and_set_explicit(this, __o);}
1485 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001486 bool test_and_set(memory_order __o = memory_order_seq_cst)
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001487 {return atomic_flag_test_and_set_explicit(this, __o);}
Howard Hinnanted760f42010-09-29 18:13:54 +00001488
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001489 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001490 void clear(memory_order __o = memory_order_seq_cst) volatile
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001491 {atomic_flag_clear_explicit(this, __o);}
1492 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001493 void clear(memory_order __o = memory_order_seq_cst)
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001494 {atomic_flag_clear_explicit(this, __o);}
Howard Hinnanted760f42010-09-29 18:13:54 +00001495
1496#ifndef _LIBCPP_HAS_NO_DEFAULTED_FUNCTIONS
1497 atomic_flag() = default;
1498#else
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001499 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant79101ae2010-09-30 21:05:29 +00001500 atomic_flag() {};
Howard Hinnanted760f42010-09-29 18:13:54 +00001501#endif
1502
Howard Hinnanted760f42010-09-29 18:13:54 +00001503#ifndef _LIBCPP_HAS_NO_DELETED_FUNCTIONS
1504 atomic_flag(const atomic_flag&) = delete;
1505 atomic_flag& operator=(const atomic_flag&) = delete;
1506 atomic_flag& operator=(const atomic_flag&) volatile = delete;
1507#else
1508private:
1509 atomic_flag(const atomic_flag&);
1510 atomic_flag& operator=(const atomic_flag&);
1511 atomic_flag& operator=(const atomic_flag&) volatile;
1512public:
1513#endif
Howard Hinnanted760f42010-09-29 18:13:54 +00001514} atomic_flag;
1515
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001516inline _LIBCPP_INLINE_VISIBILITY
1517bool
1518atomic_flag_test_and_set(volatile atomic_flag* __f)
1519{
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001520 return __atomic_exchange(&__f->__flg_, __atomic_flag__(true),
1521 memory_order_seq_cst)
Howard Hinnant6cac2c22010-10-05 14:02:23 +00001522 == __atomic_flag__(true);
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001523}
Howard Hinnanted760f42010-09-29 18:13:54 +00001524
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001525inline _LIBCPP_INLINE_VISIBILITY
1526bool
1527atomic_flag_test_and_set(atomic_flag* __f)
1528{
1529 return atomic_flag_test_and_set(const_cast<volatile atomic_flag*>(__f));
1530}
1531
1532inline _LIBCPP_INLINE_VISIBILITY
1533bool
1534atomic_flag_test_and_set_explicit(volatile atomic_flag* __f, memory_order __o)
1535{
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001536 return __atomic_exchange(&__f->__flg_, __atomic_flag__(true), __o)
Howard Hinnant6cac2c22010-10-05 14:02:23 +00001537 == __atomic_flag__(true);
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001538}
1539
1540inline _LIBCPP_INLINE_VISIBILITY
1541bool
1542atomic_flag_test_and_set_explicit(atomic_flag* __f, memory_order __o)
1543{
Howard Hinnant611fdaf2010-10-04 18:52:54 +00001544 return atomic_flag_test_and_set_explicit(const_cast<volatile atomic_flag*>
1545 (__f), __o);
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001546}
1547
1548inline _LIBCPP_INLINE_VISIBILITY
1549void
1550atomic_flag_clear(volatile atomic_flag* __f)
1551{
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001552 __atomic_store(&__f->__flg_, __atomic_flag__(false), memory_order_seq_cst);
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001553}
1554
1555inline _LIBCPP_INLINE_VISIBILITY
1556void
1557atomic_flag_clear(atomic_flag* __f)
1558{
1559 atomic_flag_clear(const_cast<volatile atomic_flag*>(__f));
1560}
1561
1562inline _LIBCPP_INLINE_VISIBILITY
1563void
1564atomic_flag_clear_explicit(volatile atomic_flag* __f, memory_order __o)
1565{
Howard Hinnant21ef47f2010-10-18 20:39:07 +00001566 __atomic_store(&__f->__flg_, __atomic_flag__(false), __o);
Howard Hinnant767ae2b2010-09-29 21:20:03 +00001567}
1568
1569inline _LIBCPP_INLINE_VISIBILITY
1570void
1571atomic_flag_clear_explicit(atomic_flag* __f, memory_order __o)
1572{
1573 atomic_flag_clear_explicit(const_cast<volatile atomic_flag*>(__f), __o);
1574}
1575
1576#define ATOMIC_FLAG_INIT {false}
Howard Hinnant611fdaf2010-10-04 18:52:54 +00001577#define ATOMIC_VAR_INIT(__v) {__v}
1578
1579inline _LIBCPP_INLINE_VISIBILITY
1580memory_order
1581__translate_memory_order(memory_order __o)
1582{
1583 switch (__o)
1584 {
1585 case memory_order_acq_rel:
1586 return memory_order_acquire;
1587 case memory_order_release:
1588 return memory_order_relaxed;
1589 }
1590 return __o;
1591}
1592
Howard Hinnant4777bf22010-12-06 23:10:08 +00001593*/
Howard Hinnant8f73c632010-09-27 21:17:38 +00001594_LIBCPP_END_NAMESPACE_STD
1595
1596#endif // _LIBCPP_ATOMIC