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/object_watcher.h" michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: namespace base { michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: struct ObjectWatcher::Watch : public Task { michael@0: ObjectWatcher* watcher; // The associated ObjectWatcher instance michael@0: HANDLE object; // The object being watched michael@0: HANDLE wait_object; // Returned by RegisterWaitForSingleObject michael@0: MessageLoop* origin_loop; // Used to get back to the origin thread michael@0: Delegate* delegate; // Delegate to notify when signaled michael@0: bool did_signal; // DoneWaiting was called michael@0: michael@0: virtual void Run() { michael@0: // The watcher may have already been torn down, in which case we need to michael@0: // just get out of dodge. michael@0: if (!watcher) michael@0: return; michael@0: michael@0: DCHECK(did_signal); michael@0: watcher->StopWatching(); michael@0: michael@0: delegate->OnObjectSignaled(object); michael@0: } michael@0: }; michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: ObjectWatcher::ObjectWatcher() : watch_(NULL) { michael@0: } michael@0: michael@0: ObjectWatcher::~ObjectWatcher() { michael@0: StopWatching(); michael@0: } michael@0: michael@0: bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) { michael@0: if (watch_) { michael@0: NOTREACHED() << "Already watching an object"; michael@0: return false; michael@0: } michael@0: michael@0: Watch* watch = new Watch; michael@0: watch->watcher = this; michael@0: watch->object = object; michael@0: watch->origin_loop = MessageLoop::current(); michael@0: watch->delegate = delegate; michael@0: watch->did_signal = false; michael@0: michael@0: // Since our job is to just notice when an object is signaled and report the michael@0: // result back to this thread, we can just run on a Windows wait thread. michael@0: DWORD wait_flags = WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE; michael@0: michael@0: if (!RegisterWaitForSingleObject(&watch->wait_object, object, DoneWaiting, michael@0: watch, INFINITE, wait_flags)) { michael@0: NOTREACHED() << "RegisterWaitForSingleObject failed: " << GetLastError(); michael@0: delete watch; michael@0: return false; michael@0: } michael@0: michael@0: watch_ = watch; michael@0: michael@0: // We need to know if the current message loop is going away so we can michael@0: // prevent the wait thread from trying to access a dead message loop. michael@0: MessageLoop::current()->AddDestructionObserver(this); michael@0: return true; michael@0: } michael@0: michael@0: bool ObjectWatcher::StopWatching() { michael@0: if (!watch_) michael@0: return false; michael@0: michael@0: // Make sure ObjectWatcher is used in a single-threaded fashion. michael@0: DCHECK(watch_->origin_loop == MessageLoop::current()); michael@0: michael@0: // If DoneWaiting is in progress, we wait for it to finish. We know whether michael@0: // DoneWaiting happened or not by inspecting the did_signal flag. michael@0: if (!UnregisterWaitEx(watch_->wait_object, INVALID_HANDLE_VALUE)) { michael@0: NOTREACHED() << "UnregisterWaitEx failed: " << GetLastError(); michael@0: return false; michael@0: } michael@0: michael@0: // Make sure that we see any mutation to did_signal. This should be a no-op michael@0: // since we expect that UnregisterWaitEx resulted in a memory barrier, but michael@0: // just to be sure, we're going to be explicit. michael@0: MemoryBarrier(); michael@0: michael@0: // If the watch has been posted, then we need to make sure it knows not to do michael@0: // anything once it is run. michael@0: watch_->watcher = NULL; michael@0: michael@0: // If DoneWaiting was called, then the watch would have been posted as a michael@0: // task, and will therefore be deleted by the MessageLoop. Otherwise, we michael@0: // need to take care to delete it here. michael@0: if (!watch_->did_signal) michael@0: delete watch_; michael@0: michael@0: watch_ = NULL; michael@0: michael@0: MessageLoop::current()->RemoveDestructionObserver(this); michael@0: return true; michael@0: } michael@0: michael@0: HANDLE ObjectWatcher::GetWatchedObject() { michael@0: if (!watch_) michael@0: return NULL; michael@0: michael@0: return watch_->object; michael@0: } michael@0: michael@0: // static michael@0: void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) { michael@0: DCHECK(!timed_out); michael@0: michael@0: Watch* watch = static_cast(param); michael@0: michael@0: // Record that we ran this function. michael@0: watch->did_signal = true; michael@0: michael@0: // We rely on the locking in PostTask() to ensure that a memory barrier is michael@0: // provided, which in turn ensures our change to did_signal can be observed michael@0: // on the target thread. michael@0: watch->origin_loop->PostTask(FROM_HERE, watch); michael@0: } michael@0: michael@0: void ObjectWatcher::WillDestroyCurrentMessageLoop() { michael@0: // Need to shutdown the watch so that we don't try to access the MessageLoop michael@0: // after this point. michael@0: StopWatching(); michael@0: } michael@0: michael@0: } // namespace base