blob: 4e6e57ab9f1e3b68927b6596e9a731ed44fd4953 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// 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.com6a4581b2008-08-02 06:04:03 +09004
5#include "base/object_watcher.h"
6
darin@google.com6a4581b2008-08-02 06:04:03 +09007#include "base/logging.h"
8
9namespace base {
10
11//-----------------------------------------------------------------------------
12
darin@google.com1efaea62008-08-09 17:28:45 +090013struct ObjectWatcher::Watch : public Task {
darin@google.com6a4581b2008-08-02 06:04:03 +090014 ObjectWatcher* watcher; // The associated ObjectWatcher instance
15 HANDLE object; // The object being watched
16 HANDLE wait_object; // Returned by RegisterWaitForSingleObject
darin@google.com1efaea62008-08-09 17:28:45 +090017 MessageLoop* origin_loop; // Used to get back to the origin thread
darin@google.com18f923a2008-08-05 02:46:47 +090018 Delegate* delegate; // Delegate to notify when signaled
darin@google.com6a4581b2008-08-02 06:04:03 +090019 bool did_signal; // DoneWaiting was called
20
darin@google.com1efaea62008-08-09 17:28:45 +090021 virtual void Run() {
darin@google.com6a4581b2008-08-02 06:04:03 +090022 // 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;
26
darin@google.com18f923a2008-08-05 02:46:47 +090027 DCHECK(did_signal);
28 watcher->StopWatching();
darin@google.com6a4581b2008-08-02 06:04:03 +090029
darin@google.com18f923a2008-08-05 02:46:47 +090030 delegate->OnObjectSignaled(object);
darin@google.com6a4581b2008-08-02 06:04:03 +090031 }
32};
33
34//-----------------------------------------------------------------------------
35
darin@google.com18f923a2008-08-05 02:46:47 +090036ObjectWatcher::ObjectWatcher() : watch_(NULL) {
darin@google.com6a4581b2008-08-02 06:04:03 +090037}
38
39ObjectWatcher::~ObjectWatcher() {
darin@google.com18f923a2008-08-05 02:46:47 +090040 StopWatching();
darin@google.com6a4581b2008-08-02 06:04:03 +090041}
42
darin@google.com18f923a2008-08-05 02:46:47 +090043bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
44 if (watch_) {
45 NOTREACHED() << "Already watching an object";
46 return false;
47 }
darin@google.com6a4581b2008-08-02 06:04:03 +090048
darin@google.com18f923a2008-08-05 02:46:47 +090049 Watch* watch = new Watch;
darin@google.com6a4581b2008-08-02 06:04:03 +090050 watch->watcher = this;
51 watch->object = object;
darin@google.com1efaea62008-08-09 17:28:45 +090052 watch->origin_loop = MessageLoop::current();
darin@google.com18f923a2008-08-05 02:46:47 +090053 watch->delegate = delegate;
darin@google.com6a4581b2008-08-02 06:04:03 +090054 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.com18f923a2008-08-05 02:46:47 +090061 watch, INFINITE, wait_flags)) {
darin@google.com6a4581b2008-08-02 06:04:03 +090062 NOTREACHED() << "RegisterWaitForSingleObject failed: " << GetLastError();
darin@google.com18f923a2008-08-05 02:46:47 +090063 delete watch;
darin@google.com6a4581b2008-08-02 06:04:03 +090064 return false;
65 }
66
darin@google.com18f923a2008-08-05 02:46:47 +090067 watch_ = watch;
darin@google.com965e5342008-08-06 08:16:41 +090068
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.com6a4581b2008-08-02 06:04:03 +090072 return true;
73}
74
darin@google.com18f923a2008-08-05 02:46:47 +090075bool ObjectWatcher::StopWatching() {
76 if (!watch_)
darin@google.com6a4581b2008-08-02 06:04:03 +090077 return false;
78
darin@google.com18f923a2008-08-05 02:46:47 +090079 // Make sure ObjectWatcher is used in a single-threaded fashion.
darin@google.com1efaea62008-08-09 17:28:45 +090080 DCHECK(watch_->origin_loop == MessageLoop::current());
darin@google.com6a4581b2008-08-02 06:04:03 +090081
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.com18f923a2008-08-05 02:46:47 +090084 if (!UnregisterWaitEx(watch_->wait_object, INVALID_HANDLE_VALUE)) {
darin@google.com6a4581b2008-08-02 06:04:03 +090085 NOTREACHED() << "UnregisterWaitEx failed: " << GetLastError();
86 return false;
87 }
88
darin@google.com18f923a2008-08-05 02:46:47 +090089 // 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.com6a4581b2008-08-02 06:04:03 +090093
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.com18f923a2008-08-05 02:46:47 +090096 watch_->watcher = NULL;
darin@google.com6a4581b2008-08-02 06:04:03 +090097
darin@google.com18f923a2008-08-05 02:46:47 +090098 // 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.com6a4581b2008-08-02 06:04:03 +0900103
darin@google.com18f923a2008-08-05 02:46:47 +0900104 watch_ = NULL;
darin@google.com965e5342008-08-06 08:16:41 +0900105
106 MessageLoop::current()->RemoveDestructionObserver(this);
darin@google.com6a4581b2008-08-02 06:04:03 +0900107 return true;
108}
109
110// static
111void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) {
112 DCHECK(!timed_out);
113
114 Watch* watch = static_cast<Watch*>(param);
115
116 // Record that we ran this function.
117 watch->did_signal = true;
118
darin@google.com1efaea62008-08-09 17:28:45 +0900119 // We rely on the locking in PostTask() to ensure that a memory barrier is
120 // provided, which in turn ensures our change to did_signal can be observed
121 // on the target thread.
122 watch->origin_loop->PostTask(FROM_HERE, watch);
darin@google.com6a4581b2008-08-02 06:04:03 +0900123}
124
darin@google.com965e5342008-08-06 08:16:41 +0900125void ObjectWatcher::WillDestroyCurrentMessageLoop() {
126 // Need to shutdown the watch so that we don't try to access the MessageLoop
127 // after this point.
128 StopWatching();
129}
130
darin@google.com6a4581b2008-08-02 06:04:03 +0900131} // namespace base
license.botf003cfe2008-08-24 09:55:55 +0900132