ipc/chromium/src/base/object_watcher.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

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/object_watcher.h"
michael@0 6
michael@0 7 #include "base/logging.h"
michael@0 8
michael@0 9 namespace base {
michael@0 10
michael@0 11 //-----------------------------------------------------------------------------
michael@0 12
michael@0 13 struct ObjectWatcher::Watch : public Task {
michael@0 14 ObjectWatcher* watcher; // The associated ObjectWatcher instance
michael@0 15 HANDLE object; // The object being watched
michael@0 16 HANDLE wait_object; // Returned by RegisterWaitForSingleObject
michael@0 17 MessageLoop* origin_loop; // Used to get back to the origin thread
michael@0 18 Delegate* delegate; // Delegate to notify when signaled
michael@0 19 bool did_signal; // DoneWaiting was called
michael@0 20
michael@0 21 virtual void Run() {
michael@0 22 // The watcher may have already been torn down, in which case we need to
michael@0 23 // just get out of dodge.
michael@0 24 if (!watcher)
michael@0 25 return;
michael@0 26
michael@0 27 DCHECK(did_signal);
michael@0 28 watcher->StopWatching();
michael@0 29
michael@0 30 delegate->OnObjectSignaled(object);
michael@0 31 }
michael@0 32 };
michael@0 33
michael@0 34 //-----------------------------------------------------------------------------
michael@0 35
michael@0 36 ObjectWatcher::ObjectWatcher() : watch_(NULL) {
michael@0 37 }
michael@0 38
michael@0 39 ObjectWatcher::~ObjectWatcher() {
michael@0 40 StopWatching();
michael@0 41 }
michael@0 42
michael@0 43 bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
michael@0 44 if (watch_) {
michael@0 45 NOTREACHED() << "Already watching an object";
michael@0 46 return false;
michael@0 47 }
michael@0 48
michael@0 49 Watch* watch = new Watch;
michael@0 50 watch->watcher = this;
michael@0 51 watch->object = object;
michael@0 52 watch->origin_loop = MessageLoop::current();
michael@0 53 watch->delegate = delegate;
michael@0 54 watch->did_signal = false;
michael@0 55
michael@0 56 // Since our job is to just notice when an object is signaled and report the
michael@0 57 // result back to this thread, we can just run on a Windows wait thread.
michael@0 58 DWORD wait_flags = WT_EXECUTEDEFAULT | WT_EXECUTEONLYONCE;
michael@0 59
michael@0 60 if (!RegisterWaitForSingleObject(&watch->wait_object, object, DoneWaiting,
michael@0 61 watch, INFINITE, wait_flags)) {
michael@0 62 NOTREACHED() << "RegisterWaitForSingleObject failed: " << GetLastError();
michael@0 63 delete watch;
michael@0 64 return false;
michael@0 65 }
michael@0 66
michael@0 67 watch_ = watch;
michael@0 68
michael@0 69 // We need to know if the current message loop is going away so we can
michael@0 70 // prevent the wait thread from trying to access a dead message loop.
michael@0 71 MessageLoop::current()->AddDestructionObserver(this);
michael@0 72 return true;
michael@0 73 }
michael@0 74
michael@0 75 bool ObjectWatcher::StopWatching() {
michael@0 76 if (!watch_)
michael@0 77 return false;
michael@0 78
michael@0 79 // Make sure ObjectWatcher is used in a single-threaded fashion.
michael@0 80 DCHECK(watch_->origin_loop == MessageLoop::current());
michael@0 81
michael@0 82 // If DoneWaiting is in progress, we wait for it to finish. We know whether
michael@0 83 // DoneWaiting happened or not by inspecting the did_signal flag.
michael@0 84 if (!UnregisterWaitEx(watch_->wait_object, INVALID_HANDLE_VALUE)) {
michael@0 85 NOTREACHED() << "UnregisterWaitEx failed: " << GetLastError();
michael@0 86 return false;
michael@0 87 }
michael@0 88
michael@0 89 // Make sure that we see any mutation to did_signal. This should be a no-op
michael@0 90 // since we expect that UnregisterWaitEx resulted in a memory barrier, but
michael@0 91 // just to be sure, we're going to be explicit.
michael@0 92 MemoryBarrier();
michael@0 93
michael@0 94 // If the watch has been posted, then we need to make sure it knows not to do
michael@0 95 // anything once it is run.
michael@0 96 watch_->watcher = NULL;
michael@0 97
michael@0 98 // If DoneWaiting was called, then the watch would have been posted as a
michael@0 99 // task, and will therefore be deleted by the MessageLoop. Otherwise, we
michael@0 100 // need to take care to delete it here.
michael@0 101 if (!watch_->did_signal)
michael@0 102 delete watch_;
michael@0 103
michael@0 104 watch_ = NULL;
michael@0 105
michael@0 106 MessageLoop::current()->RemoveDestructionObserver(this);
michael@0 107 return true;
michael@0 108 }
michael@0 109
michael@0 110 HANDLE ObjectWatcher::GetWatchedObject() {
michael@0 111 if (!watch_)
michael@0 112 return NULL;
michael@0 113
michael@0 114 return watch_->object;
michael@0 115 }
michael@0 116
michael@0 117 // static
michael@0 118 void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) {
michael@0 119 DCHECK(!timed_out);
michael@0 120
michael@0 121 Watch* watch = static_cast<Watch*>(param);
michael@0 122
michael@0 123 // Record that we ran this function.
michael@0 124 watch->did_signal = true;
michael@0 125
michael@0 126 // We rely on the locking in PostTask() to ensure that a memory barrier is
michael@0 127 // provided, which in turn ensures our change to did_signal can be observed
michael@0 128 // on the target thread.
michael@0 129 watch->origin_loop->PostTask(FROM_HERE, watch);
michael@0 130 }
michael@0 131
michael@0 132 void ObjectWatcher::WillDestroyCurrentMessageLoop() {
michael@0 133 // Need to shutdown the watch so that we don't try to access the MessageLoop
michael@0 134 // after this point.
michael@0 135 StopWatching();
michael@0 136 }
michael@0 137
michael@0 138 } // namespace base

mercurial