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