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 // Explicitly run StopWatching to avoid dependency on the members | |
danakj
2017/04/27 20:01:08
nit: StopWatching()
atuchin
2017/04/28 06:44:29
Done.
| |
17 // destruction order (|event_handle_| must outlive working |watcher_|). | |
danakj
2017/04/27 20:01:08
Normally I'd just put a comment in the header file
atuchin
2017/04/28 06:44:30
Ok, removed that code and added comments to header
| |
18 watcher_.StopWatching(); | |
16 } | 19 } |
17 | 20 |
18 bool WaitableEventWatcher::StartWatching(WaitableEvent* event, | 21 bool WaitableEventWatcher::StartWatching(WaitableEvent* event, |
19 EventCallback callback) { | 22 EventCallback callback) { |
23 DCHECK(event); | |
20 callback_ = std::move(callback); | 24 callback_ = std::move(callback); |
21 event_ = event; | 25 event_ = event; |
22 return watcher_.StartWatchingOnce(event->handle(), this); | 26 |
27 // Duplicate and hold the event handle until a callback is returned or | |
28 // waiting is stopped. | |
29 HANDLE event_handle = nullptr; | |
danakj
2017/04/27 20:01:08
Can we avoid having a member and local var with th
atuchin
2017/04/28 06:44:29
Ok, I renamed:
local event_handle -> handle
event_
| |
30 if (!::DuplicateHandle(::GetCurrentProcess(), // hSourceProcessHandle | |
31 event->handle(), | |
32 ::GetCurrentProcess(), // hTargetProcessHandle | |
33 &event_handle, | |
34 0, // dwDesiredAccess ignored due to SAME_ACCESS | |
35 FALSE, // !bInheritHandle | |
36 DUPLICATE_SAME_ACCESS)) { | |
37 return false; | |
38 } | |
39 event_handle_.Set(event_handle); | |
40 return watcher_.StartWatchingOnce(event_handle, this); | |
23 } | 41 } |
24 | 42 |
25 void WaitableEventWatcher::StopWatching() { | 43 void WaitableEventWatcher::StopWatching() { |
26 callback_.Reset(); | 44 callback_.Reset(); |
27 event_ = NULL; | 45 event_ = NULL; |
28 watcher_.StopWatching(); | 46 watcher_.StopWatching(); |
47 event_handle_.Close(); | |
29 } | 48 } |
30 | 49 |
31 void WaitableEventWatcher::OnObjectSignaled(HANDLE h) { | 50 void WaitableEventWatcher::OnObjectSignaled(HANDLE h) { |
51 DCHECK_EQ(event_handle_.Get(), h); | |
32 WaitableEvent* event = event_; | 52 WaitableEvent* event = event_; |
33 EventCallback callback = std::move(callback_); | 53 EventCallback callback = std::move(callback_); |
34 event_ = NULL; | 54 event_ = NULL; |
55 event_handle_.Close(); | |
35 DCHECK(event); | 56 DCHECK(event); |
36 | 57 |
37 std::move(callback).Run(event); | 58 std::move(callback).Run(event); |
38 } | 59 } |
39 | 60 |
40 } // namespace base | 61 } // namespace base |
OLD | NEW |