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 (disable: 4748)
17#pragma optimize( "", off )
18
19// Pass the important values as function arguments so that they are available
20// in crash dumps.
21void CheckEventWait(WSAEVENT hEvent, DWORD wait_rv, DWORD expected) {
22  if (wait_rv != expected) {
23    DWORD err = ERROR_SUCCESS;
24    if (wait_rv == WAIT_FAILED)
25      err = GetLastError();
26    CHECK(false);  // Crash.
27  }
28}
29
30#pragma optimize( "", on )
31#pragma warning (default: 4748)
32
33}  // namespace
34
35void AssertEventNotSignaled(WSAEVENT hEvent) {
36  DWORD wait_rv = WaitForSingleObject(hEvent, 0);
37  CheckEventWait(hEvent, wait_rv, WAIT_TIMEOUT);
38}
39
40bool ResetEventIfSignaled(WSAEVENT hEvent) {
41  // TODO(wtc): Remove the CHECKs after enough testing.
42  DWORD wait_rv = WaitForSingleObject(hEvent, 0);
43  if (wait_rv == WAIT_TIMEOUT)
44    return false;  // The event object is not signaled.
45  CheckEventWait(hEvent, wait_rv, WAIT_OBJECT_0);
46  BOOL ok = WSAResetEvent(hEvent);
47  CHECK(ok);
48  return true;
49}
50
51}  // namespace net
52