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

Side by Side Diff: base/observer_list_threadsafe.h

Issue 7604006: Use MessageLoopProxy instead of MessageLoop to dispatch notifications in ObserverListThreadsafe. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: 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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
89 ObserverList<ObserverType>* list = NULL; 90 ObserverList<ObserverType>* list = NULL;
90 MessageLoop* loop = MessageLoop::current(); 91 MessageLoop* loop = MessageLoop::current();
91 // TODO(mbelshe): Get rid of this check. Its needed right now because 92 // TODO(mbelshe): Get rid of this check. Its needed right now because
92 // Time currently triggers usage of the ObserverList. 93 // Time currently triggers usage of the ObserverList.
93 // And unittests use time without a MessageLoop. 94 // And unittests use time without a MessageLoop.
94 if (!loop) 95 if (!loop)
95 return; // Some unittests may access this without a message loop. 96 return; // Some unittests may access this without a message loop.
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) == observer_lists_.end())
99 observer_lists_[loop] = new ObserverList<ObserverType>(type_); 100 observer_lists_[loop] = new ObserverListContext(type_);
darin (slow to review) 2011/08/09 19:32:31 it is very surprising that insert did not work. i
adamk 2011/08/09 20:33:40 The failure was due to NULL not being "typed" enou
100 list = observer_lists_[loop]; 101 list = &(observer_lists_[loop]->list);
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 ObserverListContext* context = NULL;
111 ObserverList<ObserverType>* list = NULL; 113 ObserverList<ObserverType>* list = NULL;
112 MessageLoop* loop = MessageLoop::current(); 114 MessageLoop* loop = MessageLoop::current();
113 if (!loop) 115 if (!loop)
114 return; // On shutdown, it is possible that current() is already null. 116 return; // On shutdown, it is possible that current() is already null.
115 { 117 {
116 base::AutoLock lock(list_lock_); 118 base::AutoLock lock(list_lock_);
117 typename ObserversListMap::iterator it = observer_lists_.find(loop); 119 typename ObserversListMap::iterator it = observer_lists_.find(loop);
118 if (it == observer_lists_.end()) { 120 if (it == observer_lists_.end()) {
119 // This may happen if we try to remove an observer on a thread 121 // This will happen if we try to remove an observer on a thread
120 // we never added an observer for. 122 // we never added an observer for.
121 return; 123 return;
122 } 124 }
123 list = it->second; 125 context = it->second;
126 list = &context->list;
124 127
125 // If we're about to remove the last observer from the list, 128 // If we're about to remove the last observer from the list,
126 // then we can remove this observer_list entirely. 129 // then we can remove this observer_list entirely.
127 if (list->HasObserver(obs) && list->size() == 1) 130 if (list->HasObserver(obs) && list->size() == 1)
128 observer_lists_.erase(it); 131 observer_lists_.erase(it);
129 } 132 }
130 list->RemoveObserver(obs); 133 list->RemoveObserver(obs);
131 134
132 // If RemoveObserver is called from a notification, the size will be 135 // If RemoveObserver is called from a notification, the size will be
133 // nonzero. Instead of deleting here, the NotifyWrapper will delete 136 // nonzero. Instead of deleting here, the NotifyWrapper will delete
134 // when it finishes iterating. 137 // when it finishes iterating.
135 if (list->size() == 0) 138 if (list->size() == 0)
136 delete list; 139 delete context;
137 } 140 }
138 141
139 // Notify methods. 142 // Notify methods.
140 // Make a thread-safe callback to each Observer in the list. 143 // Make a thread-safe callback to each Observer in the list.
141 // Note, these calls are effectively asynchronous. You cannot assume 144 // Note, these calls are effectively asynchronous. You cannot assume
142 // that at the completion of the Notify call that all Observers have 145 // that at the completion of the Notify call that all Observers have
143 // been Notified. The notification may still be pending delivery. 146 // been Notified. The notification may still be pending delivery.
144 template <class Method> 147 template <class Method>
145 void Notify(Method m) { 148 void Notify(Method m) {
146 UnboundMethod<ObserverType, Method, Tuple0> method(m, MakeTuple()); 149 UnboundMethod<ObserverType, Method, Tuple0> method(m, MakeTuple());
(...skipping 26 matching lines...) Expand all
173 m, MakeTuple(a, b, c, d)); 176 m, MakeTuple(a, b, c, d));
174 Notify<Method, Tuple4<A, B, C, D> >(method); 177 Notify<Method, Tuple4<A, B, C, D> >(method);
175 } 178 }
176 179
177 // TODO(mbelshe): Add more wrappers for Notify() with more arguments. 180 // TODO(mbelshe): Add more wrappers for Notify() with more arguments.
178 181
179 private: 182 private:
180 // See comment above ObserverListThreadSafeTraits' definition. 183 // See comment above ObserverListThreadSafeTraits' definition.
181 friend struct ObserverListThreadSafeTraits<ObserverType>; 184 friend struct ObserverListThreadSafeTraits<ObserverType>;
182 185
186 struct ObserverListContext {
187 explicit ObserverListContext(NotificationType type)
188 : loop(base::MessageLoopProxy::CreateForCurrentThread()),
189 list(type) {
190 }
191
192 scoped_refptr<base::MessageLoopProxy> loop;
193 ObserverList<ObserverType> list;
194
195 DISALLOW_COPY_AND_ASSIGN(ObserverListContext);
196 };
197
183 ~ObserverListThreadSafe() { 198 ~ObserverListThreadSafe() {
184 typename ObserversListMap::const_iterator it; 199 typename ObserversListMap::const_iterator it;
185 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) 200 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it)
186 delete (*it).second; 201 delete (*it).second;
187 observer_lists_.clear(); 202 observer_lists_.clear();
188 } 203 }
189 204
190 template <class Method, class Params> 205 template <class Method, class Params>
191 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) { 206 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) {
192 base::AutoLock lock(list_lock_); 207 base::AutoLock lock(list_lock_);
193 typename ObserversListMap::iterator it; 208 typename ObserversListMap::iterator it;
194 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { 209 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) {
195 MessageLoop* loop = (*it).first; 210 ObserverListContext* context = (*it).second;
196 ObserverList<ObserverType>* list = (*it).second; 211 context->loop->PostTask(
197 loop->PostTask(
198 FROM_HERE, 212 FROM_HERE,
199 NewRunnableMethod(this, 213 NewRunnableMethod(this,
200 &ObserverListThreadSafe<ObserverType>:: 214 &ObserverListThreadSafe<ObserverType>::
201 template NotifyWrapper<Method, Params>, list, method)); 215 template NotifyWrapper<Method, Params>, context, method));
202 } 216 }
203 } 217 }
204 218
205 // Wrapper which is called to fire the notifications for each thread's 219 // Wrapper which is called to fire the notifications for each thread's
206 // ObserverList. This function MUST be called on the thread which owns 220 // ObserverList. This function MUST be called on the thread which owns
207 // the unsafe ObserverList. 221 // the unsafe ObserverList.
208 template <class Method, class Params> 222 template <class Method, class Params>
209 void NotifyWrapper(ObserverList<ObserverType>* list, 223 void NotifyWrapper(ObserverListContext* context,
210 const UnboundMethod<ObserverType, Method, Params>& method) { 224 const UnboundMethod<ObserverType, Method, Params>& method) {
211 225
212 // Check that this list still needs notifications. 226 // Check that this list still needs notifications.
213 { 227 {
214 base::AutoLock lock(list_lock_); 228 base::AutoLock lock(list_lock_);
215 typename ObserversListMap::iterator it = 229 typename ObserversListMap::iterator it =
216 observer_lists_.find(MessageLoop::current()); 230 observer_lists_.find(MessageLoop::current());
217 231
218 // The ObserverList could have been removed already. In fact, it could 232 // 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 233 // 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 234 // does not match this one, then we do not need to finish this
221 // notification. 235 // notification.
222 if (it == observer_lists_.end() || it->second != list) 236 if (it == observer_lists_.end() || it->second != context)
223 return; 237 return;
224 } 238 }
225 239
226 { 240 {
227 typename ObserverList<ObserverType>::Iterator it(*list); 241 typename ObserverList<ObserverType>::Iterator it(context->list);
228 ObserverType* obs; 242 ObserverType* obs;
229 while ((obs = it.GetNext()) != NULL) 243 while ((obs = it.GetNext()) != NULL)
230 method.Run(obs); 244 method.Run(obs);
231 } 245 }
232 246
233 // If there are no more observers on the list, we can now delete it. 247 // If there are no more observers on the list, we can now delete it.
234 if (list->size() == 0) { 248 if (context->list.size() == 0) {
235 { 249 {
236 base::AutoLock lock(list_lock_); 250 base::AutoLock lock(list_lock_);
237 // Remove |list| if it's not already removed. 251 // Remove |list| if it's not already removed.
238 // This can happen if multiple observers got removed in a notification. 252 // This can happen if multiple observers got removed in a notification.
239 // See http://crbug.com/55725. 253 // See http://crbug.com/55725.
240 typename ObserversListMap::iterator it = 254 typename ObserversListMap::iterator it =
241 observer_lists_.find(MessageLoop::current()); 255 observer_lists_.find(MessageLoop::current());
242 if (it != observer_lists_.end() && it->second == list) 256 if (it != observer_lists_.end() && it->second == context)
243 observer_lists_.erase(it); 257 observer_lists_.erase(it);
244 } 258 }
245 delete list; 259 delete context;
246 } 260 }
247 } 261 }
248 262
249 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; 263 typedef std::map<MessageLoop*, ObserverListContext*> ObserversListMap;
250 264
251 // These are marked mutable to facilitate having NotifyAll be const.
252 base::Lock list_lock_; // Protects the observer_lists_. 265 base::Lock list_lock_; // Protects the observer_lists_.
253 ObserversListMap observer_lists_; 266 ObserversListMap observer_lists_;
254 const NotificationType type_; 267 const NotificationType type_;
255 268
256 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); 269 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe);
257 }; 270 };
258 271
259 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ 272 #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