This source file includes following definitions.
- wait_result_
- Init
- Awake
#include "mojo/system/waiter.h"
#include <limits>
#include "base/logging.h"
#include "base/time/time.h"
namespace mojo {
namespace system {
Waiter::Waiter()
: cv_(&lock_),
#ifndef NDEBUG
initialized_(false),
#endif
awoken_(false),
wait_result_(MOJO_RESULT_INTERNAL) {
}
Waiter::~Waiter() {
}
void Waiter::Init() {
#ifndef NDEBUG
initialized_ = true;
#endif
awoken_ = false;
wait_result_ = MOJO_RESULT_INTERNAL;
}
MojoResult Waiter::Wait(MojoDeadline deadline) {
base::AutoLock locker(lock_);
#ifndef NDEBUG
DCHECK(initialized_);
initialized_ = false;
#endif
if (awoken_) {
DCHECK_NE(wait_result_, MOJO_RESULT_INTERNAL);
return wait_result_;
}
if (deadline > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) {
do {
cv_.Wait();
} while (!awoken_);
} else {
const base::TimeTicks end_time = base::TimeTicks::HighResNow() +
base::TimeDelta::FromMicroseconds(static_cast<int64_t>(deadline));
do {
base::TimeTicks now_time = base::TimeTicks::HighResNow();
if (now_time >= end_time)
return MOJO_RESULT_DEADLINE_EXCEEDED;
cv_.TimedWait(end_time - now_time);
} while (!awoken_);
}
DCHECK_NE(wait_result_, MOJO_RESULT_INTERNAL);
return wait_result_;
}
void Waiter::Awake(MojoResult wait_result) {
base::AutoLock locker(lock_);
if (awoken_)
return;
awoken_ = true;
wait_result_ = wait_result;
cv_.Signal();
}
}
}