0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #ifndef _NGX_ATOMIC_H_INCLUDED_
|
|
8 #define _NGX_ATOMIC_H_INCLUDED_
|
|
9
|
|
10
|
|
11 #include <ngx_config.h>
|
|
12 #include <ngx_core.h>
|
|
13
|
|
14
|
566
|
15 #if (NGX_HAVE_LIBATOMIC)
|
|
16
|
|
17 #include <atomic_ops.h>
|
|
18
|
|
19 #define NGX_HAVE_ATOMIC_OPS 1
|
|
20
|
|
21 typedef long ngx_atomic_int_t;
|
|
22 typedef AO_t ngx_atomic_uint_t;
|
|
23 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
|
24
|
|
25 #if (NGX_PTR_SIZE == 8)
|
|
26 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
|
|
27 #else
|
|
28 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
|
29 #endif
|
|
30
|
|
31 #define ngx_atomic_cmp_set(lock, old, new) \
|
|
32 AO_compare_and_swap(lock, old, new)
|
|
33 #define ngx_atomic_fetch_add(value, add) \
|
|
34 AO_fetch_and_add(value, add)
|
|
35 #define ngx_memory_barrier() AO_nop()
|
|
36 #define ngx_cpu_pause()
|
|
37
|
|
38
|
|
39 #elif (NGX_DARWIN_ATOMIC)
|
270
|
40
|
|
41 /*
|
|
42 * use Darwin 8 atomic(3) and barrier(3) operations
|
|
43 * optimized at run-time for UP and SMP
|
|
44 */
|
|
45
|
|
46 #include <libkern/OSAtomic.h>
|
|
47
|
394
|
48 /* "bool" conflicts with perl's CORE/handy.h */
|
270
|
49 #undef bool
|
|
50
|
|
51
|
|
52 #define NGX_HAVE_ATOMIC_OPS 1
|
|
53
|
|
54 #if (NGX_PTR_SIZE == 8)
|
|
55
|
|
56 typedef int64_t ngx_atomic_int_t;
|
|
57 typedef uint64_t ngx_atomic_uint_t;
|
542
|
58 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
|
270
|
59
|
|
60 #define ngx_atomic_cmp_set(lock, old, new) \
|
|
61 OSAtomicCompareAndSwap64Barrier(old, new, (int64_t *) lock)
|
|
62
|
|
63 #define ngx_atomic_fetch_add(value, add) \
|
|
64 (OSAtomicAdd64(add, (int64_t *) value) - add)
|
|
65
|
|
66 #else
|
|
67
|
|
68 typedef int32_t ngx_atomic_int_t;
|
|
69 typedef uint32_t ngx_atomic_uint_t;
|
542
|
70 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
270
|
71
|
|
72 #define ngx_atomic_cmp_set(lock, old, new) \
|
|
73 OSAtomicCompareAndSwap32Barrier(old, new, (int32_t *) lock)
|
|
74
|
|
75 #define ngx_atomic_fetch_add(value, add) \
|
|
76 (OSAtomicAdd32(add, (int32_t *) value) - add)
|
|
77
|
|
78 #endif
|
|
79
|
|
80 #define ngx_memory_barrier() OSMemoryBarrier()
|
|
81
|
|
82 #define ngx_cpu_pause()
|
|
83
|
|
84 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
|
85
|
|
86
|
566
|
87 #elif (NGX_HAVE_GCC_ATOMIC)
|
|
88
|
|
89 /* GCC 4.1 builtin atomic operations */
|
|
90
|
|
91 #define NGX_HAVE_ATOMIC_OPS 1
|
|
92
|
|
93 typedef long ngx_atomic_int_t;
|
|
94 typedef unsigned long ngx_atomic_uint_t;
|
|
95
|
|
96 #if (NGX_PTR_SIZE == 8)
|
|
97 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
|
|
98 #else
|
|
99 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
|
100 #endif
|
|
101
|
|
102 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
270
|
103
|
|
104
|
566
|
105 #define ngx_atomic_cmp_set(lock, old, set) \
|
|
106 __sync_bool_compare_and_swap(lock, old, set)
|
|
107
|
|
108 #define ngx_atomic_fetch_add(value, add) \
|
|
109 __sync_fetch_and_add(value, add)
|
|
110
|
|
111 #define ngx_memory_barrier() __sync_synchronize()
|
|
112
|
|
113 #if ( __i386__ || __i386 || __amd64__ || __amd64 )
|
|
114 #define ngx_cpu_pause() __asm__ ("pause")
|
|
115 #else
|
|
116 #define ngx_cpu_pause()
|
|
117 #endif
|
|
118
|
|
119
|
|
120 #elif ( __i386__ || __i386 )
|
0
|
121
|
110
|
122 typedef int32_t ngx_atomic_int_t;
|
|
123 typedef uint32_t ngx_atomic_uint_t;
|
44
|
124 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
542
|
125 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
42
|
126
|
|
127
|
110
|
128 #if ( __SUNPRO_C )
|
42
|
129
|
110
|
130 #define NGX_HAVE_ATOMIC_OPS 1
|
42
|
131
|
110
|
132 ngx_atomic_uint_t
|
|
133 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
|
|
134 ngx_atomic_uint_t set);
|
42
|
135
|
110
|
136 ngx_atomic_int_t
|
|
137 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
|
42
|
138
|
320
|
139 /*
|
|
140 * Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
|
|
141 * so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_x86.il
|
|
142 */
|
|
143
|
|
144 void
|
|
145 ngx_cpu_pause(void);
|
|
146
|
110
|
147 /* the code in src/os/unix/ngx_sunpro_x86.il */
|
42
|
148
|
112
|
149 #define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
|
|
150
|
42
|
151
|
110
|
152 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
|
|
153
|
|
154 #define NGX_HAVE_ATOMIC_OPS 1
|
42
|
155
|
110
|
156 #include "ngx_gcc_atomic_x86.h"
|
42
|
157
|
110
|
158 #endif
|
42
|
159
|
|
160
|
96
|
161 #elif ( __amd64__ || __amd64 )
|
42
|
162
|
110
|
163 typedef int64_t ngx_atomic_int_t;
|
|
164 typedef uint64_t ngx_atomic_uint_t;
|
44
|
165 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
542
|
166 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
|
0
|
167
|
|
168
|
110
|
169 #if ( __SUNPRO_C )
|
0
|
170
|
110
|
171 #define NGX_HAVE_ATOMIC_OPS 1
|
0
|
172
|
110
|
173 ngx_atomic_uint_t
|
|
174 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
|
|
175 ngx_atomic_uint_t set);
|
0
|
176
|
110
|
177 ngx_atomic_int_t
|
|
178 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
|
0
|
179
|
320
|
180 /*
|
|
181 * Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
|
|
182 * so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_amd64.il
|
|
183 */
|
|
184
|
|
185 void
|
|
186 ngx_cpu_pause(void);
|
|
187
|
110
|
188 /* the code in src/os/unix/ngx_sunpro_amd64.il */
|
0
|
189
|
112
|
190 #define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
|
|
191
|
0
|
192
|
110
|
193 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
|
0
|
194
|
110
|
195 #define NGX_HAVE_ATOMIC_OPS 1
|
0
|
196
|
110
|
197 #include "ngx_gcc_atomic_amd64.h"
|
0
|
198
|
110
|
199 #endif
|
0
|
200
|
|
201
|
320
|
202 #elif ( __sparc__ || __sparc || __sparcv9 )
|
0
|
203
|
110
|
204 #if (NGX_PTR_SIZE == 8)
|
|
205
|
|
206 typedef int64_t ngx_atomic_int_t;
|
|
207 typedef uint64_t ngx_atomic_uint_t;
|
542
|
208 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
|
0
|
209
|
42
|
210 #else
|
110
|
211
|
|
212 typedef int32_t ngx_atomic_int_t;
|
|
213 typedef uint32_t ngx_atomic_uint_t;
|
542
|
214 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
110
|
215
|
42
|
216 #endif
|
|
217
|
44
|
218 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
0
|
219
|
|
220
|
110
|
221 #if ( __SUNPRO_C )
|
0
|
222
|
110
|
223 #define NGX_HAVE_ATOMIC_OPS 1
|
0
|
224
|
110
|
225 #include "ngx_sunpro_atomic_sparc64.h"
|
42
|
226
|
0
|
227
|
110
|
228 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
|
42
|
229
|
110
|
230 #define NGX_HAVE_ATOMIC_OPS 1
|
0
|
231
|
110
|
232 #include "ngx_gcc_atomic_sparc64.h"
|
0
|
233
|
110
|
234 #endif
|
42
|
235
|
|
236
|
162
|
237 #elif ( __powerpc__ || __POWERPC__ )
|
42
|
238
|
|
239 #define NGX_HAVE_ATOMIC_OPS 1
|
|
240
|
|
241 #if (NGX_PTR_SIZE == 8)
|
110
|
242
|
|
243 typedef int64_t ngx_atomic_int_t;
|
|
244 typedef uint64_t ngx_atomic_uint_t;
|
542
|
245 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
|
110
|
246
|
42
|
247 #else
|
110
|
248
|
|
249 typedef int32_t ngx_atomic_int_t;
|
|
250 typedef uint32_t ngx_atomic_uint_t;
|
542
|
251 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
110
|
252
|
42
|
253 #endif
|
|
254
|
44
|
255 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
42
|
256
|
|
257
|
110
|
258 #include "ngx_gcc_atomic_ppc.h"
|
42
|
259
|
270
|
260 #endif
|
|
261
|
42
|
262
|
110
|
263 #if !(NGX_HAVE_ATOMIC_OPS)
|
0
|
264
|
|
265 #define NGX_HAVE_ATOMIC_OPS 0
|
|
266
|
110
|
267 typedef int32_t ngx_atomic_int_t;
|
|
268 typedef uint32_t ngx_atomic_uint_t;
|
44
|
269 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
|
542
|
270 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
|
0
|
271
|
|
272
|
44
|
273 static ngx_inline ngx_atomic_uint_t
|
|
274 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
|
|
275 ngx_atomic_uint_t set)
|
0
|
276 {
|
120
|
277 if (*lock == old) {
|
110
|
278 *lock = set;
|
|
279 return 1;
|
|
280 }
|
|
281
|
|
282 return 0;
|
|
283 }
|
|
284
|
|
285
|
|
286 static ngx_inline ngx_atomic_int_t
|
|
287 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add)
|
|
288 {
|
|
289 ngx_atomic_int_t old;
|
|
290
|
|
291 old = *value;
|
|
292 *value += add;
|
|
293
|
|
294 return old;
|
0
|
295 }
|
|
296
|
120
|
297 #define ngx_memory_barrier()
|
160
|
298 #define ngx_cpu_pause()
|
120
|
299
|
0
|
300 #endif
|
|
301
|
270
|
302
|
160
|
303 void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
|
0
|
304
|
|
305 #define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
|
|
306 #define ngx_unlock(lock) *(lock) = 0
|
|
307
|
|
308
|
|
309 #endif /* _NGX_ATOMIC_H_INCLUDED_ */
|