Revision: 132 http://trac.macosforge.org/projects/libdispatch/changeset/132 Author: robert@fledge.watson.org Date: 2009-11-04 14:44:23 -0800 (Wed, 04 Nov 2009) Log Message: ----------- Rework conversion of timeouts to sem_timedwait() arguments further: reintroduce tests for CLOCK_UPTIME/CLOCK_MONOTONIC, and restore using them for absolute times on non-Mac OS X systems; instead perform a more mature but more costly conversion when calculating a timeout. Perhaps we should cache the delta between our absolute clock and the realtime clock to avoid a clock read each time we calculate a timeout. This should re-eliminate the restriction that POSIX semaphores can't be used on systems where we use mach_absolute_time(). Modified Paths: -------------- trunk/configure.ac trunk/src/shims/time.h trunk/src/time.c Modified: trunk/configure.ac =================================================================== --- trunk/configure.ac 2009-11-04 21:15:54 UTC (rev 131) +++ trunk/configure.ac 2009-11-04 22:44:23 UTC (rev 132) @@ -177,7 +177,8 @@ # # Find functions and declarations we care about. # -AC_CHECK_DECLS([CLOCK_REALTIME], [], [], [[#include <time.h>]]) +AC_CHECK_DECLS([CLOCK_UPTIME, CLOCK_MONOTONIC, CLOCK_REALTIME], [], [], + [[#include <time.h>]]) AC_CHECK_DECLS([EVFILT_SESSION, NOTE_NONE, NOTE_REAP, NOTE_SIGNAL], [], [], [[#include <sys/event.h>]]) AC_CHECK_DECLS([FD_COPY], [], [], [[#include <sys/select.h>]]) Modified: trunk/src/shims/time.h =================================================================== --- trunk/src/shims/time.h 2009-11-04 21:15:54 UTC (rev 131) +++ trunk/src/shims/time.h 2009-11-04 22:44:23 UTC (rev 132) @@ -43,7 +43,13 @@ struct timespec ts; int ret; - ret = clock_gettime(CLOCK_REALTIME, &ts); +#if HAVE_DECL_CLOCK_UPTIME + ret = clock_gettime(CLOCK_UPTIME, &ts); +#elif HAVE_DECL_CLOCK_MONOTONIC + ret = clock_gettime(CLOCK_MONOTONIC, &ts); +#else +#error "clock_gettime: no supported absolute time clock" +#endif (void)dispatch_assume_zero(ret); /* XXXRW: Some kind of overflow detection needed? */ Modified: trunk/src/time.c =================================================================== --- trunk/src/time.c 2009-11-04 21:15:54 UTC (rev 131) +++ trunk/src/time.c 2009-11-04 22:44:23 UTC (rev 132) @@ -184,44 +184,47 @@ } #if USE_POSIX_SEM -#ifdef HAVE_MACH_ABSOLUTE_TIME -#error "Cannot currently use USE_POSIX_SEM and HAVE_MACH_ABSOLUTE_TIME together" -#endif /* * Unlike Mach semaphores, POSIX semaphores take an absolute, real time as an * argument to sem_timedwait(). This routine converts from dispatch_time_t * but assumes the caller has already handled the possibility of * DISPATCH_TIME_FOREVER. - * - * XXXRW: Does not currently with with mach_absolute_time()/ */ struct timespec _dispatch_timeout_ts(dispatch_time_t when) { - struct timespec ts; + struct timespec ts_realtime; + uint64_t abstime; int ret; + printf("_dispatch_timeout_ts: req %ld\n", (int64_t)when); + if (when == 0) { - when = _dispatch_absolute_time(); - ts.tv_sec = when / NSEC_PER_SEC; - ts.tv_nsec = when % NSEC_PER_SEC; - return (ts); + ret = clock_gettime(CLOCK_REALTIME, &ts_realtime); + (void)dispatch_assume_zero(ret); + return (ts_realtime); } if ((int64_t)when < 0) { - when = -(int64_t)when + _dispatch_absolute_time(); - ts.tv_sec = when / NSEC_PER_SEC; - ts.tv_nsec = when % NSEC_PER_SEC; - return (ts); + ret = clock_gettime(CLOCK_REALTIME, &ts_realtime); + (void)dispatch_assume_zero(ret); + when = -(int64_t)when + ts_realtime.tv_sec * NSEC_PER_SEC + + ts_realtime.tv_nsec; + ts_realtime.tv_sec = when / NSEC_PER_SEC; + ts_realtime.tv_nsec = when % NSEC_PER_SEC; + return (ts_realtime); } /* - * XXXRW: This code assumes that dispatch_time_t absolute times are - * with respect to CLOCK_REALTIME. If we want to support - * USE_POSIX_SEM with mach_absolute_time(), a conversion is required - * here. + * Rebase 'when': (when - abstime) + realtime. + * + * XXXRW: Should we cache this delta to avoid system calls? */ - ts.tv_sec = when / NSEC_PER_SEC; - ts.tv_nsec = when % NSEC_PER_SEC; - return (ts); + abstime = _dispatch_absolute_time(); + ret = clock_gettime(CLOCK_REALTIME, &ts_realtime); + (void)dispatch_assume_zero(ret); + printf("now is %d.%lu\n", ts_realtime.tv_sec, ts_realtime.tv_nsec); + ts_realtime.tv_sec += (when - abstime) / NSEC_PER_SEC; + ts_realtime.tv_nsec += (when - abstime) % NSEC_PER_SEC; + return (ts_realtime); } #endif
participants (1)
-
source_changes@macosforge.org