aboutsummaryrefslogtreecommitdiff
path: root/src/core/ngx_atomic.h
blob: 5511ea76591a5bc680c47c431bf66bb780f69966 (plain)
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
#ifndef _NGX_ATOMIC_H_INCLUDED_
#define _NGX_ATOMIC_H_INCLUDED_


#include <ngx_config.h>
#include <ngx_core.h>


#if ( __i386__ || __amd64__ )

typedef volatile uint32_t  ngx_atomic_t;

#if (NGX_SMP)
#define NGX_SMP_LOCK  "lock;"
#else
#define NGX_SMP_LOCK
#endif


static ngx_inline uint32_t ngx_atomic_inc(ngx_atomic_t *value)
{
    uint32_t  old;

    __asm__ volatile (

        NGX_SMP_LOCK
    "   xaddl  %0, %2;   "
    "   incl   %0;       "

    : "=q" (old) : "0" (1), "m" (*value));

    return old;
}


static ngx_inline uint32_t ngx_atomic_dec(ngx_atomic_t *value)
{
    uint32_t  old;

    __asm__ volatile (

        NGX_SMP_LOCK
    "   xaddl  %0, %1;   "
    "   decl   %0;       "

    : "=q" (old) : "0" (-1), "m" (*value));

    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;

    __asm__ volatile (

        NGX_SMP_LOCK
    "   cmpxchgl  %3, %1;   "
    "   setz      %%al;     "
    "   movzbl    %%al, %0; "

    : "=a" (res) : "m" (*lock), "a" (old), "q" (set));

    return res;
}


#elif (WIN32)

#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
#define ngx_atomic_cmp_set(lock, old, set)                                   \
                                  InterlockedCompareExchange(lock, set, old)
#endif


#else

typedef volatile uint32_t  ngx_atomic_t;

/* STUB */
#define ngx_atomic_inc(x)   (*(x))++;
#define ngx_atomic_dec(x)   (*(x))--;
#define ngx_atomic_cmp_set(lock, old, set)   1
/**/

#endif


#endif /* _NGX_ATOMIC_H_INCLUDED_ */