From b49054718ac2c49f629427c41a4ff2a6ba4bdb09 Mon Sep 17 00:00:00 2001 From: "darin@google.com" Date: Mon, 4 Aug 2008 17:46:47 +0000 Subject: Revise the ObjectWatcher API to be one-to-one with the object being watched. This greatly simplifies the implementation and API. Now consumers can use ObjectWatcher as a "smart pointer" class, which automatically cleans-up after itself when it goes out of scope. I also switched away from the Task based API to one that is more compatible with MessageLoop::WatchObject. That allows me to make minimal changes to existing code, and it also means that consumers do not have to allocate Task objects to use this API. In this CL, I included changes to make a couple consumers use ObjectWatcher instead of ML::WatchObject. git-svn-id: svn://svn.chromium.org/chrome/trunk/src@322 0039d316-1c4b-4281-b951-d872f2087c98 --- base/thread.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'base/thread.cc') diff --git a/base/thread.cc b/base/thread.cc index cec558b..25379e1 100644 --- a/base/thread.cc +++ b/base/thread.cc @@ -33,10 +33,13 @@ #include "base/thread.h" #include "base/message_loop.h" +#include "base/object_watcher.h" #include "base/ref_counted.h" #include "base/string_util.h" #include "base/win_util.h" +namespace { + // This class is used when starting a thread. It passes information to the // thread function. It is referenced counted so we can cleanup the event // object used to synchronize thread startup properly. @@ -65,20 +68,15 @@ class ThreadQuitTask : public Task { }; // Once an object is signaled, quits the current inner message loop. -class QuitOnSignal : public MessageLoop::Watcher { +class QuitOnSignal : public base::ObjectWatcher::Delegate { public: - explicit QuitOnSignal(HANDLE signal) : signal_(signal) { - } virtual void OnObjectSignaled(HANDLE object) { - DCHECK_EQ(object, signal_); - MessageLoop::current()->WatchObject(signal_, NULL); MessageLoop::current()->Quit(); } - private: - HANDLE signal_; - DISALLOW_EVIL_CONSTRUCTORS(QuitOnSignal); }; +} // namespace + Thread::Thread(const char *name) : thread_(NULL), thread_id_(0), @@ -198,8 +196,10 @@ void Thread::InternalStop(bool run_message_loop) { message_loop_->PostTask(FROM_HERE, new ThreadQuitTask()); if (run_message_loop) { - QuitOnSignal signal_watcher(thread_); - MessageLoop::current()->WatchObject(thread_, &signal_watcher); + QuitOnSignal quit_on_signal; + base::ObjectWatcher signal_watcher; + CHECK(signal_watcher.StartWatching(thread_, &quit_on_signal)); + bool old_state = MessageLoop::current()->NestableTasksAllowed(); MessageLoop::current()->SetNestableTasksAllowed(true); MessageLoop::current()->Run(); -- cgit v1.1