[vlc-devel] [RFC PATCH 6/6] android: fix monotonic cond_timedwait on newer versions
Thomas Guillem
tom at gllm.fr
Wed Oct 8 18:26:09 CEST 2014
android-L drops support for pthread_cond_timedwait_monotonic_np and adds
support for pthread_condattr_setclock. So, use the good function depending on
configure detection.
---
configure.ac | 2 +-
src/android/thread.c | 33 +++++++++++++++++++++------------
2 files changed, 22 insertions(+), 13 deletions(-)
diff --git a/configure.ac b/configure.ac
index 616ba32..7d4b6b6 100644
--- a/configure.ac
+++ b/configure.ac
@@ -541,7 +541,7 @@ need_libc=false
dnl Check for usual libc functions
AC_CHECK_DECLS([nanosleep],,,[#include <time.h>])
-AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mmap openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale])
+AC_CHECK_FUNCS([daemon fcntl fstatvfs fork getenv getpwuid_r isatty lstat memalign mmap openat pread posix_fadvise posix_madvise setlocale stricmp strnicmp strptime uselocale pthread_cond_timedwait_monotonic_np pthread_condattr_setclock])
AC_REPLACE_FUNCS([atof atoll dirfd fdopendir flockfile fsync getdelim getpid gmtime_r lldiv localtime_r nrand48 poll posix_memalign rewind setenv strcasecmp strcasestr strdup strlcpy strndup strnlen strsep strtof strtok_r strtoll swab tdestroy strverscmp])
AC_CHECK_FUNCS(fdatasync,,
[AC_DEFINE(fdatasync, fsync, [Alias fdatasync() to fsync() if missing.])
diff --git a/src/android/thread.c b/src/android/thread.c
index 1553073..50b3edd 100644
--- a/src/android/thread.c
+++ b/src/android/thread.c
@@ -185,8 +185,18 @@ void vlc_threads_setup (libvlc_int_t *p_libvlc)
void vlc_cond_init (vlc_cond_t *condvar)
{
+#ifdef HAVE_PTHREAD_CONDATTR_SETCLOCK
+ pthread_condattr_t attr;
+
+ pthread_condattr_init (&attr);
+ pthread_condattr_setclock (&attr, CLOCK_MONOTONIC);
+
+ if (unlikely(pthread_cond_init (&condvar->cond, &attr)))
+ abort ();
+#else
if (unlikely(pthread_cond_init (&condvar->cond, NULL)))
abort ();
+#endif
condvar->clock = CLOCK_MONOTONIC;
}
@@ -257,7 +267,7 @@ int vlc_cond_timedwait (vlc_cond_t *condvar, vlc_mutex_t *p_mutex,
{
struct timespec ts = mtime_to_ts (deadline);
vlc_thread_t th = thread;
- int (*cb)(pthread_cond_t *, pthread_mutex_t *, const struct timespec *);
+ int (*cb)(pthread_cond_t *, pthread_mutex_t *, const struct timespec *) = NULL;
if (th != NULL)
{
@@ -277,17 +287,16 @@ int vlc_cond_timedwait (vlc_cond_t *condvar, vlc_mutex_t *p_mutex,
}
}
- switch (condvar->clock)
- {
- case CLOCK_REALTIME:
- cb = pthread_cond_timedwait;
- break;
- case CLOCK_MONOTONIC:
- cb = pthread_cond_timedwait_monotonic_np;
- break;
- default:
- assert (0);
- }
+ if (condvar->clock == CLOCK_MONOTONIC) {
+#if defined (HAVE_PTHREAD_CONDATTR_SETCLOCK)
+ cb = pthread_cond_timedwait;
+#elif defined (HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC_NP)
+ cb = pthread_cond_timedwait_monotonic_np;
+#endif
+ } else if (condvar->clock == CLOCK_REALTIME)
+ cb = pthread_cond_timedwait;
+ if (!cb)
+ assert (0);
int val = cb (&condvar->cond, p_mutex, &ts);
if (val != ETIMEDOUT)
--
2.1.0
More information about the vlc-devel
mailing list