From bf52da0f1bc1fddf55a8ca4fc7ba8f01d48c5f37 Mon Sep 17 00:00:00 2001 From: Joshua Peraza Date: Wed, 21 Jun 2017 01:17:58 -0700 Subject: [PATCH] posix: Fix Semaphore::TimedWait wait time TimedWait is implemented using `sem_timedwait` which waits until an absolute time (time since the epoch) has passed. Previously, the time to wait (relative to now) was passed without adding the current time. Change-Id: I3c169d5b107b8263577c21a8f47dc504058bd708 Reviewed-on: https://chromium-review.googlesource.com/540984 Commit-Queue: Joshua Peraza Reviewed-by: Mark Mentovai --- util/synchronization/semaphore_posix.cc | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/util/synchronization/semaphore_posix.cc b/util/synchronization/semaphore_posix.cc index c781e4c6..9696e9da 100644 --- a/util/synchronization/semaphore_posix.cc +++ b/util/synchronization/semaphore_posix.cc @@ -15,6 +15,7 @@ #include "util/synchronization/semaphore.h" #include +#include #include @@ -25,6 +26,19 @@ namespace crashpad { #if !defined(OS_MACOSX) +namespace { + +void AddTimespec(const timespec& ts1, const timespec& ts2, timespec* result) { + result->tv_sec = ts1.tv_sec + ts2.tv_sec; + result->tv_nsec = ts1.tv_nsec + ts2.tv_nsec; + if (result->tv_nsec > static_cast(1E9)) { + ++result->tv_sec; + result->tv_nsec -= static_cast(1E9); + } +} + +} // namespace + Semaphore::Semaphore(int value) { PCHECK(sem_init(&semaphore_, 0, value) == 0) << "sem_init"; } @@ -45,9 +59,15 @@ bool Semaphore::TimedWait(double seconds) { return true; } + timespec current_time; + if (clock_gettime(CLOCK_REALTIME, ¤t_time) != 0) { + PLOG(ERROR) << "clock_gettime"; + return false; + } timespec timeout; timeout.tv_sec = seconds; timeout.tv_nsec = (seconds - trunc(seconds)) * 1E9; + AddTimespec(current_time, timeout, &timeout); int rv = HANDLE_EINTR(sem_timedwait(&semaphore_, &timeout)); PCHECK(rv == 0 || errno == ETIMEDOUT) << "sem_timedwait";