blob: 7d3449d7e1e1a7553e0def4b708c7e79c27a0e1a [file] [log] [blame]
Jeffrey Yasskin39370832010-05-03 19:29:34 +00001#ifndef Py_ATOMIC_H
2#define Py_ATOMIC_H
3/* XXX: When compilers start offering a stdatomic.h with lock-free
4 atomic_int and atomic_address types, include that here and rewrite
5 the atomic operations in terms of it. */
6
7#include "dynamic_annotations.h"
8
9#ifdef __cplusplus
10extern "C" {
11#endif
12
13/* This is modeled after the atomics interface from C1x, according to
14 * the draft at
15 * http://www.open-std.org/JTC1/SC22/wg14/www/docs/n1425.pdf.
16 * Operations and types are named the same except with a _Py_ prefix
17 * and have the same semantics.
18 *
19 * Beware, the implementations here are deep magic.
20 */
21
22typedef enum _Py_memory_order {
23 _Py_memory_order_relaxed,
24 _Py_memory_order_acquire,
25 _Py_memory_order_release,
26 _Py_memory_order_acq_rel,
27 _Py_memory_order_seq_cst
28} _Py_memory_order;
29
30typedef struct _Py_atomic_address {
31 void *_value;
32} _Py_atomic_address;
33
34typedef struct _Py_atomic_int {
35 int _value;
36} _Py_atomic_int;
37
38/* Only support GCC (for expression statements) and x86 (for simple
39 * atomic semantics) for now */
40#if defined(__GNUC__) && (defined(__i386__) || defined(__amd64))
41
42static __inline__ void
43_Py_atomic_signal_fence(_Py_memory_order order)
44{
45 if (order != _Py_memory_order_relaxed)
46 __asm__ volatile("":::"memory");
47}
48
49static __inline__ void
50_Py_atomic_thread_fence(_Py_memory_order order)
51{
52 if (order != _Py_memory_order_relaxed)
53 __asm__ volatile("mfence":::"memory");
54}
55
56/* Tell the race checker about this operation's effects. */
57static __inline__ void
58_Py_ANNOTATE_MEMORY_ORDER(const volatile void *address, _Py_memory_order order)
59{
60 switch(order) {
61 case _Py_memory_order_release:
62 case _Py_memory_order_acq_rel:
63 case _Py_memory_order_seq_cst:
64 _Py_ANNOTATE_HAPPENS_BEFORE(address);
65 break;
66 default:
67 break;
68 }
69 switch(order) {
70 case _Py_memory_order_acquire:
71 case _Py_memory_order_acq_rel:
72 case _Py_memory_order_seq_cst:
73 _Py_ANNOTATE_HAPPENS_AFTER(address);
74 break;
75 default:
76 break;
77 }
78}
79
80#define _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, ORDER) \
81 __extension__ ({ \
82 __typeof__(ATOMIC_VAL) atomic_val = ATOMIC_VAL; \
83 __typeof__(atomic_val->_value) new_val = NEW_VAL;\
84 volatile __typeof__(new_val) *volatile_data = &atomic_val->_value; \
85 _Py_memory_order order = ORDER; \
86 _Py_ANNOTATE_MEMORY_ORDER(atomic_val, order); \
87 \
88 /* Perform the operation. */ \
89 _Py_ANNOTATE_IGNORE_WRITES_BEGIN(); \
90 switch(order) { \
91 case _Py_memory_order_release: \
92 _Py_atomic_signal_fence(_Py_memory_order_release); \
93 /* fallthrough */ \
94 case _Py_memory_order_relaxed: \
95 *volatile_data = new_val; \
96 break; \
97 \
98 case _Py_memory_order_acquire: \
99 case _Py_memory_order_acq_rel: \
100 case _Py_memory_order_seq_cst: \
101 __asm__ volatile("xchg %0, %1" \
102 : "+r"(new_val) \
103 : "m"(atomic_val->_value) \
104 : "memory"); \
105 break; \
106 } \
107 _Py_ANNOTATE_IGNORE_WRITES_END(); \
108 })
109
110#define _Py_atomic_load_explicit(ATOMIC_VAL, ORDER) \
111 __extension__ ({ \
112 __typeof__(ATOMIC_VAL) atomic_val = ATOMIC_VAL; \
113 __typeof__(atomic_val->_value) result; \
114 volatile __typeof__(result) *volatile_data = &atomic_val->_value; \
115 _Py_memory_order order = ORDER; \
116 _Py_ANNOTATE_MEMORY_ORDER(atomic_val, order); \
117 \
118 /* Perform the operation. */ \
119 _Py_ANNOTATE_IGNORE_READS_BEGIN(); \
120 switch(order) { \
121 case _Py_memory_order_release: \
122 case _Py_memory_order_acq_rel: \
123 case _Py_memory_order_seq_cst: \
124 /* Loads on x86 are not releases by default, so need a */ \
125 /* thread fence. */ \
126 _Py_atomic_thread_fence(_Py_memory_order_release); \
127 break; \
128 default: \
129 /* No fence */ \
130 break; \
131 } \
132 result = *volatile_data; \
133 switch(order) { \
134 case _Py_memory_order_acquire: \
135 case _Py_memory_order_acq_rel: \
136 case _Py_memory_order_seq_cst: \
137 /* Loads on x86 are automatically acquire operations so */ \
138 /* can get by with just a compiler fence. */ \
139 _Py_atomic_signal_fence(_Py_memory_order_acquire); \
140 break; \
141 default: \
142 /* No fence */ \
143 break; \
144 } \
145 _Py_ANNOTATE_IGNORE_READS_END(); \
146 result; \
147 })
148
149#else /* !gcc x86 */
150/* Fall back to other compilers and processors by assuming that simple
151 volatile accesses are atomic. This is false, so people should port
152 this. */
153#define _Py_atomic_signal_fence(/*memory_order*/ ORDER) ((void)0)
154#define _Py_atomic_thread_fence(/*memory_order*/ ORDER) ((void)0)
155#define _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, ORDER) \
156 ((ATOMIC_VAL)->_value = NEW_VAL)
157#define _Py_atomic_load_explicit(ATOMIC_VAL, ORDER) \
158 ((ATOMIC_VAL)->_value)
159
160#endif /* !gcc x86 */
161
162/* Standardized shortcuts. */
163#define _Py_atomic_store(ATOMIC_VAL, NEW_VAL) \
164 _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, _Py_memory_order_seq_cst)
165#define _Py_atomic_load(ATOMIC_VAL) \
166 _Py_atomic_load_explicit(ATOMIC_VAL, _Py_memory_order_seq_cst)
167
168/* Python-local extensions */
169
170#define _Py_atomic_store_relaxed(ATOMIC_VAL, NEW_VAL) \
171 _Py_atomic_store_explicit(ATOMIC_VAL, NEW_VAL, _Py_memory_order_relaxed)
172#define _Py_atomic_load_relaxed(ATOMIC_VAL) \
173 _Py_atomic_load_explicit(ATOMIC_VAL, _Py_memory_order_relaxed)
174
175#ifdef __cplusplus
176}
177#endif
178
179#endif /* Py_ATOMIC_H */