• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 namespace base {
12 
ObjectWatcherHelper(WaitableEventWatcher * watcher)13 WaitableEventWatcher::ObjectWatcherHelper::ObjectWatcherHelper(
14     WaitableEventWatcher* watcher)
15     : watcher_(watcher) {
16 };
17 
OnObjectSignaled(HANDLE h)18 void WaitableEventWatcher::ObjectWatcherHelper::OnObjectSignaled(HANDLE h) {
19   watcher_->OnObjectSignaled();
20 }
21 
22 
WaitableEventWatcher()23 WaitableEventWatcher::WaitableEventWatcher()
24     : ALLOW_THIS_IN_INITIALIZER_LIST(helper_(this)),
25       event_(NULL),
26       delegate_(NULL) {
27 }
28 
~WaitableEventWatcher()29 WaitableEventWatcher::~WaitableEventWatcher() {
30 }
31 
StartWatching(WaitableEvent * event,Delegate * delegate)32 bool WaitableEventWatcher::StartWatching(WaitableEvent* event,
33                                          Delegate* delegate) {
34   delegate_ = delegate;
35   event_ = event;
36 
37   return watcher_.StartWatching(event->handle(), &helper_);
38 }
39 
StopWatching()40 void WaitableEventWatcher::StopWatching() {
41   delegate_ = NULL;
42   event_ = NULL;
43   watcher_.StopWatching();
44 }
45 
GetWatchedEvent()46 WaitableEvent* WaitableEventWatcher::GetWatchedEvent() {
47   return event_;
48 }
49 
OnObjectSignaled()50 void WaitableEventWatcher::OnObjectSignaled() {
51   WaitableEvent* event = event_;
52   Delegate* delegate = delegate_;
53   event_ = NULL;
54   delegate_ = NULL;
55   DCHECK(event);
56 
57   delegate->OnWaitableEventSignaled(event);
58 }
59 
60 }  // namespace base
61