summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorylavic <ylavic@13f79535-47bb-0310-9956-ffa450edef68>2019-03-22 01:27:18 +0000
committerylavic <ylavic@13f79535-47bb-0310-9956-ffa450edef68>2019-03-22 01:27:18 +0000
commitcc0c2279907bf74bf22c56cb2a1bb6ba6d1b99c3 (patch)
tree5c1f096f453ec077fb86a2325439fefdb821c5c1
parentaa3211180809dfb97946921c0794fa01a925e5dc (diff)
downloadlibapr-cc0c2279907bf74bf22c56cb2a1bb6ba6d1b99c3.tar.gz
Use proc mutex pthread by default when robust[_np]
On platforms that support pshared and robust pthread mutex, this is usually the best interprocess mutex mechanism because it's efficient, posix, not limited and not persistent on the system when the program exits (i.e. no need to delete it explicitely before leaving, like IPC SysV or files for instance). Note that on older POSIX systems pthread_mutex_{setrobust,consistent}() funcs existed with the non-posix _np() suffix, and we consider them equivalent. git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@1856022 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r--build/apr_threads.m433
-rw-r--r--configure.in8
-rw-r--r--locks/unix/proc_mutex.c28
3 files changed, 62 insertions, 7 deletions
diff --git a/build/apr_threads.m4 b/build/apr_threads.m4
index cbc677b14..6895c3a67 100644
--- a/build/apr_threads.m4
+++ b/build/apr_threads.m4
@@ -262,6 +262,33 @@ int main(int argc, char **argv)
exit(1);
if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))
exit(2);
+ if (pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST))
+ exit(3);
+ if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT))
+ exit(4);
+ if (pthread_mutex_init(&mutex, &attr))
+ exit(5);
+ if (pthread_mutexattr_destroy(&attr))
+ exit(6);
+ if (pthread_mutex_destroy(&mutex))
+ exit(7);
+
+ exit(0);
+}], [apr_cv_mutex_robust_shared=yes], [
+AC_TRY_RUN([
+#include <sys/types.h>
+#include <pthread.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv)
+{
+ pthread_mutex_t mutex;
+ pthread_mutexattr_t attr;
+
+ if (pthread_mutexattr_init(&attr))
+ exit(1);
+ if (pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED))
+ exit(2);
if (pthread_mutexattr_setrobust_np(&attr, PTHREAD_MUTEX_ROBUST_NP))
exit(3);
if (pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT))
@@ -274,10 +301,14 @@ int main(int argc, char **argv)
exit(7);
exit(0);
-}], [apr_cv_mutex_robust_shared=yes], [apr_cv_mutex_robust_shared=no])])
+}], [apr_cv_mutex_robust_shared=np], [apr_cv_mutex_robust_shared=no])
+])])
if test "$apr_cv_mutex_robust_shared" = "yes"; then
AC_DEFINE([HAVE_PTHREAD_MUTEX_ROBUST], 1,
[Define if cross-process robust mutexes are available])
+elif test "$apr_cv_mutex_robust_shared" = "np"; then
+ AC_DEFINE([HAVE_PTHREAD_MUTEX_ROBUST_NP], 1,
+ [Define if non-posix/portable cross-process robust mutexes are available])
fi
])
diff --git a/configure.in b/configure.in
index 4a0ef8293..be06e39cb 100644
--- a/configure.in
+++ b/configure.in
@@ -2324,6 +2324,14 @@ APR_IFALLYES(func:semget func:semctl func:semop define:SEM_UNDO,
APR_DECIDE(USE_SYSVSEM_SERIALIZE, [SysV IPC semget()]))
APR_IFALLYES(header:OS.h func:create_sem func:acquire_sem func:acquire_sem_etc,
APR_DECIDE(USE_BEOSSEM, [BeOS Semaphores]))
+# pthread mutex both pshared and robust[_np] is the best default
+case "$apr_cv_mutex_robust_shared" in
+"yes"|"np")
+ APR_DECIDE(USE_PROC_PTHREAD_SERIALIZE, [pthread pshared mutex])
+ ;;
+*)
+ ;;
+esac
if test "x$apr_lock_method" != "x"; then
APR_DECISION_FORCE($apr_lock_method)
fi
diff --git a/locks/unix/proc_mutex.c b/locks/unix/proc_mutex.c
index a8002c6bd..649c6f00e 100644
--- a/locks/unix/proc_mutex.c
+++ b/locks/unix/proc_mutex.c
@@ -619,9 +619,13 @@ static apr_status_t proc_mutex_pthread_create(apr_proc_mutex_t *new_mutex,
return rv;
}
+#if defined(HAVE_PTHREAD_MUTEX_ROBUST) || defined(HAVE_PTHREAD_MUTEX_ROBUST_NP)
#ifdef HAVE_PTHREAD_MUTEX_ROBUST
- if ((rv = pthread_mutexattr_setrobust_np(&mattr,
- PTHREAD_MUTEX_ROBUST_NP))) {
+ rv = pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST);
+#else
+ rv = pthread_mutexattr_setrobust_np(&mattr, PTHREAD_MUTEX_ROBUST_NP);
+#endif
+ if (rv) {
#ifdef HAVE_ZOS_PTHREADS
rv = errno;
#endif
@@ -637,7 +641,7 @@ static apr_status_t proc_mutex_pthread_create(apr_proc_mutex_t *new_mutex,
pthread_mutexattr_destroy(&mattr);
return rv;
}
-#endif /* HAVE_PTHREAD_MUTEX_ROBUST */
+#endif /* HAVE_PTHREAD_MUTEX_ROBUST[_NP] */
if ((rv = pthread_mutex_init(&proc_pthread_mutex(new_mutex), &mattr))) {
#ifdef HAVE_ZOS_PTHREADS
@@ -689,11 +693,15 @@ static apr_status_t proc_mutex_pthread_acquire_ex(apr_proc_mutex_t *mutex,
#ifdef HAVE_ZOS_PTHREADS
rv = errno;
#endif
-#ifdef HAVE_PTHREAD_MUTEX_ROBUST
+#if defined(HAVE_PTHREAD_MUTEX_ROBUST) || defined(HAVE_PTHREAD_MUTEX_ROBUST_NP)
/* Okay, our owner died. Let's try to make it consistent again. */
if (rv == EOWNERDEAD) {
proc_pthread_mutex_dec(mutex);
+#ifdef HAVE_PTHREAD_MUTEX_ROBUST
+ pthread_mutex_consistent(&proc_pthread_mutex(mutex));
+#else
pthread_mutex_consistent_np(&proc_pthread_mutex(mutex));
+#endif
}
else
#endif
@@ -801,11 +809,15 @@ static apr_status_t proc_mutex_pthread_acquire_ex(apr_proc_mutex_t *mutex,
}
}
if (rv) {
-#ifdef HAVE_PTHREAD_MUTEX_ROBUST
+#if defined(HAVE_PTHREAD_MUTEX_ROBUST) || defined(HAVE_PTHREAD_MUTEX_ROBUST_NP)
/* Okay, our owner died. Let's try to make it consistent again. */
if (rv == EOWNERDEAD) {
proc_pthread_mutex_dec(mutex);
+#ifdef HAVE_PTHREAD_MUTEX_ROBUST
+ pthread_mutex_consistent(&proc_pthread_mutex(mutex));
+#else
pthread_mutex_consistent_np(&proc_pthread_mutex(mutex));
+#endif
}
else
#endif
@@ -847,11 +859,15 @@ static apr_status_t proc_mutex_pthread_release(apr_proc_mutex_t *mutex)
#ifdef HAVE_ZOS_PTHREADS
rv = errno;
#endif
-#ifdef HAVE_PTHREAD_MUTEX_ROBUST
+#if defined(HAVE_PTHREAD_MUTEX_ROBUST) || defined(HAVE_PTHREAD_MUTEX_ROBUST_NP)
/* Okay, our owner died. Let's try to make it consistent again. */
if (rv == EOWNERDEAD) {
proc_pthread_mutex_dec(mutex);
+#ifdef HAVE_PTHREAD_MUTEX_ROBUST
+ pthread_mutex_consistent(&proc_pthread_mutex(mutex));
+#else
pthread_mutex_consistent_np(&proc_pthread_mutex(mutex));
+#endif
}
else
#endif