ipc/chromium/src/base/waitable_event_win.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial