michael@0: // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #include "base/waitable_event_watcher.h" michael@0: michael@0: #include "base/condition_variable.h" michael@0: #include "base/lock.h" michael@0: #include "base/message_loop.h" michael@0: #include "base/waitable_event.h" michael@0: michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: namespace base { michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // WaitableEventWatcher (async waits). michael@0: // michael@0: // The basic design is that we add an AsyncWaiter to the wait-list of the event. michael@0: // That AsyncWaiter has a pointer to MessageLoop, and a Task to be posted to it. michael@0: // The MessageLoop ends up running the task, which calls the delegate. michael@0: // michael@0: // Since the wait can be canceled, we have a thread-safe Flag object which is michael@0: // set when the wait has been canceled. At each stage in the above, we check the michael@0: // flag before going onto the next stage. Since the wait may only be canceled in michael@0: // the MessageLoop which runs the Task, we are assured that the delegate cannot michael@0: // be called after canceling... michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // A thread-safe, reference-counted, write-once flag. michael@0: // ----------------------------------------------------------------------------- michael@0: class Flag : public RefCountedThreadSafe { michael@0: public: michael@0: Flag() { flag_ = false; } michael@0: michael@0: void Set() { michael@0: AutoLock locked(lock_); michael@0: flag_ = true; michael@0: } michael@0: michael@0: bool value() const { michael@0: AutoLock locked(lock_); michael@0: return flag_; michael@0: } michael@0: michael@0: private: michael@0: mutable Lock lock_; michael@0: bool flag_; michael@0: }; michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // This is an asynchronous waiter which posts a task to a MessageLoop when michael@0: // fired. An AsyncWaiter may only be in a single wait-list. michael@0: // ----------------------------------------------------------------------------- michael@0: class AsyncWaiter MOZ_FINAL : public WaitableEvent::Waiter { michael@0: public: michael@0: AsyncWaiter(MessageLoop* message_loop, Task* task, Flag* flag) michael@0: : message_loop_(message_loop), michael@0: cb_task_(task), michael@0: flag_(flag) { } michael@0: michael@0: bool Fire(WaitableEvent* event) { michael@0: if (flag_->value()) { michael@0: // If the callback has been canceled, we don't enqueue the task, we just michael@0: // delete it instead. michael@0: delete cb_task_; michael@0: } else { michael@0: message_loop_->PostTask(FROM_HERE, cb_task_); michael@0: } michael@0: michael@0: // We are removed from the wait-list by the WaitableEvent itself. It only michael@0: // remains to delete ourselves. michael@0: delete this; michael@0: michael@0: // We can always return true because an AsyncWaiter is never in two michael@0: // different wait-lists at the same time. michael@0: return true; michael@0: } michael@0: michael@0: // See StopWatching for discussion michael@0: bool Compare(void* tag) { michael@0: return tag == flag_.get(); michael@0: } michael@0: michael@0: private: michael@0: MessageLoop *const message_loop_; michael@0: Task *const cb_task_; michael@0: scoped_refptr flag_; michael@0: }; michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // For async waits we need to make a callback in a MessageLoop thread. We do michael@0: // this by posting this task, which calls the delegate and keeps track of when michael@0: // the event is canceled. michael@0: // ----------------------------------------------------------------------------- michael@0: class AsyncCallbackTask : public Task { michael@0: public: michael@0: AsyncCallbackTask(Flag* flag, WaitableEventWatcher::Delegate* delegate, michael@0: WaitableEvent* event) michael@0: : flag_(flag), michael@0: delegate_(delegate), michael@0: event_(event) { michael@0: } michael@0: michael@0: void Run() { michael@0: // Runs in MessageLoop thread. michael@0: if (!flag_->value()) { michael@0: // This is to let the WaitableEventWatcher know that the event has occured michael@0: // because it needs to be able to return NULL from GetWatchedObject michael@0: flag_->Set(); michael@0: delegate_->OnWaitableEventSignaled(event_); michael@0: } michael@0: michael@0: // We are deleted by the MessageLoop michael@0: } michael@0: michael@0: private: michael@0: scoped_refptr flag_; michael@0: WaitableEventWatcher::Delegate *const delegate_; michael@0: WaitableEvent *const event_; michael@0: }; michael@0: michael@0: WaitableEventWatcher::WaitableEventWatcher() michael@0: : event_(NULL), michael@0: message_loop_(NULL), michael@0: cancel_flag_(NULL), michael@0: callback_task_(NULL) { michael@0: } michael@0: michael@0: WaitableEventWatcher::~WaitableEventWatcher() { michael@0: StopWatching(); michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // The Handle is how the user cancels a wait. After deleting the Handle we michael@0: // insure that the delegate cannot be called. michael@0: // ----------------------------------------------------------------------------- michael@0: bool WaitableEventWatcher::StartWatching michael@0: (WaitableEvent* event, WaitableEventWatcher::Delegate* delegate) { michael@0: MessageLoop *const current_ml = MessageLoop::current(); michael@0: DCHECK(current_ml) << "Cannot create WaitableEventWatcher without a " michael@0: "current MessageLoop"; michael@0: michael@0: // A user may call StartWatching from within the callback function. In this michael@0: // case, we won't know that we have finished watching, expect that the Flag michael@0: // will have been set in AsyncCallbackTask::Run() michael@0: if (cancel_flag_.get() && cancel_flag_->value()) { michael@0: if (message_loop_) { michael@0: message_loop_->RemoveDestructionObserver(this); michael@0: message_loop_ = NULL; michael@0: } michael@0: michael@0: cancel_flag_ = NULL; michael@0: } michael@0: michael@0: DCHECK(!cancel_flag_.get()) << "StartWatching called while still watching"; michael@0: michael@0: cancel_flag_ = new Flag; michael@0: callback_task_ = new AsyncCallbackTask(cancel_flag_, delegate, event); michael@0: WaitableEvent::WaitableEventKernel* kernel = event->kernel_.get(); michael@0: michael@0: AutoLock locked(kernel->lock_); michael@0: michael@0: if (kernel->signaled_) { michael@0: if (!kernel->manual_reset_) michael@0: kernel->signaled_ = false; michael@0: michael@0: // No hairpinning - we can't call the delegate directly here. We have to michael@0: // enqueue a task on the MessageLoop as normal. michael@0: current_ml->PostTask(FROM_HERE, callback_task_); michael@0: return true; michael@0: } michael@0: michael@0: message_loop_ = current_ml; michael@0: current_ml->AddDestructionObserver(this); michael@0: michael@0: event_ = event; michael@0: kernel_ = kernel; michael@0: waiter_ = new AsyncWaiter(current_ml, callback_task_, cancel_flag_); michael@0: event->Enqueue(waiter_); michael@0: michael@0: return true; michael@0: } michael@0: michael@0: void WaitableEventWatcher::StopWatching() { michael@0: if (message_loop_) { michael@0: message_loop_->RemoveDestructionObserver(this); michael@0: message_loop_ = NULL; michael@0: } michael@0: michael@0: if (!cancel_flag_.get()) // if not currently watching... michael@0: return; michael@0: michael@0: if (cancel_flag_->value()) { michael@0: // In this case, the event has fired, but we haven't figured that out yet. michael@0: // The WaitableEvent may have been deleted too. michael@0: cancel_flag_ = NULL; michael@0: return; michael@0: } michael@0: michael@0: if (!kernel_.get()) { michael@0: // We have no kernel. This means that we never enqueued a Waiter on an michael@0: // event because the event was already signaled when StartWatching was michael@0: // called. michael@0: // michael@0: // In this case, a task was enqueued on the MessageLoop and will run. michael@0: // We set the flag in case the task hasn't yet run. The flag will stop the michael@0: // delegate getting called. If the task has run then we have the last michael@0: // reference to the flag and it will be deleted immedately after. michael@0: cancel_flag_->Set(); michael@0: cancel_flag_ = NULL; michael@0: return; michael@0: } michael@0: michael@0: AutoLock locked(kernel_->lock_); michael@0: // We have a lock on the kernel. No one else can signal the event while we michael@0: // have it. michael@0: michael@0: // We have a possible ABA issue here. If Dequeue was to compare only the michael@0: // pointer values then it's possible that the AsyncWaiter could have been michael@0: // fired, freed and the memory reused for a different Waiter which was michael@0: // enqueued in the same wait-list. We would think that that waiter was our michael@0: // AsyncWaiter and remove it. michael@0: // michael@0: // To stop this, Dequeue also takes a tag argument which is passed to the michael@0: // virtual Compare function before the two are considered a match. So we need michael@0: // a tag which is good for the lifetime of this handle: the Flag. Since we michael@0: // have a reference to the Flag, its memory cannot be reused while this object michael@0: // still exists. So if we find a waiter with the correct pointer value, and michael@0: // which shares a Flag pointer, we have a real match. michael@0: if (kernel_->Dequeue(waiter_, cancel_flag_.get())) { michael@0: // Case 2: the waiter hasn't been signaled yet; it was still on the wait michael@0: // list. We've removed it, thus we can delete it and the task (which cannot michael@0: // have been enqueued with the MessageLoop because the waiter was never michael@0: // signaled) michael@0: delete waiter_; michael@0: delete callback_task_; michael@0: cancel_flag_ = NULL; michael@0: return; michael@0: } michael@0: michael@0: // Case 3: the waiter isn't on the wait-list, thus it was signaled. It may michael@0: // not have run yet, so we set the flag to tell it not to bother enqueuing the michael@0: // task on the MessageLoop, but to delete it instead. The Waiter deletes michael@0: // itself once run. michael@0: cancel_flag_->Set(); michael@0: cancel_flag_ = NULL; michael@0: michael@0: // If the waiter has already run then the task has been enqueued. If the Task michael@0: // hasn't yet run, the flag will stop the delegate from getting called. (This michael@0: // is thread safe because one may only delete a Handle from the MessageLoop michael@0: // thread.) michael@0: // michael@0: // If the delegate has already been called then we have nothing to do. The michael@0: // task has been deleted by the MessageLoop. michael@0: } michael@0: michael@0: WaitableEvent* WaitableEventWatcher::GetWatchedEvent() { michael@0: if (!cancel_flag_.get()) michael@0: return NULL; michael@0: michael@0: if (cancel_flag_->value()) michael@0: return NULL; michael@0: michael@0: return event_; michael@0: } michael@0: michael@0: // ----------------------------------------------------------------------------- michael@0: // This is called when the MessageLoop which the callback will be run it is michael@0: // deleted. We need to cancel the callback as if we had been deleted, but we michael@0: // will still be deleted at some point in the future. michael@0: // ----------------------------------------------------------------------------- michael@0: void WaitableEventWatcher::WillDestroyCurrentMessageLoop() { michael@0: StopWatching(); michael@0: } michael@0: michael@0: } // namespace base