waitImpl(int milliseconds) shouldn't use gettimeofday

Please post support and help requests here.
Posts: 4
Joined: 11 Jan 2011, 16:23

waitImpl(int milliseconds) shouldn't use gettimeofday

Postby hrkenna » 05 Jul 2012, 21:25

At least on Linux systems (Fedora/RH/CentOS), the gettimeofday call can bite you when used as a base time for calls into the pthread_cond_timedwait call. Case in point, the "powers that be" adjusted the world time backwards by one second the weekend of July 1, 2012. All of a sudden a call to Semaphore::tryWait(1000) was returning false (not set) immediately causing the process to chew up a bunch of useless cpu cycles. This was due to the Semaphore.cpp implementation of waitImpl which calls gettimeofday to a acquire a base time. The 1000 milliseconds was then added to the struct timeval. Then a call was made to pthread_cond_timedwait.

However, pthread_cond_timewait (at least on Linux) is running off of CLOCK_REALTIME which is available from clock_gettime. This caused the net time of gettimeofday + 1000 to (roughly) equal clock_gettime(CLOCK_REALTIME) and the wait timer immediately expired.

I know not all systems have clock_gettime (MacOS for instance), but perhaps there is a better choice than gettimeofday. There is also a way to specify a clock of choice on the condition variable, but that may be even less portable as that's a later addition to pthreads.

For us, we'll simply swap clock_gettime(CLOCK_REALTIME...) which is the default clock for the condition variable.

Posts: 1375
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: waitImpl(int milliseconds) shouldn't use gettimeofday

Postby alex » 05 Jul 2012, 22:33

This concern has already been raised. There seems to be a workaround. I have reopened the old SF bug and turned it into a patch.

Return to “Support”

Who is online

Users browsing this forum: No registered users and 1 guest