OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/synchronization/waitable_event_watcher.h" | 5 #include "base/synchronization/waitable_event_watcher.h" |
6 | 6 |
7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/win/object_watcher.h" | 9 #include "base/win/object_watcher.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 | 12 |
13 WaitableEventWatcher::WaitableEventWatcher() = default; | 13 WaitableEventWatcher::WaitableEventWatcher() = default; |
14 | 14 |
15 WaitableEventWatcher::~WaitableEventWatcher() { | 15 WaitableEventWatcher::~WaitableEventWatcher() {} |
16 } | |
17 | 16 |
18 bool WaitableEventWatcher::StartWatching(WaitableEvent* event, | 17 bool WaitableEventWatcher::StartWatching(WaitableEvent* event, |
19 EventCallback callback) { | 18 EventCallback callback) { |
| 19 DCHECK(event); |
20 callback_ = std::move(callback); | 20 callback_ = std::move(callback); |
21 event_ = event; | 21 event_ = event; |
22 return watcher_.StartWatchingOnce(event->handle(), this); | 22 |
| 23 // Duplicate and hold the event handle until a callback is returned or |
| 24 // waiting is stopped. |
| 25 HANDLE handle = nullptr; |
| 26 if (!::DuplicateHandle(::GetCurrentProcess(), // hSourceProcessHandle |
| 27 event->handle(), |
| 28 ::GetCurrentProcess(), // hTargetProcessHandle |
| 29 &handle, |
| 30 0, // dwDesiredAccess ignored due to SAME_ACCESS |
| 31 FALSE, // !bInheritHandle |
| 32 DUPLICATE_SAME_ACCESS)) { |
| 33 return false; |
| 34 } |
| 35 duplicated_event_handle_.Set(handle); |
| 36 return watcher_.StartWatchingOnce(handle, this); |
23 } | 37 } |
24 | 38 |
25 void WaitableEventWatcher::StopWatching() { | 39 void WaitableEventWatcher::StopWatching() { |
26 callback_.Reset(); | 40 callback_.Reset(); |
27 event_ = NULL; | 41 event_ = NULL; |
28 watcher_.StopWatching(); | 42 watcher_.StopWatching(); |
| 43 duplicated_event_handle_.Close(); |
29 } | 44 } |
30 | 45 |
31 void WaitableEventWatcher::OnObjectSignaled(HANDLE h) { | 46 void WaitableEventWatcher::OnObjectSignaled(HANDLE h) { |
| 47 DCHECK_EQ(duplicated_event_handle_.Get(), h); |
32 WaitableEvent* event = event_; | 48 WaitableEvent* event = event_; |
33 EventCallback callback = std::move(callback_); | 49 EventCallback callback = std::move(callback_); |
34 event_ = NULL; | 50 event_ = NULL; |
| 51 duplicated_event_handle_.Close(); |
35 DCHECK(event); | 52 DCHECK(event); |
36 | 53 |
37 std::move(callback).Run(event); | 54 std::move(callback).Run(event); |
38 } | 55 } |
39 | 56 |
40 } // namespace base | 57 } // namespace base |
OLD | NEW |