blob: 770c582aedeaf1975ec166efcf1fc94a9b8355ea [file] [log] [blame]
James Robinson646469d2014-10-03 15:33:28 -07001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "base/synchronization/waitable_event.h"
6
7#include <math.h>
8#include <windows.h>
9
10#include "base/logging.h"
11#include "base/threading/thread_restrictions.h"
12#include "base/time/time.h"
13
14namespace base {
15
16WaitableEvent::WaitableEvent(bool manual_reset, bool signaled)
17 : handle_(CreateEvent(NULL, manual_reset, signaled, NULL)) {
18 // We're probably going to crash anyways if this is ever NULL, so we might as
19 // well make our stack reports more informative by crashing here.
20 CHECK(handle_.IsValid());
21}
22
James Robinson6a64b812014-12-03 13:38:42 -080023WaitableEvent::WaitableEvent(win::ScopedHandle handle)
24 : handle_(handle.Pass()) {
James Robinson646469d2014-10-03 15:33:28 -070025 CHECK(handle_.IsValid()) << "Tried to create WaitableEvent from NULL handle";
26}
27
28WaitableEvent::~WaitableEvent() {
29}
30
James Robinson646469d2014-10-03 15:33:28 -070031void WaitableEvent::Reset() {
32 ResetEvent(handle_.Get());
33}
34
35void WaitableEvent::Signal() {
36 SetEvent(handle_.Get());
37}
38
39bool WaitableEvent::IsSignaled() {
40 return TimedWait(TimeDelta::FromMilliseconds(0));
41}
42
43void WaitableEvent::Wait() {
44 base::ThreadRestrictions::AssertWaitAllowed();
45 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE);
46 // It is most unexpected that this should ever fail. Help consumers learn
47 // about it if it should ever fail.
48 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed";
49}
50
51bool WaitableEvent::TimedWait(const TimeDelta& max_time) {
52 base::ThreadRestrictions::AssertWaitAllowed();
53 DCHECK(max_time >= TimeDelta::FromMicroseconds(0));
54 // Be careful here. TimeDelta has a precision of microseconds, but this API
55 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6?
56 // It should be 6 to avoid returning too early.
57 double timeout = ceil(max_time.InMillisecondsF());
58 DWORD result = WaitForSingleObject(handle_.Get(),
59 static_cast<DWORD>(timeout));
60 switch (result) {
61 case WAIT_OBJECT_0:
62 return true;
63 case WAIT_TIMEOUT:
64 return false;
65 }
66 // It is most unexpected that this should ever fail. Help consumers learn
67 // about it if it should ever fail.
68 NOTREACHED() << "WaitForSingleObject failed";
69 return false;
70}
71
72// static
73size_t WaitableEvent::WaitMany(WaitableEvent** events, size_t count) {
74 base::ThreadRestrictions::AssertWaitAllowed();
75 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
76 CHECK_LE(count, MAXIMUM_WAIT_OBJECTS)
77 << "Can only wait on " << MAXIMUM_WAIT_OBJECTS << " with WaitMany";
78
79 for (size_t i = 0; i < count; ++i)
80 handles[i] = events[i]->handle();
81
82 // The cast is safe because count is small - see the CHECK above.
83 DWORD result =
84 WaitForMultipleObjects(static_cast<DWORD>(count),
85 handles,
86 FALSE, // don't wait for all the objects
87 INFINITE); // no timeout
88 if (result >= WAIT_OBJECT_0 + count) {
89 DPLOG(FATAL) << "WaitForMultipleObjects failed";
90 return 0;
91 }
92
93 return result - WAIT_OBJECT_0;
94}
95
96} // namespace base