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 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/location.h" | 13 #include "base/location.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/message_loop.h" | 16 #include "base/message_loop/message_loop.h" |
17 #include "base/message_loop/message_loop_proxy.h" | |
18 #include "base/observer_list.h" | 17 #include "base/observer_list.h" |
| 18 #include "base/single_thread_task_runner.h" |
19 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
| 20 #include "base/thread_task_runner_handle.h" |
20 #include "base/threading/platform_thread.h" | 21 #include "base/threading/platform_thread.h" |
21 | 22 |
22 /////////////////////////////////////////////////////////////////////////////// | 23 /////////////////////////////////////////////////////////////////////////////// |
23 // | 24 // |
24 // OVERVIEW: | 25 // OVERVIEW: |
25 // | 26 // |
26 // A thread-safe container for a list of observers. | 27 // A thread-safe container for a list of observers. |
27 // This is similar to the observer_list (see observer_list.h), but it | 28 // This is similar to the observer_list (see observer_list.h), but it |
28 // is more robust for multi-threaded situations. | 29 // is more robust for multi-threaded situations. |
29 // | 30 // |
(...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 template <class Method, class... Params> | 171 template <class Method, class... Params> |
171 void Notify(const tracked_objects::Location& from_here, | 172 void Notify(const tracked_objects::Location& from_here, |
172 Method m, | 173 Method m, |
173 const Params&... params) { | 174 const Params&... params) { |
174 UnboundMethod<ObserverType, Method, Tuple<Params...>> method( | 175 UnboundMethod<ObserverType, Method, Tuple<Params...>> method( |
175 m, MakeTuple(params...)); | 176 m, MakeTuple(params...)); |
176 | 177 |
177 base::AutoLock lock(list_lock_); | 178 base::AutoLock lock(list_lock_); |
178 for (const auto& entry : observer_lists_) { | 179 for (const auto& entry : observer_lists_) { |
179 ObserverListContext* context = entry.second; | 180 ObserverListContext* context = entry.second; |
180 context->loop->PostTask( | 181 context->task_runner->PostTask( |
181 from_here, | 182 from_here, |
182 base::Bind( | 183 base::Bind( |
183 &ObserverListThreadSafe<ObserverType>::template NotifyWrapper< | 184 &ObserverListThreadSafe<ObserverType>::template NotifyWrapper< |
184 Method, Tuple<Params...>>, | 185 Method, Tuple<Params...>>, |
185 this, context, method)); | 186 this, context, method)); |
186 } | 187 } |
187 } | 188 } |
188 | 189 |
189 private: | 190 private: |
190 // See comment above ObserverListThreadSafeTraits' definition. | 191 // See comment above ObserverListThreadSafeTraits' definition. |
191 friend struct ObserverListThreadSafeTraits<ObserverType>; | 192 friend struct ObserverListThreadSafeTraits<ObserverType>; |
192 | 193 |
193 struct ObserverListContext { | 194 struct ObserverListContext { |
194 explicit ObserverListContext(NotificationType type) | 195 explicit ObserverListContext(NotificationType type) |
195 : loop(base::MessageLoopProxy::current()), | 196 : task_runner(base::ThreadTaskRunnerHandle::Get()), list(type) {} |
196 list(type) { | |
197 } | |
198 | 197 |
199 scoped_refptr<base::MessageLoopProxy> loop; | 198 scoped_refptr<base::SingleThreadTaskRunner> task_runner; |
200 ObserverList<ObserverType> list; | 199 ObserverList<ObserverType> list; |
201 | 200 |
202 private: | 201 private: |
203 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); | 202 DISALLOW_COPY_AND_ASSIGN(ObserverListContext); |
204 }; | 203 }; |
205 | 204 |
206 ~ObserverListThreadSafe() { | 205 ~ObserverListThreadSafe() { |
207 STLDeleteValues(&observer_lists_); | 206 STLDeleteValues(&observer_lists_); |
208 } | 207 } |
209 | 208 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
257 ObserversListMap; | 256 ObserversListMap; |
258 | 257 |
259 mutable base::Lock list_lock_; // Protects the observer_lists_. | 258 mutable base::Lock list_lock_; // Protects the observer_lists_. |
260 ObserversListMap observer_lists_; | 259 ObserversListMap observer_lists_; |
261 const NotificationType type_; | 260 const NotificationType type_; |
262 | 261 |
263 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 262 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
264 }; | 263 }; |
265 | 264 |
266 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 265 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
OLD | NEW |