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