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

Side by Side Diff: base/win/object_watcher.cc

Issue 2598753002: Allow ObjectWatcher to be used from sequenced tasks. (Closed)
Patch Set: self-review Created 4 years 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/win/object_watcher.h ('k') | no next file » | 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) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/win/object_watcher.h" 5 #include "base/win/object_watcher.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/threading/thread_task_runner_handle.h" 9 #include "base/threading/sequenced_task_runner_handle.h"
10 10
11 namespace base { 11 namespace base {
12 namespace win { 12 namespace win {
13 13
14 //----------------------------------------------------------------------------- 14 //-----------------------------------------------------------------------------
15 15
16 ObjectWatcher::ObjectWatcher() : weak_factory_(this) {} 16 ObjectWatcher::ObjectWatcher() : weak_factory_(this) {}
17 17
18 ObjectWatcher::~ObjectWatcher() { 18 ObjectWatcher::~ObjectWatcher() {
19 StopWatching(); 19 StopWatching();
20 } 20 }
21 21
22 bool ObjectWatcher::StartWatchingOnce(HANDLE object, Delegate* delegate) { 22 bool ObjectWatcher::StartWatchingOnce(HANDLE object, Delegate* delegate) {
23 return StartWatchingInternal(object, delegate, true); 23 return StartWatchingInternal(object, delegate, true);
24 } 24 }
25 25
26 bool ObjectWatcher::StartWatchingMultipleTimes(HANDLE object, 26 bool ObjectWatcher::StartWatchingMultipleTimes(HANDLE object,
27 Delegate* delegate) { 27 Delegate* delegate) {
28 return StartWatchingInternal(object, delegate, false); 28 return StartWatchingInternal(object, delegate, false);
29 } 29 }
30 30
31 bool ObjectWatcher::StopWatching() { 31 bool ObjectWatcher::StopWatching() {
32 if (!wait_object_) 32 if (!wait_object_)
33 return false; 33 return false;
34 34
35 // Make sure ObjectWatcher is used in a single-threaded fashion. 35 // Make sure ObjectWatcher is used in a sequenced fashion.
36 DCHECK(task_runner_->BelongsToCurrentThread()); 36 DCHECK(task_runner_->RunsTasksOnCurrentThread());
37 37
38 // Blocking call to cancel the wait. Any callbacks already in progress will 38 // Blocking call to cancel the wait. Any callbacks already in progress will
39 // finish before we return from this call. 39 // finish before we return from this call.
40 if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) { 40 if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) {
41 DPLOG(FATAL) << "UnregisterWaitEx failed"; 41 DPLOG(FATAL) << "UnregisterWaitEx failed";
42 return false; 42 return false;
43 } 43 }
44 44
45 Reset(); 45 Reset();
46 return true; 46 return true;
(...skipping 16 matching lines...) Expand all
63 ObjectWatcher* that = static_cast<ObjectWatcher*>(param); 63 ObjectWatcher* that = static_cast<ObjectWatcher*>(param);
64 that->task_runner_->PostTask(FROM_HERE, that->callback_); 64 that->task_runner_->PostTask(FROM_HERE, that->callback_);
65 if (that->run_once_) 65 if (that->run_once_)
66 that->callback_.Reset(); 66 that->callback_.Reset();
67 } 67 }
68 68
69 bool ObjectWatcher::StartWatchingInternal(HANDLE object, Delegate* delegate, 69 bool ObjectWatcher::StartWatchingInternal(HANDLE object, Delegate* delegate,
70 bool execute_only_once) { 70 bool execute_only_once) {
71 DCHECK(delegate); 71 DCHECK(delegate);
72 DCHECK(!wait_object_) << "Already watching an object"; 72 DCHECK(!wait_object_) << "Already watching an object";
73 DCHECK(ThreadTaskRunnerHandle::IsSet()); 73 DCHECK(SequencedTaskRunnerHandle::IsSet());
74 74
75 task_runner_ = ThreadTaskRunnerHandle::Get(); 75 task_runner_ = SequencedTaskRunnerHandle::Get();
76 76
77 run_once_ = execute_only_once; 77 run_once_ = execute_only_once;
78 78
79 // Since our job is to just notice when an object is signaled and report the 79 // Since our job is to just notice when an object is signaled and report the
80 // result back to this thread, we can just run on a Windows wait thread. 80 // result back to this sequence, we can just run on a Windows wait thread.
81 DWORD wait_flags = WT_EXECUTEINWAITTHREAD; 81 DWORD wait_flags = WT_EXECUTEINWAITTHREAD;
82 if (run_once_) 82 if (run_once_)
83 wait_flags |= WT_EXECUTEONLYONCE; 83 wait_flags |= WT_EXECUTEONLYONCE;
84 84
85 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject, 85 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject,
86 // so set up all state now. 86 // so set up all state now.
87 callback_ = 87 callback_ =
88 Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), delegate); 88 Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), delegate);
89 object_ = object; 89 object_ = object;
90 90
(...skipping 21 matching lines...) Expand all
112 callback_.Reset(); 112 callback_.Reset();
113 object_ = nullptr; 113 object_ = nullptr;
114 wait_object_ = nullptr; 114 wait_object_ = nullptr;
115 task_runner_ = nullptr; 115 task_runner_ = nullptr;
116 run_once_ = true; 116 run_once_ = true;
117 weak_factory_.InvalidateWeakPtrs(); 117 weak_factory_.InvalidateWeakPtrs();
118 } 118 }
119 119
120 } // namespace win 120 } // namespace win
121 } // namespace base 121 } // namespace base
OLDNEW
« no previous file with comments | « base/win/object_watcher.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698