2004-09-28 16:34:51 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2002-2004 Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
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-09-23 00:18:21 +08:00
|
|
|
#define NGX_HAVE_ATOMIC_OPS 1
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-09-23 00:18:21 +08:00
|
|
|
#if 0
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2004-09-23 00:18:21 +08:00
|
|
|
#endif
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
2004-09-14 23:55:24 +08:00
|
|
|
#elif ( __sparc__ )
|
|
|
|
|
2004-09-23 00:18:21 +08:00
|
|
|
#define NGX_HAVE_ATOMIC_OPS 1
|
|
|
|
|
2004-09-14 23:55:24 +08:00
|
|
|
typedef volatile uint32_t ngx_atomic_t;
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_inline uint32_t ngx_atomic_inc(ngx_atomic_t *value)
|
|
|
|
{
|
|
|
|
uint32_t old, new, res;
|
|
|
|
|
|
|
|
old = *value;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
new = old + 1;
|
|
|
|
res = new;
|
|
|
|
|
|
|
|
__asm__ volatile (
|
|
|
|
|
2004-09-21 23:47:05 +08:00
|
|
|
"casa [%1] 0x80, %2, %0"
|
2004-09-14 23:55:24 +08:00
|
|
|
|
|
|
|
: "+r" (res) : "r" (value), "r" (old));
|
|
|
|
|
|
|
|
if (res == old) {
|
|
|
|
return new;
|
|
|
|
}
|
|
|
|
|
|
|
|
old = res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_inline uint32_t ngx_atomic_cmp_set(ngx_atomic_t *lock,
|
|
|
|
ngx_atomic_t old,
|
|
|
|
ngx_atomic_t set)
|
|
|
|
{
|
2004-09-17 00:10:13 +08:00
|
|
|
uint32_t res = (uint32_t) set;
|
2004-09-14 23:55:24 +08:00
|
|
|
|
|
|
|
__asm__ volatile (
|
|
|
|
|
2004-09-21 23:47:05 +08:00
|
|
|
"casa [%1] 0x80, %2, %0"
|
2004-09-14 23:55:24 +08:00
|
|
|
|
|
|
|
: "+r" (res) : "r" (lock), "r" (old));
|
|
|
|
|
|
|
|
return (res == old);
|
|
|
|
}
|
|
|
|
|
2004-02-24 04:57:12 +08:00
|
|
|
#else
|
|
|
|
|
2004-09-23 00:18:21 +08:00
|
|
|
#define NGX_HAVE_ATOMIC_OPS 0
|
|
|
|
|
2004-02-25 01:31:46 +08:00
|
|
|
typedef volatile uint32_t ngx_atomic_t;
|
2004-02-24 04:57:12 +08:00
|
|
|
|
2004-09-23 00:18:21 +08:00
|
|
|
#define ngx_atomic_inc(x) ++(*(x));
|
|
|
|
|
|
|
|
static ngx_inline uint32_t ngx_atomic_cmp_set(ngx_atomic_t *lock,
|
|
|
|
ngx_atomic_t old,
|
|
|
|
ngx_atomic_t set)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2004-02-24 04:57:12 +08:00
|
|
|
|
|
|
|
#endif
|
2003-12-19 16:15:11 +08:00
|
|
|
|
|
|
|
|
2004-06-30 23:30:41 +08:00
|
|
|
void ngx_spinlock(ngx_atomic_t *lock, ngx_uint_t spin);
|
|
|
|
|
2004-06-29 00:05:02 +08:00
|
|
|
#define ngx_trylock(lock) (*(lock) == 0 && ngx_atomic_cmp_set(lock, 0, 1))
|
|
|
|
#define ngx_unlock(lock) *(lock) = 0
|
2004-06-28 02:01:57 +08:00
|
|
|
|
|
|
|
|
2003-12-19 16:15:11 +08:00
|
|
|
#endif /* _NGX_ATOMIC_H_INCLUDED_ */
|