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