summaryrefslogtreecommitdiffstats
path: root/base/shared_event.cc
blob: 3a9ce9c80b6a154d74bf380eccbfa767f03560d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/shared_event.h"
#include "base/logging.h"
#include "base/time.h"

SharedEvent::~SharedEvent() {
  Close();
}

bool SharedEvent::Create(bool manual_reset, bool initial_state) {
  DCHECK(!event_handle_);
  event_handle_ = CreateEvent(NULL /* security attributes */, manual_reset,
                              initial_state, NULL /* name */);
  DCHECK(event_handle_);
  return !!event_handle_;
}

void SharedEvent::Close() {
  if (event_handle_) {
    BOOL rv = CloseHandle(event_handle_);
    DCHECK(rv);
    event_handle_ = NULL;
  }
}

bool SharedEvent::SetSignaledState(bool signaled) {
  DCHECK(event_handle_);
  BOOL rv;
  if (signaled) {
    rv = SetEvent(event_handle_);
  } else {
    rv = ResetEvent(event_handle_);
  }
  return rv ? true : false;
}

bool SharedEvent::IsSignaled() {
  DCHECK(event_handle_);
  DWORD event_state = ::WaitForSingleObject(event_handle_, 0);
  DCHECK(WAIT_OBJECT_0 == event_state || WAIT_TIMEOUT == event_state);
  return event_state == WAIT_OBJECT_0;
}

bool SharedEvent::WaitUntilSignaled(const TimeDelta& timeout) {
  DCHECK(event_handle_);
  DWORD event_state = ::WaitForSingleObject(event_handle_,
      static_cast<DWORD>(timeout.InMillisecondsF()));
  return event_state == WAIT_OBJECT_0;
}

bool SharedEvent::WaitForeverUntilSignaled() {
  DCHECK(event_handle_);
  DWORD event_state = ::WaitForSingleObject(event_handle_,
                                            INFINITE);
  return event_state == WAIT_OBJECT_0;
}

bool SharedEvent::ShareToProcess(ProcessHandle process,
                                 SharedEventHandle *new_handle) {
  DCHECK(event_handle_);
  HANDLE event_handle_copy;
  BOOL rv = DuplicateHandle(GetCurrentProcess(), event_handle_, process,
      &event_handle_copy, 0, FALSE, DUPLICATE_SAME_ACCESS);

  if (rv)
    *new_handle = event_handle_copy;
  return rv ? true : false;
}

bool SharedEvent::GiveToProcess(ProcessHandle process,
                                SharedEventHandle *new_handle) {
  bool rv = ShareToProcess(process, new_handle);
  if (rv)
    Close();
  return rv;
}