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