nginx/src/core/ngx_atomic.h

111 lines
2.0 KiB
C
Raw Normal View History

2003-12-19 16:15:11 +08:00
#ifndef _NGX_ATOMIC_H_INCLUDED_
#define _NGX_ATOMIC_H_INCLUDED_
#include <ngx_config.h>
#include <ngx_core.h>
2004-02-25 01:31:46 +08:00
#if ( __i386__ || __amd64__ )
2004-02-24 04:57:12 +08:00
2004-02-25 01:31:46 +08:00
typedef volatile uint32_t ngx_atomic_t;
2004-02-24 04:57:12 +08:00
#if (NGX_SMP)
2004-03-31 04:31:58 +08:00
#define NGX_SMP_LOCK "lock;"
2004-02-24 04:57:12 +08:00
#else
#define NGX_SMP_LOCK
#endif
static ngx_inline uint32_t ngx_atomic_inc(ngx_atomic_t *value)
{
uint32_t old;
2004-02-25 01:31:46 +08:00
__asm__ volatile (
2004-02-24 04:57:12 +08:00
2004-02-25 01:31:46 +08:00
NGX_SMP_LOCK
2004-03-31 04:31:58 +08:00
" xaddl %0, %2; "
2004-03-31 23:26:46 +08:00
" incl %0; "
2004-02-24 04:57:12 +08:00
2004-03-31 04:31:58 +08:00
: "=q" (old) : "0" (1), "m" (*value));
2004-02-24 04:57:12 +08:00
return old;
}
static ngx_inline uint32_t ngx_atomic_dec(ngx_atomic_t *value)
{
uint32_t old;
2004-02-25 01:31:46 +08:00
__asm__ volatile (
2004-02-24 04:57:12 +08:00
2004-02-25 01:31:46 +08:00
NGX_SMP_LOCK
" xaddl %0, %1; "
2004-03-31 23:26:46 +08:00
" decl %0; "
2004-02-24 04:57:12 +08:00
2004-03-31 04:31:58 +08:00
: "=q" (old) : "0" (-1), "m" (*value));
2004-02-24 04:57:12 +08:00
return old;
}
static ngx_inline uint32_t ngx_atomic_cmp_set(ngx_atomic_t *lock,
ngx_atomic_t old,
ngx_atomic_t set)
{
uint32_t res;
2004-02-25 01:31:46 +08:00
__asm__ volatile (
2004-02-24 04:57:12 +08:00
2004-02-25 01:31:46 +08:00
NGX_SMP_LOCK
" cmpxchgl %3, %1; "
2004-03-01 05:03:02 +08:00
" setz %%al; "
2004-02-25 01:31:46 +08:00
" movzbl %%al, %0; "
2004-02-24 04:57:12 +08:00
2004-03-30 14:27:36 +08:00
: "=a" (res) : "m" (*lock), "a" (old), "q" (set));
2004-02-24 04:57:12 +08:00
return res;
}
2004-03-31 23:26:46 +08:00
#elif (WIN32)
2004-04-01 14:21:13 +08:00
#define ngx_atomic_inc(p) InterlockedIncrement((long *) p)
#define ngx_atomic_dec(p) InterlockedDecrement((long *) p)
/* STUB */
#define ngx_atomic_cmp_set(lock, old, set) 1
#if 0
2004-03-31 23:26:46 +08:00
#define ngx_atomic_cmp_set(lock, old, set) \
InterlockedCompareExchange(lock, set, old)
2004-04-01 14:21:13 +08:00
#endif
2004-03-31 23:26:46 +08:00
2004-02-24 04:57:12 +08:00
#else
2004-02-25 01:31:46 +08:00
typedef volatile uint32_t ngx_atomic_t;
2004-02-24 04:57:12 +08:00
2003-12-19 20:45:27 +08:00
/* STUB */
2004-03-04 15:04:55 +08:00
#define ngx_atomic_inc(x) (*(x))++;
#define ngx_atomic_dec(x) (*(x))--;
2004-04-01 14:21:13 +08:00
#define ngx_atomic_cmp_set(lock, old, set) 1
2004-02-24 04:57:12 +08:00
/**/
#endif
2003-12-19 16:15:11 +08:00
2004-06-28 02:01:57 +08:00
static ngx_inline ngx_int_t ngx_trylock(ngx_atomic_t *lock)
{
if (*lock) {
return NGX_BUSY;
}
if (ngx_atomic_cmp_set(lock, 0, 1)) {
return NGX_OK;
}
return NGX_BUSY;
}
2003-12-19 16:15:11 +08:00
#endif /* _NGX_ATOMIC_H_INCLUDED_ */