OLD | NEW |
---|---|
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 | 10 |
10 namespace base { | 11 namespace base { |
11 namespace win { | 12 namespace win { |
12 | 13 |
13 //----------------------------------------------------------------------------- | 14 //----------------------------------------------------------------------------- |
14 | 15 |
15 ObjectWatcher::ObjectWatcher() | 16 ObjectWatcher::ObjectWatcher() : weak_factory_(this) {} |
16 : object_(NULL), | |
17 wait_object_(NULL), | |
18 origin_loop_(NULL), | |
19 run_once_(true), | |
20 weak_factory_(this) { | |
21 } | |
22 | 17 |
23 ObjectWatcher::~ObjectWatcher() { | 18 ObjectWatcher::~ObjectWatcher() { |
24 StopWatching(); | 19 StopWatching(); |
25 } | 20 } |
26 | 21 |
27 bool ObjectWatcher::StartWatchingOnce(HANDLE object, Delegate* delegate) { | 22 bool ObjectWatcher::StartWatchingOnce(HANDLE object, Delegate* delegate) { |
28 return StartWatchingInternal(object, delegate, true); | 23 return StartWatchingInternal(object, delegate, true); |
29 } | 24 } |
30 | 25 |
31 bool ObjectWatcher::StartWatchingMultipleTimes(HANDLE object, | 26 bool ObjectWatcher::StartWatchingMultipleTimes(HANDLE object, |
32 Delegate* delegate) { | 27 Delegate* delegate) { |
33 return StartWatchingInternal(object, delegate, false); | 28 return StartWatchingInternal(object, delegate, false); |
34 } | 29 } |
35 | 30 |
36 bool ObjectWatcher::StopWatching() { | 31 bool ObjectWatcher::StopWatching() { |
37 if (!wait_object_) | 32 if (!wait_object_) |
38 return false; | 33 return false; |
39 | 34 |
40 // Make sure ObjectWatcher is used in a single-threaded fashion. | 35 // Make sure ObjectWatcher is used in a single-threaded fashion. |
41 DCHECK_EQ(origin_loop_, MessageLoop::current()); | 36 DCHECK(task_runner_->BelongsToCurrentThread()); |
42 | 37 |
43 // 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 |
44 // finish before we return from this call. | 39 // finish before we return from this call. |
45 if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) { | 40 if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) { |
46 DPLOG(FATAL) << "UnregisterWaitEx failed"; | 41 DPLOG(FATAL) << "UnregisterWaitEx failed"; |
47 return false; | 42 return false; |
48 } | 43 } |
49 | 44 |
50 weak_factory_.InvalidateWeakPtrs(); | 45 weak_factory_.InvalidateWeakPtrs(); |
51 object_ = NULL; | 46 object_ = NULL; |
grt (UTC plus 2)
2016/07/14 07:19:11
nit: nullptr throughout
fdoray
2016/07/18 16:05:18
Done.
| |
52 wait_object_ = NULL; | 47 wait_object_ = NULL; |
grt (UTC plus 2)
2016/07/14 07:19:11
take absolutely everything back to its initial sta
fdoray
2016/07/18 16:05:18
Done.
| |
53 | 48 |
54 origin_loop_->RemoveDestructionObserver(this); | |
55 return true; | 49 return true; |
56 } | 50 } |
57 | 51 |
58 bool ObjectWatcher::IsWatching() const { | 52 bool ObjectWatcher::IsWatching() const { |
59 return object_ != NULL; | 53 return object_ != NULL; |
60 } | 54 } |
61 | 55 |
62 HANDLE ObjectWatcher::GetWatchedObject() const { | 56 HANDLE ObjectWatcher::GetWatchedObject() const { |
63 return object_; | 57 return object_; |
64 } | 58 } |
65 | 59 |
66 // static | 60 // static |
67 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { | 61 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { |
68 DCHECK(!timed_out); | 62 DCHECK(!timed_out); |
69 | 63 |
70 // The destructor blocks on any callbacks that are in flight, so we know that | 64 // The destructor blocks on any callbacks that are in flight, so we know that |
71 // that is always a pointer to a valid ObjectWater. | 65 // that is always a pointer to a valid ObjectWater. |
72 ObjectWatcher* that = static_cast<ObjectWatcher*>(param); | 66 ObjectWatcher* that = static_cast<ObjectWatcher*>(param); |
73 that->origin_loop_->task_runner()->PostTask(FROM_HERE, that->callback_); | 67 that->task_runner_->PostTask(FROM_HERE, that->callback_); |
74 if (that->run_once_) | 68 if (that->run_once_) |
75 that->callback_.Reset(); | 69 that->callback_.Reset(); |
76 } | 70 } |
77 | 71 |
78 bool ObjectWatcher::StartWatchingInternal(HANDLE object, Delegate* delegate, | 72 bool ObjectWatcher::StartWatchingInternal(HANDLE object, Delegate* delegate, |
79 bool execute_only_once) { | 73 bool execute_only_once) { |
80 CHECK(delegate); | 74 CHECK(delegate); |
grt (UTC plus 2)
2016/07/14 07:19:11
seems to me that this should be DCHECK since it re
fdoray
2016/07/18 16:05:18
Done.
| |
81 if (wait_object_) { | 75 if (wait_object_) { |
82 NOTREACHED() << "Already watching an object"; | 76 NOTREACHED() << "Already watching an object"; |
83 return false; | 77 return false; |
84 } | 78 } |
85 | 79 |
86 origin_loop_ = MessageLoop::current(); | 80 if (!ThreadTaskRunnerHandle::IsSet()) |
grt (UTC plus 2)
2016/07/14 07:19:11
NOTREACHED for this, too? isn't it a programming e
fdoray
2016/07/18 16:05:18
Done.
| |
87 if (!origin_loop_) | |
88 return false; | 81 return false; |
82 task_runner_ = ThreadTaskRunnerHandle::Get(); | |
89 | 83 |
90 run_once_ = execute_only_once; | 84 run_once_ = execute_only_once; |
91 | 85 |
92 // Since our job is to just notice when an object is signaled and report the | 86 // Since our job is to just notice when an object is signaled and report the |
93 // result back to this thread, we can just run on a Windows wait thread. | 87 // result back to this thread, we can just run on a Windows wait thread. |
94 DWORD wait_flags = WT_EXECUTEINWAITTHREAD; | 88 DWORD wait_flags = WT_EXECUTEINWAITTHREAD; |
95 if (run_once_) | 89 if (run_once_) |
96 wait_flags |= WT_EXECUTEONLYONCE; | 90 wait_flags |= WT_EXECUTEONLYONCE; |
97 | 91 |
98 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject, | 92 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject, |
99 // so set up all state now. | 93 // so set up all state now. |
100 callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), | 94 callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), |
101 delegate); | 95 delegate); |
102 object_ = object; | 96 object_ = object; |
103 | 97 |
104 if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting, | 98 if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting, |
105 this, INFINITE, wait_flags)) { | 99 this, INFINITE, wait_flags)) { |
106 DPLOG(FATAL) << "RegisterWaitForSingleObject failed"; | 100 DPLOG(FATAL) << "RegisterWaitForSingleObject failed"; |
107 object_ = NULL; | 101 object_ = NULL; |
108 wait_object_ = NULL; | 102 wait_object_ = NULL; |
109 return false; | 103 return false; |
110 } | 104 } |
111 | 105 |
112 // We need to know if the current message loop is going away so we can | |
113 // prevent the wait thread from trying to access a dead message loop. | |
114 origin_loop_->AddDestructionObserver(this); | |
115 return true; | 106 return true; |
116 } | 107 } |
117 | 108 |
118 void ObjectWatcher::Signal(Delegate* delegate) { | 109 void ObjectWatcher::Signal(Delegate* delegate) { |
119 // Signaling the delegate may result in our destruction or a nested call to | 110 // Signaling the delegate may result in our destruction or a nested call to |
120 // StartWatching(). As a result, we save any state we need and clear previous | 111 // StartWatching(). As a result, we save any state we need and clear previous |
121 // watcher state before signaling the delegate. | 112 // watcher state before signaling the delegate. |
122 HANDLE object = object_; | 113 HANDLE object = object_; |
123 if (run_once_) | 114 if (run_once_) |
124 StopWatching(); | 115 StopWatching(); |
125 delegate->OnObjectSignaled(object); | 116 delegate->OnObjectSignaled(object); |
126 } | 117 } |
127 | 118 |
128 void ObjectWatcher::WillDestroyCurrentMessageLoop() { | |
129 // Need to shutdown the watch so that we don't try to access the MessageLoop | |
130 // after this point. | |
131 StopWatching(); | |
132 } | |
133 | |
134 } // namespace win | 119 } // namespace win |
135 } // namespace base | 120 } // namespace base |
OLD | NEW |