summaryrefslogtreecommitdiffstats
path: root/base/synchronization/waitable_event_watcher_win.cc
blob: 46d47ac581a8e3ff58f6d17a692fe3d5117196ae (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
// Copyright (c) 2011 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/synchronization/waitable_event_watcher.h"

#include "base/compiler_specific.h"
#include "base/synchronization/waitable_event.h"
#include "base/win/object_watcher.h"

namespace base {

WaitableEventWatcher::WaitableEventWatcher()
    : event_(NULL) {
}

WaitableEventWatcher::~WaitableEventWatcher() {
}

bool WaitableEventWatcher::StartWatching(
    WaitableEvent* event,
    const EventCallback& callback) {
  callback_ = callback;
  event_ = event;
  return watcher_.StartWatching(event->handle(), this);
}

void WaitableEventWatcher::StopWatching() {
  callback_.Reset();
  event_ = NULL;
  watcher_.StopWatching();
}

WaitableEvent* WaitableEventWatcher::GetWatchedEvent() {
  return event_;
}

void WaitableEventWatcher::OnObjectSignaled(HANDLE h) {
  WaitableEvent* event = event_;
  EventCallback callback = callback_;
  event_ = NULL;
  callback_.Reset();
  DCHECK(event);

  callback.Run(event);
}

}  // namespace base