OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 | 7 |
8 #include <algorithm> | 8 #include <algorithm> |
9 #include <map> | 9 #include <map> |
10 | 10 |
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
184 for (const auto& entry : observer_lists_) { | 184 for (const auto& entry : observer_lists_) { |
185 ObserverListContext* context = entry.second; | 185 ObserverListContext* context = entry.second; |
186 context->task_runner->PostTask( | 186 context->task_runner->PostTask( |
187 from_here, | 187 from_here, |
188 Bind(&ObserverListThreadSafe<ObserverType>::template NotifyWrapper< | 188 Bind(&ObserverListThreadSafe<ObserverType>::template NotifyWrapper< |
189 Method, Tuple<Params...>>, | 189 Method, Tuple<Params...>>, |
190 this, context, method)); | 190 this, context, method)); |
191 } | 191 } |
192 } | 192 } |
193 | 193 |
194 // SyncNotify methods. | |
195 // Make a thread-safe callback to each Observer in the list. | |
196 // Note, these calls are synchronous. | |
Mark Mentovai
2016/03/01 15:21:20
…and happen on thread that calls SyncNotify, which
| |
197 template <class Method, class... Params> | |
198 void SyncNotify(const tracked_objects::Location& from_here, | |
199 Method m, | |
200 const Params&... params) { | |
201 internal::UnboundMethod<ObserverType, Method, Tuple<Params...>> method( | |
202 m, MakeTuple(params...)); | |
203 | |
204 AutoLock lock(list_lock_); | |
205 for (const auto& entry : observer_lists_) { | |
206 ObserverListContext* context = entry.second; | |
207 typename ObserverList<ObserverType>::Iterator it(&context->list); | |
208 ObserverType* obs; | |
209 while ((obs = it.GetNext()) != nullptr) | |
210 method.Run(obs); | |
211 } | |
212 } | |
213 | |
194 private: | 214 private: |
195 // See comment above ObserverListThreadSafeTraits' definition. | 215 // See comment above ObserverListThreadSafeTraits' definition. |
196 friend struct ObserverListThreadSafeTraits<ObserverType>; | 216 friend struct ObserverListThreadSafeTraits<ObserverType>; |
197 | 217 |
198 struct ObserverListContext { | 218 struct ObserverListContext { |
199 explicit ObserverListContext(NotificationType type) | 219 explicit ObserverListContext(NotificationType type) |
200 : task_runner(ThreadTaskRunnerHandle::Get()), list(type) {} | 220 : task_runner(ThreadTaskRunnerHandle::Get()), list(type) {} |
201 | 221 |
202 scoped_refptr<SingleThreadTaskRunner> task_runner; | 222 scoped_refptr<SingleThreadTaskRunner> task_runner; |
203 ObserverList<ObserverType> list; | 223 ObserverList<ObserverType> list; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
263 mutable Lock list_lock_; // Protects the observer_lists_. | 283 mutable Lock list_lock_; // Protects the observer_lists_. |
264 ObserversListMap observer_lists_; | 284 ObserversListMap observer_lists_; |
265 const NotificationType type_; | 285 const NotificationType type_; |
266 | 286 |
267 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 287 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
268 }; | 288 }; |
269 | 289 |
270 } // namespace base | 290 } // namespace base |
271 | 291 |
272 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 292 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |