Update and clean Tomato RAF files
[tomato.git] / release / src / router / nginx / src / os / unix / ngx_atomic.h
blob417cd86ff29fe6156bc8f926663d048826abb06d
2 /*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Nginx, Inc.
5 */
8 #ifndef _NGX_ATOMIC_H_INCLUDED_
9 #define _NGX_ATOMIC_H_INCLUDED_
12 #include <ngx_config.h>
13 #include <ngx_core.h>
16 #if (NGX_HAVE_LIBATOMIC)
18 #define AO_REQUIRE_CAS
19 #include <atomic_ops.h>
21 #define NGX_HAVE_ATOMIC_OPS 1
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;
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
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()
41 #elif (NGX_DARWIN_ATOMIC)
44 * use Darwin 8 atomic(3) and barrier(3) operations
45 * optimized at run-time for UP and SMP
48 #include <libkern/OSAtomic.h>
50 /* "bool" conflicts with perl's CORE/handy.h */
51 #if 0
52 #undef bool
53 #endif
56 #define NGX_HAVE_ATOMIC_OPS 1
58 #if (NGX_PTR_SIZE == 8)
60 typedef int64_t ngx_atomic_int_t;
61 typedef uint64_t ngx_atomic_uint_t;
62 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
64 #define ngx_atomic_cmp_set(lock, old, new) \
65 OSAtomicCompareAndSwap64Barrier(old, new, (int64_t *) lock)
67 #define ngx_atomic_fetch_add(value, add) \
68 (OSAtomicAdd64(add, (int64_t *) value) - add)
70 #else
72 typedef int32_t ngx_atomic_int_t;
73 typedef uint32_t ngx_atomic_uint_t;
74 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
76 #define ngx_atomic_cmp_set(lock, old, new) \
77 OSAtomicCompareAndSwap32Barrier(old, new, (int32_t *) lock)
79 #define ngx_atomic_fetch_add(value, add) \
80 (OSAtomicAdd32(add, (int32_t *) value) - add)
82 #endif
84 #define ngx_memory_barrier() OSMemoryBarrier()
86 #define ngx_cpu_pause()
88 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
91 #elif (NGX_HAVE_GCC_ATOMIC)
93 /* GCC 4.1 builtin atomic operations */
95 #define NGX_HAVE_ATOMIC_OPS 1
97 typedef long ngx_atomic_int_t;
98 typedef unsigned long ngx_atomic_uint_t;
100 #if (NGX_PTR_SIZE == 8)
101 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
102 #else
103 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
104 #endif
106 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
109 #define ngx_atomic_cmp_set(lock, old, set) \
110 __sync_bool_compare_and_swap(lock, old, set)
112 #define ngx_atomic_fetch_add(value, add) \
113 __sync_fetch_and_add(value, add)
115 #define ngx_memory_barrier() __sync_synchronize()
117 #if ( __i386__ || __i386 || __amd64__ || __amd64 )
118 #define ngx_cpu_pause() __asm__ ("pause")
119 #else
120 #define ngx_cpu_pause()
121 #endif
124 #elif ( __i386__ || __i386 )
126 typedef int32_t ngx_atomic_int_t;
127 typedef uint32_t ngx_atomic_uint_t;
128 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
129 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
132 #if ( __SUNPRO_C )
134 #define NGX_HAVE_ATOMIC_OPS 1
136 ngx_atomic_uint_t
137 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
138 ngx_atomic_uint_t set);
140 ngx_atomic_int_t
141 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
144 * Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
145 * so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_x86.il
148 void
149 ngx_cpu_pause(void);
151 /* the code in src/os/unix/ngx_sunpro_x86.il */
153 #define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
156 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
158 #define NGX_HAVE_ATOMIC_OPS 1
160 #include "ngx_gcc_atomic_x86.h"
162 #endif
165 #elif ( __amd64__ || __amd64 )
167 typedef int64_t ngx_atomic_int_t;
168 typedef uint64_t ngx_atomic_uint_t;
169 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
170 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
173 #if ( __SUNPRO_C )
175 #define NGX_HAVE_ATOMIC_OPS 1
177 ngx_atomic_uint_t
178 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
179 ngx_atomic_uint_t set);
181 ngx_atomic_int_t
182 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
185 * Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
186 * so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_amd64.il
189 void
190 ngx_cpu_pause(void);
192 /* the code in src/os/unix/ngx_sunpro_amd64.il */
194 #define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
197 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
199 #define NGX_HAVE_ATOMIC_OPS 1
201 #include "ngx_gcc_atomic_amd64.h"
203 #endif
206 #elif ( __sparc__ || __sparc || __sparcv9 )
208 #if (NGX_PTR_SIZE == 8)
210 typedef int64_t ngx_atomic_int_t;
211 typedef uint64_t ngx_atomic_uint_t;
212 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
214 #else
216 typedef int32_t ngx_atomic_int_t;
217 typedef uint32_t ngx_atomic_uint_t;
218 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
220 #endif
222 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
225 #if ( __SUNPRO_C )
227 #define NGX_HAVE_ATOMIC_OPS 1
229 #include "ngx_sunpro_atomic_sparc64.h"
232 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
234 #define NGX_HAVE_ATOMIC_OPS 1
236 #include "ngx_gcc_atomic_sparc64.h"
238 #endif
241 #elif ( __powerpc__ || __POWERPC__ )
243 #define NGX_HAVE_ATOMIC_OPS 1
245 #if (NGX_PTR_SIZE == 8)
247 typedef int64_t ngx_atomic_int_t;
248 typedef uint64_t ngx_atomic_uint_t;
249 #define NGX_ATOMIC_T_LEN (sizeof("-9223372036854775808") - 1)
251 #else
253 typedef int32_t ngx_atomic_int_t;
254 typedef uint32_t ngx_atomic_uint_t;
255 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
257 #endif
259 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
262 #include "ngx_gcc_atomic_ppc.h"
264 #endif
267 #if !(NGX_HAVE_ATOMIC_OPS)
269 #define NGX_HAVE_ATOMIC_OPS 0
271 typedef int32_t ngx_atomic_int_t;
272 typedef uint32_t ngx_atomic_uint_t;
273 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
274 #define NGX_ATOMIC_T_LEN (sizeof("-2147483648") - 1)
277 static ngx_inline ngx_atomic_uint_t
278 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
279 ngx_atomic_uint_t set)
281 if (*lock == old) {
282 *lock = set;
283 return 1;
286 return 0;
290 static ngx_inline ngx_atomic_int_t
291 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add)
293 ngx_atomic_int_t old;
295 old = *value;
296 *value += add;
298 return old;
301 #define ngx_memory_barrier()
302 #define ngx_cpu_pause()
304 #endif
307 void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
309 #define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
310 #define ngx_unlock(lock) *(lock) = 0
313 #endif /* _NGX_ATOMIC_H_INCLUDED_ */