Skip to content

Commit 0a891b8

Browse files
LionsAdnikic
authored andcommitted
Issue #338: Add mutex implementation.
1 parent 3d5b209 commit 0a891b8

File tree

5 files changed

+149
-3
lines changed

5 files changed

+149
-3
lines changed

apc_mutex.c

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| APCu |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2013 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Fabian Franz <fabian@lionsad.de> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
#include "apc_mutex.h"
19+
20+
#ifdef APC_HAS_PTHREAD_MUTEX
21+
22+
static zend_bool apc_mutex_ready = 0;
23+
static pthread_mutexattr_t apc_mutex_attr;
24+
25+
PHP_APCU_API zend_bool apc_mutex_init() {
26+
if (apc_mutex_ready) {
27+
return 1;
28+
}
29+
apc_mutex_ready = 1;
30+
31+
if (pthread_mutexattr_init(&apc_mutex_attr) != SUCCESS) {
32+
return 0;
33+
}
34+
35+
if (pthread_mutexattr_setpshared(&apc_mutex_attr, PTHREAD_PROCESS_SHARED) != SUCCESS) {
36+
return 0;
37+
}
38+
39+
return 1;
40+
}
41+
42+
PHP_APCU_API void apc_mutex_cleanup() {
43+
if (!apc_mutex_ready) {
44+
return;
45+
}
46+
apc_mutex_ready = 0;
47+
48+
pthread_mutexattr_destroy(&apc_mutex_attr);
49+
}
50+
51+
PHP_APCU_API zend_bool apc_mutex_create(apc_mutex_t *lock) {
52+
pthread_mutex_init(lock, &apc_mutex_attr);
53+
return 1;
54+
}
55+
56+
PHP_APCU_API zend_bool apc_mutex_lock(apc_mutex_t *lock) {
57+
HANDLE_BLOCK_INTERRUPTIONS();
58+
if (pthread_mutex_lock(lock) == 0) {
59+
return 1;
60+
}
61+
62+
HANDLE_UNBLOCK_INTERRUPTIONS();
63+
apc_warning("Failed to acquire lock");
64+
return 0;
65+
}
66+
67+
PHP_APCU_API zend_bool apc_mutex_unlock(apc_mutex_t *lock) {
68+
pthread_mutex_unlock(lock);
69+
HANDLE_UNBLOCK_INTERRUPTIONS();
70+
return 1;
71+
}
72+
73+
PHP_APCU_API void apc_mutex_destroy(apc_mutex_t *lock) {
74+
pthread_mutex_destroy(lock);
75+
}
76+
77+
#endif

apc_mutex.h

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| APCu |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 2013 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.01 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_01.txt |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| license@php.net so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Joe Watkins <joe.watkins@live.co.uk> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
#ifndef APC_MUTEX_H
20+
#define APC_MUTEX_H
21+
22+
#include "apc.h"
23+
24+
#ifdef APC_HAS_PTHREAD_MUTEX
25+
26+
#include "pthread.h"
27+
28+
typedef pthread_mutex_t apc_mutex_t;
29+
30+
PHP_APCU_API zend_bool apc_mutex_init();
31+
PHP_APCU_API void apc_mutex_cleanup();
32+
PHP_APCU_API zend_bool apc_mutex_create(apc_mutex_t *lock);
33+
PHP_APCU_API zend_bool apc_mutex_lock(apc_mutex_t *lock);
34+
PHP_APCU_API zend_bool apc_mutex_unlock(apc_mutex_t *lock);
35+
PHP_APCU_API void apc_mutex_destroy(apc_mutex_t *lock);
36+
37+
#define APC_MUTEX_INIT() apc_mutex_init()
38+
#define APC_MUTEX_CLEANUP() apc_mutex_cleanup()
39+
40+
#define APC_CREATE_MUTEX(lock) apc_mutex_create(lock)
41+
#define APC_DESTROY_MUTEX(lock) apc_mutex_destroy(lock)
42+
#define APC_MUTEX_LOCK(lock) apc_mutex_lock(lock)
43+
#define APC_MUTEX_UNLOCK(lock) apc_mutex_unlock(lock)
44+
45+
#else
46+
47+
#include "apc_lock.h"
48+
49+
typedef apc_lock_t apc_mutex_t;
50+
51+
// Fallback to normal locks
52+
53+
#define APC_MUTEX_INIT()
54+
#define APC_MUTEX_CLEANUP()
55+
56+
#define APC_CREATE_MUTEX(lock) CREATE_LOCK(lock)
57+
#define APC_DESTROY_MUTEX(lock) DESTROY_LOCK(lock)
58+
#define APC_MUTEX_LOCK(lock) WLOCK(lock)
59+
#define APC_MUTEX_UNLOCK(lock) WUNLOCK(lock)
60+
61+
#endif
62+
63+
#endif

config.m4

+4-3
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ if test "$PHP_APCU" != "no"; then
135135
LIBS="$orig_LIBS"
136136
fi
137137

138-
if test "$PHP_APCU_RWLOCKS" = "no"; then
138+
if test "$PHP_APCU" != "no"; then
139139
orig_LIBS="$LIBS"
140140
LIBS="$LIBS -lpthread"
141141
AC_RUN_IFELSE([AC_LANG_SOURCE([[
@@ -172,6 +172,7 @@ if test "$PHP_APCU" != "no"; then
172172
PHP_ADD_LIBRARY(pthread)
173173
PHP_LDFLAGS="$PHP_LDFLAGS -lpthread"
174174
AC_MSG_WARN([APCu has access to mutexes])
175+
AC_DEFINE(APC_HAS_PTHREAD_MUTEX, 1, [ ])
175176
],[ dnl -Failure-
176177
AC_MSG_WARN([It doesn't appear that pthread mutexes are supported on your system])
177178
PHP_APCU_MUTEX=no
@@ -225,7 +226,7 @@ if test "$PHP_APCU" != "no"; then
225226
[AC_DEFINE([HAVE_VALGRIND_MEMCHECK_H],1, [enable valgrind memchecks])])
226227
])
227228

228-
apc_sources="apc.c apc_lock.c php_apc.c \
229+
apc_sources="apc.c apc_lock.c apc_mutex.c php_apc.c \
229230
apc_cache.c \
230231
apc_mmap.c \
231232
apc_shm.c \
@@ -240,7 +241,7 @@ if test "$PHP_APCU" != "no"; then
240241
PHP_SUBST(APCU_SHARED_LIBADD)
241242
PHP_SUBST(APCU_CFLAGS)
242243
PHP_SUBST(PHP_LDFLAGS)
243-
PHP_INSTALL_HEADERS(ext/apcu, [php_apc.h apc.h apc_api.h apc_arginfo.h apc_cache.h apc_cache_api.h apc_globals.h apc_iterator.h apc_lock.h apc_lock_api.h apc_sma.h apc_sma_api.h apc_serializer.h apc_stack.h])
244+
PHP_INSTALL_HEADERS(ext/apcu, [php_apc.h apc.h apc_api.h apc_arginfo.h apc_cache.h apc_cache_api.h apc_globals.h apc_iterator.h apc_lock.h apc_mutex.h apc_lock_api.h apc_sma.h apc_sma_api.h apc_serializer.h apc_stack.h])
244245
AC_DEFINE(HAVE_APCU, 1, [ ])
245246
fi
246247

package.xml

+2
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@
123123
<file name="apc_lock.h" role="src" />
124124
<file name="apc_mmap.c" role="src" />
125125
<file name="apc_mmap.h" role="src" />
126+
<file name="apc_mutex.c" role="src" />
127+
<file name="apc_mutex.h" role="src" />
126128
<file name="apc_persist.c" role="src" />
127129
<file name="apc.php" role="src" />
128130
<file name="apc_php.h" role="src" />

php_apc.c

+3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "apc_iterator.h"
3535
#include "apc_sma.h"
3636
#include "apc_lock.h"
37+
#include "apc_mutex.h"
3738
#include "apc_strings.h"
3839
#include "php_globals.h"
3940
#include "php_ini.h"
@@ -227,6 +228,7 @@ static PHP_MINIT_FUNCTION(apcu)
227228

228229
/* locks initialized regardless of settings */
229230
apc_lock_init();
231+
APC_MUTEX_INIT();
230232

231233
/* Disable APC in cli mode unless overridden by apc.enable_cli */
232234
if (!APCG(enable_cli) && !strcmp(sapi_module.name, "cli")) {
@@ -290,6 +292,7 @@ static PHP_MSHUTDOWN_FUNCTION(apcu)
290292

291293
/* locks shutdown regardless of settings */
292294
apc_lock_cleanup();
295+
APC_MUTEX_CLEANUP();
293296

294297
/* only shut down if APC is enabled */
295298
if (APCG(enabled)) {

0 commit comments

Comments
 (0)