diff options
author | adamk@chromium.org <adamk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-09 18:19:15 +0000 |
---|---|---|
committer | adamk@chromium.org <adamk@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-08-09 18:19:15 +0000 |
commit | e8ae300ce5099346e35772a8ed6f8deeaec94f23 (patch) | |
tree | 1855e98940ce360b839fefb38e522f2de0fb1907 /base/observer_list_threadsafe.h | |
parent | ed3e49056e2380689a34c0fa91cddcae09939952 (diff) | |
download | chromium_src-e8ae300ce5099346e35772a8ed6f8deeaec94f23.zip chromium_src-e8ae300ce5099346e35772a8ed6f8deeaec94f23.tar.gz chromium_src-e8ae300ce5099346e35772a8ed6f8deeaec94f23.tar.bz2 |
Revert 96013 - Use MessageLoopProxy instead of MessageLoop to dispatch notifications in ObserverListThreadsafe.
BUG=91589
Review URL: http://codereview.chromium.org/7584016
TBR=adamk@chromium.org
Review URL: http://codereview.chromium.org/7605002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@96024 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/observer_list_threadsafe.h')
-rw-r--r-- | base/observer_list_threadsafe.h | 52 |
1 files changed, 18 insertions, 34 deletions
diff --git a/base/observer_list_threadsafe.h b/base/observer_list_threadsafe.h index a978c6c..f4ff68d 100644 --- a/base/observer_list_threadsafe.h +++ b/base/observer_list_threadsafe.h @@ -14,7 +14,6 @@ #include "base/logging.h" #include "base/memory/ref_counted.h" #include "base/message_loop.h" -#include "base/message_loop_proxy.h" #include "base/observer_list.h" #include "base/task.h" @@ -96,12 +95,9 @@ class ObserverListThreadSafe return; // Some unittests may access this without a message loop. { base::AutoLock lock(list_lock_); - std::pair<typename ObserversListMap::iterator, bool> result = - observer_lists_.insert( - typename ObserversListMap::value_type(loop, NULL)); - if (result.second) - result.first->second = new ObserverListContext(type_); - list = &(result.first->second->list); + if (observer_lists_.find(loop) == observer_lists_.end()) + observer_lists_[loop] = new ObserverList<ObserverType>(type_); + list = observer_lists_[loop]; } list->AddObserver(obs); } @@ -112,7 +108,6 @@ class ObserverListThreadSafe // If the observer to be removed is in the list, RemoveObserver MUST // be called from the same thread which called AddObserver. void RemoveObserver(ObserverType* obs) { - ObserverListContext* context = NULL; ObserverList<ObserverType>* list = NULL; MessageLoop* loop = MessageLoop::current(); if (!loop) @@ -121,12 +116,11 @@ class ObserverListThreadSafe base::AutoLock lock(list_lock_); typename ObserversListMap::iterator it = observer_lists_.find(loop); if (it == observer_lists_.end()) { - // This will happen if we try to remove an observer on a thread + // This may happen if we try to remove an observer on a thread // we never added an observer for. return; } - context = it->second; - list = &context->list; + list = it->second; // If we're about to remove the last observer from the list, // then we can remove this observer_list entirely. @@ -139,7 +133,7 @@ class ObserverListThreadSafe // nonzero. Instead of deleting here, the NotifyWrapper will delete // when it finishes iterating. if (list->size() == 0) - delete context; + delete list; } // Notify methods. @@ -186,18 +180,6 @@ class ObserverListThreadSafe // See comment above ObserverListThreadSafeTraits' definition. friend struct ObserverListThreadSafeTraits<ObserverType>; - struct ObserverListContext { - explicit ObserverListContext(NotificationType type) - : loop(base::MessageLoopProxy::CreateForCurrentThread()), - list(type) { - } - - scoped_refptr<base::MessageLoopProxy> loop; - ObserverList<ObserverType> list; - - DISALLOW_COPY_AND_ASSIGN(ObserverListContext); - }; - ~ObserverListThreadSafe() { typename ObserversListMap::const_iterator it; for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) @@ -210,12 +192,13 @@ class ObserverListThreadSafe base::AutoLock lock(list_lock_); typename ObserversListMap::iterator it; for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { - ObserverListContext* context = (*it).second; - context->loop->PostTask( + MessageLoop* loop = (*it).first; + ObserverList<ObserverType>* list = (*it).second; + loop->PostTask( FROM_HERE, NewRunnableMethod(this, &ObserverListThreadSafe<ObserverType>:: - template NotifyWrapper<Method, Params>, context, method)); + template NotifyWrapper<Method, Params>, list, method)); } } @@ -223,7 +206,7 @@ class ObserverListThreadSafe // ObserverList. This function MUST be called on the thread which owns // the unsafe ObserverList. template <class Method, class Params> - void NotifyWrapper(ObserverListContext* context, + void NotifyWrapper(ObserverList<ObserverType>* list, const UnboundMethod<ObserverType, Method, Params>& method) { // Check that this list still needs notifications. @@ -236,19 +219,19 @@ class ObserverListThreadSafe // have been removed and then re-added! If the master list's loop // does not match this one, then we do not need to finish this // notification. - if (it == observer_lists_.end() || it->second != context) + if (it == observer_lists_.end() || it->second != list) return; } { - typename ObserverList<ObserverType>::Iterator it(context->list); + typename ObserverList<ObserverType>::Iterator it(*list); ObserverType* obs; while ((obs = it.GetNext()) != NULL) method.Run(obs); } // If there are no more observers on the list, we can now delete it. - if (context->list.size() == 0) { + if (list->size() == 0) { { base::AutoLock lock(list_lock_); // Remove |list| if it's not already removed. @@ -256,15 +239,16 @@ class ObserverListThreadSafe // See http://crbug.com/55725. typename ObserversListMap::iterator it = observer_lists_.find(MessageLoop::current()); - if (it != observer_lists_.end() && it->second == context) + if (it != observer_lists_.end() && it->second == list) observer_lists_.erase(it); } - delete context; + delete list; } } - typedef std::map<MessageLoop*, ObserverListContext*> ObserversListMap; + typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; + // These are marked mutable to facilitate having NotifyAll be const. base::Lock list_lock_; // Protects the observer_lists_. ObserversListMap observer_lists_; const NotificationType type_; |