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