From 658068526081d0b97967ad015629986d81c7b6a1 Mon Sep 17 00:00:00 2001 From: Yueh-Hsuan Chiang Date: Thu, 3 Jul 2014 10:22:08 -0700 Subject: [PATCH] Add TimedWait() API to CondVar. Summary: Add TimedWait() API to CondVar, which will be used in the future to support TimedOut Write API and Rate limiter. Test Plan: make db_test -j32 Reviewers: sdong, ljin Reviewed By: ljin Subscribers: leveldb Differential Revision: https://reviews.facebook.net/D19431 --- port/port_posix.cc | 25 ++++++++++++++++++++++++- port/port_posix.h | 2 ++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/port/port_posix.cc b/port/port_posix.cc index 2ad10f58f..90dde3227 100644 --- a/port/port_posix.cc +++ b/port/port_posix.cc @@ -9,10 +9,12 @@ #include "port/port_posix.h" -#include #include #include +#include +#include #include +#include #include "util/logging.h" namespace rocksdb { @@ -83,6 +85,27 @@ void CondVar::Wait() { #endif } +bool CondVar::TimedWait(uint64_t abs_time_us) { + struct timespec ts; + ts.tv_sec = abs_time_us / 1000000; + ts.tv_nsec = (abs_time_us % 1000000) * 1000; + +#ifndef NDEBUG + mu_->locked_ = false; +#endif + int err = pthread_cond_timedwait(&cv_, &mu_->mu_, &ts); +#ifndef NDEBUG + mu_->locked_ = true; +#endif + if (err == ETIMEDOUT) { + return true; + } + if (err != 0) { + PthreadCall("timedwait", err); + } + return false; +} + void CondVar::Signal() { PthreadCall("signal", pthread_cond_signal(&cv_)); } diff --git a/port/port_posix.h b/port/port_posix.h index c2070c7cb..2e3c868b3 100644 --- a/port/port_posix.h +++ b/port/port_posix.h @@ -137,6 +137,8 @@ class CondVar { explicit CondVar(Mutex* mu); ~CondVar(); void Wait(); + // Timed condition wait. Returns true if timeout occurred. + bool TimedWait(uint64_t abs_time_us); void Signal(); void SignalAll(); private: -- GitLab