Reviewers: Søren Gjesse, Description: - Fix delta time calculation in LinuxSemaphore::Wait.
Please review this at http://codereview.chromium.org/69024 SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/ Affected files: M src/platform-freebsd.cc M src/platform-linux.cc Index: src/platform-linux.cc =================================================================== --- src/platform-linux.cc (revision 1730) +++ src/platform-linux.cc (working copy) @@ -509,26 +509,24 @@ bool LinuxSemaphore::Wait(int timeout) { const long kOneSecondMicros = 1000000; // NOLINT - const long kOneSecondNanos = 1000000000; // NOLINT - + // Split timeout into second and nanosecond parts. - long nanos = (timeout % kOneSecondMicros) * 1000; // NOLINT - time_t secs = timeout / kOneSecondMicros; - - // Get the current realtime clock. - struct timespec ts; - if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + struct timeval delta; + delta.tv_usec = timeout % kOneSecondMicros; + delta.tv_sec = timeout / kOneSecondMicros; + + struct timeval current_time; + // Get the current time. + if (gettimeofday(¤t_time, NULL) == -1) { return false; } - - // Calculate real time for end of timeout. - ts.tv_nsec += nanos; - if (ts.tv_nsec >= kOneSecondNanos) { - ts.tv_nsec -= kOneSecondNanos; - ts.tv_nsec++; - } - ts.tv_sec += secs; - + + // Calculate time for end of timeout. + struct timeval end_time; + timeradd(¤t_time, &delta, &end_time); + + struct timespec ts; + TIMEVAL_TO_TIMESPEC(&end_time, &ts); // Wait for semaphore signalled or timeout. while (true) { int result = sem_timedwait(&sem_, &ts); Index: src/platform-freebsd.cc =================================================================== --- src/platform-freebsd.cc (revision 1730) +++ src/platform-freebsd.cc (working copy) @@ -503,27 +503,24 @@ bool FreeBSDSemaphore::Wait(int timeout) { const long kOneSecondMicros = 1000000; // NOLINT - const long kOneSecondNanos = 1000000000; // NOLINT - + // Split timeout into second and nanosecond parts. - long nanos = (timeout % kOneSecondMicros) * 1000; // NOLINT - time_t secs = timeout / kOneSecondMicros; - - // Get the current real time clock. - struct timespec ts; - if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { + struct timeval delta; + delta.tv_usec = timeout % kOneSecondMicros; + delta.tv_sec = timeout / kOneSecondMicros; + + struct timeval current_time; + // Get the current time. + if (gettimeofday(¤t_time, NULL) == -1) { return false; } - - // Calculate realtime for end of timeout. - ts.tv_nsec += nanos; - if (ts.tv_nsec >= kOneSecondNanos) { - ts.tv_nsec -= kOneSecondNanos; - ts.tv_nsec++; - } - ts.tv_sec += secs; - - // Wait for semaphore signalled or timeout. + + // Calculate time for end of timeout. + struct timeval end_time; + timeradd(¤t_time, &delta, &end_time); + + struct timespec ts; + TIMEVAL_TO_TIMESPEC(&end_time, &ts); while (true) { int result = sem_timedwait(&sem_, &ts); if (result == 0) return true; // Successfully got semaphore. --~--~---------~--~----~------------~-------~--~----~ v8-dev mailing list [email protected] http://groups.google.com/group/v8-dev -~----------~----~----~----~------~----~------~--~---
