File: atomic

package info (click to toggle)
cppreference-doc 20170409-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 259,872 kB
  • sloc: xml: 570,184; python: 1,923; php: 520; makefile: 168; sh: 25; cpp: 9; ansic: 9
file content (426 lines) | stat: -rw-r--r-- 15,827 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/*  Copyright (C) 2015  Povilas Kanapickas <povilas@radix.lt>

    This file is part of cppreference-doc

    This work is licensed under the Creative Commons Attribution-ShareAlike 3.0
    Unported License. To view a copy of this license, visit
    http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative
    Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.

    Permission is granted to copy, distribute and/or modify this document
    under the terms of the GNU Free Documentation License, Version 1.3 or
    any later version published by the Free Software Foundation; with no
    Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
*/

#ifndef CPPREFERENCE_ATOMIC_H
#define CPPREFERENCE_ATOMIC_H

namespace std {

#if CPPREFERENCE_STDVER>= 2011

enum memory_order {
    memory_order_relaxed,
    memory_order_consume,
    memory_order_acquire,
    memory_order_release,
    memory_order_acq_rel,
    memory_order_seq_cst
};

template<class T>
struct atomic {
    atomic() = default;
    constexpr atomic(T desired);
    atomic(const atomic&) = delete;

    T operator=(T desired);
    T operator=(T desired) volatile;

    atomic& operator=(const atomic&) = delete;
    atomic& operator=(const atomic&) volatile = delete;

    bool is_lock_free() const;
    bool is_lock_free() const volatile;

    void store(T desired, std::memory_order order = std::memory_order_seq_cst);
    void store(T desired, std::memory_order order = std::memory_order_seq_cst) volatile;

    T load(std::memory_order order = std::memory_order_seq_cst) const;
    T load(std::memory_order order = std::memory_order_seq_cst) const volatile;

    operator T() const;
    operator T() const volatile;

    T exchange(T desired, std::memory_order order = std::memory_order_seq_cst);
    T exchange(T desired, std::memory_order order = std::memory_order_seq_cst) volatile;

    bool compare_exchange_weak(T& expected, T desired,
                               std::memory_order success,
                               std::memory_order failure);

    bool compare_exchange_weak(T& expected, T desired,

                               std::memory_order success,
                               std::memory_order failure) volatile;

    bool compare_exchange_weak(T& expected, T desired,
                               std::memory_order order =
                                   std::memory_order_seq_cst);

    bool compare_exchange_weak(T& expected, T desired,
                               std::memory_order order =
                                   std::memory_order_seq_cst) volatile;

    bool compare_exchange_strong(T& expected, T desired,
                                 std::memory_order success,
                                 std::memory_order failure);

    bool compare_exchange_strong(T& expected, T desired,
                                 std::memory_order success,
                                 std::memory_order failure) volatile;

    bool compare_exchange_strong(T& expected, T desired,
                                 std::memory_order order =
                                     std::memory_order_seq_cst);

    bool compare_exchange_strong(T& expected, T desired,
                                 std::memory_order order =
                                     std::memory_order_seq_cst) volatile;

    // member of atomic<Integral>
    T fetch_add(T arg,
                memory_order = std::memory_order_seq_cst);
    T fetch_add(T arg,
                memory_order = std::memory_order_seq_cst) volatile;

    // member of atomic<T*>
    T* fetch_add(std::ptrdiff_t arg,
                 memory_order = std::memory_order_seq_cst);
    T* fetch_add(std::ptrdiff_t arg,
                 memory_order = std::memory_order_seq_cst) volatile;

    // member of atomic<Integral>
    T fetch_sub(T arg,
                memory_order = std::memory_order_seq_cst);
    T fetch_sub(T arg,
                memory_order = std::memory_order_seq_cst) volatile;

    // member of atomic<T*>
    T* fetch_sub(std::ptrdiff_t arg,
                 memory_order = std::memory_order_seq_cst);
    T* fetch_sub(std::ptrdiff_t arg,
                 memory_order = std::memory_order_seq_cst) volatile;

    // member of atomic<Integral>
    T fetch_and(T arg,
                memory_order = std::memory_order_seq_cst);
    T fetch_and(T arg,
                memory_order = std::memory_order_seq_cst) volatile;

    // member of atomic<Integral>
    T fetch_or(T arg,
               memory_order = std::memory_order_seq_cst);
    T fetch_or(T arg,
               memory_order = std::memory_order_seq_cst) volatile;

    // member of atomic<Integral>
    T fetch_xor(T arg,
                memory_xorder = std::memory_xorder_seq_cst);
    T fetch_xor(T arg,
                memory_xorder = std::memory_xorder_seq_cst) volatile;

    // member of atomic<Integral>
    T operator++();
    T operator++() volatile;
    T operator++(int);
    T operator++(int) volatile;
    T operator--();
    T operator--() volatile;
    T operator--(int);
    T operator--(int) volatile;
    T operator+=(T arg);
    T operator+=(T arg) volatile;
    T operator-=(T arg);
    T operator-=(T arg) volatile;
    T operator&=(T arg);
    T operator&=(T arg) volatile;
    T operator|=(T arg);
    T operator|=(T arg) volatile;
    T operator^=(T arg);
    T operator^=(T arg) volatile;

    // member of atomic<T*>
    T* operator++();
    T* operator++() volatile;
    T* operator++(int);
    T* operator++(int) volatile;
    T* operator--();
    T* operator--() volatile;
    T* operator--(int);
    T* operator--(int) volatile;
    T* operator+=(std::ptrdiff_t arg);
    T* operator+=(std::ptrdiff_t arg) volatile;
    T* operator-=(std::ptrdiff_t arg);
    T* operator-=(std::ptrdiff_t arg) volatile;
};

typedef atomic<bool>               atomic_bool;
typedef atomic<char>               atomic_char;
typedef atomic<signed char>        atomic_schar;
typedef atomic<unsigned char>      atomic_uchar;
typedef atomic<short>              atomic_short;
typedef atomic<unsigned short>     atomic_ushort;
typedef atomic<int>                atomic_int;
typedef atomic<unsigned int>       atomic_uint;
typedef atomic<long>               atomic_long;
typedef atomic<unsigned long>      atomic_ulong;
typedef atomic<long long>          atomic_llong;
typedef atomic<unsigned long long> atomic_ullong;
typedef atomic<char16_t>           atomic_char16_t;
typedef atomic<char32_t>           atomic_char32_t;
typedef atomic<wchar_t>            atomic_wchar_t;

typedef atomic<int_least8_t>   atomic_int_least8_t;
typedef atomic<uint_least8_t>  atomic_uint_least8_t;
typedef atomic<int_least16_t>  atomic_int_least16_t;
typedef atomic<uint_least16_t> atomic_uint_least16_t;
typedef atomic<int_least32_t>  atomic_int_least32_t;
typedef atomic<uint_least32_t> atomic_uint_least32_t;
typedef atomic<int_least64_t>  atomic_int_least64_t;
typedef atomic<uint_least64_t> atomic_uint_least64_t;

typedef atomic<int_fast8_t>   atomic_int_fast8_t;
typedef atomic<uint_fast8_t>  atomic_uint_fast8_t;
typedef atomic<int_fast16_t>  atomic_int_fast16_t;
typedef atomic<uint_fast16_t> atomic_uint_fast16_t;
typedef atomic<int_fast32_t>  atomic_int_fast32_t;
typedef atomic<uint_fast32_t> atomic_uint_fast32_t;
typedef atomic<int_fast64_t>  atomic_int_fast64_t;
typedef atomic<uint_fast64_t> atomic_uint_fast64_t;

typedef atomic<intptr_t>  atomic_intptr_t;
typedef atomic<uintptr_t> atomic_uintptr_t;
typedef atomic<size_t>    atomic_size_t;
typedef atomic<ptrdiff_t> atomic_ptrdiff_t;
typedef atomic<intmax_t>  atomic_intmax_t;
typedef atomic<uintmax_t> atomic_uintmax_t;

template<class T>
bool atomic_is_lock_free(const volatile std::atomic<T>* obj);
template<class T>
bool atomic_is_lock_free(const std::atomic<T>* obj);

template<class T>
void atomic_store(std::atomic<T>* obj, T desr);
template<class T>
void atomic_store(volatile std::atomic<T>* obj, T desr);

template<class T>
void atomic_store_explicit(std::atomic<T>* obj, T desr,
                           std::memory_order order);
template<class T>
void atomic_store_explicit(volatile std::atomic<T>* obj, T desr,
                           std::memory_order order);

template<class T>
T atomic_load(const std::atomic<T>* obj);
template<class T>
T atomic_load(const volatile std::atomic<T>* obj);

template<class T>
T atomic_load_explicit(const std::atomic<T>* obj,
                       std::memory_order order);
template<class T>
T atomic_load_explicit(const volatile std::atomic<T>* obj,
                       std::memory_order order);

template<class T>
T atomic_exchange(std::atomic<T>* obj, T desr);
template<class T>
T atomic_exchange(volatile std::atomic<T>* obj, T desr);

template<class T>
T atomic_exchange_explicit(std::atomic<T>* obj, T desr,
                           std::memory_order order);
template<class T>
T atomic_exchange_explicit(volatile std::atomic<T>* obj, T desr,
                           std::memory_order order);

template<class T>
bool atomic_compare_exchange_weak(std::atomic<T>* obj,
                                  T* expected, T desired);
template<class T>
bool atomic_compare_exchange_weak(volatile std::atomic<T>* obj,
                                  T* expected, T desired);

template<class T>
bool atomic_compare_exchange_strong(std::atomic<T>* obj,
                                    T* expected, T desired);
template<class T>
bool atomic_compare_exchange_strong(volatile std::atomic<T>* obj,
                                    T* expected, T desired);

template<class T>
bool atomic_compare_exchange_weak_explicit(std::atomic<T>* obj,
        T* expected, T desired,
        std::memory_order succ,
        std::memory_order fail);
template<class T>
bool atomic_compare_exchange_weak_explicit(volatile std::atomic<T>* obj,
        T* expected, T desired,
        std::memory_order succ,
        std::memory_order fail);

template<class T>
bool atomic_compare_exchange_strong_explicit(std::atomic<T>* obj,
        T* expected, T desired,
        std::memory_order succ,
        std::memory_order fail);
template<class T>
bool atomic_compare_exchange_strong_explicit(volatile std::atomic<T>* obj,
        T* expected, T desired,
        std::memory_order succ,
        std::memory_order fail);

template<class Integral>
Integral atomic_fetch_add(std::atomic<Integral>* obj, Integral arg);
template<class Integral>
Integral atomic_fetch_add(volatile std::atomic<Integral>* obj, Integral arg);

template<class Integral>
Integral atomic_fetch_add_explicit(std::atomic<Integral>* obj, Integral arg,
                                   std::memory_order order);
template<class Integral>
Integral atomic_fetch_add_explicit(volatile std::atomic<Integral>* obj, Integral arg,
                                   std::memory_order order);

template<class T>
T* atomic_fetch_add(std::atomic<T*>* obj, std::ptrdiff_t arg);
template<class T>
T* atomic_fetch_add(volatile std::atomic<T*>* obj, std::ptrdiff_t arg);

template<class T>
T* atomic_fetch_add_explicit(std::atomic<T*>* obj, std::ptrdiff_t arg,
                             std::memory_order order);
template<class T>
T* atomic_fetch_add_explicit(volatile std::atomic<T*>* obj, std::ptrdiff_t arg,
                             std::memory_order order);

template<class Integral>
Integral atomic_fetch_sub(std::atomic<Integral>* obj, Integral arg);
template<class Integral>
Integral atomic_fetch_sub(volatile std::atomic<Integral>* obj, Integral arg);

template<class Integral>
Integral atomic_fetch_sub_explicit(std::atomic<Integral>* obj, Integral arg,
                                   std::memory_order order);
template<class Integral>
Integral atomic_fetch_sub_explicit(volatile std::atomic<Integral>* obj, Integral arg,
                                   std::memory_order order);

template<class T>
T* atomic_fetch_sub(std::atomic<T*>* obj, std::ptrdiff_t arg);
template<class T>
T* atomic_fetch_sub(volatile std::atomic<T*>* obj, std::ptrdiff_t arg);

template<class T>
T* atomic_fetch_sub_explicit(std::atomic<T*>* obj, std::ptrdiff_t arg,
                             std::memory_order order);
template<class T>
T* atomic_fetch_sub_explicit(volatile std::atomic<T*>* obj, std::ptrdiff_t arg,
                             std::memory_order order);

template<class Integral>
Integral atomic_fetch_and(std::atomic<Integral>* obj, Integral arg);
template<class Integral>
Integral atomic_fetch_and(volatile std::atomic<Integral>* obj, Integral arg);

template<class Integral>
Integral atomic_fetch_and_explicit(std::atomic<Integral>* obj,
                                   Integral arg,
                                   std::memory_order order);

template<class Integral>
Integral atomic_fetch_and_explicit(volatile std::atomic<Integral>* obj,
                                   Integral arg,
                                   std::memory_order order);
template<class Integral>
Integral atomic_fetch_or(std::atomic<Integral>* obj, Integral arg);
template<class Integral>
Integral atomic_fetch_or(volatile std::atomic<Integral>* obj, Integral arg);

template<class Integral>
Integral atomic_fetch_or_explicit(std::atomic<Integral>* obj,
                                  Integral arg,
                                  std::memory_order order);

template<class Integral>
Integral atomic_fetch_or_explicit(volatile std::atomic<Integral>* obj,
                                  Integral arg,
                                  std::memory_order order);

template<class Integral>
Integral atomic_fetch_xor(std::atomic<Integral>* obj, Integral arg);
template<class Integral>
Integral atomic_fetch_xor(volatile std::atomic<Integral>* obj, Integral arg);

template<class Integral>
Integral atomic_fetch_xor_explicit(std::atomic<Integral>* obj,
                                   Integral arg,
                                   std::memory_order order);

template<class Integral>
Integral atomic_fetch_xor_explicit(volatile std::atomic<Integral>* obj,
                                   Integral arg,
                                   std::memory_order order);

class atomic_flag {
public:
    atomic_flag();
    atomic_flag(const atomic_flag&) = delete;

    atomic_flag& operator=(const atomic_flag&) = delete;
    atomic_flag& operator=(const atomic_flag&) volatile = delete;

    void clear(std::memory_order order = std::memory_order_seq_cst) volatile;
    void clear(std::memory_order order = std::memory_order_seq_cst);

    bool test_and_set(std::memory_order order = std::memory_order_seq_cst) volatile;
    bool test_and_set(std::memory_order order = std::memory_order_seq_cst);
};

bool atomic_flag_test_and_set(volatile std::atomic_flag* p);
bool atomic_flag_test_and_set(std::atomic_flag* p);
bool atomic_flag_test_and_set_explicit(volatile std::atomic_flag* p,
                                       std::memory_order order);
bool atomic_flag_test_and_set_explicit(std::atomic_flag* p,
                                       std::memory_order order);

void atomic_flag_clear(volatile std::atomic_flag* p);
void atomic_flag_clear(std::atomic_flag* p);
void atomic_flag_clear_explicit(volatile std::atomic_flag* p,
                                std::memory_order order);
void atomic_flag_clear_explicit(std::atomic_flag* p,
                                std::memory_order order);

void atomic_signal_fence(std::memory_order order);
void atomic_thread_fence(std::memory_order order);
template<class T>
T kill_dependency(T y);

template<class T>
void atomic_init(std::atomic<T>* obj, T desired);

template<class T>
void atomic_init(volatile std::atomic<T>* obj, T desired);

#define ATOMIC_VAR_INIT(value) // impl-defined
#define ATOMIC_FLAG_INIT // impl-defined

#endif // CPPREFERENCE_STDVER>= 2011

} // namespace std

#endif // CPPREFERENCE_ATOMIC_H