libwinpr-synch: don't use timed waits on OS X until they are properly ported

This commit is contained in:
Marc-André Moreau 2013-10-25 10:48:37 -04:00
parent 723e423799
commit 89d45690c6

View File

@ -30,6 +30,7 @@
#include <winpr/crt.h> #include <winpr/crt.h>
#include <winpr/synch.h> #include <winpr/synch.h>
#include <winpr/platform.h>
#include "synch.h" #include "synch.h"
#include "../thread/thread.h" #include "../thread/thread.h"
@ -114,29 +115,30 @@ static int pthread_timedjoin_np(pthread_t td, void **res,
return ETIMEDOUT; return ETIMEDOUT;
} }
static int pthread_mutex_timedlock(pthread_mutex_t *mutex, static int pthread_mutex_timedlock(pthread_mutex_t *mutex, const struct timespec *timeout)
const struct timespec *timeout)
{ {
struct timeval timenow; struct timeval timenow;
struct timespec sleepytime; struct timespec sleepytime;
int retcode; int retcode;
/* This is just to avoid a completely busy wait */ /* This is just to avoid a completely busy wait */
sleepytime.tv_sec = 0; sleepytime.tv_sec = 0;
sleepytime.tv_nsec = 10000000; /* 10ms */ sleepytime.tv_nsec = 10000000; /* 10ms */
while ((retcode = pthread_mutex_trylock (mutex)) == EBUSY) { while ((retcode = pthread_mutex_trylock (mutex)) == EBUSY)
gettimeofday (&timenow, NULL); {
gettimeofday (&timenow, NULL);
if (timenow.tv_sec >= timeout->tv_sec &&
(timenow.tv_usec * 1000) >= timeout->tv_nsec) { if (timenow.tv_sec >= timeout->tv_sec &&
return ETIMEDOUT; (timenow.tv_usec * 1000) >= timeout->tv_nsec)
} {
return ETIMEDOUT;
nanosleep (&sleepytime, NULL); }
}
nanosleep (&sleepytime, NULL);
return retcode; }
return retcode;
} }
#endif #endif
@ -170,6 +172,7 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
if (thread->started) if (thread->started)
{ {
#ifdef __linux__
if (dwMilliseconds != INFINITE) if (dwMilliseconds != INFINITE)
{ {
struct timespec timeout; struct timespec timeout;
@ -183,10 +186,12 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
ts_add_ms(&timeout, dwMilliseconds); ts_add_ms(&timeout, dwMilliseconds);
status = pthread_timedjoin_np(thread->thread, &thread_status, &timeout); status = pthread_timedjoin_np(thread->thread, &thread_status, &timeout);
if (ETIMEDOUT == status) if (ETIMEDOUT == status)
return WAIT_TIMEOUT; return WAIT_TIMEOUT;
} }
else else
#endif
status = pthread_join(thread->thread, &thread_status); status = pthread_join(thread->thread, &thread_status);
if (status != 0) if (status != 0)
@ -214,26 +219,27 @@ DWORD WaitForSingleObject(HANDLE hHandle, DWORD dwMilliseconds)
} }
else if (Type == HANDLE_TYPE_MUTEX) else if (Type == HANDLE_TYPE_MUTEX)
{ {
int status;
WINPR_MUTEX* mutex; WINPR_MUTEX* mutex;
mutex = (WINPR_MUTEX*) Object; mutex = (WINPR_MUTEX*) Object;
#ifdef __linux__
if (dwMilliseconds != INFINITE) if (dwMilliseconds != INFINITE)
{ {
int status;
struct timespec timeout; struct timespec timeout;
clock_gettime(CLOCK_REALTIME, &timeout); clock_gettime(CLOCK_REALTIME, &timeout);
ts_add_ms(&timeout, dwMilliseconds); ts_add_ms(&timeout, dwMilliseconds);
status = pthread_mutex_timedlock(&mutex->mutex, &timeout); status = pthread_mutex_timedlock(&mutex->mutex, &timeout);
if (ETIMEDOUT == status) if (ETIMEDOUT == status)
return WAIT_TIMEOUT; return WAIT_TIMEOUT;
} }
else else
{ #endif
pthread_mutex_lock(&mutex->mutex); pthread_mutex_lock(&mutex->mutex);
}
} }
else if (Type == HANDLE_TYPE_EVENT) else if (Type == HANDLE_TYPE_EVENT)
{ {