LWN.net Logo

Absolute time

Absolute time

Posted Dec 22, 2006 2:21 UTC (Fri) by slamb (guest, #1070)
Parent article: Kevent take 26

Is the absolute time CLOCK_REALTIME or CLOCK_MONOTONIC? If the former...ugh...it's useless; I could see why Evgeniy didn't want it. If the latter, it can provide more precise timing for those who worry about that sort of thing. That group notably includes the designers of pthread_cond_timedwait(). A couple manpage snippets:

If the Clock Selection option is supported, the condition variable shall have a clock attribute which specifies the clock that shall be used to measure the time specified by the abstime argument. When such timeouts occur, pthread_cond_timedwait() shall nonetheless release and re-acquire the mutex referenced by mutex. The pthread_cond_timedwait() function is also a cancellation point.

...

An absolute time measure was chosen for specifying the timeout parameter for two reasons. First, a relative time measure can be easily implemented on top of a function that specifies absolute time, but there is a race condition associated with specifying an absolute timeout on top of a function that specifies relative timeouts. For example, assume that clock_gettime() returns the current time and cond_relative_timed_wait() uses relative timeouts:

              clock_gettime(CLOCK_REALTIME, &now)
              reltime = sleep_til_this_absolute_time -now;
              cond_relative_timed_wait(c, m, &reltime);

If the thread is preempted between the first statement and the last statement, the thread blocks for too long. Blocking, however, is irrelevant if an absolute timeout is used. An absolute timeout also need not be recomputed if it is used multiple times in a loop, such as that enclosing a condition wait.


(Log in to post comments)

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds