From 25cea89d90efca01c69f5749e4ee01b38b0c96b1 Mon Sep 17 00:00:00 2001
From: Tilghman Lesher <tilghman@meg.abyt.es>
Date: Wed, 22 Apr 2009 21:38:15 +0000
Subject: [PATCH] Merged revisions 190092 via svnmerge from
 https://origsvn.digium.com/svn/asterisk/branches/1.4

........
  r190092 | tilghman | 2009-04-22 16:35:03 -0500 (Wed, 22 Apr 2009) | 7 lines

  Detect availability of pthread_rwlock_timedwrlock() before using it.
  (closes issue #14930)
   Reported by: tilghman
   Patches:
         20090420__bug14930.diff.txt uploaded by tilghman (license 14)
   Tested by: mvanbaak, tilghman
........


git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@190093 65c4cc65-6c06-0410-ace0-fbb531ad65f3
---
 configure                        | 66 +++++++++++++++++++++++++++-
 configure.ac                     | 18 ++++++++
 include/asterisk/autoconfig.h.in |  3 ++
 include/asterisk/lock.h          | 75 +++++++++++++++++++++++++++++++-
 4 files changed, 159 insertions(+), 3 deletions(-)

diff --git a/configure b/configure
index 6291aae0b1..6eed86c20f 100755
--- a/configure
+++ b/configure
@@ -1,5 +1,5 @@
 #! /bin/sh
-# From configure.ac Revision: 189629 .
+# From configure.ac Revision: 189813 .
 # Guess values for system-dependent variables and create Makefiles.
 # Generated by GNU Autoconf 2.61 for asterisk 1.6.
 #
@@ -17258,6 +17258,70 @@ fi
 rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
       conftest$ac_exeext conftest.$ac_ext
 
+{ echo "$as_me:$LINENO: checking for pthread_rwlock_timedwrlock() in pthread.h" >&5
+echo $ECHO_N "checking for pthread_rwlock_timedwrlock() in pthread.h... $ECHO_C" >&6; }
+cat >conftest.$ac_ext <<_ACEOF
+/* confdefs.h.  */
+_ACEOF
+cat confdefs.h >>conftest.$ac_ext
+cat >>conftest.$ac_ext <<_ACEOF
+/* end confdefs.h.  */
+#include <pthread.h>
+     #include <time.h>
+int
+main ()
+{
+pthread_rwlock_t foo; struct timespec bar; pthread_rwlock_timedwrlock(&foo, &bar)
+  ;
+  return 0;
+}
+
+_ACEOF
+rm -f conftest.$ac_objext conftest$ac_exeext
+if { (ac_try="$ac_link"
+case "(($ac_try" in
+  *\"* | *\`* | *\\*) ac_try_echo=\$ac_try;;
+  *) ac_try_echo=$ac_try;;
+esac
+eval "echo \"\$as_me:$LINENO: $ac_try_echo\"") >&5
+  (eval "$ac_link") 2>conftest.er1
+  ac_status=$?
+  grep -v '^ *+' conftest.er1 >conftest.err
+  rm -f conftest.er1
+  cat conftest.err >&5
+  echo "$as_me:$LINENO: \$? = $ac_status" >&5
+  (exit $ac_status); } && {
+	 test -z "$ac_c_werror_flag" ||
+	 test ! -s conftest.err
+       } && test -s conftest$ac_exeext &&
+       $as_test_x conftest$ac_exeext; then
+
+    { echo "$as_me:$LINENO: result: yes" >&5
+echo "${ECHO_T}yes" >&6; }
+    ac_cv_pthread_rwlock_timedwrlock="yes"
+
+else
+  echo "$as_me: failed program was:" >&5
+sed 's/^/| /' conftest.$ac_ext >&5
+
+
+    { echo "$as_me:$LINENO: result: no" >&5
+echo "${ECHO_T}no" >&6; }
+    ac_cv_pthread_rwlock_timedwrlock="no"
+
+
+fi
+
+rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
+      conftest$ac_exeext conftest.$ac_ext
+if test "${ac_cv_pthread_rwlock_timedwrlock}" = "yes"; then
+
+cat >>confdefs.h <<\_ACEOF
+#define HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK 1
+_ACEOF
+
+fi
+
 
     if test "x${PBX_PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP}" != "x1"; then
 	{ echo "$as_me:$LINENO: checking for PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP in pthread.h" >&5
diff --git a/configure.ac b/configure.ac
index 95ab0bb58b..211df5e7b4 100644
--- a/configure.ac
+++ b/configure.ac
@@ -461,6 +461,24 @@ AC_DEFINE([HAVE_PTHREAD_MUTEX_RECURSIVE_NP], 1, [Define to 1 if your system defi
 AC_MSG_RESULT(no)
 )
 
+AC_MSG_CHECKING(for pthread_rwlock_timedwrlock() in pthread.h)
+AC_LINK_IFELSE(
+  [AC_LANG_PROGRAM(
+    [#include <pthread.h>
+     #include <time.h>],
+    [pthread_rwlock_t foo; struct timespec bar; pthread_rwlock_timedwrlock(&foo, &bar)])
+  ],[
+    AC_MSG_RESULT(yes)
+    ac_cv_pthread_rwlock_timedwrlock="yes"
+  ],[
+    AC_MSG_RESULT(no)
+    ac_cv_pthread_rwlock_timedwrlock="no"
+  ]
+)
+if test "${ac_cv_pthread_rwlock_timedwrlock}" = "yes"; then
+  AC_DEFINE([HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK], 1, [Define if your system has pthread_rwlock_timedwrlock()])
+fi
+
 AST_C_DEFINE_CHECK([PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP], [PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP], [pthread.h])
 
 #if test "${cross_compiling}" = "no";
diff --git a/include/asterisk/autoconfig.h.in b/include/asterisk/autoconfig.h.in
index 50ef2fa50a..57987139f5 100644
--- a/include/asterisk/autoconfig.h.in
+++ b/include/asterisk/autoconfig.h.in
@@ -716,6 +716,9 @@
    pthread.h */
 #undef HAVE_PTHREAD_RWLOCK_PREFER_WRITER_NP
 
+/* Define if your system has pthread_rwlock_timedwrlock() */
+#undef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
+
 /* Define to 1 if the system has the type `ptrdiff_t'. */
 #undef HAVE_PTRDIFF_T
 
diff --git a/include/asterisk/lock.h b/include/asterisk/lock.h
index bba20c85df..1bd300e406 100644
--- a/include/asterisk/lock.h
+++ b/include/asterisk/lock.h
@@ -49,10 +49,15 @@
 #define _ASTERISK_LOCK_H
 
 #include <pthread.h>
+#include <time.h>
 #include <sys/param.h>
 #ifdef HAVE_BKTR
 #include <execinfo.h>
 #endif
+
+#ifndef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
+#include "asterisk/time.h"
+#endif
 #include "asterisk/logger.h"
 
 /* internal macro to profile mutexes. Only computes the delay on
@@ -1395,7 +1400,23 @@ static inline int _ast_rwlock_timedrdlock(ast_rwlock_t *t, const char *name,
 		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
 #endif
 	}
+#ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
 	res = pthread_rwlock_timedrdlock(&t->lock, abs_timeout);
+#else
+	do {
+		struct timeval _start = ast_tvnow(), _diff;
+		for (;;) {
+			if (!(res = pthread_rwlock_tryrdlock(&t->lock))) {
+				break;
+			}
+			_diff = ast_tvsub(ast_tvnow(), _start);
+			if (_diff.tv_sec > abs_timeout->tv_sec || (_diff.tv_sec == abs_timeout->tv_sec && _diff.tv_usec * 1000 > abs_timeout->tv_nsec)) {
+				break;
+			}
+			usleep(1);
+		}
+	} while (0);
+#endif
 	if (!res) {
 		ast_reentrancy_lock(lt);
 		if (lt->reentrancy < AST_MAX_REENTRANCY) {
@@ -1474,7 +1495,23 @@ static inline int _ast_rwlock_timedwrlock(ast_rwlock_t *t, const char *name,
 		ast_store_lock_info(AST_WRLOCK, filename, line, func, name, t);
 #endif
 	}
+#ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
 	res = pthread_rwlock_timedwrlock(&t->lock, abs_timeout);
+#else
+	do {
+		struct timeval _start = ast_tvnow(), _diff;
+		for (;;) {
+			if (!(res = pthread_rwlock_trywrlock(&t->lock))) {
+				break;
+			}
+			_diff = ast_tvsub(ast_tvnow(), _start);
+			if (_diff.tv_sec > abs_timeout->tv_sec || (_diff.tv_sec == abs_timeout->tv_sec && _diff.tv_usec * 1000 > abs_timeout->tv_nsec)) {
+				break;
+			}
+			usleep(1);
+		}
+	} while (0);
+#endif
 	if (!res) {
 		ast_reentrancy_lock(lt);
 		if (lt->reentrancy < AST_MAX_REENTRANCY) {
@@ -1762,7 +1799,23 @@ static inline int ast_rwlock_rdlock(ast_rwlock_t *prwlock)
 
 static inline int ast_rwlock_timedrdlock(ast_rwlock_t *prwlock, const struct timespec *abs_timeout)
 {
-	return pthread_rwlock_timedrdlock(prwlock, abs_timeout);
+	int res;
+#ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
+	res = pthread_rwlock_timedrdlock(prwlock, abs_timeout);
+#else
+	struct timeval _start = ast_tvnow(), _diff;
+	for (;;) {
+		if (!(res = pthread_rwlock_tryrdlock(prwlock))) {
+			break;
+		}
+		_diff = ast_tvsub(ast_tvnow(), _start);
+		if (_diff.tv_sec > abs_timeout->tv_sec || (_diff.tv_sec == abs_timeout->tv_sec && _diff.tv_usec * 1000 > abs_timeout->tv_nsec)) {
+			break;
+		}
+		usleep(1);
+	}
+#endif
+	return res;
 }
 
 static inline int ast_rwlock_tryrdlock(ast_rwlock_t *prwlock)
@@ -1777,7 +1830,25 @@ static inline int ast_rwlock_wrlock(ast_rwlock_t *prwlock)
 
 static inline int ast_rwlock_timedwrlock(ast_rwlock_t *prwlock, const struct timespec *abs_timeout)
 {
-	return pthread_rwlock_timedwrlock(prwlock, abs_timeout);
+	int res;
+#ifdef HAVE_PTHREAD_RWLOCK_TIMEDWRLOCK
+	res = pthread_rwlock_timedwrlock(prwlock, abs_timeout);
+#else
+	do {
+		struct timeval _start = ast_tvnow(), _diff;
+		for (;;) {
+			if (!(res = pthread_rwlock_trywrlock(prwlock))) {
+				break;
+			}
+			_diff = ast_tvsub(ast_tvnow(), _start);
+			if (_diff.tv_sec > abs_timeout->tv_sec || (_diff.tv_sec == abs_timeout->tv_sec && _diff.tv_usec * 1000 > abs_timeout->tv_nsec)) {
+				break;
+			}
+			usleep(1);
+		}
+	} while (0);
+#endif
+	return res;
 }
 
 static inline int ast_rwlock_trywrlock(ast_rwlock_t *prwlock)
-- 
GitLab