forked from krakjoe/apcu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
apc_mutex.c
77 lines (63 loc) · 2.24 KB
/
apc_mutex.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
/*
+----------------------------------------------------------------------+
| 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: Fabian Franz <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "apc_mutex.h"
#ifdef APC_HAS_PTHREAD_MUTEX
static zend_bool apc_mutex_ready = 0;
static pthread_mutexattr_t apc_mutex_attr;
PHP_APCU_API zend_bool apc_mutex_init() {
if (apc_mutex_ready) {
return 1;
}
apc_mutex_ready = 1;
if (pthread_mutexattr_init(&apc_mutex_attr) != SUCCESS) {
return 0;
}
if (pthread_mutexattr_setpshared(&apc_mutex_attr, PTHREAD_PROCESS_SHARED) != SUCCESS) {
return 0;
}
return 1;
}
PHP_APCU_API void apc_mutex_cleanup() {
if (!apc_mutex_ready) {
return;
}
apc_mutex_ready = 0;
pthread_mutexattr_destroy(&apc_mutex_attr);
}
PHP_APCU_API zend_bool apc_mutex_create(apc_mutex_t *lock) {
pthread_mutex_init(lock, &apc_mutex_attr);
return 1;
}
PHP_APCU_API zend_bool apc_mutex_lock(apc_mutex_t *lock) {
HANDLE_BLOCK_INTERRUPTIONS();
if (pthread_mutex_lock(lock) == 0) {
return 1;
}
HANDLE_UNBLOCK_INTERRUPTIONS();
apc_warning("Failed to acquire lock");
return 0;
}
PHP_APCU_API zend_bool apc_mutex_unlock(apc_mutex_t *lock) {
pthread_mutex_unlock(lock);
HANDLE_UNBLOCK_INTERRUPTIONS();
return 1;
}
PHP_APCU_API void apc_mutex_destroy(apc_mutex_t *lock) {
pthread_mutex_destroy(lock);
}
#endif