160
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9
|
|
10
|
|
11 #if (NGX_HAVE_ATOMIC_OPS)
|
|
12
|
|
13
|
650
|
14 static void ngx_shmtx_wakeup(ngx_shmtx_t *mtx);
|
|
15
|
|
16
|
160
|
17 ngx_int_t
|
650
|
18 ngx_shmtx_create(ngx_shmtx_t *mtx, ngx_shmtx_sh_t *addr, u_char *name)
|
160
|
19 {
|
650
|
20 mtx->lock = &addr->lock;
|
160
|
21
|
624
|
22 if (mtx->spin == (ngx_uint_t) -1) {
|
|
23 return NGX_OK;
|
|
24 }
|
|
25
|
|
26 mtx->spin = 2048;
|
|
27
|
|
28 #if (NGX_HAVE_POSIX_SEM)
|
|
29
|
650
|
30 mtx->wait = &addr->wait;
|
|
31
|
624
|
32 if (sem_init(&mtx->sem, 1, 0) == -1) {
|
|
33 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
|
34 "sem_init() failed");
|
|
35 } else {
|
|
36 mtx->semaphore = 1;
|
|
37 }
|
|
38
|
|
39 #endif
|
|
40
|
160
|
41 return NGX_OK;
|
|
42 }
|
|
43
|
624
|
44
|
|
45 void
|
|
46 ngx_shmtx_destory(ngx_shmtx_t *mtx)
|
|
47 {
|
|
48 #if (NGX_HAVE_POSIX_SEM)
|
|
49
|
|
50 if (mtx->semaphore) {
|
|
51 if (sem_destroy(&mtx->sem) == -1) {
|
|
52 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
|
53 "sem_destroy() failed");
|
|
54 }
|
|
55 }
|
|
56
|
|
57 #endif
|
|
58 }
|
|
59
|
|
60
|
|
61 ngx_uint_t
|
|
62 ngx_shmtx_trylock(ngx_shmtx_t *mtx)
|
|
63 {
|
650
|
64 return (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid));
|
624
|
65 }
|
|
66
|
|
67
|
|
68 void
|
|
69 ngx_shmtx_lock(ngx_shmtx_t *mtx)
|
|
70 {
|
|
71 ngx_uint_t i, n;
|
|
72
|
|
73 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "shmtx lock");
|
|
74
|
|
75 for ( ;; ) {
|
|
76
|
650
|
77 if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
|
624
|
78 return;
|
|
79 }
|
|
80
|
|
81 if (ngx_ncpu > 1) {
|
|
82
|
|
83 for (n = 1; n < mtx->spin; n <<= 1) {
|
|
84
|
|
85 for (i = 0; i < n; i++) {
|
|
86 ngx_cpu_pause();
|
|
87 }
|
|
88
|
650
|
89 if (*mtx->lock == 0
|
|
90 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid))
|
624
|
91 {
|
|
92 return;
|
|
93 }
|
|
94 }
|
|
95 }
|
|
96
|
|
97 #if (NGX_HAVE_POSIX_SEM)
|
|
98
|
|
99 if (mtx->semaphore) {
|
650
|
100 (void) ngx_atomic_fetch_add(mtx->wait, 1);
|
624
|
101
|
650
|
102 if (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)) {
|
|
103 return;
|
|
104 }
|
|
105
|
|
106 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
107 "shmtx wait %uA", *mtx->wait);
|
624
|
108
|
650
|
109 while (sem_wait(&mtx->sem) == -1) {
|
|
110 ngx_err_t err;
|
624
|
111
|
650
|
112 err = ngx_errno;
|
624
|
113
|
650
|
114 if (err != NGX_EINTR) {
|
|
115 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err,
|
|
116 "sem_wait() failed while waiting on shmtx");
|
|
117 break;
|
624
|
118 }
|
|
119
|
|
120 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
121 "shmtx awoke");
|
|
122 }
|
|
123
|
|
124 continue;
|
|
125 }
|
|
126
|
|
127 #endif
|
|
128
|
|
129 ngx_sched_yield();
|
|
130 }
|
|
131 }
|
|
132
|
|
133
|
|
134 void
|
|
135 ngx_shmtx_unlock(ngx_shmtx_t *mtx)
|
|
136 {
|
|
137 if (mtx->spin != (ngx_uint_t) -1) {
|
|
138 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, "shmtx unlock");
|
|
139 }
|
|
140
|
650
|
141 if (ngx_atomic_cmp_set(mtx->lock, ngx_pid, 0)) {
|
|
142 ngx_shmtx_wakeup(mtx);
|
|
143 }
|
|
144 }
|
|
145
|
|
146
|
|
147 ngx_uint_t
|
|
148 ngx_shmtx_force_unlock(ngx_shmtx_t *mtx, ngx_pid_t pid)
|
|
149 {
|
|
150 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
151 "shmtx forced unlock");
|
|
152
|
|
153 if (ngx_atomic_cmp_set(mtx->lock, pid, 0)) {
|
|
154 ngx_shmtx_wakeup(mtx);
|
|
155 return 1;
|
|
156 }
|
|
157
|
|
158 return 0;
|
|
159 }
|
|
160
|
|
161
|
|
162 static void
|
|
163 ngx_shmtx_wakeup(ngx_shmtx_t *mtx)
|
|
164 {
|
|
165 #if (NGX_HAVE_POSIX_SEM)
|
|
166 ngx_atomic_uint_t wait;
|
|
167
|
|
168 if (!mtx->semaphore) {
|
|
169 return;
|
|
170 }
|
|
171
|
624
|
172 for ( ;; ) {
|
|
173
|
650
|
174 wait = *mtx->wait;
|
624
|
175
|
650
|
176 if (wait == 0) {
|
|
177 return;
|
|
178 }
|
|
179
|
|
180 if (ngx_atomic_cmp_set(mtx->wait, wait, wait - 1)) {
|
624
|
181 break;
|
|
182 }
|
|
183 }
|
|
184
|
|
185 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
650
|
186 "shmtx wake %uA", wait);
|
624
|
187
|
|
188 if (sem_post(&mtx->sem) == -1) {
|
|
189 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
|
190 "sem_post() failed while wake shmtx");
|
|
191 }
|
|
192
|
|
193 #endif
|
|
194 }
|
|
195
|
|
196
|
160
|
197 #else
|
|
198
|
|
199
|
|
200 ngx_int_t
|
650
|
201 ngx_shmtx_create(ngx_shmtx_t *mtx, ngx_shmtx_sh_t *addr, u_char *name)
|
160
|
202 {
|
|
203 if (mtx->name) {
|
|
204
|
|
205 if (ngx_strcmp(name, mtx->name) == 0) {
|
|
206 mtx->name = name;
|
|
207 return NGX_OK;
|
|
208 }
|
|
209
|
|
210 ngx_shmtx_destory(mtx);
|
|
211 }
|
|
212
|
284
|
213 mtx->fd = ngx_open_file(name, NGX_FILE_RDWR, NGX_FILE_CREATE_OR_OPEN,
|
|
214 NGX_FILE_DEFAULT_ACCESS);
|
160
|
215
|
|
216 if (mtx->fd == NGX_INVALID_FILE) {
|
258
|
217 ngx_log_error(NGX_LOG_EMERG, ngx_cycle->log, ngx_errno,
|
160
|
218 ngx_open_file_n " \"%s\" failed", name);
|
|
219 return NGX_ERROR;
|
|
220 }
|
|
221
|
|
222 if (ngx_delete_file(name) == NGX_FILE_ERROR) {
|
258
|
223 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
160
|
224 ngx_delete_file_n " \"%s\" failed", name);
|
|
225 }
|
|
226
|
|
227 mtx->name = name;
|
|
228
|
|
229 return NGX_OK;
|
|
230 }
|
|
231
|
|
232
|
|
233 void
|
|
234 ngx_shmtx_destory(ngx_shmtx_t *mtx)
|
|
235 {
|
|
236 if (ngx_close_file(mtx->fd) == NGX_FILE_ERROR) {
|
258
|
237 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, ngx_errno,
|
160
|
238 ngx_close_file_n " \"%s\" failed", mtx->name);
|
|
239 }
|
|
240 }
|
|
241
|
|
242
|
624
|
243 ngx_uint_t
|
|
244 ngx_shmtx_trylock(ngx_shmtx_t *mtx)
|
|
245 {
|
|
246 ngx_err_t err;
|
|
247
|
|
248 err = ngx_trylock_fd(mtx->fd);
|
|
249
|
|
250 if (err == 0) {
|
|
251 return 1;
|
|
252 }
|
|
253
|
|
254 if (err == NGX_EAGAIN) {
|
|
255 return 0;
|
|
256 }
|
|
257
|
|
258 #if __osf__ /* Tru64 UNIX */
|
|
259
|
|
260 if (err == NGX_EACCESS) {
|
|
261 return 0;
|
|
262 }
|
|
263
|
160
|
264 #endif
|
624
|
265
|
|
266 ngx_log_abort(err, ngx_trylock_fd_n " %s failed", mtx->name);
|
|
267
|
|
268 return 0;
|
|
269 }
|
|
270
|
|
271
|
|
272 void
|
|
273 ngx_shmtx_lock(ngx_shmtx_t *mtx)
|
|
274 {
|
|
275 ngx_err_t err;
|
|
276
|
|
277 err = ngx_lock_fd(mtx->fd);
|
|
278
|
|
279 if (err == 0) {
|
|
280 return;
|
|
281 }
|
|
282
|
|
283 ngx_log_abort(err, ngx_lock_fd_n " %s failed", mtx->name);
|
|
284 }
|
|
285
|
|
286
|
|
287 void
|
|
288 ngx_shmtx_unlock(ngx_shmtx_t *mtx)
|
|
289 {
|
|
290 ngx_err_t err;
|
|
291
|
|
292 err = ngx_unlock_fd(mtx->fd);
|
|
293
|
|
294 if (err == 0) {
|
|
295 return;
|
|
296 }
|
|
297
|
|
298 ngx_log_abort(err, ngx_unlock_fd_n " %s failed", mtx->name);
|
|
299 }
|
|
300
|
650
|
301
|
|
302 ngx_uint_t
|
|
303 ngx_shmtx_force_unlock(ngx_shmtx_t *mtx, ngx_pid_t pid)
|
|
304 {
|
|
305 return 0;
|
|
306 }
|
|
307
|
624
|
308 #endif
|