OLD | NEW |
---|---|
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 Loading... | |
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 ObserverListContext* context = 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 typename ObserversListMap::iterator it = observer_lists_.find(loop); |
darin (slow to review)
2011/08/08 19:33:21
nit: maybe it is not worth it, but you could just
adamk
2011/08/08 19:50:04
I've made the code do this. Certainly not a big d
| |
99 observer_lists_[loop] = new ObserverList<ObserverType>(type_); | 100 if (it == observer_lists_.end()) { |
100 list = observer_lists_[loop]; | 101 std::pair<typename ObserversListMap::iterator, bool> result = |
102 observer_lists_.insert( | |
103 std::make_pair(loop, new ObserverListContext)); | |
104 it = result.first; | |
105 } | |
106 context = it->second; | |
101 } | 107 } |
102 list->AddObserver(obs); | 108 context->list().AddObserver(obs); |
103 } | 109 } |
104 | 110 |
105 // Remove an observer from the list if it is in the list. | 111 // 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 | 112 // If there are pending notifications in-transit to the observer, they will |
107 // be aborted. | 113 // be aborted. |
108 // If the observer to be removed is in the list, RemoveObserver MUST | 114 // If the observer to be removed is in the list, RemoveObserver MUST |
109 // be called from the same thread which called AddObserver. | 115 // be called from the same thread which called AddObserver. |
110 void RemoveObserver(ObserverType* obs) { | 116 void RemoveObserver(ObserverType* obs) { |
111 ObserverList<ObserverType>* list = NULL; | 117 ObserverListContext* context = NULL; |
112 MessageLoop* loop = MessageLoop::current(); | 118 MessageLoop* loop = MessageLoop::current(); |
113 if (!loop) | 119 if (!loop) |
114 return; // On shutdown, it is possible that current() is already null. | 120 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); |
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 will happen if we try to remove an observer on a thread |
120 // we never added an observer for. | 126 // we never added an observer for. |
121 return; | 127 return; |
122 } | 128 } |
123 list = it->second; | 129 context = 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 (context->list().HasObserver(obs) && |
134 context->list().size() == 1) | |
128 observer_lists_.erase(it); | 135 observer_lists_.erase(it); |
129 } | 136 } |
130 list->RemoveObserver(obs); | 137 context->list().RemoveObserver(obs); |
131 | 138 |
132 // If RemoveObserver is called from a notification, the size will be | 139 // If RemoveObserver is called from a notification, the size will be |
133 // nonzero. Instead of deleting here, the NotifyWrapper will delete | 140 // nonzero. Instead of deleting here, the NotifyWrapper will delete |
134 // when it finishes iterating. | 141 // when it finishes iterating. |
135 if (list->size() == 0) | 142 if (context->list().size() == 0) |
136 delete list; | 143 delete context; |
137 } | 144 } |
138 | 145 |
139 // Notify methods. | 146 // Notify methods. |
140 // Make a thread-safe callback to each Observer in the list. | 147 // Make a thread-safe callback to each Observer in the list. |
141 // Note, these calls are effectively asynchronous. You cannot assume | 148 // Note, these calls are effectively asynchronous. You cannot assume |
142 // that at the completion of the Notify call that all Observers have | 149 // that at the completion of the Notify call that all Observers have |
143 // been Notified. The notification may still be pending delivery. | 150 // been Notified. The notification may still be pending delivery. |
144 template <class Method> | 151 template <class Method> |
145 void Notify(Method m) { | 152 void Notify(Method m) { |
146 UnboundMethod<ObserverType, Method, Tuple0> method(m, MakeTuple()); | 153 UnboundMethod<ObserverType, Method, Tuple0> method(m, MakeTuple()); |
(...skipping 26 matching lines...) Expand all Loading... | |
173 m, MakeTuple(a, b, c, d)); | 180 m, MakeTuple(a, b, c, d)); |
174 Notify<Method, Tuple4<A, B, C, D> >(method); | 181 Notify<Method, Tuple4<A, B, C, D> >(method); |
175 } | 182 } |
176 | 183 |
177 // TODO(mbelshe): Add more wrappers for Notify() with more arguments. | 184 // TODO(mbelshe): Add more wrappers for Notify() with more arguments. |
178 | 185 |
179 private: | 186 private: |
180 // See comment above ObserverListThreadSafeTraits' definition. | 187 // See comment above ObserverListThreadSafeTraits' definition. |
181 friend struct ObserverListThreadSafeTraits<ObserverType>; | 188 friend struct ObserverListThreadSafeTraits<ObserverType>; |
182 | 189 |
190 class ObserverListContext { | |
191 public: | |
192 ObserverListContext() | |
193 : message_loop_proxy_( | |
194 base::MessageLoopProxy::CreateForCurrentThread()) { | |
195 } | |
196 | |
197 base::MessageLoopProxy& loop() { return *message_loop_proxy_; } | |
darin (slow to review)
2011/08/08 19:33:21
perhaps there is no point in these member variable
adamk
2011/08/08 19:50:04
I suppose not. Made this a struct instead (though
| |
198 ObserverList<ObserverType>& list() { return observer_list_; } | |
199 | |
200 private: | |
201 scoped_refptr<base::MessageLoopProxy> message_loop_proxy_; | |
202 ObserverList<ObserverType> observer_list_; | |
203 | |
204 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); | |
205 }; | |
206 | |
183 ~ObserverListThreadSafe() { | 207 ~ObserverListThreadSafe() { |
184 typename ObserversListMap::const_iterator it; | 208 typename ObserversListMap::const_iterator it; |
185 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) | 209 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) |
186 delete (*it).second; | 210 delete (*it).second; |
187 observer_lists_.clear(); | 211 observer_lists_.clear(); |
188 } | 212 } |
189 | 213 |
190 template <class Method, class Params> | 214 template <class Method, class Params> |
191 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) { | 215 void Notify(const UnboundMethod<ObserverType, Method, Params>& method) { |
192 base::AutoLock lock(list_lock_); | 216 base::AutoLock lock(list_lock_); |
193 typename ObserversListMap::iterator it; | 217 typename ObserversListMap::iterator it; |
194 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { | 218 for (it = observer_lists_.begin(); it != observer_lists_.end(); ++it) { |
195 MessageLoop* loop = (*it).first; | 219 ObserverListContext* context = (*it).second; |
196 ObserverList<ObserverType>* list = (*it).second; | 220 context->loop().PostTask( |
197 loop->PostTask( | |
198 FROM_HERE, | 221 FROM_HERE, |
199 NewRunnableMethod(this, | 222 NewRunnableMethod(this, |
200 &ObserverListThreadSafe<ObserverType>:: | 223 &ObserverListThreadSafe<ObserverType>:: |
201 template NotifyWrapper<Method, Params>, list, method)); | 224 template NotifyWrapper<Method, Params>, context, method)); |
202 } | 225 } |
203 } | 226 } |
204 | 227 |
205 // Wrapper which is called to fire the notifications for each thread's | 228 // Wrapper which is called to fire the notifications for each thread's |
206 // ObserverList. This function MUST be called on the thread which owns | 229 // ObserverList. This function MUST be called on the thread which owns |
207 // the unsafe ObserverList. | 230 // the unsafe ObserverList. |
208 template <class Method, class Params> | 231 template <class Method, class Params> |
209 void NotifyWrapper(ObserverList<ObserverType>* list, | 232 void NotifyWrapper(ObserverListContext* context, |
210 const UnboundMethod<ObserverType, Method, Params>& method) { | 233 const UnboundMethod<ObserverType, Method, Params>& method) { |
211 | 234 |
212 // Check that this list still needs notifications. | 235 // Check that this list still needs notifications. |
213 { | 236 { |
214 base::AutoLock lock(list_lock_); | 237 base::AutoLock lock(list_lock_); |
215 typename ObserversListMap::iterator it = | 238 typename ObserversListMap::iterator it = |
216 observer_lists_.find(MessageLoop::current()); | 239 observer_lists_.find(MessageLoop::current()); |
217 | 240 |
218 // The ObserverList could have been removed already. In fact, it could | 241 // 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 | 242 // 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 | 243 // does not match this one, then we do not need to finish this |
221 // notification. | 244 // notification. |
222 if (it == observer_lists_.end() || it->second != list) | 245 if (it == observer_lists_.end() || it->second != context) |
223 return; | 246 return; |
224 } | 247 } |
225 | 248 |
226 { | 249 { |
227 typename ObserverList<ObserverType>::Iterator it(*list); | 250 typename ObserverList<ObserverType>::Iterator it(context->list()); |
228 ObserverType* obs; | 251 ObserverType* obs; |
229 while ((obs = it.GetNext()) != NULL) | 252 while ((obs = it.GetNext()) != NULL) |
230 method.Run(obs); | 253 method.Run(obs); |
231 } | 254 } |
232 | 255 |
233 // If there are no more observers on the list, we can now delete it. | 256 // If there are no more observers on the list, we can now delete it. |
234 if (list->size() == 0) { | 257 if (context->list().size() == 0) { |
235 { | 258 { |
236 base::AutoLock lock(list_lock_); | 259 base::AutoLock lock(list_lock_); |
237 // Remove |list| if it's not already removed. | 260 // Remove |list| if it's not already removed. |
238 // This can happen if multiple observers got removed in a notification. | 261 // This can happen if multiple observers got removed in a notification. |
239 // See http://crbug.com/55725. | 262 // See http://crbug.com/55725. |
240 typename ObserversListMap::iterator it = | 263 typename ObserversListMap::iterator it = |
241 observer_lists_.find(MessageLoop::current()); | 264 observer_lists_.find(MessageLoop::current()); |
242 if (it != observer_lists_.end() && it->second == list) | 265 if (it != observer_lists_.end() && it->second == context) |
243 observer_lists_.erase(it); | 266 observer_lists_.erase(it); |
244 } | 267 } |
245 delete list; | 268 delete context; |
246 } | 269 } |
247 } | 270 } |
248 | 271 |
249 typedef std::map<MessageLoop*, ObserverList<ObserverType>*> ObserversListMap; | 272 typedef std::map<MessageLoop*, ObserverListContext*> ObserversListMap; |
250 | 273 |
251 // These are marked mutable to facilitate having NotifyAll be const. | |
252 base::Lock list_lock_; // Protects the observer_lists_. | 274 base::Lock list_lock_; // Protects the observer_lists_. |
253 ObserversListMap observer_lists_; | 275 ObserversListMap observer_lists_; |
254 const NotificationType type_; | 276 const NotificationType type_; |
255 | 277 |
256 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 278 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
257 }; | 279 }; |
258 | 280 |
259 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 281 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |