Skip to content

Commit

Permalink
Add TimedWait() API to CondVar.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yhchiang committed Jul 3, 2014
1 parent 2459f7e commit 6580685
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
25 changes: 24 additions & 1 deletion port/port_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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_));
}
Expand Down
2 changes: 2 additions & 0 deletions port/port_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 6580685

Please sign in to comment.