Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(100)

Side by Side Diff: base/observer_list_threadsafe.h

Issue 7584016: Use MessageLoopProxy instead of MessageLoop to dispatch notifications in ObserverListThreadsafe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Remove scoped_ptr dependency Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | base/observer_list_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef BASE_OBSERVER_LIST_THREADSAFE_H_ 5 #ifndef BASE_OBSERVER_LIST_THREADSAFE_H_
6 #define BASE_OBSERVER_LIST_THREADSAFE_H_ 6 #define BASE_OBSERVER_LIST_THREADSAFE_H_
7 #pragma once 7 #pragma once
8 8
9 #include <algorithm> 9 #include <algorithm>
10 #include <map> 10 #include <map>
11 11
12 #include "base/basictypes.h" 12 #include "base/basictypes.h"
13 #include "base/callback_old.h" 13 #include "base/callback_old.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "base/memory/ref_counted.h" 15 #include "base/memory/ref_counted.h"
16 #include "base/message_loop.h" 16 #include "base/message_loop.h"
17 #include "base/message_loop_proxy.h"
17 #include "base/observer_list.h" 18 #include "base/observer_list.h"
18 #include "base/task.h" 19 #include "base/task.h"
19 20
20 /////////////////////////////////////////////////////////////////////////////// 21 ///////////////////////////////////////////////////////////////////////////////
21 // 22 //
22 // OVERVIEW: 23 // OVERVIEW:
23 // 24 //
24 // A thread-safe container for a list of observers. 25 // A thread-safe container for a list of observers.
25 // This is similar to the observer_list (see observer_list.h), but it 26 // This is similar to the observer_list (see observer_list.h), but it
26 // is more robust for multi-threaded situations. 27 // is more robust for multi-threaded situations.
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 typedef typename ObserverList<ObserverType>::NotificationType 80 typedef typename ObserverList<ObserverType>::NotificationType
80 NotificationType; 81 NotificationType;
81 82
82 ObserverListThreadSafe() 83 ObserverListThreadSafe()
83 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} 84 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {}
84 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} 85 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {}
85 86
86 // Add an observer to the list. An observer should not be added to 87 // Add an observer to the list. An observer should not be added to
87 // the same list more than once. 88 // the same list more than once.
88 void AddObserver(ObserverType* obs) { 89 void AddObserver(ObserverType* obs) {
89 ObserverList<ObserverType>* list = NULL;
90 MessageLoop* loop = MessageLoop::current();
91 // TODO(mbelshe): Get rid of this check. Its needed right now because 90 // TODO(mbelshe): Get rid of this check. Its needed right now because
92 // Time currently triggers usage of the ObserverList. 91 // Time currently triggers usage of the ObserverList.
93 // And unittests use time without a MessageLoop. 92 // And unittests use time without a MessageLoop.
94 if (!loop) 93 if (!MessageLoop::current())
95 return; // Some unittests may access this without a message loop. 94 return; // Some unittests may access this without a message loop.
95 base::MessageLoopProxy* loop_proxy = GetOrCreateMessageLoopProxy();
96 ObserverList<ObserverType>* list = NULL;
96 { 97 {
97 base::AutoLock lock(list_lock_); 98 base::AutoLock lock(list_lock_);
98 if (observer_lists_.find(loop) == observer_lists_.end()) 99 if (observer_lists_.find(loop_proxy) == observer_lists_.end())
99 observer_lists_[loop] = new ObserverList<ObserverType>(type_); 100 observer_lists_[loop_proxy] = new ObserverList<ObserverType>(type_);
100 list = observer_lists_[loop]; 101 list = observer_lists_[loop_proxy];
101 } 102 }
102 list->AddObserver(obs); 103 list->AddObserver(obs);
103 } 104 }
104 105
105 // Remove an observer from the list if it is in the list. 106 // Remove an observer from the list if it is in the list.
106 // If there are pending notifications in-transit to the observer, they will 107 // If there are pending notifications in-transit to the observer, they will
107 // be aborted. 108 // be aborted.
108 // If the observer to be removed is in the list, RemoveObserver MUST 109 // If the observer to be removed is in the list, RemoveObserver MUST
109 // be called from the same thread which called AddObserver. 110 // be called from the same thread which called AddObserver.
110 void RemoveObserver(ObserverType* obs) { 111 void RemoveObserver(ObserverType* obs) {
112 if (!MessageLoop::current())
113 return; // On shutdown, it is possible that current() is already null.
114 base::MessageLoopProxy* loop_proxy = GetExistingMessageLoopProxy();
115 if (!loop_proxy) {
116 // This will happen if we try to remove an observer on a thread
117 // we never added an observer for.
118 return;
119 }
111 ObserverList<ObserverType>* list = NULL; 120 ObserverList<ObserverType>* list = NULL;
112 MessageLoop* loop = MessageLoop::current();
113 if (!loop)
114 return; // On shutdown, it is possible that current() is already null.
115 { 121 {
116 base::AutoLock lock(list_lock_); 122 base::AutoLock lock(list_lock_);
117 typename ObserversListMap::iterator it = observer_lists_.find(loop); 123 typename ObserversListMap::iterator it = observer_lists_.find(loop_proxy);
118 if (it == observer_lists_.end()) { 124 if (it == observer_lists_.end()) {
119 // This may happen if we try to remove an observer on a thread 125 // This can happen if a previously-used MessageLoopProxy
120 // we never added an observer for. 126 // no longer has any observers (we don't clean out the cache).
121 return; 127 return;
122 } 128 }
123 list = it->second; 129 list = it->second;
124 130
125 // If we're about to remove the last observer from the list, 131 // If we're about to remove the last observer from the list,
126 // then we can remove this observer_list entirely. 132 // then we can remove this observer_list entirely.
127 if (list->HasObserver(obs) && list->size() == 1) 133 if (list->HasObserver(obs) && list->size() == 1)
128 observer_lists_.erase(it); 134 observer_lists_.erase(it);
129 } 135 }
130 list->RemoveObserver(obs); 136 list->RemoveObserver(obs);
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) 191 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it)
186 delete (*it).second; 192 delete (*it).second;
187 observer_lists_.clear(); 193 observer_lists_.clear();
188 } 194 }
189 195
190 template <class Method, class Params> 196 template <class Method, class Params>
191 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) { 197 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) {
192 base::AutoLock lock(list_lock_); 198 base::AutoLock lock(list_lock_);
193 typename ObserversListMap::iterator it; 199 typename ObserversListMap::iterator it;
194 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { 200 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) {
195 MessageLoop* loop = (*it).first; 201 scoped_refptr<base::MessageLoopProxy> loop = (*it).first;
196 ObserverList<ObserverType>* list = (*it).second; 202 ObserverList<ObserverType>* list = (*it).second;
197 loop->PostTask( 203 loop->PostTask(
198 FROM_HERE, 204 FROM_HERE,
199 NewRunnableMethod(this, 205 NewRunnableMethod(this,
200 &ObserverListThreadSafe<ObserverType>:: 206 &ObserverListThreadSafe<ObserverType>::
201 template NotifyWrapper<Method, Params>, list, method)); 207 template NotifyWrapper<Method, Params>, list, method));
202 } 208 }
203 } 209 }
204 210
205 // Wrapper which is called to fire the notifications for each thread's 211 // Wrapper which is called to fire the notifications for each thread's
206 // ObserverList. This function MUST be called on the thread which owns 212 // ObserverList. This function MUST be called on the thread which owns
207 // the unsafe ObserverList. 213 // the unsafe ObserverList.
208 template <class Method, class Params> 214 template <class Method, class Params>
209 void NotifyWrapper(ObserverList<ObserverType>* list, 215 void NotifyWrapper(ObserverList<ObserverType>* list,
210 const UnboundMethod<ObserverType, Method, Params>& method) { 216 const UnboundMethod<ObserverType, Method, Params>& method) {
211 217
212 // Check that this list still needs notifications. 218 // Check that this list still needs notifications.
213 { 219 {
214 base::AutoLock lock(list_lock_); 220 base::AutoLock lock(list_lock_);
215 typename ObserversListMap::iterator it = 221 typename ObserversListMap::iterator it =
216 observer_lists_.find(MessageLoop::current()); 222 observer_lists_.find(GetExistingMessageLoopProxy());
217 223
218 // The ObserverList could have been removed already. In fact, it could 224 // The ObserverList could have been removed already. In fact, it could
219 // have been removed and then re-added! If the master list's loop 225 // have been removed and then re-added! If the master list's loop
220 // does not match this one, then we do not need to finish this 226 // does not match this one, then we do not need to finish this
221 // notification. 227 // notification.
222 if (it == observer_lists_.end() || it->second != list) 228 if (it == observer_lists_.end() || it->second != list)
223 return; 229 return;
224 } 230 }
225 231
226 { 232 {
227 typename ObserverList<ObserverType>::Iterator it(*list); 233 typename ObserverList<ObserverType>::Iterator it(*list);
228 ObserverType* obs; 234 ObserverType* obs;
229 while ((obs = it.GetNext()) != NULL) 235 while ((obs = it.GetNext()) != NULL)
230 method.Run(obs); 236 method.Run(obs);
231 } 237 }
232 238
233 // If there are no more observers on the list, we can now delete it. 239 // If there are no more observers on the list, we can now delete it.
234 if (list->size() == 0) { 240 if (list->size() == 0) {
235 { 241 {
236 base::AutoLock lock(list_lock_); 242 base::AutoLock lock(list_lock_);
237 // Remove |list| if it's not already removed. 243 // Remove |list| if it's not already removed.
238 // This can happen if multiple observers got removed in a notification. 244 // This can happen if multiple observers got removed in a notification.
239 // See http://crbug.com/55725. 245 // See http://crbug.com/55725.
240 typename ObserversListMap::iterator it = 246 typename ObserversListMap::iterator it =
241 observer_lists_.find(MessageLoop::current()); 247 observer_lists_.find(GetExistingMessageLoopProxy());
242 if (it != observer_lists_.end() && it->second == list) 248 if (it != observer_lists_.end() && it->second == list)
243 observer_lists_.erase(it); 249 observer_lists_.erase(it);
244 } 250 }
245 delete list; 251 delete list;
246 } 252 }
247 } 253 }
248 254
249 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; 255 base::MessageLoopProxy* GetExistingMessageLoopProxy() {
256 base::AutoLock lock(message_loop_lock_);
257 return GetExistingMessageLoopProxyUnlocked();
258 }
250 259
251 // These are marked mutable to facilitate having NotifyAll be const. 260 base::MessageLoopProxy* GetOrCreateMessageLoopProxy() {
261 base::AutoLock lock(message_loop_lock_);
262 base::MessageLoopProxy* loop_proxy = GetExistingMessageLoopProxyUnlocked();
263 if (!loop_proxy) {
264 scoped_refptr<base::MessageLoopProxy> loop_proxy_refptr =
265 base::MessageLoopProxy::CreateForCurrentThread();
266 message_loop_proxy_cache_[MessageLoop::current()] = loop_proxy_refptr;
267 loop_proxy = loop_proxy_refptr.get();
268 }
269 return loop_proxy;
270 }
271
272 base::MessageLoopProxy* GetExistingMessageLoopProxyUnlocked() {
darin (slow to review) 2011/08/08 18:18:26 The "Unlocked" suffix is a little confusing to me.
273 typename MessageLoopMap::iterator it =
274 message_loop_proxy_cache_.find(MessageLoop::current());
275 if (it != message_loop_proxy_cache_.end())
276 return (it->second).get();
277 return NULL;
278 }
279
280 typedef std::map<base::MessageLoopProxy*, ObserverList<ObserverType>*>
281 ObserversListMap;
282 typedef std::map<MessageLoop*, scoped_refptr<base::MessageLoopProxy> >
283 MessageLoopMap;
284
252 base::Lock list_lock_; // Protects the observer_lists_. 285 base::Lock list_lock_; // Protects the observer_lists_.
253 ObserversListMap observer_lists_; 286 ObserversListMap observer_lists_;
287 base::Lock message_loop_lock_; // Protects message_loop_proxy_cache_;
288 MessageLoopMap message_loop_proxy_cache_;
254 const NotificationType type_; 289 const NotificationType type_;
255 290
256 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); 291 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe);
257 }; 292 };
258 293
259 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ 294 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_
OLDNEW
« no previous file with comments | « no previous file | base/observer_list_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698