- Notifications
You must be signed in to change notification settings - Fork 7.8k
/
Copy pathzend_atomic.h
179 lines (137 loc) · 5.57 KB
/
zend_atomic.h
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
/*
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| https://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Levi Morrison <morrison.levi@gmail.com> |
+----------------------------------------------------------------------+
*/
#ifndefZEND_ATOMIC_H
#defineZEND_ATOMIC_H
#include"zend_portability.h"
#include<stdbool.h>
#defineZEND_GCC_PREREQ(x, y) \
((__GNUC__ == (x) && __GNUC_MINOR__ >= (y)) || (__GNUC__ > (x)))
/* Builtins are used to avoid library linkage */
#if__has_feature(c_atomic) && defined(__clang__)
#defineHAVE_C11_ATOMICS 1
#elifZEND_GCC_PREREQ(4, 7)
#defineHAVE_GNUC_ATOMICS 1
#elif defined(__GNUC__)
#defineHAVE_SYNC_ATOMICS 1
#elif !defined(ZEND_WIN32)
#defineHAVE_NO_ATOMICS 1
#endif
#undef ZEND_GCC_PREREQ
/* Treat zend_atomic_* types as opaque. They have definitions only for size
* and alignment purposes.
*/
#if defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)
typedefstructzend_atomic_bool_s {
volatilecharvalue;
} zend_atomic_bool;
#elif defined(HAVE_C11_ATOMICS)
typedefstructzend_atomic_bool_s {
_Atomic(bool) value;
} zend_atomic_bool;
#else
typedefstructzend_atomic_bool_s {
volatileboolvalue;
} zend_atomic_bool;
#endif
BEGIN_EXTERN_C()
#ifdefZEND_WIN32
#ifndefInterlockedExchange8
#defineInterlockedExchange8 _InterlockedExchange8
#endif
#ifndefInterlockedOr8
#defineInterlockedOr8 _InterlockedOr8
#endif
#defineZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired))
staticzend_always_inlineboolzend_atomic_bool_exchange_ex(zend_atomic_bool*obj, booldesired) {
returnInterlockedExchange8(&obj->value, desired);
}
/* On this platform it is non-const due to Iterlocked API*/
staticzend_always_inlineboolzend_atomic_bool_load_ex(zend_atomic_bool*obj) {
/* Or'ing with false won't change the value. */
returnInterlockedOr8(&obj->value, false);
}
staticzend_always_inlinevoidzend_atomic_bool_store_ex(zend_atomic_bool*obj, booldesired) {
(void)InterlockedExchange8(&obj->value, desired);
}
#elif defined(HAVE_C11_ATOMICS)
#defineZEND_ATOMIC_BOOL_INIT(obj, desired) __c11_atomic_init(&(obj)->value, (desired))
staticzend_always_inlineboolzend_atomic_bool_exchange_ex(zend_atomic_bool*obj, booldesired) {
return__c11_atomic_exchange(&obj->value, desired, __ATOMIC_SEQ_CST);
}
staticzend_always_inlineboolzend_atomic_bool_load_ex(constzend_atomic_bool*obj) {
return__c11_atomic_load(&obj->value, __ATOMIC_SEQ_CST);
}
staticzend_always_inlinevoidzend_atomic_bool_store_ex(zend_atomic_bool*obj, booldesired) {
__c11_atomic_store(&obj->value, desired, __ATOMIC_SEQ_CST);
}
#elif defined(HAVE_GNUC_ATOMICS)
#defineZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired))
staticzend_always_inlineboolzend_atomic_bool_exchange_ex(zend_atomic_bool*obj, booldesired) {
boolprev= false;
__atomic_exchange(&obj->value, &desired, &prev, __ATOMIC_SEQ_CST);
returnprev;
}
staticzend_always_inlineboolzend_atomic_bool_load_ex(constzend_atomic_bool*obj) {
boolprev= false;
__atomic_load(&obj->value, &prev, __ATOMIC_SEQ_CST);
returnprev;
}
staticzend_always_inlinevoidzend_atomic_bool_store_ex(zend_atomic_bool*obj, booldesired) {
__atomic_store(&obj->value, &desired, __ATOMIC_SEQ_CST);
}
#elif defined(HAVE_SYNC_ATOMICS)
#defineZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired))
staticzend_always_inlineboolzend_atomic_bool_exchange_ex(zend_atomic_bool*obj, booldesired) {
boolprev=__sync_lock_test_and_set(&obj->value, desired);
/* __sync_lock_test_and_set only does an acquire barrier, so sync
* immediately after.
*/
__sync_synchronize();
returnprev;
}
staticzend_always_inlineboolzend_atomic_bool_load_ex(zend_atomic_bool*obj) {
/* Or'ing false won't change the value */
return__sync_fetch_and_or(&obj->value, false);
}
staticzend_always_inlinevoidzend_atomic_bool_store_ex(zend_atomic_bool*obj, booldesired) {
__sync_synchronize();
obj->value=desired;
__sync_synchronize();
}
#elif defined(HAVE_NO_ATOMICS)
#warning No atomics support detected. Please open an issue with platform details.
#defineZEND_ATOMIC_BOOL_INIT(obj, desired) ((obj)->value = (desired))
staticzend_always_inlinevoidzend_atomic_bool_store_ex(zend_atomic_bool*obj, booldesired) {
obj->value=desired;
}
staticzend_always_inlineboolzend_atomic_bool_load_ex(constzend_atomic_bool*obj) {
returnobj->value;
}
staticzend_always_inlineboolzend_atomic_bool_exchange_ex(zend_atomic_bool*obj, booldesired) {
boolprev=obj->value;
obj->value=desired;
returnprev;
}
#endif
ZEND_APIvoidzend_atomic_bool_init(zend_atomic_bool*obj, booldesired);
ZEND_APIboolzend_atomic_bool_exchange(zend_atomic_bool*obj, booldesired);
ZEND_APIvoidzend_atomic_bool_store(zend_atomic_bool*obj, booldesired);
#if defined(ZEND_WIN32) || defined(HAVE_SYNC_ATOMICS)
/* On these platforms it is non-const due to underlying APIs. */
ZEND_APIboolzend_atomic_bool_load(zend_atomic_bool*obj);
#else
ZEND_APIboolzend_atomic_bool_load(constzend_atomic_bool*obj);
#endif
END_EXTERN_C()
#endif