1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2019 Intel Corporation. |
4 | ** |
5 | ** Permission is hereby granted, free of charge, to any person obtaining a copy |
6 | ** of this software and associated documentation files (the "Software"), to deal |
7 | ** in the Software without restriction, including without limitation the rights |
8 | ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9 | ** copies of the Software, and to permit persons to whom the Software is |
10 | ** furnished to do so, subject to the following conditions: |
11 | ** |
12 | ** The above copyright notice and this permission notice shall be included in |
13 | ** all copies or substantial portions of the Software. |
14 | ** |
15 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16 | ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17 | ** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18 | ** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 | ** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 | ** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21 | ** THE SOFTWARE. |
22 | ** |
23 | ****************************************************************************/ |
24 | |
25 | #ifndef FFD_ATOMIC_C11_H |
26 | #define FFD_ATOMIC_C11_H |
27 | |
28 | /* atomics */ |
29 | /* Using the C11 <stdatomic.h> header or C++11's <atomic> |
30 | */ |
31 | |
32 | #if defined(__cplusplus) |
33 | # include <atomic> |
34 | # define ffd_atomic_pointer(type) std::atomic<type*> |
35 | # define FFD_ATOMIC_RELAXED std::memory_order_relaxed |
36 | # define FFD_ATOMIC_ACQUIRE std::memory_order_acquire |
37 | # define FFD_ATOMIC_RELEASE std::memory_order_release |
38 | // acq_rel & cst not necessary |
39 | typedef std::atomic<int> ffd_atomic_int; |
40 | #else |
41 | # include <stdatomic.h> |
42 | # define ffd_atomic_pointer(type) _Atomic(type*) |
43 | # define FFD_ATOMIC_RELAXED memory_order_relaxed |
44 | # define FFD_ATOMIC_ACQUIRE memory_order_acquire |
45 | # define FFD_ATOMIC_RELEASE memory_order_release |
46 | // acq_rel & cst not necessary |
47 | |
48 | typedef atomic_int ffd_atomic_int; |
49 | #endif |
50 | |
51 | #ifdef __cpp_lib_atomic_value_initialization |
52 | #define FFD_ATOMIC_INIT(val) { val } |
53 | #else |
54 | #define FFD_ATOMIC_INIT(val) ATOMIC_VAR_INIT(val) |
55 | #endif |
56 | |
57 | #define ffd_atomic_load(ptr, order) \ |
58 | atomic_load_explicit(ptr, order) |
59 | #define ffd_atomic_store(ptr, val, order) \ |
60 | atomic_store_explicit(ptr, val, order) |
61 | #define ffd_atomic_exchange(ptr,val,order) \ |
62 | atomic_exchange_explicit(ptr, val, order) |
63 | #define ffd_atomic_compare_exchange(ptr, expected, desired, order1, order2) \ |
64 | atomic_compare_exchange_strong_explicit(ptr, expected, desired, order1, order2) |
65 | #define ffd_atomic_add_fetch(ptr, val, order) \ |
66 | (atomic_fetch_add_explicit(ptr, val, order) + (val)) |
67 | |
68 | #endif |
69 | |