diff options
author | nduca@chromium.org <nduca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-15 20:33:46 +0000 |
---|---|---|
committer | nduca@chromium.org <nduca@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-15 20:33:46 +0000 |
commit | edd685f15dfd2841aaeddab567aea0623db1ae69 (patch) | |
tree | 9caa53e84eb2787471dd16009daf02fb85f8890e /base | |
parent | 5b79e5250785613d6dc7a42a27a45bb44b9cf7a0 (diff) | |
download | chromium_src-edd685f15dfd2841aaeddab567aea0623db1ae69.zip chromium_src-edd685f15dfd2841aaeddab567aea0623db1ae69.tar.gz chromium_src-edd685f15dfd2841aaeddab567aea0623db1ae69.tar.bz2 |
Add MessageLoopProxy::current
Review URL: http://codereview.chromium.org/7583053
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96819 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/file_util_proxy.cc | 2 | ||||
-rw-r--r-- | base/files/file_path_watcher_browsertest.cc | 2 | ||||
-rw-r--r-- | base/files/file_path_watcher_linux.cc | 2 | ||||
-rw-r--r-- | base/files/file_path_watcher_mac.cc | 2 | ||||
-rw-r--r-- | base/files/file_path_watcher_win.cc | 2 | ||||
-rw-r--r-- | base/message_loop.cc | 8 | ||||
-rw-r--r-- | base/message_loop.h | 9 | ||||
-rw-r--r-- | base/message_loop_proxy.h | 15 | ||||
-rw-r--r-- | base/message_loop_proxy_impl.cc | 16 | ||||
-rw-r--r-- | base/message_loop_proxy_impl.h | 21 | ||||
-rw-r--r-- | base/message_loop_proxy_impl_unittest.cc | 5 | ||||
-rw-r--r-- | base/observer_list_threadsafe.h | 2 | ||||
-rw-r--r-- | base/threading/thread.cc | 4 | ||||
-rw-r--r-- | base/threading/thread.h | 6 |
14 files changed, 55 insertions, 41 deletions
diff --git a/base/file_util_proxy.cc b/base/file_util_proxy.cc index be6465d..a192cbf 100644 --- a/base/file_util_proxy.cc +++ b/base/file_util_proxy.cc @@ -73,7 +73,7 @@ class MessageLoopRelay public: MessageLoopRelay() : origin_message_loop_proxy_( - base::MessageLoopProxy::CreateForCurrentThread()), + base::MessageLoopProxy::current()), error_code_(base::PLATFORM_FILE_OK) { } diff --git a/base/files/file_path_watcher_browsertest.cc b/base/files/file_path_watcher_browsertest.cc index 49b652f..a422f84 100644 --- a/base/files/file_path_watcher_browsertest.cc +++ b/base/files/file_path_watcher_browsertest.cc @@ -41,7 +41,7 @@ class NotificationCollector : public base::RefCountedThreadSafe<NotificationCollector> { public: NotificationCollector() - : loop_(base::MessageLoopProxy::CreateForCurrentThread()) {} + : loop_(base::MessageLoopProxy::current()) {} // Called from the file thread by the delegates. void OnChange(TestDelegate* delegate) { diff --git a/base/files/file_path_watcher_linux.cc b/base/files/file_path_watcher_linux.cc index 0fb10e4..aa116e3 100644 --- a/base/files/file_path_watcher_linux.cc +++ b/base/files/file_path_watcher_linux.cc @@ -370,7 +370,7 @@ bool FilePathWatcherImpl::Watch(const FilePath& path, DCHECK(target_.empty()); DCHECK(MessageLoopForIO::current()); - set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); + set_message_loop(base::MessageLoopProxy::current()); delegate_ = delegate; target_ = path; MessageLoop::current()->AddDestructionObserver(this); diff --git a/base/files/file_path_watcher_mac.cc b/base/files/file_path_watcher_mac.cc index 7b61f76..c3b9351 100644 --- a/base/files/file_path_watcher_mac.cc +++ b/base/files/file_path_watcher_mac.cc @@ -427,7 +427,7 @@ bool FilePathWatcherImpl::Watch(const FilePath& path, target_ = path; MessageLoop::current()->AddDestructionObserver(this); - io_message_loop_ = base::MessageLoopProxy::CreateForCurrentThread(); + io_message_loop_ = base::MessageLoopProxy::current(); kqueue_ = kqueue(); if (kqueue_ == -1) { diff --git a/base/files/file_path_watcher_win.cc b/base/files/file_path_watcher_win.cc index eedcf22..b87654e 100644 --- a/base/files/file_path_watcher_win.cc +++ b/base/files/file_path_watcher_win.cc @@ -81,7 +81,7 @@ bool FilePathWatcherImpl::Watch(const FilePath& path, FilePathWatcher::Delegate* delegate) { DCHECK(target_.value().empty()); // Can only watch one path. - set_message_loop(base::MessageLoopProxy::CreateForCurrentThread()); + set_message_loop(base::MessageLoopProxy::current()); delegate_ = delegate; target_ = path; MessageLoop::current()->AddDestructionObserver(this); diff --git a/base/message_loop.cc b/base/message_loop.cc index 10f4a26..02534f5 100644 --- a/base/message_loop.cc +++ b/base/message_loop.cc @@ -12,6 +12,7 @@ #include "base/lazy_instance.h" #include "base/logging.h" #include "base/memory/scoped_ptr.h" +#include "base/message_loop_proxy_impl.h" #include "base/message_pump_default.h" #include "base/metrics/histogram.h" #include "base/third_party/dynamic_annotations/dynamic_annotations.h" @@ -135,6 +136,8 @@ MessageLoop::MessageLoop(Type type) DCHECK(!current()) << "should only have one message loop per thread"; lazy_tls_ptr.Pointer()->Set(this); + message_loop_proxy_ = new base::MessageLoopProxyImpl(); + // TODO(rvargas): Get rid of the OS guards. #if defined(OS_WIN) #define MESSAGE_PUMP_UI new base::MessagePumpForUI() @@ -202,6 +205,11 @@ MessageLoop::~MessageLoop() { FOR_EACH_OBSERVER(DestructionObserver, destruction_observers_, WillDestroyCurrentMessageLoop()); + // Tell the message_loop_proxy that we are dying. + static_cast<base::MessageLoopProxyImpl*>(message_loop_proxy_.get())-> + WillDestroyCurrentMessageLoop(); + message_loop_proxy_ = NULL; + // OK, now make it so that no one can find us. lazy_tls_ptr.Pointer()->Set(NULL); diff --git a/base/message_loop.h b/base/message_loop.h index 9355ec9..976c5ce 100644 --- a/base/message_loop.h +++ b/base/message_loop.h @@ -13,6 +13,7 @@ #include "base/basictypes.h" #include "base/callback.h" #include "base/memory/ref_counted.h" +#include "base/message_loop_proxy.h" #include "base/message_pump.h" #include "base/observer_list.h" #include "base/synchronization/lock.h" @@ -271,6 +272,11 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { } const std::string& thread_name() const { return thread_name_; } + // Gets the message loop proxy associated with this message loop proxy + scoped_refptr<base::MessageLoopProxy> message_loop_proxy() { + return message_loop_proxy_.get(); + } + // Enables or disables the recursive task processing. This happens in the case // of recursive message loops. Some unwanted message loop may occurs when // using common controls or printer functions. By default, recursive task @@ -573,6 +579,9 @@ class BASE_EXPORT MessageLoop : public base::MessagePump::Delegate { ObserverList<TaskObserver> task_observers_; + // The message loop proxy associated with this message loop, if one exists. + scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; + private: DISALLOW_COPY_AND_ASSIGN(MessageLoop); }; diff --git a/base/message_loop_proxy.h b/base/message_loop_proxy.h index 646b2ef..9a856bf 100644 --- a/base/message_loop_proxy.h +++ b/base/message_loop_proxy.h @@ -17,9 +17,12 @@ namespace base { struct MessageLoopProxyTraits; // This class provides a thread-safe refcounted interface to the Post* methods -// of a message loop. This class can outlive the target message loop. You can -// obtain a MessageLoopProxy via Thread::message_loop_proxy() or -// MessageLoopProxy::CreateForCurrentThread(). +// of a message loop. This class can outlive the target message loop. +// MessageLoopProxy objects are constructed automatically for all MessageLoops. +// So, to access them, you can use any of the following: +// Thread::message_loop_proxy() +// MessageLoop::current()->message_loop_proxy() +// MessageLoopProxy::current() class BASE_EXPORT MessageLoopProxy : public base::RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits> { @@ -78,9 +81,9 @@ class BASE_EXPORT MessageLoopProxy return PostNonNestableTask(from_here, new ReleaseTask<T>(object)); } - // Factory method for creating an implementation of MessageLoopProxy - // for the current thread. - static scoped_refptr<MessageLoopProxy> CreateForCurrentThread(); + // Gets the MessageLoopProxy for the current message loop, creating one if + // needed. + static scoped_refptr<MessageLoopProxy> current(); protected: friend class RefCountedThreadSafe<MessageLoopProxy, MessageLoopProxyTraits>; diff --git a/base/message_loop_proxy_impl.cc b/base/message_loop_proxy_impl.cc index af0d214..6b52351 100644 --- a/base/message_loop_proxy_impl.cc +++ b/base/message_loop_proxy_impl.cc @@ -8,13 +8,6 @@ namespace base { MessageLoopProxyImpl::~MessageLoopProxyImpl() { - AutoLock lock(message_loop_lock_); - // If the target message loop still exists, the d'tor WILL execute on the - // target loop. - if (target_message_loop_) { - DCHECK(MessageLoop::current() == target_message_loop_); - MessageLoop::current()->RemoveDestructionObserver(this); - } } // MessageLoopProxy implementation @@ -102,7 +95,6 @@ void MessageLoopProxyImpl::OnDestruct() const { MessageLoopProxyImpl::MessageLoopProxyImpl() : target_message_loop_(MessageLoop::current()) { - target_message_loop_->AddDestructionObserver(this); } bool MessageLoopProxyImpl::PostTaskHelper( @@ -143,9 +135,11 @@ bool MessageLoopProxyImpl::PostTaskHelper( } scoped_refptr<MessageLoopProxy> -MessageLoopProxy::CreateForCurrentThread() { - scoped_refptr<MessageLoopProxy> ret(new MessageLoopProxyImpl()); - return ret; +MessageLoopProxy::current() { + MessageLoop* cur_loop = MessageLoop::current(); + if (!cur_loop) + return NULL; + return cur_loop->message_loop_proxy(); } } // namespace base diff --git a/base/message_loop_proxy_impl.h b/base/message_loop_proxy_impl.h index 57896ff..2966394 100644 --- a/base/message_loop_proxy_impl.h +++ b/base/message_loop_proxy_impl.h @@ -13,12 +13,11 @@ namespace base { -// A stock implementation of MessageLoopProxy that takes in a MessageLoop -// and keeps track of its lifetime using the MessageLoop DestructionObserver. -// For now a MessageLoopProxyImpl can only be created for the current thread. +// A stock implementation of MessageLoopProxy that is created and managed by a +// MessageLoop. For now a MessageLoopProxyImpl can only be created as part of a +// MessageLoop. class BASE_EXPORT MessageLoopProxyImpl - : public MessageLoopProxy, - public MessageLoop::DestructionObserver { + : public MessageLoopProxy { public: virtual ~MessageLoopProxyImpl(); @@ -47,9 +46,6 @@ class BASE_EXPORT MessageLoopProxyImpl int64 delay_ms); virtual bool BelongsToCurrentThread(); - // MessageLoop::DestructionObserver implementation - virtual void WillDestroyCurrentMessageLoop(); - protected: // Override OnDestruct so that we can delete the object on the target message // loop if it still exists. @@ -57,6 +53,11 @@ class BASE_EXPORT MessageLoopProxyImpl private: MessageLoopProxyImpl(); + + // Called directly by MessageLoop::~MessageLoop. + virtual void WillDestroyCurrentMessageLoop(); + + // TODO(ajwong): Remove this after we've fully migrated to base::Closure. bool PostTaskHelper(const tracked_objects::Location& from_here, Task* task, @@ -67,8 +68,8 @@ class BASE_EXPORT MessageLoopProxyImpl int64 delay_ms, bool nestable); - // For the factory method to work - friend class MessageLoopProxy; + // Allow the messageLoop to create a MessageLoopProxyImpl. + friend class ::MessageLoop; // The lock that protects access to target_message_loop_. mutable base::Lock message_loop_lock_; diff --git a/base/message_loop_proxy_impl_unittest.cc b/base/message_loop_proxy_impl_unittest.cc index d635acb..1707b22 100644 --- a/base/message_loop_proxy_impl_unittest.cc +++ b/base/message_loop_proxy_impl_unittest.cc @@ -7,6 +7,7 @@ #include "base/bind.h" #include "base/memory/scoped_ptr.h" #include "base/message_loop.h" +#include "base/message_loop_proxy.h" #include "base/threading/thread.h" #include "testing/gtest/include/gtest/gtest.h" #include "testing/platform_test.h" @@ -25,10 +26,14 @@ class MessageLoopProxyImplTest : public testing::Test { void AssertOnIOThread() const { ASSERT_TRUE(io_thread_->message_loop_proxy()->BelongsToCurrentThread()); + ASSERT_EQ(io_thread_->message_loop_proxy(), + base::MessageLoopProxy::current()); } void AssertOnFileThread() const { ASSERT_TRUE(file_thread_->message_loop_proxy()->BelongsToCurrentThread()); + ASSERT_EQ(file_thread_->message_loop_proxy(), + base::MessageLoopProxy::current()); } protected: diff --git a/base/observer_list_threadsafe.h b/base/observer_list_threadsafe.h index 3b47c83..22cc951 100644 --- a/base/observer_list_threadsafe.h +++ b/base/observer_list_threadsafe.h @@ -185,7 +185,7 @@ class ObserverListThreadSafe struct ObserverListContext { explicit ObserverListContext(NotificationType type) - : loop(base::MessageLoopProxy::CreateForCurrentThread()), + : loop(base::MessageLoopProxy::current()), list(type) { } diff --git a/base/threading/thread.cc b/base/threading/thread.cc index 7a100ca..616aac8 100644 --- a/base/threading/thread.cc +++ b/base/threading/thread.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -151,7 +151,6 @@ void Thread::ThreadMain() { ANNOTATE_THREAD_NAME(name_.c_str()); // Tell the name to race detector. message_loop.set_thread_name(name_); message_loop_ = &message_loop; - message_loop_proxy_ = MessageLoopProxy::CreateForCurrentThread(); // Let the thread do extra initialization. // Let's do this before signaling we are started. @@ -171,7 +170,6 @@ void Thread::ThreadMain() { // We can't receive messages anymore. message_loop_ = NULL; - message_loop_proxy_ = NULL; } thread_id_ = kInvalidThreadId; } diff --git a/base/threading/thread.h b/base/threading/thread.h index 58a4cc2..d7451ec 100644 --- a/base/threading/thread.h +++ b/base/threading/thread.h @@ -113,7 +113,7 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { // is gone. // TODO(sanjeevr): Look into merging MessageLoop and MessageLoopProxy. scoped_refptr<MessageLoopProxy> message_loop_proxy() const { - return message_loop_proxy_; + return message_loop_->message_loop_proxy(); } // Set the name of this thread (for display in debugger too). @@ -175,10 +175,6 @@ class BASE_EXPORT Thread : PlatformThread::Delegate { // by the created thread. MessageLoop* message_loop_; - // A MessageLoopProxy implementation that targets this thread. This can - // outlive the thread. - scoped_refptr<MessageLoopProxy> message_loop_proxy_; - // Our thread's ID. PlatformThreadId thread_id_; |