3 * Copyright (C) Igor Sysoev
7 #ifndef _NGX_ATOMIC_H_INCLUDED_
8 #define _NGX_ATOMIC_H_INCLUDED_
11 #include <ngx_config.h>
15 #if (NGX_DARWIN_ATOMIC)
18 * use Darwin 8 atomic(3) and barrier(3) operations
19 * optimized at run-time for UP and SMP
22 #include <libkern/OSAtomic.h>
24 /* "bool" conflicts with perl's CORE/handy.h */
28 #define NGX_HAVE_ATOMIC_OPS 1
30 #if (NGX_PTR_SIZE == 8)
32 typedef int64_t ngx_atomic_int_t;
33 typedef uint64_t ngx_atomic_uint_t;
34 #define NGX_ATOMIC_T_LEN sizeof("-9223372036854775808") - 1
36 #define ngx_atomic_cmp_set(lock, old, new) \
37 OSAtomicCompareAndSwap64Barrier(old, new, (int64_t *) lock)
39 #define ngx_atomic_fetch_add(value, add) \
40 (OSAtomicAdd64(add, (int64_t *) value) - add)
44 typedef int32_t ngx_atomic_int_t;
45 typedef uint32_t ngx_atomic_uint_t;
46 #define NGX_ATOMIC_T_LEN sizeof("-2147483648") - 1
48 #define ngx_atomic_cmp_set(lock, old, new) \
49 OSAtomicCompareAndSwap32Barrier(old, new, (int32_t *) lock)
51 #define ngx_atomic_fetch_add(value, add) \
52 (OSAtomicAdd32(add, (int32_t *) value) - add)
56 #define ngx_memory_barrier() OSMemoryBarrier()
58 #define ngx_cpu_pause()
60 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
63 #else /* !(NGX_DARWIN) */
66 #if ( __i386__ || __i386 )
68 typedef int32_t ngx_atomic_int_t;
69 typedef uint32_t ngx_atomic_uint_t;
70 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
71 #define NGX_ATOMIC_T_LEN sizeof("-2147483648") - 1
76 #define NGX_HAVE_ATOMIC_OPS 1
79 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
80 ngx_atomic_uint_t set);
83 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
86 * Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
87 * so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_x86.il
93 /* the code in src/os/unix/ngx_sunpro_x86.il */
95 #define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
98 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
100 #define NGX_HAVE_ATOMIC_OPS 1
102 #include "ngx_gcc_atomic_x86.h"
107 #elif ( __amd64__ || __amd64 )
109 typedef int64_t ngx_atomic_int_t;
110 typedef uint64_t ngx_atomic_uint_t;
111 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
112 #define NGX_ATOMIC_T_LEN sizeof("-9223372036854775808") - 1
117 #define NGX_HAVE_ATOMIC_OPS 1
120 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
121 ngx_atomic_uint_t set);
124 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add);
127 * Sun Studio 12 exits with segmentation fault on '__asm ("pause")',
128 * so ngx_cpu_pause is declared in src/os/unix/ngx_sunpro_amd64.il
134 /* the code in src/os/unix/ngx_sunpro_amd64.il */
136 #define ngx_memory_barrier() __asm (".volatile"); __asm (".nonvolatile")
139 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
141 #define NGX_HAVE_ATOMIC_OPS 1
143 #include "ngx_gcc_atomic_amd64.h"
148 #elif ( __sparc__ || __sparc || __sparcv9 )
150 #if (NGX_PTR_SIZE == 8)
152 typedef int64_t ngx_atomic_int_t;
153 typedef uint64_t ngx_atomic_uint_t;
154 #define NGX_ATOMIC_T_LEN sizeof("-9223372036854775808") - 1
158 typedef int32_t ngx_atomic_int_t;
159 typedef uint32_t ngx_atomic_uint_t;
160 #define NGX_ATOMIC_T_LEN sizeof("-2147483648") - 1
164 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
169 #define NGX_HAVE_ATOMIC_OPS 1
171 #include "ngx_sunpro_atomic_sparc64.h"
174 #else /* ( __GNUC__ || __INTEL_COMPILER ) */
176 #define NGX_HAVE_ATOMIC_OPS 1
178 #include "ngx_gcc_atomic_sparc64.h"
183 #elif ( __powerpc__ || __POWERPC__ )
185 #define NGX_HAVE_ATOMIC_OPS 1
187 #if (NGX_PTR_SIZE == 8)
189 typedef int64_t ngx_atomic_int_t;
190 typedef uint64_t ngx_atomic_uint_t;
191 #define NGX_ATOMIC_T_LEN sizeof("-9223372036854775808") - 1
195 typedef int32_t ngx_atomic_int_t;
196 typedef uint32_t ngx_atomic_uint_t;
197 #define NGX_ATOMIC_T_LEN sizeof("-2147483648") - 1
201 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
204 #include "ngx_gcc_atomic_ppc.h"
212 #if !(NGX_HAVE_ATOMIC_OPS)
214 #define NGX_HAVE_ATOMIC_OPS 0
216 typedef int32_t ngx_atomic_int_t;
217 typedef uint32_t ngx_atomic_uint_t;
218 typedef volatile ngx_atomic_uint_t ngx_atomic_t;
219 #define NGX_ATOMIC_T_LEN sizeof("-2147483648") - 1
222 static ngx_inline ngx_atomic_uint_t
223 ngx_atomic_cmp_set(ngx_atomic_t *lock, ngx_atomic_uint_t old,
224 ngx_atomic_uint_t set)
235 static ngx_inline ngx_atomic_int_t
236 ngx_atomic_fetch_add(ngx_atomic_t *value, ngx_atomic_int_t add)
238 ngx_atomic_int_t old;
246 #define ngx_memory_barrier()
247 #define ngx_cpu_pause()
252 void ngx_spinlock(ngx_atomic_t *lock, ngx_atomic_int_t value, ngx_uint_t spin);
254 #define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
255 #define ngx_unlock(lock) *(lock) = 0
258 #endif /* _NGX_ATOMIC_H_INCLUDED_ */