summaryrefslogtreecommitdiffstats
path: root/base/win
diff options
context:
space:
mode:
authordcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-29 06:22:25 +0000
committerdcheng@chromium.org <dcheng@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-11-29 06:22:25 +0000
commit5e3f88d646ba60504dfd55099e82a4e29caea3af (patch)
tree02b0d4513f07772fec1623ea15330235fa026767 /base/win
parentb68b1197c9116104118b1b3e7563e2c600c4e621 (diff)
downloadchromium_src-5e3f88d646ba60504dfd55099e82a4e29caea3af.zip
chromium_src-5e3f88d646ba60504dfd55099e82a4e29caea3af.tar.gz
chromium_src-5e3f88d646ba60504dfd55099e82a4e29caea3af.tar.bz2
Remove several more custom Task implementations from base/
BUG=none TEST=trybots Committed: http://src.chromium.org/viewvc/chrome?view=rev&revision=111865 Review URL: http://codereview.chromium.org/8702016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@111879 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/win')
-rw-r--r--base/win/object_watcher.cc110
-rw-r--r--base/win/object_watcher.h11
2 files changed, 49 insertions, 72 deletions
diff --git a/base/win/object_watcher.cc b/base/win/object_watcher.cc
index 4f5e7ab..6b500cd 100644
--- a/base/win/object_watcher.cc
+++ b/base/win/object_watcher.cc
@@ -4,6 +4,7 @@
#include "base/win/object_watcher.h"
+#include "base/bind.h"
#include "base/logging.h"
namespace base {
@@ -11,30 +12,11 @@ namespace win {
//-----------------------------------------------------------------------------
-struct ObjectWatcher::Watch : public Task {
- ObjectWatcher* watcher; // The associated ObjectWatcher instance
- HANDLE object; // The object being watched
- HANDLE wait_object; // Returned by RegisterWaitForSingleObject
- MessageLoop* origin_loop; // Used to get back to the origin thread
- Delegate* delegate; // Delegate to notify when signaled
- bool did_signal; // DoneWaiting was called
-
- virtual void Run() {
- // The watcher may have already been torn down, in which case we need to
- // just get out of dodge.
- if (!watcher)
- return;
-
- DCHECK(did_signal);
- watcher->StopWatching();
-
- delegate->OnObjectSignaled(object);
- }
-};
-
-//-----------------------------------------------------------------------------
-
-ObjectWatcher::ObjectWatcher() : watch_(NULL) {
+ObjectWatcher::ObjectWatcher()
+ : weak_factory_(this),
+ object_(NULL),
+ wait_object_(NULL),
+ origin_loop_(NULL) {
}
ObjectWatcher::~ObjectWatcher() {
@@ -42,31 +24,30 @@ ObjectWatcher::~ObjectWatcher() {
}
bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
- if (watch_) {
+ if (wait_object_) {
NOTREACHED() << "Already watching an object";
return false;
}
- Watch* watch = new Watch;
- watch->watcher = this;
- watch->object = object;
- watch->origin_loop = MessageLoop::current();
- watch->delegate = delegate;
- watch->did_signal = false;
-
// Since our job is to just notice when an object is signaled and report the
// result back to this thread, we can just run on a Windows wait thread.
DWORD wait_flags = WT_EXECUTEINWAITTHREAD | WT_EXECUTEONLYONCE;
- if (!RegisterWaitForSingleObject(&watch->wait_object, object, DoneWaiting,
- watch, INFINITE, wait_flags)) {
+ // DoneWaiting can be synchronously called from RegisterWaitForSingleObject,
+ // so set up all state now.
+ callback_ = base::Bind(&ObjectWatcher::Signal, weak_factory_.GetWeakPtr(),
+ delegate);
+ object_ = object;
+ origin_loop_ = MessageLoop::current();
+
+ if (!RegisterWaitForSingleObject(&wait_object_, object, DoneWaiting,
+ this, INFINITE, wait_flags)) {
NOTREACHED() << "RegisterWaitForSingleObject failed: " << GetLastError();
- delete watch;
+ object_ = NULL;
+ wait_object_ = NULL;
return false;
}
- watch_ = watch;
-
// We need to know if the current message loop is going away so we can
// prevent the wait thread from trying to access a dead message loop.
MessageLoop::current()->AddDestructionObserver(this);
@@ -74,60 +55,49 @@ bool ObjectWatcher::StartWatching(HANDLE object, Delegate* delegate) {
}
bool ObjectWatcher::StopWatching() {
- if (!watch_)
+ if (!wait_object_)
return false;
// Make sure ObjectWatcher is used in a single-threaded fashion.
- DCHECK(watch_->origin_loop == MessageLoop::current());
+ DCHECK(origin_loop_ == MessageLoop::current());
- // If DoneWaiting is in progress, we wait for it to finish. We know whether
- // DoneWaiting happened or not by inspecting the did_signal flag.
- if (!UnregisterWaitEx(watch_->wait_object, INVALID_HANDLE_VALUE)) {
+ // Blocking call to cancel the wait. Any callbacks already in progress will
+ // finish before we return from this call.
+ if (!UnregisterWaitEx(wait_object_, INVALID_HANDLE_VALUE)) {
NOTREACHED() << "UnregisterWaitEx failed: " << GetLastError();
return false;
}
- // Make sure that we see any mutation to did_signal. This should be a no-op
- // since we expect that UnregisterWaitEx resulted in a memory barrier, but
- // just to be sure, we're going to be explicit.
- MemoryBarrier();
-
- // If the watch has been posted, then we need to make sure it knows not to do
- // anything once it is run.
- watch_->watcher = NULL;
-
- // If DoneWaiting was called, then the watch would have been posted as a
- // task, and will therefore be deleted by the MessageLoop. Otherwise, we
- // need to take care to delete it here.
- if (!watch_->did_signal)
- delete watch_;
-
- watch_ = NULL;
+ weak_factory_.InvalidateWeakPtrs();
+ object_ = NULL;
+ wait_object_ = NULL;
MessageLoop::current()->RemoveDestructionObserver(this);
return true;
}
HANDLE ObjectWatcher::GetWatchedObject() {
- if (!watch_)
- return NULL;
-
- return watch_->object;
+ return object_;
}
// static
void CALLBACK ObjectWatcher::DoneWaiting(void* param, BOOLEAN timed_out) {
DCHECK(!timed_out);
- Watch* watch = static_cast<Watch*>(param);
-
- // Record that we ran this function.
- watch->did_signal = true;
+ // The destructor blocks on any callbacks that are in flight, so we know that
+ // that is always a pointer to a valid ObjectWater.
+ ObjectWatcher* that = static_cast<ObjectWatcher*>(param);
+ that->origin_loop_->PostTask(FROM_HERE, that->callback_);
+ that->callback_.Reset();
+}
- // We rely on the locking in PostTask() to ensure that a memory barrier is
- // provided, which in turn ensures our change to did_signal can be observed
- // on the target thread.
- watch->origin_loop->PostTask(FROM_HERE, watch);
+void ObjectWatcher::Signal(Delegate* delegate) {
+ // Signaling the delegate may result in our destruction or a nested call to
+ // StartWatching(). As a result, we save any state we need and clear previous
+ // watcher state before signaling the delegate.
+ HANDLE object = object_;
+ StopWatching();
+ delegate->OnObjectSignaled(object);
}
void ObjectWatcher::WillDestroyCurrentMessageLoop() {
diff --git a/base/win/object_watcher.h b/base/win/object_watcher.h
index f5a46eb..2a515a1 100644
--- a/base/win/object_watcher.h
+++ b/base/win/object_watcher.h
@@ -9,6 +9,8 @@
#include <windows.h>
#include "base/base_export.h"
+#include "base/callback.h"
+#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
namespace base {
@@ -79,12 +81,17 @@ class BASE_EXPORT ObjectWatcher : public MessageLoop::DestructionObserver {
// Called on a background thread when done waiting.
static void CALLBACK DoneWaiting(void* param, BOOLEAN timed_out);
+ void Signal(Delegate* delegate);
+
// MessageLoop::DestructionObserver implementation:
virtual void WillDestroyCurrentMessageLoop();
// Internal state.
- struct Watch;
- Watch* watch_;
+ WeakPtrFactory<ObjectWatcher> weak_factory_;
+ Closure callback_;
+ HANDLE object_; // The object being watched
+ HANDLE wait_object_; // Returned by RegisterWaitForSingleObject
+ MessageLoop* origin_loop_; // Used to get back to the origin thread
DISALLOW_COPY_AND_ASSIGN(ObjectWatcher);
};