Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #include "base/waitable_event_watcher.h" |
michael@0 | 6 | |
michael@0 | 7 | #include "base/condition_variable.h" |
michael@0 | 8 | #include "base/lock.h" |
michael@0 | 9 | #include "base/message_loop.h" |
michael@0 | 10 | #include "base/waitable_event.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "mozilla/Attributes.h" |
michael@0 | 13 | |
michael@0 | 14 | namespace base { |
michael@0 | 15 | |
michael@0 | 16 | // ----------------------------------------------------------------------------- |
michael@0 | 17 | // WaitableEventWatcher (async waits). |
michael@0 | 18 | // |
michael@0 | 19 | // The basic design is that we add an AsyncWaiter to the wait-list of the event. |
michael@0 | 20 | // That AsyncWaiter has a pointer to MessageLoop, and a Task to be posted to it. |
michael@0 | 21 | // The MessageLoop ends up running the task, which calls the delegate. |
michael@0 | 22 | // |
michael@0 | 23 | // Since the wait can be canceled, we have a thread-safe Flag object which is |
michael@0 | 24 | // set when the wait has been canceled. At each stage in the above, we check the |
michael@0 | 25 | // flag before going onto the next stage. Since the wait may only be canceled in |
michael@0 | 26 | // the MessageLoop which runs the Task, we are assured that the delegate cannot |
michael@0 | 27 | // be called after canceling... |
michael@0 | 28 | |
michael@0 | 29 | // ----------------------------------------------------------------------------- |
michael@0 | 30 | // A thread-safe, reference-counted, write-once flag. |
michael@0 | 31 | // ----------------------------------------------------------------------------- |
michael@0 | 32 | class Flag : public RefCountedThreadSafe<Flag> { |
michael@0 | 33 | public: |
michael@0 | 34 | Flag() { flag_ = false; } |
michael@0 | 35 | |
michael@0 | 36 | void Set() { |
michael@0 | 37 | AutoLock locked(lock_); |
michael@0 | 38 | flag_ = true; |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | bool value() const { |
michael@0 | 42 | AutoLock locked(lock_); |
michael@0 | 43 | return flag_; |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | private: |
michael@0 | 47 | mutable Lock lock_; |
michael@0 | 48 | bool flag_; |
michael@0 | 49 | }; |
michael@0 | 50 | |
michael@0 | 51 | // ----------------------------------------------------------------------------- |
michael@0 | 52 | // This is an asynchronous waiter which posts a task to a MessageLoop when |
michael@0 | 53 | // fired. An AsyncWaiter may only be in a single wait-list. |
michael@0 | 54 | // ----------------------------------------------------------------------------- |
michael@0 | 55 | class AsyncWaiter MOZ_FINAL : public WaitableEvent::Waiter { |
michael@0 | 56 | public: |
michael@0 | 57 | AsyncWaiter(MessageLoop* message_loop, Task* task, Flag* flag) |
michael@0 | 58 | : message_loop_(message_loop), |
michael@0 | 59 | cb_task_(task), |
michael@0 | 60 | flag_(flag) { } |
michael@0 | 61 | |
michael@0 | 62 | bool Fire(WaitableEvent* event) { |
michael@0 | 63 | if (flag_->value()) { |
michael@0 | 64 | // If the callback has been canceled, we don't enqueue the task, we just |
michael@0 | 65 | // delete it instead. |
michael@0 | 66 | delete cb_task_; |
michael@0 | 67 | } else { |
michael@0 | 68 | message_loop_->PostTask(FROM_HERE, cb_task_); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | // We are removed from the wait-list by the WaitableEvent itself. It only |
michael@0 | 72 | // remains to delete ourselves. |
michael@0 | 73 | delete this; |
michael@0 | 74 | |
michael@0 | 75 | // We can always return true because an AsyncWaiter is never in two |
michael@0 | 76 | // different wait-lists at the same time. |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | // See StopWatching for discussion |
michael@0 | 81 | bool Compare(void* tag) { |
michael@0 | 82 | return tag == flag_.get(); |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | private: |
michael@0 | 86 | MessageLoop *const message_loop_; |
michael@0 | 87 | Task *const cb_task_; |
michael@0 | 88 | scoped_refptr<Flag> flag_; |
michael@0 | 89 | }; |
michael@0 | 90 | |
michael@0 | 91 | // ----------------------------------------------------------------------------- |
michael@0 | 92 | // For async waits we need to make a callback in a MessageLoop thread. We do |
michael@0 | 93 | // this by posting this task, which calls the delegate and keeps track of when |
michael@0 | 94 | // the event is canceled. |
michael@0 | 95 | // ----------------------------------------------------------------------------- |
michael@0 | 96 | class AsyncCallbackTask : public Task { |
michael@0 | 97 | public: |
michael@0 | 98 | AsyncCallbackTask(Flag* flag, WaitableEventWatcher::Delegate* delegate, |
michael@0 | 99 | WaitableEvent* event) |
michael@0 | 100 | : flag_(flag), |
michael@0 | 101 | delegate_(delegate), |
michael@0 | 102 | event_(event) { |
michael@0 | 103 | } |
michael@0 | 104 | |
michael@0 | 105 | void Run() { |
michael@0 | 106 | // Runs in MessageLoop thread. |
michael@0 | 107 | if (!flag_->value()) { |
michael@0 | 108 | // This is to let the WaitableEventWatcher know that the event has occured |
michael@0 | 109 | // because it needs to be able to return NULL from GetWatchedObject |
michael@0 | 110 | flag_->Set(); |
michael@0 | 111 | delegate_->OnWaitableEventSignaled(event_); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | // We are deleted by the MessageLoop |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | private: |
michael@0 | 118 | scoped_refptr<Flag> flag_; |
michael@0 | 119 | WaitableEventWatcher::Delegate *const delegate_; |
michael@0 | 120 | WaitableEvent *const event_; |
michael@0 | 121 | }; |
michael@0 | 122 | |
michael@0 | 123 | WaitableEventWatcher::WaitableEventWatcher() |
michael@0 | 124 | : event_(NULL), |
michael@0 | 125 | message_loop_(NULL), |
michael@0 | 126 | cancel_flag_(NULL), |
michael@0 | 127 | callback_task_(NULL) { |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | WaitableEventWatcher::~WaitableEventWatcher() { |
michael@0 | 131 | StopWatching(); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | // ----------------------------------------------------------------------------- |
michael@0 | 135 | // The Handle is how the user cancels a wait. After deleting the Handle we |
michael@0 | 136 | // insure that the delegate cannot be called. |
michael@0 | 137 | // ----------------------------------------------------------------------------- |
michael@0 | 138 | bool WaitableEventWatcher::StartWatching |
michael@0 | 139 | (WaitableEvent* event, WaitableEventWatcher::Delegate* delegate) { |
michael@0 | 140 | MessageLoop *const current_ml = MessageLoop::current(); |
michael@0 | 141 | DCHECK(current_ml) << "Cannot create WaitableEventWatcher without a " |
michael@0 | 142 | "current MessageLoop"; |
michael@0 | 143 | |
michael@0 | 144 | // A user may call StartWatching from within the callback function. In this |
michael@0 | 145 | // case, we won't know that we have finished watching, expect that the Flag |
michael@0 | 146 | // will have been set in AsyncCallbackTask::Run() |
michael@0 | 147 | if (cancel_flag_.get() && cancel_flag_->value()) { |
michael@0 | 148 | if (message_loop_) { |
michael@0 | 149 | message_loop_->RemoveDestructionObserver(this); |
michael@0 | 150 | message_loop_ = NULL; |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | cancel_flag_ = NULL; |
michael@0 | 154 | } |
michael@0 | 155 | |
michael@0 | 156 | DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; |
michael@0 | 157 | |
michael@0 | 158 | cancel_flag_ = new Flag; |
michael@0 | 159 | callback_task_ = new AsyncCallbackTask(cancel_flag_, delegate, event); |
michael@0 | 160 | WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); |
michael@0 | 161 | |
michael@0 | 162 | AutoLock locked(kernel->lock_); |
michael@0 | 163 | |
michael@0 | 164 | if (kernel->signaled_) { |
michael@0 | 165 | if (!kernel->manual_reset_) |
michael@0 | 166 | kernel->signaled_ = false; |
michael@0 | 167 | |
michael@0 | 168 | // No hairpinning - we can't call the delegate directly here. We have to |
michael@0 | 169 | // enqueue a task on the MessageLoop as normal. |
michael@0 | 170 | current_ml->PostTask(FROM_HERE, callback_task_); |
michael@0 | 171 | return true; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | message_loop_ = current_ml; |
michael@0 | 175 | current_ml->AddDestructionObserver(this); |
michael@0 | 176 | |
michael@0 | 177 | event_ = event; |
michael@0 | 178 | kernel_ = kernel; |
michael@0 | 179 | waiter_ = new AsyncWaiter(current_ml, callback_task_, cancel_flag_); |
michael@0 | 180 | event->Enqueue(waiter_); |
michael@0 | 181 | |
michael@0 | 182 | return true; |
michael@0 | 183 | } |
michael@0 | 184 | |
michael@0 | 185 | void WaitableEventWatcher::StopWatching() { |
michael@0 | 186 | if (message_loop_) { |
michael@0 | 187 | message_loop_->RemoveDestructionObserver(this); |
michael@0 | 188 | message_loop_ = NULL; |
michael@0 | 189 | } |
michael@0 | 190 | |
michael@0 | 191 | if (!cancel_flag_.get()) // if not currently watching... |
michael@0 | 192 | return; |
michael@0 | 193 | |
michael@0 | 194 | if (cancel_flag_->value()) { |
michael@0 | 195 | // In this case, the event has fired, but we haven't figured that out yet. |
michael@0 | 196 | // The WaitableEvent may have been deleted too. |
michael@0 | 197 | cancel_flag_ = NULL; |
michael@0 | 198 | return; |
michael@0 | 199 | } |
michael@0 | 200 | |
michael@0 | 201 | if (!kernel_.get()) { |
michael@0 | 202 | // We have no kernel. This means that we never enqueued a Waiter on an |
michael@0 | 203 | // event because the event was already signaled when StartWatching was |
michael@0 | 204 | // called. |
michael@0 | 205 | // |
michael@0 | 206 | // In this case, a task was enqueued on the MessageLoop and will run. |
michael@0 | 207 | // We set the flag in case the task hasn't yet run. The flag will stop the |
michael@0 | 208 | // delegate getting called. If the task has run then we have the last |
michael@0 | 209 | // reference to the flag and it will be deleted immedately after. |
michael@0 | 210 | cancel_flag_->Set(); |
michael@0 | 211 | cancel_flag_ = NULL; |
michael@0 | 212 | return; |
michael@0 | 213 | } |
michael@0 | 214 | |
michael@0 | 215 | AutoLock locked(kernel_->lock_); |
michael@0 | 216 | // We have a lock on the kernel. No one else can signal the event while we |
michael@0 | 217 | // have it. |
michael@0 | 218 | |
michael@0 | 219 | // We have a possible ABA issue here. If Dequeue was to compare only the |
michael@0 | 220 | // pointer values then it's possible that the AsyncWaiter could have been |
michael@0 | 221 | // fired, freed and the memory reused for a different Waiter which was |
michael@0 | 222 | // enqueued in the same wait-list. We would think that that waiter was our |
michael@0 | 223 | // AsyncWaiter and remove it. |
michael@0 | 224 | // |
michael@0 | 225 | // To stop this, Dequeue also takes a tag argument which is passed to the |
michael@0 | 226 | // virtual Compare function before the two are considered a match. So we need |
michael@0 | 227 | // a tag which is good for the lifetime of this handle: the Flag. Since we |
michael@0 | 228 | // have a reference to the Flag, its memory cannot be reused while this object |
michael@0 | 229 | // still exists. So if we find a waiter with the correct pointer value, and |
michael@0 | 230 | // which shares a Flag pointer, we have a real match. |
michael@0 | 231 | if (kernel_->Dequeue(waiter_, cancel_flag_.get())) { |
michael@0 | 232 | // Case 2: the waiter hasn't been signaled yet; it was still on the wait |
michael@0 | 233 | // list. We've removed it, thus we can delete it and the task (which cannot |
michael@0 | 234 | // have been enqueued with the MessageLoop because the waiter was never |
michael@0 | 235 | // signaled) |
michael@0 | 236 | delete waiter_; |
michael@0 | 237 | delete callback_task_; |
michael@0 | 238 | cancel_flag_ = NULL; |
michael@0 | 239 | return; |
michael@0 | 240 | } |
michael@0 | 241 | |
michael@0 | 242 | // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may |
michael@0 | 243 | // not have run yet, so we set the flag to tell it not to bother enqueuing the |
michael@0 | 244 | // task on the MessageLoop, but to delete it instead. The Waiter deletes |
michael@0 | 245 | // itself once run. |
michael@0 | 246 | cancel_flag_->Set(); |
michael@0 | 247 | cancel_flag_ = NULL; |
michael@0 | 248 | |
michael@0 | 249 | // If the waiter has already run then the task has been enqueued. If the Task |
michael@0 | 250 | // hasn't yet run, the flag will stop the delegate from getting called. (This |
michael@0 | 251 | // is thread safe because one may only delete a Handle from the MessageLoop |
michael@0 | 252 | // thread.) |
michael@0 | 253 | // |
michael@0 | 254 | // If the delegate has already been called then we have nothing to do. The |
michael@0 | 255 | // task has been deleted by the MessageLoop. |
michael@0 | 256 | } |
michael@0 | 257 | |
michael@0 | 258 | WaitableEvent* WaitableEventWatcher::GetWatchedEvent() { |
michael@0 | 259 | if (!cancel_flag_.get()) |
michael@0 | 260 | return NULL; |
michael@0 | 261 | |
michael@0 | 262 | if (cancel_flag_->value()) |
michael@0 | 263 | return NULL; |
michael@0 | 264 | |
michael@0 | 265 | return event_; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | // ----------------------------------------------------------------------------- |
michael@0 | 269 | // This is called when the MessageLoop which the callback will be run it is |
michael@0 | 270 | // deleted. We need to cancel the callback as if we had been deleted, but we |
michael@0 | 271 | // will still be deleted at some point in the future. |
michael@0 | 272 | // ----------------------------------------------------------------------------- |
michael@0 | 273 | void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { |
michael@0 | 274 | StopWatching(); |
michael@0 | 275 | } |
michael@0 | 276 | |
michael@0 | 277 | } // namespace base |