Commit 017a1bcf authored by Kyle Qian's avatar Kyle Qian Committed by Commit Bot

Update CountDownLatchImpl to Match Specifications

This CL changes the constructor for CountDownLatchImpl to use a DCHECK
to disallow initialization counts less than 0. An initialization count
of exactly 0 is still allowed, in which case countDown() will never
Signal() its WaitableEvent, and all calls to Await() will immediately
return.

Bug: 861813
Change-Id: Ic7807544c792be010f46ca58391a02f64ab1638e
Reviewed-on: https://chromium-review.googlesource.com/1141101
Commit-Queue: Kyle Qian <kyleqian@google.com>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Reviewed-by: default avatarKyle Horimoto <khorimoto@chromium.org>
Reviewed-by: default avatarRyan Hansberry <hansberry@chromium.org>
Cr-Commit-Position: refs/heads/master@{#576144}
parent 7dbb56c0
......@@ -13,30 +13,31 @@ namespace nearby {
CountDownLatchImpl::CountDownLatchImpl(int32_t count)
: count_(count),
count_waitable_event_(
base::WaitableEvent::ResetPolicy::MANUAL /* reset_policy */,
count <= 0 ? base::WaitableEvent::InitialState::SIGNALED
: base::WaitableEvent::InitialState::
NOT_SIGNALED /* initial_state*/) {}
base::WaitableEvent::ResetPolicy::MANUAL,
count_.IsZero() ? base::WaitableEvent::InitialState::SIGNALED
: base::WaitableEvent::InitialState::NOT_SIGNALED) {
DCHECK_GE(count, 0);
}
CountDownLatchImpl::~CountDownLatchImpl() = default;
location::nearby::Exception::Value CountDownLatchImpl::await() {
if (count_waitable_event_.IsSignaled())
return location::nearby::Exception::NONE;
if (!count_waitable_event_.IsSignaled())
count_waitable_event_.Wait();
count_waitable_event_.Wait();
return location::nearby::Exception::NONE;
}
location::nearby::ExceptionOr<bool> CountDownLatchImpl::await(
int32_t timeout_millis) {
if (count_waitable_event_.IsSignaled())
return location::nearby::ExceptionOr<bool>(true);
// Return true if |count_waitable_event_| is signaled before it times out.
// Otherwise, this returns false due to timing out.
return location::nearby::ExceptionOr<bool>(count_waitable_event_.TimedWait(
base::TimeDelta::FromMilliseconds(static_cast<int64_t>(timeout_millis))));
if (!count_waitable_event_.IsSignaled())
// Return true if |count_waitable_event_| is signaled before TimedAwait()
// times out. Otherwise, this returns false due to timing out.
return location::nearby::ExceptionOr<bool>(
count_waitable_event_.TimedWait(base::TimeDelta::FromMilliseconds(
static_cast<int64_t>(timeout_millis))));
return location::nearby::ExceptionOr<bool>(true);
}
void CountDownLatchImpl::countDown() {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment