Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(263)

Side by Side Diff: base/synchronization/waitable_event_watcher_win.cc

Issue 2839213002: [Synchronization] Fix a crash in WaitableEventWatcher (Closed)
Patch Set: Fix grammar in comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/synchronization/waitable_event_watcher_unittest.cc ('k') | base/win/object_watcher.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
OLDNEW
« no previous file with comments | « base/synchronization/waitable_event_watcher_unittest.cc ('k') | base/win/object_watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698