diff --git a/Foundation/src/Event_POSIX.cpp b/Foundation/src/Event_POSIX.cpp index 4d7cd2367..d2fc8c0f5 100644 --- a/Foundation/src/Event_POSIX.cpp +++ b/Foundation/src/Event_POSIX.cpp @@ -24,6 +24,19 @@ #endif +// +// Note: pthread_cond_timedwait() with CLOCK_MONOTONIC is supported +// on Linux and QNX, as well as on Android >= 5.0. On Android < 5.0, +// HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC is defined to indicate +// availability of non-standard pthread_cond_timedwait_monotonic(). +// +#ifndef POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT +#if (defined(__linux__) || defined(__QNX__)) && !(defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) +#define POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT 1 +#endif +#endif + + namespace Poco { @@ -40,7 +53,7 @@ EventImpl::EventImpl(EventTypeImpl type): _auto(type == EVENT_AUTORESET_IMPL), _ if (pthread_mutex_init(&_mutex, NULL)) throw SystemException("cannot create event (mutex)"); -#if defined(__linux__) || defined(__QNX__) +#if defined(POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT) pthread_condattr_t attr; if (pthread_condattr_init(&attr)) { @@ -105,7 +118,7 @@ bool EventImpl::waitImpl(long milliseconds) delta.tv_sec = milliseconds / 1000; delta.tv_nsec = (milliseconds % 1000)*1000000; pthread_get_expiration_np(&delta, &abstime); -#elif defined(__linux__) || defined(__QNX__) +#elif defined(POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT) clock_gettime(CLOCK_MONOTONIC, &abstime); abstime.tv_sec += milliseconds / 1000; abstime.tv_nsec += (milliseconds % 1000)*1000000; diff --git a/Foundation/src/Semaphore_POSIX.cpp b/Foundation/src/Semaphore_POSIX.cpp index 88e768ed1..57c5dae15 100644 --- a/Foundation/src/Semaphore_POSIX.cpp +++ b/Foundation/src/Semaphore_POSIX.cpp @@ -24,6 +24,19 @@ #endif +// +// Note: pthread_cond_timedwait() with CLOCK_MONOTONIC is supported +// on Linux and QNX, as well as on Android >= 5.0. On Android < 5.0, +// HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC is defined to indicate +// availability of non-standard pthread_cond_timedwait_monotonic(). +// +#ifndef POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT +#if (defined(__linux__) || defined(__QNX__)) && !(defined(__ANDROID__) && defined(HAVE_PTHREAD_COND_TIMEDWAIT_MONOTONIC)) +#define POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT 1 +#endif +#endif + + namespace Poco { @@ -41,7 +54,7 @@ SemaphoreImpl::SemaphoreImpl(int n, int max): _n(n), _max(max) if (pthread_mutex_init(&_mutex, NULL)) throw SystemException("cannot create semaphore (mutex)"); -#if defined(__linux__) || defined(__QNX__) +#if defined(POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT) pthread_condattr_t attr; if (pthread_condattr_init(&attr)) { @@ -105,7 +118,7 @@ bool SemaphoreImpl::waitImpl(long milliseconds) delta.tv_sec = milliseconds / 1000; delta.tv_nsec = (milliseconds % 1000)*1000000; pthread_get_expiration_np(&delta, &abstime); -#elif defined(__linux__) || defined(__QNX__) +#elif defined(POCO_HAVE_MONOTONIC_PTHREAD_COND_TIMEDWAIT) clock_gettime(CLOCK_MONOTONIC, &abstime); abstime.tv_sec += milliseconds / 1000; abstime.tv_nsec += (milliseconds % 1000)*1000000;