summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Dröge <slomo@circular-chaos.org>2013-07-04 10:27:02 +0200
committerSebastian Dröge <slomo@circular-chaos.org>2013-07-04 10:41:59 +0200
commitdbdfcb69ce4a7f14bac784c2684c1a66bfe6a2c9 (patch)
treef89a0dfd7f0e7675ad269418e1146a52e718b56f
parenta4c352cd99738095ba34e04a86a2ffa9cc659cfe (diff)
downloadglib-dbdfcb69ce4a7f14bac784c2684c1a66bfe6a2c9.tar.gz
gthread: Use pthread_cond_timedwait_monotonic() if available
Otherwise we have to rely on pthread_cond_timedwait() actually using the monotonic clock, which might be true or not. On Android at least it is using the realtime clock, no pthread_condattr_setclock() is available but instead pthread_cond_timedwait_monotonic() can be used.
-rw-r--r--configure.ac18
-rw-r--r--glib/gthread-posix.c9
2 files changed, 27 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index 1fba2da58..9bfc3d802 100644
--- a/configure.ac
+++ b/configure.ac
@@ -2383,6 +2383,24 @@ AS_IF([ test x"$have_threads" = xposix], [
AC_DEFINE(HAVE_PTHREAD_CONDATTR_SETCLOCK,1,
[Have function pthread_condattr_setclock])],
[AC_MSG_RESULT(no)])
+ AC_MSG_CHECKING(for pthread_cond_timedwait_monotonic)
+ AC_LINK_IFELSE(
+ [AC_LANG_PROGRAM(
+ [#include <pthread.h>],
+ [pthread_cond_timedwait_monotonic(NULL, NULL, NULL)])],
+ [AC_MSG_RESULT(yes)
+ AC_DEFINE(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC,1,
+ [Have function pthread_cond_timedwait_monotonic])],
+ [AC_MSG_RESULT(no)])
+ AC_MSG_CHECKING(for pthread_cond_timedwait_monotonic_np)
+ AC_LINK_IFELSE(
+ [AC_LANG_PROGRAM(
+ [#include <pthread.h>],
+ [pthread_cond_timedwait_monotonic_np(NULL, NULL, NULL)])],
+ [AC_MSG_RESULT(yes)
+ AC_DEFINE(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP,1,
+ [Have function pthread_cond_timedwait_monotonic_np])],
+ [AC_MSG_RESULT(no)])
CPPFLAGS="$glib_save_CPPFLAGS"
])
diff --git a/glib/gthread-posix.c b/glib/gthread-posix.c
index 23371ae0f..c7b68a0a6 100644
--- a/glib/gthread-posix.c
+++ b/glib/gthread-posix.c
@@ -859,8 +859,17 @@ g_cond_wait_until (GCond *cond,
ts.tv_sec = end_time / 1000000;
ts.tv_nsec = (end_time % 1000000) * 1000;
+#if defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)
+ if ((status = pthread_cond_timedwait_monotonic (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
+ return TRUE;
+#elif defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP)
+ if ((status = pthread_cond_timedwait_monotonic_np (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
+ return TRUE;
+#else
+ /* Pray that the cond is actually using the monotonic clock */
if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
return TRUE;
+#endif
if G_UNLIKELY (status != ETIMEDOUT)
g_thread_abort (status, "pthread_cond_timedwait");