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
main
Yueh-Hsuan Chiang 11 years ago
parent 2459f7ec4e
commit 6580685260
  1. 25
      port/port_posix.cc
  2. 2
      port/port_posix.h

@ -9,10 +9,12 @@
#include "port/port_posix.h" #include "port/port_posix.h"
#include <cstdlib>
#include <stdio.h> #include <stdio.h>
#include <assert.h> #include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <string.h> #include <string.h>
#include <cstdlib>
#include "util/logging.h" #include "util/logging.h"
namespace rocksdb { namespace rocksdb {
@ -83,6 +85,27 @@ void CondVar::Wait() {
#endif #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() { void CondVar::Signal() {
PthreadCall("signal", pthread_cond_signal(&cv_)); PthreadCall("signal", pthread_cond_signal(&cv_));
} }

@ -137,6 +137,8 @@ class CondVar {
explicit CondVar(Mutex* mu); explicit CondVar(Mutex* mu);
~CondVar(); ~CondVar();
void Wait(); void Wait();
// Timed condition wait. Returns true if timeout occurred.
bool TimedWait(uint64_t abs_time_us);
void Signal(); void Signal();
void SignalAll(); void SignalAll();
private: private:

Loading…
Cancel
Save