forked from krakjoe/apcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc_lock.c
340 lines (265 loc) · 7.36 KB
/
apc_lock.c
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
+----------------------------------------------------------------------+
| APCu |
+----------------------------------------------------------------------+
| Copyright (c) 2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Joe Watkins <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_APC_LOCK_H
# include "apc_lock.h"
#endif
/*
* While locking calls should never fail, apcu checks for the success of write-lock
* acquisitions, to prevent more damage when a deadlock is detected.
*/
#ifdef PHP_WIN32
PHP_APCU_API zend_bool apc_lock_init() {
return 1;
}
PHP_APCU_API void apc_lock_cleanup() {
}
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
return NULL != apc_windows_cs_create(lock);
}
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
apc_windows_cs_rdlock(lock);
return 1;
}
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
apc_windows_cs_lock(lock);
return 1;
}
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock) {
apc_windows_cs_unlock_wr(lock);
return 1;
}
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock) {
apc_windows_cs_unlock_rd(lock);
return 1;
}
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock) {
apc_windows_cs_destroy(lock);
}
#elif defined(APC_NATIVE_RWLOCK)
static zend_bool apc_lock_ready = 0;
static pthread_rwlockattr_t apc_lock_attr;
PHP_APCU_API zend_bool apc_lock_init() {
if (apc_lock_ready) {
return 1;
}
apc_lock_ready = 1;
if (pthread_rwlockattr_init(&apc_lock_attr) != SUCCESS) {
return 0;
}
if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) != SUCCESS) {
return 0;
}
return 1;
}
PHP_APCU_API void apc_lock_cleanup() {
if (!apc_lock_ready) {
return;
}
apc_lock_ready = 0;
pthread_rwlockattr_destroy(&apc_lock_attr);
}
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
return pthread_rwlock_init(lock, &apc_lock_attr) == SUCCESS;
}
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
return pthread_rwlock_rdlock(lock) == 0;
}
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
return pthread_rwlock_wrlock(lock) == 0;
}
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock) {
pthread_rwlock_unlock(lock);
return 1;
}
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock) {
pthread_rwlock_unlock(lock);
return 1;
}
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock) {
pthread_rwlock_destroy(lock);
}
#elif defined(APC_LOCK_RECURSIVE)
static zend_bool apc_lock_ready = 0;
static pthread_mutexattr_t apc_lock_attr;
PHP_APCU_API zend_bool apc_lock_init() {
if (apc_lock_ready) {
return 1;
}
apc_lock_ready = 1;
if (pthread_mutexattr_init(&apc_lock_attr) != SUCCESS) {
return 0;
}
if (pthread_mutexattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) != SUCCESS) {
return 0;
}
pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE);
return 1;
}
PHP_APCU_API void apc_lock_cleanup() {
if (!apc_lock_ready) {
return;
}
apc_lock_ready = 0;
pthread_mutexattr_destroy(&apc_lock_attr);
}
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
pthread_mutex_init(lock, &apc_lock_attr);
return 1;
}
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
return pthread_mutex_lock(lock) == 0;
}
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
return pthread_mutex_lock(lock) == 0;
}
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock) {
pthread_mutex_unlock(lock);
return 1;
}
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock) {
pthread_mutex_unlock(lock);
return 1;
}
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock) {
pthread_mutex_destroy(lock);
}
#elif defined(APC_SPIN_LOCK)
static int apc_lock_try(apc_lock_t *lock) {
int failed = 1;
asm volatile
(
"xchgl %0, 0(%1)" :
"=r" (failed) : "r" (&lock->state),
"0" (failed)
);
return failed;
}
static int apc_lock_get(apc_lock_t *lock) {
int failed = 1;
do {
failed = apc_lock_try(lock);
#ifdef APC_LOCK_NICE
usleep(0);
#endif
} while (failed);
return failed;
}
static int apc_lock_release(apc_lock_t *lock) {
int released = 0;
asm volatile (
"xchg %0, 0(%1)" : "=r" (released) : "r" (&lock->state),
"0" (released)
);
return !released;
}
PHP_APCU_API zend_bool apc_lock_init() {
return 0;
}
PHP_APCU_API void apc_lock_cleanup() {
}
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
lock->state = 0;
}
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
apc_lock_get(lock);
return 1;
}
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
apc_lock_get(lock);
return 1;
}
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock) {
apc_lock_release(lock);
return 1;
}
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock) {
apc_lock_release(lock);
return 1;
}
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock) {
}
#else
#include <unistd.h>
#include <fcntl.h>
static int apc_fcntl_call(int fd, int cmd, int type, off_t offset, int whence, off_t len) {
int ret;
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
lock.l_pid = 0;
do {
ret = fcntl(fd, cmd, &lock) ;
} while(ret < 0 && errno == EINTR);
return(ret);
}
PHP_APCU_API zend_bool apc_lock_init() {
return 0;
}
PHP_APCU_API void apc_lock_cleanup() {
}
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
char lock_path[] = "/tmp/.apc.XXXXXX";
*lock = mkstemp(lock_path);
if (*lock > 0) {
unlink(lock_path);
return 1;
} else {
return 0;
}
}
static inline zend_bool apc_lock_rlock_impl(apc_lock_t *lock) {
apc_fcntl_call((*lock), F_SETLKW, F_RDLCK, 0, SEEK_SET, 0);
return 1;
}
static inline zend_bool apc_lock_wlock_impl(apc_lock_t *lock) {
apc_fcntl_call((*lock), F_SETLKW, F_WRLCK, 0, SEEK_SET, 0);
return 1;
}
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock) {
apc_fcntl_call((*lock), F_SETLKW, F_UNLCK, 0, SEEK_SET, 0);
return 1;
}
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock) {
apc_fcntl_call((*lock), F_SETLKW, F_UNLCK, 0, SEEK_SET, 0);
return 1;
}
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock) {
close(*lock);
}
#endif
/* Shared for all lock implementations */
PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock) {
HANDLE_BLOCK_INTERRUPTIONS();
if (apc_lock_wlock_impl(lock)) {
return 1;
}
HANDLE_UNBLOCK_INTERRUPTIONS();
apc_warning("Failed to acquire write lock");
return 0;
}
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
HANDLE_BLOCK_INTERRUPTIONS();
if (apc_lock_rlock_impl(lock)) {
return 1;
}
HANDLE_UNBLOCK_INTERRUPTIONS();
apc_warning("Failed to acquire read lock");
return 0;
}