Skip to content

Commit

Permalink
Base: Truncate the timeout of WaitableEvent::TimedWait on Windows.
Browse files Browse the repository at this point in the history
The timeout has to be adjusted to milliseconds (the resolution of the
underlying call), but it should be truncated instead of extended,
because the documentation clearly states that the function can fail in
less time than the passed-in timeout, and by definition, the timeout
is the maximum time that the caller is willing to block.

BUG=none

Review URL: https://codereview.chromium.org/1040833002

Cr-Commit-Position: refs/heads/master@{#323178}
  • Loading branch information
rvargas authored and Commit bot committed Apr 1, 2015
1 parent 0bd2a73 commit d65edb7
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions base/synchronization/waitable_event_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ void WaitableEvent::Wait() {
bool WaitableEvent::TimedWait(const TimeDelta& max_time) {
base::ThreadRestrictions::AssertWaitAllowed();
DCHECK_GE(max_time, TimeDelta());
// Be careful here. TimeDelta has a precision of microseconds, but this API
// is in milliseconds. If there are 5.5ms left, should the delay be 5 or 6?
// It should be 6 to avoid returning too early.
DWORD timeout = saturated_cast<DWORD>(max_time.InMillisecondsRoundedUp());
// Truncate the timeout to milliseconds. The API specifies that this method
// can return in less than |max_time| (when returning false), as the argument
// is the maximum time that a caller is willing to wait.
DWORD timeout = saturated_cast<DWORD>(max_time.InMilliseconds());

DWORD result = WaitForSingleObject(handle_.Get(), timeout);
switch (result) {
Expand Down

0 comments on commit d65edb7

Please sign in to comment.