Skip to content

Commit

Permalink
- prefer clock_getttime() over gettimeofday() if available
Browse files Browse the repository at this point in the history
- use CLOCK_MONOTONIC for POSIX condition in Poco::Event and Poco::Semaphore if supported
  • Loading branch information
obiltschnig committed Aug 28, 2016
1 parent 7b5e974 commit f89bea4
Showing 4 changed files with 87 additions and 3 deletions.
39 changes: 38 additions & 1 deletion Foundation/src/Event_POSIX.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#include <timers.h>
#include <cstring>
#else
#include <time.h>
#include <sys/time.h>
#endif

@@ -35,10 +36,37 @@ EventImpl::EventImpl(bool autoReset): _auto(autoReset), _state(false)
// if the mutex has never been used.
std::memset(&_mutex, 0, sizeof(_mutex));
#endif

if (pthread_mutex_init(&_mutex, NULL))
throw SystemException("cannot create event (mutex)");

#if defined(__linux__) || defined(__QNX__)
pthread_condattr_t attr;
if (pthread_condattr_init(&attr))
{
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create event (condition attribute)");
}
if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
{
pthread_condattr_destroy(&attr);
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create event (condition attribute clock)");
}
if (pthread_cond_init(&_cond, &attr))
{
pthread_condattr_destroy(&attr);
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create event (condition)");
}
pthread_condattr_destroy(&attr);
#else
if (pthread_cond_init(&_cond, NULL))
{
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create event (condition)");
}
#endif
}


@@ -77,7 +105,16 @@ bool EventImpl::waitImpl(long milliseconds)
delta.tv_sec = milliseconds / 1000;
delta.tv_nsec = (milliseconds % 1000)*1000000;
pthread_get_expiration_np(&delta, &abstime);
#elif defined(POCO_VXWORKS)
#elif defined(__linux__) || defined(__QNX__)
clock_gettime(CLOCK_MONOTONIC, &abstime);
abstime.tv_sec += milliseconds / 1000;
abstime.tv_nsec += (milliseconds % 1000)*1000000;
if (abstime.tv_nsec >= 1000000000)
{
abstime.tv_nsec -= 1000000000;
abstime.tv_sec++;
}
#elif (defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)) || defined(POCO_VXWORKS)
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += milliseconds / 1000;
abstime.tv_nsec += (milliseconds % 1000)*1000000;
11 changes: 11 additions & 0 deletions Foundation/src/Mutex_POSIX.cpp
Original file line number Diff line number Diff line change
@@ -98,6 +98,16 @@ bool MutexImpl::tryLockImpl(long milliseconds)
{
#if defined(POCO_HAVE_MUTEX_TIMEOUT)
struct timespec abstime;
#if defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += milliseconds / 1000;
abstime.tv_nsec += (milliseconds % 1000)*1000000;
if (abstime.tv_nsec >= 1000000000)
{
abstime.tv_nsec -= 1000000000;
abstime.tv_sec++;
}
#else
struct timeval tv;
gettimeofday(&tv, NULL);
abstime.tv_sec = tv.tv_sec + milliseconds / 1000;
@@ -107,6 +117,7 @@ bool MutexImpl::tryLockImpl(long milliseconds)
abstime.tv_nsec -= 1000000000;
abstime.tv_sec++;
}
#endif
int rc = pthread_mutex_timedlock(&_mutex, &abstime);
if (rc == 0)
return true;
38 changes: 37 additions & 1 deletion Foundation/src/Semaphore_POSIX.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#include <timers.h>
#include <cstring>
#else
#include <time.h>
#include <sys/time.h>
#endif

@@ -39,8 +40,34 @@ SemaphoreImpl::SemaphoreImpl(int n, int max): _n(n), _max(max)
#endif
if (pthread_mutex_init(&_mutex, NULL))
throw SystemException("cannot create semaphore (mutex)");

#if defined(__linux__) || defined(__QNX__)
pthread_condattr_t attr;
if (pthread_condattr_init(&attr))
{
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create semaphore (condition attribute)");
}
if (pthread_condattr_setclock(&attr, CLOCK_MONOTONIC))
{
pthread_condattr_destroy(&attr);
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create semaphore (condition attribute clock)");
}
if (pthread_cond_init(&_cond, &attr))
{
pthread_condattr_destroy(&attr);
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create semaphore (condition)");
}
pthread_condattr_destroy(&attr);
#else
if (pthread_cond_init(&_cond, NULL))
{
pthread_mutex_destroy(&_mutex);
throw SystemException("cannot create semaphore (condition)");
}
#endif
}


@@ -78,7 +105,16 @@ bool SemaphoreImpl::waitImpl(long milliseconds)
delta.tv_sec = milliseconds / 1000;
delta.tv_nsec = (milliseconds % 1000)*1000000;
pthread_get_expiration_np(&delta, &abstime);
#elif defined(POCO_VXWORKS)
#elif defined(__linux__) || defined(__QNX__)
clock_gettime(CLOCK_MONOTONIC, &abstime);
abstime.tv_sec += milliseconds / 1000;
abstime.tv_nsec += (milliseconds % 1000)*1000000;
if (abstime.tv_nsec >= 1000000000)
{
abstime.tv_nsec -= 1000000000;
abstime.tv_sec++;
}
#elif (defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)) || defined(POCO_VXWORKS)
clock_gettime(CLOCK_REALTIME, &abstime);
abstime.tv_sec += milliseconds / 1000;
abstime.tv_nsec += (milliseconds % 1000)*1000000;
2 changes: 1 addition & 1 deletion Foundation/src/Timestamp.cpp
Original file line number Diff line number Diff line change
@@ -226,7 +226,7 @@ void Timestamp::update()
ts.QuadPart -= epoch.QuadPart;
_ts = ts.QuadPart/10;

#elif defined(POCO_VXWORKS)
#elif (defined(_POSIX_TIMERS) && defined(CLOCK_REALTIME)) || defined(POCO_VXWORKS) || defined(__QNX__)

struct timespec ts;
if (clock_gettime(CLOCK_REALTIME, &ts))

0 comments on commit f89bea4

Please sign in to comment.