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 "base/synchronization/waitable_event_watcher.h"
6
7#include "base/compiler_specific.h"
8#include "base/synchronization/waitable_event.h"
9#include "base/win/object_watcher.h"
10
11namespace base {
12
13WaitableEventWatcher::WaitableEventWatcher()
14    : event_(NULL) {
15}
16
17WaitableEventWatcher::~WaitableEventWatcher() {
18}
19
20bool WaitableEventWatcher::StartWatching(
21    WaitableEvent* event,
22    const EventCallback& callback) {
23  callback_ = callback;
24  event_ = event;
25  return watcher_.StartWatching(event->handle(), this);
26}
27
28void WaitableEventWatcher::StopWatching() {
29  callback_.Reset();
30  event_ = NULL;
31  watcher_.StopWatching();
32}
33
34WaitableEvent* WaitableEventWatcher::GetWatchedEvent() {
35  return event_;
36}
37
38void WaitableEventWatcher::OnObjectSignaled(HANDLE h) {
39  WaitableEvent* event = event_;
40  EventCallback callback = callback_;
41  event_ = NULL;
42  callback_.Reset();
43  DCHECK(event);
44
45  callback.Run(event);
46}
47
48}  // namespace base
49