[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
3 | // found in the LICENSE file. | ||||
4 | |||||
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 5 | #include "base/synchronization/waitable_event_watcher.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 6 | |
7 | #include "base/compiler_specific.h" | ||||
[email protected] | 44f9c95 | 2011-01-02 06:05:39 | [diff] [blame] | 8 | #include "base/synchronization/waitable_event.h" |
[email protected] | a8d1ebbe | 2011-01-01 18:26:16 | [diff] [blame] | 9 | #include "base/win/object_watcher.h" |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 10 | |
11 | namespace base { | ||||
12 | |||||
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 13 | WaitableEventWatcher::WaitableEventWatcher() |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 14 | : event_(NULL) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 15 | } |
16 | |||||
17 | WaitableEventWatcher::~WaitableEventWatcher() { | ||||
18 | } | ||||
19 | |||||
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 20 | bool WaitableEventWatcher::StartWatching( |
21 | WaitableEvent* event, | ||||
22 | const EventCallback& callback) { | ||||
23 | callback_ = callback; | ||||
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 24 | event_ = event; |
jam | 7fab108 | 2015-09-29 02:26:18 | [diff] [blame] | 25 | return watcher_.StartWatchingOnce(event->handle(), this); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 26 | } |
27 | |||||
28 | void WaitableEventWatcher::StopWatching() { | ||||
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 29 | callback_.Reset(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 30 | event_ = NULL; |
31 | watcher_.StopWatching(); | ||||
32 | } | ||||
33 | |||||
34 | WaitableEvent* WaitableEventWatcher::GetWatchedEvent() { | ||||
35 | return event_; | ||||
36 | } | ||||
37 | |||||
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 38 | void WaitableEventWatcher::OnObjectSignaled(HANDLE h) { |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 39 | WaitableEvent* event = event_; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 40 | EventCallback callback = callback_; |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 41 | event_ = NULL; |
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 42 | callback_.Reset(); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 43 | DCHECK(event); |
44 | |||||
[email protected] | 329be05 | 2013-02-04 18:14:28 | [diff] [blame] | 45 | callback.Run(event); |
[email protected] | 1c4947f | 2009-01-15 22:25:11 | [diff] [blame] | 46 | } |
47 | |||||
48 | } // namespace base |