160
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #ifndef _NGX_SHMTX_H_INCLUDED_
|
|
8 #define _NGX_SHMTX_H_INCLUDED_
|
|
9
|
|
10
|
|
11 #include <ngx_config.h>
|
|
12 #include <ngx_core.h>
|
|
13
|
|
14
|
|
15 typedef struct {
|
|
16 #if (NGX_HAVE_ATOMIC_OPS)
|
|
17 ngx_atomic_t *lock;
|
|
18 #else
|
|
19 ngx_fd_t fd;
|
|
20 u_char *name;
|
|
21 #endif
|
|
22 } ngx_shmtx_t;
|
|
23
|
|
24
|
258
|
25 ngx_int_t ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name);
|
160
|
26
|
|
27
|
|
28 #if (NGX_HAVE_ATOMIC_OPS)
|
|
29
|
|
30 static ngx_inline ngx_uint_t
|
|
31 ngx_shmtx_trylock(ngx_shmtx_t *mtx)
|
|
32 {
|
|
33 if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
|
|
34 return 1;
|
|
35 }
|
|
36
|
|
37 return 0;
|
|
38 }
|
|
39
|
|
40 #define ngx_shmtx_lock(mtx) ngx_spinlock((mtx)->lock, ngx_pid, 1024)
|
|
41
|
|
42 #define ngx_shmtx_unlock(mtx) (void) ngx_atomic_cmp_set((mtx)->lock, ngx_pid, 0)
|
|
43
|
|
44 #define ngx_shmtx_destory(mtx)
|
|
45
|
|
46
|
|
47 #else
|
|
48
|
|
49 static ngx_inline ngx_uint_t
|
|
50 ngx_shmtx_trylock(ngx_shmtx_t *mtx)
|
|
51 {
|
|
52 ngx_err_t err;
|
|
53
|
|
54 err = ngx_trylock_fd(mtx->fd);
|
|
55
|
|
56 if (err == 0) {
|
|
57 return 1;
|
|
58 }
|
|
59
|
|
60 if (err == NGX_EAGAIN) {
|
|
61 return 0;
|
|
62 }
|
|
63
|
258
|
64 ngx_log_abort(err, ngx_trylock_fd_n " failed");
|
160
|
65 }
|
|
66
|
|
67
|
|
68 static ngx_inline void
|
|
69 ngx_shmtx_lock(ngx_shmtx_t *mtx)
|
|
70 {
|
|
71 ngx_err_t err;
|
|
72
|
|
73 err = ngx_lock_fd(mtx->fd);
|
|
74
|
|
75 if (err == 0) {
|
|
76 return;
|
|
77 }
|
|
78
|
258
|
79 ngx_log_abort(err, ngx_lock_fd_n " failed");
|
160
|
80 }
|
|
81
|
|
82
|
|
83 static ngx_inline void
|
|
84 ngx_shmtx_unlock(ngx_shmtx_t *mtx)
|
|
85 {
|
|
86 ngx_err_t err;
|
|
87
|
|
88 err = ngx_unlock_fd(mtx->fd);
|
|
89
|
|
90 if (err == 0) {
|
|
91 return;
|
|
92 }
|
|
93
|
258
|
94 ngx_log_abort(err, ngx_unlock_fd_n " failed");
|
160
|
95 }
|
|
96
|
|
97
|
|
98 void ngx_shmtx_destory(ngx_shmtx_t *mtx);
|
|
99
|
|
100 #endif
|
|
101
|
|
102
|
|
103 #endif /* _NGX_SHMTX_H_INCLUDED_ */
|