OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/synchronization/waitable_event.h" | 5 #include "base/synchronization/waitable_event.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/numerics/safe_conversions.h" | 10 #include "base/numerics/safe_conversions.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 base::ThreadRestrictions::AssertWaitAllowed(); | 44 base::ThreadRestrictions::AssertWaitAllowed(); |
45 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); | 45 DWORD result = WaitForSingleObject(handle_.Get(), INFINITE); |
46 // It is most unexpected that this should ever fail. Help consumers learn | 46 // It is most unexpected that this should ever fail. Help consumers learn |
47 // about it if it should ever fail. | 47 // about it if it should ever fail. |
48 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; | 48 DCHECK_EQ(WAIT_OBJECT_0, result) << "WaitForSingleObject failed"; |
49 } | 49 } |
50 | 50 |
51 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { | 51 bool WaitableEvent::TimedWait(const TimeDelta& max_time) { |
52 base::ThreadRestrictions::AssertWaitAllowed(); | 52 base::ThreadRestrictions::AssertWaitAllowed(); |
53 DCHECK_GE(max_time, TimeDelta()); | 53 DCHECK_GE(max_time, TimeDelta()); |
54 // Be careful here. TimeDelta has a precision of microseconds, but this API | 54 // Truncate the timeout to milliseconds. The API specifies that this method |
55 // is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6? | 55 // can return in less than |max_time| (when returning false), as the argument |
56 // It should be 6 to avoid returning too early. | 56 // is the maximum time that a caller is willing to wait. |
57 DWORD timeout = saturated_cast<DWORD>(max_time.InMillisecondsRoundedUp()); | 57 DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds()); |
58 | 58 |
59 DWORD result = WaitForSingleObject(handle_.Get(), timeout); | 59 DWORD result = WaitForSingleObject(handle_.Get(), timeout); |
60 switch (result) { | 60 switch (result) { |
61 case WAIT_OBJECT_0: | 61 case WAIT_OBJECT_0: |
62 return true; | 62 return true; |
63 case WAIT_TIMEOUT: | 63 case WAIT_TIMEOUT: |
64 return false; | 64 return false; |
65 } | 65 } |
66 // It is most unexpected that this should ever fail. Help consumers learn | 66 // It is most unexpected that this should ever fail. Help consumers learn |
67 // about it if it should ever fail. | 67 // about it if it should ever fail. |
(...skipping 19 matching lines...) Expand all Loading... |
87 INFINITE); // no timeout | 87 INFINITE); // no timeout |
88 if (result >= WAIT_OBJECT_0 + count) { | 88 if (result >= WAIT_OBJECT_0 + count) { |
89 DPLOG(FATAL) << "WaitForMultipleObjects failed"; | 89 DPLOG(FATAL) << "WaitForMultipleObjects failed"; |
90 return 0; | 90 return 0; |
91 } | 91 } |
92 | 92 |
93 return result - WAIT_OBJECT_0; | 93 return result - WAIT_OBJECT_0; |
94 } | 94 } |
95 | 95 |
96 } // namespace base | 96 } // namespace base |
OLD | NEW |