nmathewson/Libevent — pull request #67 by dgoulet (2012-09-26 21:00:49+00:00)
Title: Fix: Time race using pthread condition timedwait

The pthread_cond_timedwait() needs an absolute timeout value, generally
taken from the realtime clock of the system (using gettimeofday or
clock_gettime(CLOCK_REALTIME, ...)). However, this clock can go backward
or forward at any time like for instance a NTP adjustment.

The race can occur between the clock time acquisition and the
pthread_cond_timedwait() call which immediately returns with a ETIMEOUT
(normal behavior). By the time the pthread cond call is rescheduled, the
system could have had a NTP adjustment thus making the timeout period
invalid.

This commit forces any pthread condition allocated by libevent and
supporting monotonic clocks to be set with the monotonic clock source
for the timedwait condition.

Also introduce three new macros. A clock_gettime wrapper, a timeval and
timespec conversion that basically use
TIMEVAL_TO_TIMESPEC/TIMESPEC_TO_TIMEVAL from libc if available.

Signed-off-by: David Goulet