Mercurial > hg > nginx
comparison src/core/ngx_shmtx.h @ 3909:0daede16d68b
use POSIX semaphores in shmtx instead of sched_yield()
number of spinlock spins are increased twice
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Tue, 10 May 2011 11:39:13 +0000 |
parents | e345fcf61a12 |
children | 3f6040cd731e 4919fb357a5d |
comparison
equal
deleted
inserted
replaced
3908:8258eb4c9d25 | 3909:0daede16d68b |
---|---|
13 | 13 |
14 | 14 |
15 typedef struct { | 15 typedef struct { |
16 #if (NGX_HAVE_ATOMIC_OPS) | 16 #if (NGX_HAVE_ATOMIC_OPS) |
17 ngx_atomic_t *lock; | 17 ngx_atomic_t *lock; |
18 #if (NGX_HAVE_POSIX_SEM) | |
19 ngx_uint_t semaphore; | |
20 sem_t sem; | |
21 #endif | |
18 #else | 22 #else |
19 ngx_fd_t fd; | 23 ngx_fd_t fd; |
20 u_char *name; | 24 u_char *name; |
21 #endif | 25 #endif |
26 ngx_uint_t spin; | |
22 } ngx_shmtx_t; | 27 } ngx_shmtx_t; |
23 | 28 |
24 | 29 |
25 ngx_int_t ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name); | 30 ngx_int_t ngx_shmtx_create(ngx_shmtx_t *mtx, void *addr, u_char *name); |
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 return (*mtx->lock == 0 && ngx_atomic_cmp_set(mtx->lock, 0, ngx_pid)); | |
34 } | |
35 | |
36 #define ngx_shmtx_lock(mtx) ngx_spinlock((mtx)->lock, ngx_pid, 1024) | |
37 | |
38 #define ngx_shmtx_unlock(mtx) (void) ngx_atomic_cmp_set((mtx)->lock, ngx_pid, 0) | |
39 | |
40 #define ngx_shmtx_destory(mtx) | |
41 | |
42 | |
43 #else | |
44 | |
45 static ngx_inline ngx_uint_t | |
46 ngx_shmtx_trylock(ngx_shmtx_t *mtx) | |
47 { | |
48 ngx_err_t err; | |
49 | |
50 err = ngx_trylock_fd(mtx->fd); | |
51 | |
52 if (err == 0) { | |
53 return 1; | |
54 } | |
55 | |
56 if (err == NGX_EAGAIN) { | |
57 return 0; | |
58 } | |
59 | |
60 #if __osf__ /* Tru64 UNIX */ | |
61 | |
62 if (err == NGX_EACCESS) { | |
63 return 0; | |
64 } | |
65 | |
66 #endif | |
67 | |
68 ngx_log_abort(err, ngx_trylock_fd_n " %s failed", mtx->name); | |
69 | |
70 return 0; | |
71 } | |
72 | |
73 | |
74 static ngx_inline void | |
75 ngx_shmtx_lock(ngx_shmtx_t *mtx) | |
76 { | |
77 ngx_err_t err; | |
78 | |
79 err = ngx_lock_fd(mtx->fd); | |
80 | |
81 if (err == 0) { | |
82 return; | |
83 } | |
84 | |
85 ngx_log_abort(err, ngx_lock_fd_n " %s failed", mtx->name); | |
86 } | |
87 | |
88 | |
89 static ngx_inline void | |
90 ngx_shmtx_unlock(ngx_shmtx_t *mtx) | |
91 { | |
92 ngx_err_t err; | |
93 | |
94 err = ngx_unlock_fd(mtx->fd); | |
95 | |
96 if (err == 0) { | |
97 return; | |
98 } | |
99 | |
100 ngx_log_abort(err, ngx_unlock_fd_n " %s failed", mtx->name); | |
101 } | |
102 | |
103 | |
104 void ngx_shmtx_destory(ngx_shmtx_t *mtx); | 31 void ngx_shmtx_destory(ngx_shmtx_t *mtx); |
105 | 32 ngx_uint_t ngx_shmtx_trylock(ngx_shmtx_t *mtx); |
106 #endif | 33 void ngx_shmtx_lock(ngx_shmtx_t *mtx); |
34 void ngx_shmtx_unlock(ngx_shmtx_t *mtx); | |
107 | 35 |
108 | 36 |
109 #endif /* _NGX_SHMTX_H_INCLUDED_ */ | 37 #endif /* _NGX_SHMTX_H_INCLUDED_ */ |