-
Notifications
You must be signed in to change notification settings - Fork 211
/
Copy pathngx_atomic.h
350 lines (224 loc) · 8.27 KB
/
ngx_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
// annotated by chrono since 2016
//
// * ngx_trylock
// * ngx_unlock
/*
* Copyright (C) Igor Sysoev
* Copyright (C) Nginx, Inc.
*/
#ifndef _NGX_ATOMIC_H_INCLUDED_
#define _NGX_ATOMIC_H_INCLUDED_
#include <ngx_config.h>
#include <ngx_core.h>
// Linux下通常使用的是gcc atomic
// 即NGX_HAVE_GCC_ATOMIC
//#define ngx_atomic_cmp_set(lock, old, set)
// __sync_bool_compare_and_swap(lock, old, set)
//
//#define ngx_atomic_fetch_add(value, add)
// __sync_fetch_and_add(value, add)
#if (NGX_HAVE_LIBATOMIC)
#define AO_REQUIRE_CAS
#include <atomic_ops.h>
#define NGX_HAVE_ATOMIC_OPS 1
typedef long ngx_atomic_int_t;
typedef AO_t ngx_atomic_uint_t;
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
#if (NGX_PTR_SIZE == 8)
#define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
#else
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
#endif
#define ngx_atomic_cmp_set(lock, old, new) \
AO_compare_and_swap(lock, old, new)
#define ngx_atomic_fetch_add(value, add) \
AO_fetch_and_add(value, add)
#define ngx_memory_barrier() AO_nop()
#define ngx_cpu_pause()
#elif (NGX_HAVE_GCC_ATOMIC)
/* GCC 4.1 builtin atomic operations */
// 肯定支持原子操作
#define NGX_HAVE_ATOMIC_OPS 1
// 原子类型实际上都是长整数
typedef long ngx_atomic_int_t;
typedef unsigned long ngx_atomic_uint_t;
// 原子整数文本表示的最大长度,T=>text
// -1是去掉字符串里结尾的'\0'
#if (NGX_PTR_SIZE == 8)
#define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
#else
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
#endif
// 原子类型实际上是无符号长整数
// 使用volatile修饰,禁止编译器优化缓存,即时取值
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
// cas赋值
#define ngx_atomic_cmp_set(lock, old, set) \
__sync_bool_compare_and_swap(lock, old, set)
// cas加法
#define ngx_atomic_fetch_add(value, add) \
__sync_fetch_and_add(value, add)
// 内存屏障, 并发操作的同步点
#define ngx_memory_barrier() __sync_synchronize()
// 降低cpu功耗,不让出cpu
#if ( __i386__ || __i386 || __amd64__ || __amd64 )
#define ngx_cpu_pause() __asm__ ("pause")
#else
#define ngx_cpu_pause()
#endif
#elif (NGX_DARWIN_ATOMIC)
/*
* use Darwin 8 atomic(3) and barrier(3) operations
* optimized at run-time for UP and SMP
*/
#include <libkern/OSAtomic.h>
/* "bool" conflicts with perl's CORE/handy.h */
#if 0
#undef bool
#endif
#define NGX_HAVE_ATOMIC_OPS 1
#if (NGX_PTR_SIZE == 8)
typedef int64_t ngx_atomic_int_t;
typedef uint64_t ngx_atomic_uint_t;
#define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
#define ngx_atomic_cmp_set(lock, old, new) \
OSAtomicCompareAndSwap64Barrier(old, new, (int64_t *) lock)
#define ngx_atomic_fetch_add(value, add) \
(OSAtomicAdd64(add, (int64_t *) value) - add)
#else
typedef int32_t ngx_atomic_int_t;
typedef uint32_t ngx_atomic_uint_t;
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
// cas赋值
#define ngx_atomic_cmp_set(lock, old, new) \
OSAtomicCompareAndSwap32Barrier(old, new, (int32_t *) lock)
// cas加法
#define ngx_atomic_fetch_add(value, add) \
(OSAtomicAdd32(add, (int32_t *) value) - add)
#endif
// 内存屏障, 并发操作的同步点
#define ngx_memory_barrier() OSMemoryBarrier()
// 降低cpu功耗,不让出cpu
#define ngx_cpu_pause()
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
#elif ( __i386__ || __i386 )
typedef int32_t ngx_atomic_int_t;
typedef uint32_t ngx_atomic_uint_t;
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
#if ( __SUNPRO_C )
// 肯定支持原子操作
#define NGX_HAVE_ATOMIC_OPS 1
ngx_atomic_uint_t
ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
ngx_atomic_uint_t set);
ngx_atomic_int_t
ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
/*
* Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
* so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_x86.il
*/
void
ngx_cpu_pause(void);
/* the code in src/os/unix/ngx_sunpro_x86.il */
#define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
#else /* ( __GNUC__ || __INTEL_COMPILER ) */
// 肯定支持原子操作
#define NGX_HAVE_ATOMIC_OPS 1
#include "ngx_gcc_atomic_x86.h"
#endif
#elif ( __amd64__ || __amd64 )
// 原子类型实际上都是长整数
typedef int64_t ngx_atomic_int_t;
typedef uint64_t ngx_atomic_uint_t;
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
// 原子整数文本表示的最大长度,T=>text
// -1是去掉字符串里结尾的'\0'
#define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
#if ( __SUNPRO_C )
#define NGX_HAVE_ATOMIC_OPS 1
ngx_atomic_uint_t
ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
ngx_atomic_uint_t set);
ngx_atomic_int_t
ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
/*
* Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
* so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_amd64.il
*/
void
ngx_cpu_pause(void);
/* the code in src/os/unix/ngx_sunpro_amd64.il */
#define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
#else /* ( __GNUC__ || __INTEL_COMPILER ) */
#define NGX_HAVE_ATOMIC_OPS 1
#include "ngx_gcc_atomic_amd64.h"
#endif
#elif ( __sparc__ || __sparc || __sparcv9 )
#if (NGX_PTR_SIZE == 8)
typedef int64_t ngx_atomic_int_t;
typedef uint64_t ngx_atomic_uint_t;
#define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
#else
typedef int32_t ngx_atomic_int_t;
typedef uint32_t ngx_atomic_uint_t;
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
#endif
// 原子类型实际上是无符号长整数
// 使用volatile修饰,禁止编译器优化缓存,即时取值
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
#if ( __SUNPRO_C )
#define NGX_HAVE_ATOMIC_OPS 1
#include "ngx_sunpro_atomic_sparc64.h"
#else /* ( __GNUC__ || __INTEL_COMPILER ) */
#define NGX_HAVE_ATOMIC_OPS 1
#include "ngx_gcc_atomic_sparc64.h"
#endif
#elif ( __powerpc__ || __POWERPC__ )
#define NGX_HAVE_ATOMIC_OPS 1
#if (NGX_PTR_SIZE == 8)
typedef int64_t ngx_atomic_int_t;
typedef uint64_t ngx_atomic_uint_t;
#define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
#else
typedef int32_t ngx_atomic_int_t;
typedef uint32_t ngx_atomic_uint_t;
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
#endif
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
#include "ngx_gcc_atomic_ppc.h"
#endif
#if !(NGX_HAVE_ATOMIC_OPS)
#define NGX_HAVE_ATOMIC_OPS 0
typedef int32_t ngx_atomic_int_t;
typedef uint32_t ngx_atomic_uint_t;
typedef volatile ngx_atomic_uint_t ngx_atomic_t;
#define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
static ngx_inline ngx_atomic_uint_t
ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
ngx_atomic_uint_t set)
{
if (*lock == old) {
*lock = set;
return 1;
}
return 0;
}
static ngx_inline ngx_atomic_int_t
ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add)
{
ngx_atomic_int_t old;
old = *value;
*value += add;
return old;
}
#define ngx_memory_barrier()
#define ngx_cpu_pause()
#endif
// 自旋锁,实现在core/ngx_spinlock.c
void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
// 尝试锁定,不会阻塞
#define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
// 解锁
#define ngx_unlock(lock) *(lock) = 0
#endif /* _NGX_ATOMIC_H_INCLUDED_ */