From bb867e524e39de37a926f836f410a619763dd44a Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Fri, 27 Oct 2017 02:13:10 +0800 Subject: [PATCH 1/7] robust lock --- apc_lock.c | 48 ++++++++++++++++++++++++++++++------------------ apc_lock_api.h | 8 +++++++- config.m4 | 16 ++++++++++++++++ 3 files changed, 53 insertions(+), 19 deletions(-) diff --git a/apc_lock.c b/apc_lock.c index 80697a41..e52bf00f 100644 --- a/apc_lock.c +++ b/apc_lock.c @@ -119,20 +119,18 @@ PHP_APCU_API zend_bool apc_lock_init() { #ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK -# ifdef APC_LOCK_RECURSIVE - if (pthread_mutexattr_init(&apc_lock_attr) == SUCCESS) { - if (pthread_mutexattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { + zend_bool ret; + if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { + if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { + # ifdef APC_LOCK_RECURSIVE pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE); - return 1; - } - } -# else - if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { - if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { - return 1; - } - } -# endif + # endif + # ifdef APC_LOCK_ROBUST + pthread_mutexattr_setrobust(&apc_lock_attr, PTHREAD_MUTEX_ROBUST); + # endif + return 1; + } + } # endif #endif return 0; @@ -212,11 +210,18 @@ PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) { #ifndef PHP_WIN32 #ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK + int result; # ifdef APC_LOCK_RECURSIVE - pthread_mutex_lock(lock); + result = pthread_mutex_lock(lock); # else - pthread_rwlock_rdlock(lock); -# endif + result = pthread_rwlock_rdlock(lock); +# endif +# ifdef APC_LOCK_ROBUST + if (result == EOWNERDEAD) + { + return 0; + } +# endif # else { /* FCNTL */ @@ -239,10 +244,17 @@ PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock) { #ifndef PHP_WIN32 #ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK + int result; # ifdef APC_LOCK_RECURSIVE - pthread_mutex_lock(lock); + result = pthread_mutex_lock(lock); # else - pthread_rwlock_wrlock(lock); + result = pthread_rwlock_wrlock(lock); +# endif +# ifdef APC_LOCK_ROBUST + if (result == EOWNERDEAD) + { + return 0; + } # endif # else { diff --git a/apc_lock_api.h b/apc_lock_api.h index 2a6e7793..304bf06c 100644 --- a/apc_lock_api.h +++ b/apc_lock_api.h @@ -86,9 +86,15 @@ PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */ /* {{{ generic locking macros */ #define CREATE_LOCK(lock) apc_lock_create(lock) #define DESTROY_LOCK(lock) apc_lock_destroy(lock) + +#ifdef APC_LOCK_ROBUST +#define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_wlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");} } +#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_rlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");)} } +#else #define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_wlock(lock); } -#define WUNLOCK(lock) { apc_lock_wunlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); } #define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_rlock(lock); } +#endif +#define WUNLOCK(lock) { apc_lock_wunlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); } #define RUNLOCK(lock) { apc_lock_runlock(lock); HANDLE_UNBLOCK_INTERRUPTIONS(); } #define LOCK WLOCK #define UNLOCK WUNLOCK diff --git a/config.m4 b/config.m4 index b2b836a8..6ade7ef6 100644 --- a/config.m4 +++ b/config.m4 @@ -16,6 +16,17 @@ AC_ARG_ENABLE(apcu-rwlocks, AC_MSG_RESULT(yes) ]) +AC_MSG_CHECKING(if APCu should use robust locking) +AC_ARG_ENABLE(apcu-robust, +[ --enable-apcu-robust Enable APCu robust locking], +[ + PHP_APCU_ROBUST=$enableval +], +[ + PHP_APCU_ROBUST=no +]) +AC_MSG_RESULT($PHP_APCU_ROBUST) + AC_MSG_CHECKING(if APCu should be built in debug mode) AC_ARG_ENABLE(apcu-debug, [ --enable-apcu-debug Enable APCu debugging], @@ -204,6 +215,11 @@ if test "$PHP_APCU" != "no"; then LIBS="$orig_LIBS" fi + if test "$PHP_APCU_ROBUST" != "no"; then + AC_DEFINE(APC_LOCK_ROBUST, 1, [ ]) + AC_MSG_WARN([APCu robust locking enabled]) + fi + if test "$PHP_APCU_RWLOCKS" == "no"; then if test "$PHP_APCU_MUTEX" == "no"; then if test "$PHP_APCU_SPINLOCK" != "no"; then From 8b77ca15791e949409e2e69a21a85d65740cd4a2 Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Fri, 27 Oct 2017 02:29:22 +0800 Subject: [PATCH 2/7] fix --- apc_lock_api.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apc_lock_api.h b/apc_lock_api.h index 304bf06c..c4929e6b 100644 --- a/apc_lock_api.h +++ b/apc_lock_api.h @@ -89,7 +89,7 @@ PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */ #ifdef APC_LOCK_ROBUST #define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_wlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");} } -#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_rlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");)} } +#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_rlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");} } #else #define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_wlock(lock); } #define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_rlock(lock); } From 9068b32f12561546558e15924ff396ef64fbed62 Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Fri, 27 Oct 2017 04:54:59 +0800 Subject: [PATCH 3/7] fix --- apc_lock.c | 8 ++++---- apc_lock_api.h | 5 +++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/apc_lock.c b/apc_lock.c index e52bf00f..599353f7 100644 --- a/apc_lock.c +++ b/apc_lock.c @@ -123,10 +123,10 @@ PHP_APCU_API zend_bool apc_lock_init() { if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { # ifdef APC_LOCK_RECURSIVE - pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE); # endif # ifdef APC_LOCK_ROBUST - pthread_mutexattr_setrobust(&apc_lock_attr, PTHREAD_MUTEX_ROBUST); + pthread_mutexattr_setrobust(&apc_lock_attr, PTHREAD_MUTEX_ROBUST); # endif return 1; } @@ -217,7 +217,7 @@ PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) { result = pthread_rwlock_rdlock(lock); # endif # ifdef APC_LOCK_ROBUST - if (result == EOWNERDEAD) + if (result != SUCCESS) { return 0; } @@ -251,7 +251,7 @@ PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock) { result = pthread_rwlock_wrlock(lock); # endif # ifdef APC_LOCK_ROBUST - if (result == EOWNERDEAD) + if (result != SUCCESS) { return 0; } diff --git a/apc_lock_api.h b/apc_lock_api.h index 304bf06c..2f087663 100644 --- a/apc_lock_api.h +++ b/apc_lock_api.h @@ -37,6 +37,7 @@ # ifndef APC_FCNTL_LOCK # if defined(APC_NATIVE_RWLOCK) && defined(HAVE_ATOMIC_OPERATIONS) typedef pthread_rwlock_t apc_lock_t; +# define APC_LOCK_RECURSIVE # define APC_LOCK_SHARED # else typedef pthread_mutex_t apc_lock_t; @@ -88,8 +89,8 @@ PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock); /* }}} */ #define DESTROY_LOCK(lock) apc_lock_destroy(lock) #ifdef APC_LOCK_ROBUST -#define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_wlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");} } -#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(apc_lock_rlock(lock)==EOWNERDEAD){apc_error("inconsistent memory");)} } +#define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(!apc_lock_wlock(lock)){apc_error("inconsistent memory");} } +#define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); if(!apc_lock_rlock(lock)){apc_error("inconsistent memory");} } #else #define WLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_wlock(lock); } #define RLOCK(lock) { HANDLE_BLOCK_INTERRUPTIONS(); apc_lock_rlock(lock); } From 7a25571c13e3dd3e035e45cf27d9652529fd603d Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Fri, 27 Oct 2017 05:06:16 +0800 Subject: [PATCH 4/7] remove dead var --- apc_lock.c | 1 - 1 file changed, 1 deletion(-) diff --git a/apc_lock.c b/apc_lock.c index 599353f7..56dc3e08 100644 --- a/apc_lock.c +++ b/apc_lock.c @@ -119,7 +119,6 @@ PHP_APCU_API zend_bool apc_lock_init() { #ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK - zend_bool ret; if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { # ifdef APC_LOCK_RECURSIVE From 23613e0a6f6258ec770d04de97260806248a10f9 Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Fri, 27 Oct 2017 14:48:08 +0800 Subject: [PATCH 5/7] fix --- apc_lock.c | 31 ++++++++++++++++++++----------- apc_lock_api.h | 3 +-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apc_lock.c b/apc_lock.c index 56dc3e08..c09ad540 100644 --- a/apc_lock.c +++ b/apc_lock.c @@ -119,17 +119,26 @@ PHP_APCU_API zend_bool apc_lock_init() { #ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK - if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { - if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { - # ifdef APC_LOCK_RECURSIVE - pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE); - # endif - # ifdef APC_LOCK_ROBUST - pthread_mutexattr_setrobust(&apc_lock_attr, PTHREAD_MUTEX_ROBUST); - # endif - return 1; - } - } +# ifdef APC_LOCK_RECURSIVE + if (pthread_mutexattr_init(&apc_lock_attr) == SUCCESS) { + if (pthread_mutexattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { + pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE); + #ifdef APC_LOCK_ROBUST + pthread_mutexattr_setrobust(&apc_lock_attr, PTHREAD_MUTEX_ROBUST); + #endif + return 1; + } + } +# else + if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { + if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { + #ifdef APC_LOCK_ROBUST + pthread_mutexattr_setrobust(&apc_lock_attr, PTHREAD_MUTEX_ROBUST); + #endif + return 1; + } + } +# endif # endif #endif return 0; diff --git a/apc_lock_api.h b/apc_lock_api.h index 2f087663..18cabbf5 100644 --- a/apc_lock_api.h +++ b/apc_lock_api.h @@ -35,9 +35,8 @@ # include "pthread.h" # ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK -# if defined(APC_NATIVE_RWLOCK) && defined(HAVE_ATOMIC_OPERATIONS) +# if defined(APC_NATIVE_RWLOCK) && defined(HAVE_ATOMIC_OPERATIONS) && !defined(APC_LOCK_ROBUST) typedef pthread_rwlock_t apc_lock_t; -# define APC_LOCK_RECURSIVE # define APC_LOCK_SHARED # else typedef pthread_mutex_t apc_lock_t; From dc8c47d7ed0e0bccfdc4c5f78a3be88f46e2ca9e Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Fri, 27 Oct 2017 15:53:09 +0800 Subject: [PATCH 6/7] fix try --- apc.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apc.h b/apc.h index abd72824..9e4a81aa 100644 --- a/apc.h +++ b/apc.h @@ -173,9 +173,8 @@ PHP_APCU_API int APC_UNSERIALIZER_NAME(eval) (APC_UNSERIALIZER_ARGS); /* }}} */ JMP_BUF *zb = EG(bailout); \ JMP_BUF ab; \ \ - EG(bailout) = &ab; \ - \ begin; \ + EG(bailout) = &ab; \ if (SETJMP(ab) == SUCCESS) { \ block \ } else { \ From 3e19f65280c802b993a466198c04b21f47e7bb45 Mon Sep 17 00:00:00 2001 From: xiezhenye Date: Mon, 30 Oct 2017 15:00:09 +0800 Subject: [PATCH 7/7] remove unused var --- .gitignore | 8 ++++++++ apc_lock.c | 1 - 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index cf615c6e..52907c51 100644 --- a/.gitignore +++ b/.gitignore @@ -254,3 +254,11 @@ win32/phpts.def win32/wsyslog.h win32/win32.dsp win32/php.dsw + +Makefile.global +acinclude.m4 +apc_lock.loT +build/ +configure.in +ltmain.sh +run-tests.php diff --git a/apc_lock.c b/apc_lock.c index e52bf00f..a79cb23f 100644 --- a/apc_lock.c +++ b/apc_lock.c @@ -119,7 +119,6 @@ PHP_APCU_API zend_bool apc_lock_init() { #ifndef APC_SPIN_LOCK # ifndef APC_FCNTL_LOCK - zend_bool ret; if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) { if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) { # ifdef APC_LOCK_RECURSIVE