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
This commit is contained in:
Yueh-Hsuan Chiang 2014-07-03 10:22:08 -07:00
parent 2459f7ec4e
commit 6580685260
2 changed files with 26 additions and 1 deletions

View File

@ -9,10 +9,12 @@
#include "port/port_posix.h"
#include <cstdlib>
#include <stdio.h>
#include <assert.h>
#include <errno.h>
#include <sys/time.h>
#include <string.h>
#include <cstdlib>
#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_));
}

View File

@ -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: