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
3// found in the LICENSE file.
4
5#include "net/base/winsock_util.h"
6
7#include "base/logging.h"
8#include "net/base/net_errors.h"
9
10namespace net {
11
12namespace {
13
14// Prevent the compiler from optimizing away the arguments so they appear
15// nicely on the stack in crash dumps.
16#pragma warning(push)
17#pragma warning (disable: 4748)
18#pragma optimize( "", off )
19
20// Pass the important values as function arguments so that they are available
21// in crash dumps.
22void CheckEventWait(WSAEVENT hEvent, DWORD wait_rv, DWORD expected) {
23  if (wait_rv != expected) {
24    DWORD err = ERROR_SUCCESS;
25    if (wait_rv == WAIT_FAILED)
26      err = GetLastError();
27    CHECK(false);  // Crash.
28  }
29}
30
31#pragma optimize( "", on )
32#pragma warning(pop)
33
34}  // namespace
35
36void AssertEventNotSignaled(WSAEVENT hEvent) {
37  DWORD wait_rv = WaitForSingleObject(hEvent, 0);
38  CheckEventWait(hEvent, wait_rv, WAIT_TIMEOUT);
39}
40
41bool ResetEventIfSignaled(WSAEVENT hEvent) {
42  // TODO(wtc): Remove the CHECKs after enough testing.
43  DWORD wait_rv = WaitForSingleObject(hEvent, 0);
44  if (wait_rv == WAIT_TIMEOUT)
45    return false;  // The event object is not signaled.
46  CheckEventWait(hEvent, wait_rv, WAIT_OBJECT_0);
47  BOOL ok = WSAResetEvent(hEvent);
48  CHECK(ok);
49  return true;
50}
51
52}  // namespace net
53