summaryrefslogtreecommitdiff
path: root/src/atomic_ops/as_stdatomic/stdatomic.h
blob: a63a66571a7ab9f84af37f5fb347fcb59bd7b3bb (plain)
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
/*
 * Copyright (c) 2011 Hewlett-Packard Development Company, L.P.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <atomic_ops.h>
#include <limits.h>

#ifndef AO_BIT
#  define AO_BIT LONG_BIT  /* CHECKME */
#endif

/* TODO */
/* #define ATOMIC_INTEGRAL_LOCK_FREE 1 */
/* #define ATOMIC_ADDRESS_LOCK_FREE 1 */


/* Memory Order and Fences
 * ======================= */

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

#define atomic_thread_fence(order) \
    do { switch (order) { \
        case memory_order_relaxed: break; \
        case memory_order_acquire: AO_nop_acquire(); break; \
        case memory_order_release: AO_nop_release(); break; \
        case memory_order_acq_rel: \
        case memory_order_seq_cst: AO_nop_full(); break; \
    } } while (0)

#define atomic_signal_fence atomic_thread_fence


/* Atomic Integers, Bool, and Address
 * ==================================
 *
 * CHECKME: Is it appropriate to use AO_t if it is smaller than some of the
 * corresponding non-atomic types? */

typedef struct { AO_t __ao_val; } atomic_bool;
typedef struct { AO_t __ao_val; } atomic_address;

typedef struct { AO_t __ao_val; } atomic_char;
typedef struct { AO_t __ao_val; } atomic_schar;
typedef struct { AO_t __ao_val; } atomic_uchar;
typedef struct { AO_t __ao_val; } atomic_short;
typedef struct { AO_t __ao_val; } atomic_ushort;
typedef struct { AO_t __ao_val; } atomic_int;
typedef struct { AO_t __ao_val; } atomic_uint;
typedef struct { AO_t __ao_val; } atomic_long;
typedef struct { AO_t __ao_val; } atomic_ulong;
#if defined LLONG_BIT && AO_BIT >= LLONG_BIT
typedef struct { AO_t __ao_val; } atomic_llong;
typedef struct { AO_t __ao_val; } atomic_ullong;
#endif

typedef struct { AO_t __ao_val; } atomic_char16_t;
typedef struct { AO_t __ao_val; } atomic_char32_t;
typedef struct { AO_t __ao_val; } atomic_wchar_t;
typedef struct { AO_t __ao_val; } atomic_int_least8_t;
typedef struct { AO_t __ao_val; } atomic_uint_least8_t;
typedef struct { AO_t __ao_val; } atomic_int_fast8_t;
typedef struct { AO_t __ao_val; } atomic_uint_fast8_t;
typedef struct { AO_t __ao_val; } atomic_int_least16_t;
typedef struct { AO_t __ao_val; } atomic_uint_least16_t;
typedef struct { AO_t __ao_val; } atomic_int_fast16_t;
typedef struct { AO_t __ao_val; } atomic_uint_fast16_t;
typedef struct { AO_t __ao_val; } atomic_int_least32_t;
typedef struct { AO_t __ao_val; } atomic_uint_least32_t;
typedef struct { AO_t __ao_val; } atomic_int_fast32_t;
typedef struct { AO_t __ao_val; } atomic_uint_fast32_t;
#if AO_BIT >= 64
typedef struct { AO_t __ao_val; } atomic_int_least64_t;
typedef struct { AO_t __ao_val; } atomic_uint_least64_t;
typedef struct { AO_t __ao_val; } atomic_int_fast64_t;
typedef struct { AO_t __ao_val; } atomic_uint_fast64_t;
#endif
typedef struct { AO_t __ao_val; } atomic_intptr_t;
typedef struct { AO_t __ao_val; } atomic_uintptr_t;
typedef struct { AO_t __ao_val; } atomic_size_t;
typedef struct { AO_t __ao_val; } atomic_ptrdiff_t;
#if 0 /* FIXME */
typedef struct { AO_t __ao_val; } atomic_ssize_t;
typedef struct { AO_t __ao_val; } atomic_intmax_t;
typedef struct { AO_t __ao_val; } atomic_uintmax_t;
#endif

#define _AO_of(a) (&(a)->__ao_val)

#if defined(ATOMIC_INTEGRAL_IS_LOCK_FREE) && \
     defined(ATOMIC_ADDRESS_IS_LOCK_FREE)
#  define atomic_is_lock_free(a) 1
#else
#  define atomic_is_lock_free(a) 0
#endif

#define atomic_store(a, x) AO_store_full(_AO_of(a), (AO_t)(x))

#define atomic_store_explicit(a, x, order) \
  do { switch (order) { \
    case memory_order_relaxed: AO_store(_AO_of(a), (AO_t)(x)); break; \
    case memory_order_acquire: AO_store_acquire(_AO_of(a), (AO_t)(x)); break; \
    case memory_order_release: AO_store_release(_AO_of(a), (AO_t)(x)); break; \
    case memory_order_acq_rel: \
    case memory_order_seq_cst: AO_store_full(_AO_of(a), (AO_t)(x)); break; \
  } } while (0)

/* FIXME: The return type of the atomic load macros is unsuitable for
 * atomic_address. */

#define atomic_load(a) AO_load_full(_AO_of(a))

#define atomic_load_explicit(a, order) \
    (((order) == memory_order_relaxed)? AO_load(_AO_of(a)) : \
     ((order) == memory_order_acquire)? AO_load_acquire(_AO_of(a)) : \
     ((order) == memory_order_release)? AO_load_release(_AO_of(a)) : \
     AO_load_full(_AO_of(a)))

/* TODO: atomic_exchange, atomic_compare_exchange_<strength>,
 *       atomic_fetch_<op> */


/* Atomic Flag
 * =========== */

typedef struct { AO_TS_t __ao_flag; } atomic_flag;

#define _AO_TS_of(af) (&(af)->__ao_flag)

#define ATOMIC_FLAG_INIT {AO_TS_INITIALIZER}

#define atomic_flag_test_and_set(af) AO_test_and_set(_AO_TS_of(af))

/* CHECKME: AO_CLEAR has release-semantic whereas N1349 seems to permit
 * memory_order_seq_cst. */
#define atomic_flag_clear(af) AO_CLEAR(_AO_TS_of(af))
#define atomic_flag_clear_explicit(af, order) AO_CLEAR(_AO_TS_of(af))