diff options
| -rw-r--r-- | src/platform/posix/posix_config.h | 9 | ||||
| -rw-r--r-- | src/platform/posix/posix_synch.c | 19 |
2 files changed, 22 insertions, 6 deletions
diff --git a/src/platform/posix/posix_config.h b/src/platform/posix/posix_config.h index 1510d739..6d282f63 100644 --- a/src/platform/posix/posix_config.h +++ b/src/platform/posix/posix_config.h @@ -38,12 +38,17 @@ #include <time.h> +// MacOS X used to lack CLOCK_MONOTONIC. Now it has it, but its +// buggy, condition variables set to use it wake early. +#ifdef __APPLE__ +#define NNG_USE_CLOCKID CLOCK_REALTIME +#endif // __APPLE__ + +#define NNG_USE_CLOCKID CLOCK_REALTIME #ifndef CLOCK_REALTIME #define NNG_USE_GETTIMEOFDAY #elif !defined(NNG_USE_CLOCKID) -#ifdef CLOCK_MONOTONIC #define NNG_USE_CLOCKID CLOCK_MONOTONIC #else #define NNG_USE_CLOCKID CLOCK_REALTIME -#endif #endif // CLOCK_REALTIME diff --git a/src/platform/posix/posix_synch.c b/src/platform/posix/posix_synch.c index 0526eb7c..d9fed53f 100644 --- a/src/platform/posix/posix_synch.c +++ b/src/platform/posix/posix_synch.c @@ -121,17 +121,28 @@ nni_cond_wait(nni_cond *c) int -nni_cond_waituntil(nni_cond *c, uint64_t usec) +nni_cond_waituntil(nni_cond *c, nni_time usec) { struct timespec ts; int rv; + nni_duration delta = usec - nni_clock(); - ts.tv_sec = usec / 1000000; - ts.tv_nsec = (usec % 1000000) * 1000; - rv = pthread_cond_timedwait(&c->cv, c->mx, &ts); + if (usec != NNI_TIME_NEVER) { + ts.tv_sec = usec / 1000000; + ts.tv_nsec = (usec % 1000000) * 1000; + + rv = pthread_cond_timedwait(&c->cv, c->mx, &ts); + } else { + rv = pthread_cond_wait(&c->cv, c->mx); + } if (rv == ETIMEDOUT) { + if (nni_clock() < usec) { + // This only happens if the implementation + // is buggy. + nni_panic("Premature wakupe!"); + } return (NNG_ETIMEDOUT); } else if (rv != 0) { nni_panic("pthread_cond_timedwait returned %d", rv); |
