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 | 9 |
10 namespace base { | 10 namespace base { |
11 namespace win { | 11 namespace win { |
12 | 12 |
13 //----------------------------------------------------------------------------- | 13 //----------------------------------------------------------------------------- |
14 | 14 |
15 ObjectWatcher::ObjectWatcher() | 15 ObjectWatcher::ObjectWatcher() |
16 : object_(NULL), | 16 : object_(NULL), |
17 wait_object_(NULL), | 17 wait_object_(NULL), |
18 origin_loop_(NULL), | 18 origin_loop_(NULL), |
| 19 wait_many_(false), |
19 weak_factory_(this) { | 20 weak_factory_(this) { |
20 } | 21 } |
21 | 22 |
22 ObjectWatcher::~ObjectWatcher() { | 23 ObjectWatcher::~ObjectWatcher() { |
23 StopWatching(); | 24 StopWatching(); |
24 } | 25 } |
25 | 26 |
26 bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) { | 27 bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate, |
| 28 bool wait_many) { |
27 CHECK(delegate); | 29 CHECK(delegate); |
28 if (wait_object_) { | 30 if (wait_object_) { |
29 NOTREACHED() << "Already watching an object"; | 31 NOTREACHED() << "Already watching an object"; |
30 return false; | 32 return false; |
31 } | 33 } |
| 34 wait_many_ = wait_many; |
32 | 35 |
33 // Since our job is to just notice when an object is signaled and report the | 36 // Since our job is to just notice when an object is signaled and report the |
34 // result back to this thread, we can just run on a Windows wait thread. | 37 // result back to this thread, we can just run on a Windows wait thread. |
35 DWORD wait_flags = WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE; | 38 DWORD wait_flags = WT_EXECUTEINWAITTHREAD; |
| 39 if (!wait_many_) |
| 40 wait_flags |= WT_EXECUTEONLYONCE; |
36 | 41 |
37 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject, | 42 // DoneWaiting can be synchronously called from RegisterWaitForSingleObject, |
38 // so set up all state now. | 43 // so set up all state now. |
39 callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), | 44 callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(), |
40 delegate); | 45 delegate); |
41 object_ = object; | 46 object_ = object; |
42 origin_loop_ = MessageLoop::current(); | 47 origin_loop_ = MessageLoop::current(); |
43 | 48 |
44 if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting, | 49 if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting, |
45 this, INFINITE, wait_flags)) { | 50 this, INFINITE, wait_flags)) { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 } | 91 } |
87 | 92 |
88 // static | 93 // static |
89 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { | 94 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { |
90 DCHECK(!timed_out); | 95 DCHECK(!timed_out); |
91 | 96 |
92 // The destructor blocks on any callbacks that are in flight, so we know that | 97 // The destructor blocks on any callbacks that are in flight, so we know that |
93 // that is always a pointer to a valid ObjectWater. | 98 // that is always a pointer to a valid ObjectWater. |
94 ObjectWatcher* that = static_cast<ObjectWatcher*>(param); | 99 ObjectWatcher* that = static_cast<ObjectWatcher*>(param); |
95 that->origin_loop_->task_runner()->PostTask(FROM_HERE, that->callback_); | 100 that->origin_loop_->task_runner()->PostTask(FROM_HERE, that->callback_); |
96 that->callback_.Reset(); | 101 if (!that->wait_many_) |
| 102 that->callback_.Reset(); |
97 } | 103 } |
98 | 104 |
99 void ObjectWatcher::Signal(Delegate* delegate) { | 105 void ObjectWatcher::Signal(Delegate* delegate) { |
100 // Signaling the delegate may result in our destruction or a nested call to | 106 // Signaling the delegate may result in our destruction or a nested call to |
101 // StartWatching(). As a result, we save any state we need and clear previous | 107 // StartWatching(). As a result, we save any state we need and clear previous |
102 // watcher state before signaling the delegate. | 108 // watcher state before signaling the delegate. |
103 HANDLE object = object_; | 109 HANDLE object = object_; |
104 StopWatching(); | 110 if (!wait_many_) |
| 111 StopWatching(); |
105 delegate->OnObjectSignaled(object); | 112 delegate->OnObjectSignaled(object); |
106 } | 113 } |
107 | 114 |
108 void ObjectWatcher::WillDestroyCurrentMessageLoop() { | 115 void ObjectWatcher::WillDestroyCurrentMessageLoop() { |
109 // Need to shutdown the watch so that we don't try to access the MessageLoop | 116 // Need to shutdown the watch so that we don't try to access the MessageLoop |
110 // after this point. | 117 // after this point. |
111 StopWatching(); | 118 StopWatching(); |
112 } | 119 } |
113 | 120 |
114 } // namespace win | 121 } // namespace win |
115 } // namespace base | 122 } // namespace base |
OLD | NEW |