diff options
author | Nick Mathewson <nickm@torproject.org> | 2016-09-24 09:12:00 -0700 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2016-09-24 09:12:00 -0700 |
commit | 951638a06d20263d33e501bdfa317c34b850f2b8 (patch) | |
tree | bbbce8bedaae58177e241509adbf13d12f7b0b82 /src/common/compat_pthreads.c | |
parent | 1eba088054eca1555b455ee4a2adfafecb888af9 (diff) | |
download | tor-951638a06d20263d33e501bdfa317c34b850f2b8.tar.gz tor-951638a06d20263d33e501bdfa317c34b850f2b8.zip |
Fix pthread_cond_timedwait() on OSX Sierra
Sierra provides clock_gettime(), but not pthread_condattr_setclock.
So we had better lot try to use CLOCK_MONOTONIC as our source for
time when waiting, since we ccan never actually tell the condition
that we mean CLOCK_MONOTONIC.
This isn't a tor bug yet, since we never actually pass a timeout to
tor_cond_wait() outside of the unit tests.
Diffstat (limited to 'src/common/compat_pthreads.c')
-rw-r--r-- | src/common/compat_pthreads.c | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/common/compat_pthreads.c b/src/common/compat_pthreads.c index 7640ebae34..4c05676655 100644 --- a/src/common/compat_pthreads.c +++ b/src/common/compat_pthreads.c @@ -192,14 +192,21 @@ tor_cond_init(tor_cond_t *cond) return -1; } -#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) \ - && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) +#if defined(HAVE_CLOCK_GETTIME) +#if defined(CLOCK_MONOTONIC) && defined(HAVE_PTHREAD_CONDATTR_SETCLOCK) /* Use monotonic time so when we timedwait() on it, any clock adjustment * won't affect the timeout value. */ if (pthread_condattr_setclock(&condattr, CLOCK_MONOTONIC)) { return -1; } -#endif +#define USE_COND_CLOCK CLOCK_MONOTONIC +#else /* !defined HAVE_PTHREAD_CONDATTR_SETCLOCK */ + /* On OSX Sierra, there is no pthread_condattr_setclock, so we are stuck + * with the realtime clock. + */ +#define USE_COND_CLOCK CLOCK_REALTIME +#endif /* which clock to use */ +#endif /* HAVE_CLOCK_GETTIME */ if (pthread_cond_init(&cond->cond, &condattr)) { return -1; } @@ -242,8 +249,8 @@ tor_cond_wait(tor_cond_t *cond, tor_mutex_t *mutex, const struct timeval *tv) struct timeval tvnow, tvsum; struct timespec ts; while (1) { -#if defined(HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) - if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0) { +#if defined(HAVE_CLOCK_GETTIME) && defined(USE_COND_CLOCK) + if (clock_gettime(USE_COND_CLOCK, &ts) < 0) { return -1; } tvnow.tv_sec = ts.tv_sec; |