[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_OBSERVER_LIST_THREADSAFE_H_ |
| 6 | #define BASE_OBSERVER_LIST_THREADSAFE_H_ |
| 7 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 8 | #include <unordered_map> |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 9 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 10 | #include "base/base_export.h" |
[email protected] | a6ffd7d | 2011-10-12 20:26:50 | [diff] [blame] | 11 | #include "base/bind.h" |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | c62dd9d | 2011-09-21 18:05:41 | [diff] [blame] | 13 | #include "base/location.h" |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 14 | #include "base/logging.h" |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 15 | #include "base/macros.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 16 | #include "base/memory/ref_counted.h" |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 17 | #include "base/observer_list.h" |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 18 | #include "base/sequenced_task_runner.h" |
| 19 | #include "base/stl_util.h" |
| 20 | #include "base/synchronization/lock.h" |
| 21 | #include "base/threading/sequenced_task_runner_handle.h" |
| 22 | #include "base/threading/thread_local.h" |
| 23 | #include "build/build_config.h" |
| 24 | |
| 25 | // TODO(fdoray): Removing these includes causes IWYU failures in other headers, |
| 26 | // remove them in a follow- up CL. |
| 27 | #include "base/memory/ptr_util.h" |
skyostil | 054861d | 2015-04-30 19:06:15 | [diff] [blame] | 28 | #include "base/single_thread_task_runner.h" |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 29 | #include "base/threading/thread_task_runner_handle.h" |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 30 | |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | // |
| 33 | // OVERVIEW: |
| 34 | // |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 35 | // A thread-safe container for a list of observers. This is similar to the |
| 36 | // observer_list (see observer_list.h), but it is more robust for multi- |
| 37 | // threaded situations. |
[email protected] | 52a261f | 2009-03-03 15:01:12 | [diff] [blame] | 38 | // |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 39 | // The following use cases are supported: |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 40 | // * Observers can register for notifications from any sequence. They are |
| 41 | // always notified on the sequence from which they were registered. |
| 42 | // * Any sequence may trigger a notification via Notify(). |
| 43 | // * Observers can remove themselves from the observer list inside of a |
| 44 | // callback. |
| 45 | // * If one sequence is notifying observers concurrently with an observer |
| 46 | // removing itself from the observer list, the notifications will be |
| 47 | // silently dropped. |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 48 | // |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 49 | // The drawback of the threadsafe observer list is that notifications are not |
| 50 | // as real-time as the non-threadsafe version of this class. Notifications |
| 51 | // will always be done via PostTask() to another sequence, whereas with the |
| 52 | // non-thread-safe observer_list, notifications happen synchronously. |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 53 | // |
| 54 | /////////////////////////////////////////////////////////////////////////////// |
[email protected] | bf68712 | 2011-01-11 21:19:54 | [diff] [blame] | 55 | |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 56 | namespace base { |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 57 | namespace internal { |
| 58 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 59 | class BASE_EXPORT ObserverListThreadSafeBase |
| 60 | : public RefCountedThreadSafe<ObserverListThreadSafeBase> { |
| 61 | public: |
| 62 | ObserverListThreadSafeBase() = default; |
tzik | b7990cc | 2016-08-25 02:19:36 | [diff] [blame] | 63 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 64 | protected: |
| 65 | template <typename ObserverType, typename Method> |
| 66 | struct Dispatcher; |
| 67 | |
| 68 | template <typename ObserverType, typename ReceiverType, typename... Params> |
| 69 | struct Dispatcher<ObserverType, void (ReceiverType::*)(Params...)> { |
| 70 | static void Run(void (ReceiverType::*m)(Params...), |
| 71 | Params... params, |
| 72 | ObserverType* obj) { |
| 73 | (obj->*m)(std::forward<Params>(params)...); |
| 74 | } |
| 75 | }; |
| 76 | |
| 77 | struct NotificationDataBase { |
| 78 | NotificationDataBase(void* observer_list_in, const Location& from_here_in) |
| 79 | : observer_list(observer_list_in), from_here(from_here_in) {} |
| 80 | |
| 81 | void* observer_list; |
| 82 | Location from_here; |
| 83 | }; |
| 84 | |
| 85 | virtual ~ObserverListThreadSafeBase() = default; |
| 86 | |
| 87 | static LazyInstance<ThreadLocalPointer<const NotificationDataBase>>::Leaky |
| 88 | tls_current_notification_; |
| 89 | |
| 90 | private: |
| 91 | friend class RefCountedThreadSafe<ObserverListThreadSafeBase>; |
| 92 | |
| 93 | DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafeBase); |
[email protected] | 4c03b2e9 | 2012-01-03 19:36:57 | [diff] [blame] | 94 | }; |
| 95 | |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 96 | } // namespace internal |
| 97 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 98 | template <class ObserverType> |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 99 | class ObserverListThreadSafe : public internal::ObserverListThreadSafeBase { |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 100 | public: |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 101 | ObserverListThreadSafe() = default; |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 102 | explicit ObserverListThreadSafe(ObserverListPolicy policy) |
| 103 | : policy_(policy) {} |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 104 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 105 | // Adds |observer| to the list. |observer| must not already be in the list. |
| 106 | void AddObserver(ObserverType* observer) { |
| 107 | // TODO(fdoray): Change this to a DCHECK once all call sites have a |
| 108 | // SequencedTaskRunnerHandle. |
| 109 | if (!SequencedTaskRunnerHandle::IsSet()) |
[email protected] | c2b1b30 | 2011-11-23 20:34:04 | [diff] [blame] | 110 | return; |
| 111 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 112 | AutoLock auto_lock(lock_); |
| 113 | |
| 114 | // Add |observer| to the list of observers. |
| 115 | DCHECK(!ContainsKey(observers_, observer)); |
| 116 | const scoped_refptr<SequencedTaskRunner> task_runner = |
| 117 | SequencedTaskRunnerHandle::Get(); |
| 118 | observers_[observer] = task_runner; |
| 119 | |
| 120 | // If this is called while a notification is being dispatched on this thread |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 121 | // and |policy_| is ALL, |observer| must be notified (if a notification is |
| 122 | // being dispatched on another thread in parallel, the notification may or |
| 123 | // may not make it to |observer| depending on the outcome of the race to |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 124 | // |lock_|). |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 125 | if (policy_ == ObserverListPolicy::ALL) { |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 126 | const NotificationDataBase* current_notification = |
| 127 | tls_current_notification_.Get().Get(); |
| 128 | if (current_notification && current_notification->observer_list == this) { |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 129 | task_runner->PostTask( |
| 130 | current_notification->from_here, |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 131 | BindOnce( |
| 132 | &ObserverListThreadSafe<ObserverType>::NotifyWrapper, this, |
| 133 | observer, |
| 134 | *static_cast<const NotificationData*>(current_notification))); |
avi | 816e3cf | 2016-10-27 04:10:51 | [diff] [blame] | 135 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 136 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 631739f | 2011-06-05 07:07:12 | [diff] [blame] | 139 | // Remove an observer from the list if it is in the list. |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 140 | // |
| 141 | // If a notification was sent to the observer but hasn't started to run yet, |
| 142 | // it will be aborted. If a notification has started to run, removing the |
| 143 | // observer won't stop it. |
| 144 | void RemoveObserver(ObserverType* observer) { |
| 145 | AutoLock auto_lock(lock_); |
| 146 | observers_.erase(observer); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 147 | } |
| 148 | |
[email protected] | f6969fe | 2012-02-08 00:22:11 | [diff] [blame] | 149 | // Verifies that the list is currently empty (i.e. there are no observers). |
| 150 | void AssertEmpty() const { |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 151 | #if DCHECK_IS_ON() |
| 152 | AutoLock auto_lock(lock_); |
| 153 | DCHECK(observers_.empty()); |
| 154 | #endif |
[email protected] | f6969fe | 2012-02-08 00:22:11 | [diff] [blame] | 155 | } |
| 156 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 157 | // Asynchronously invokes a callback on all observers, on their registration |
| 158 | // sequence. You cannot assume that at the completion of the Notify call that |
| 159 | // all Observers have been Notified. The notification may still be pending |
| 160 | // delivery. |
tzik | b7990cc | 2016-08-25 02:19:36 | [diff] [blame] | 161 | template <typename Method, typename... Params> |
Brett Wilson | 8e88b31 | 2017-09-12 05:22:16 | [diff] [blame] | 162 | void Notify(const Location& from_here, Method m, Params&&... params) { |
tzik | b7990cc | 2016-08-25 02:19:36 | [diff] [blame] | 163 | Callback<void(ObserverType*)> method = |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 164 | Bind(&Dispatcher<ObserverType, Method>::Run, m, |
| 165 | std::forward<Params>(params)...); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 166 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 167 | AutoLock lock(lock_); |
| 168 | for (const auto& observer : observers_) { |
| 169 | observer.second->PostTask( |
reillyg | 9a77a72 | 2015-02-09 20:18:33 | [diff] [blame] | 170 | from_here, |
tzik | 92b7a42 | 2017-04-11 15:00:44 | [diff] [blame] | 171 | BindOnce(&ObserverListThreadSafe<ObserverType>::NotifyWrapper, this, |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 172 | observer.first, NotificationData(this, from_here, method))); |
thakis | 662b3e4 | 2014-12-23 02:02:57 | [diff] [blame] | 173 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 174 | } |
| 175 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 176 | private: |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 177 | friend class RefCountedThreadSafe<ObserverListThreadSafeBase>; |
[email protected] | bf68712 | 2011-01-11 21:19:54 | [diff] [blame] | 178 | |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 179 | struct NotificationData : public NotificationDataBase { |
| 180 | NotificationData(ObserverListThreadSafe* observer_list_in, |
| 181 | const Location& from_here_in, |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 182 | const Callback<void(ObserverType*)>& method_in) |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 183 | : NotificationDataBase(observer_list_in, from_here_in), |
| 184 | method(method_in) {} |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 185 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 186 | Callback<void(ObserverType*)> method; |
[email protected] | 920b1fe | 2011-08-09 21:29:59 | [diff] [blame] | 187 | }; |
| 188 | |
Takuto Ikuta | 88317a4c | 2018-04-27 18:19:58 | [diff] [blame] | 189 | ~ObserverListThreadSafe() override = default; |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 190 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 191 | void NotifyWrapper(ObserverType* observer, |
| 192 | const NotificationData& notification) { |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 193 | { |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 194 | AutoLock auto_lock(lock_); |
avi | 816e3cf | 2016-10-27 04:10:51 | [diff] [blame] | 195 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 196 | // Check whether the observer still needs a notification. |
| 197 | auto it = observers_.find(observer); |
| 198 | if (it == observers_.end()) |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 199 | return; |
Yeol | a89b266 | 2017-07-25 17:09:10 | [diff] [blame] | 200 | DCHECK(it->second->RunsTasksInCurrentSequence()); |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 201 | } |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 202 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 203 | // Keep track of the notification being dispatched on the current thread. |
| 204 | // This will be used if the callback below calls AddObserver(). |
| 205 | // |
| 206 | // Note: |tls_current_notification_| may not be nullptr if this runs in a |
| 207 | // nested loop started by a notification callback. In that case, it is |
| 208 | // important to save the previous value to restore it later. |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 209 | auto& tls_current_notification = tls_current_notification_.Get(); |
| 210 | const NotificationDataBase* const previous_notification = |
| 211 | tls_current_notification.Get(); |
| 212 | tls_current_notification.Set(¬ification); |
fdoray | 5062447 | 2017-01-31 20:26:28 | [diff] [blame] | 213 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 214 | // Invoke the callback. |
| 215 | notification.method.Run(observer); |
| 216 | |
| 217 | // Reset the notification being dispatched on the current thread to its |
| 218 | // previous value. |
Francois Doray | f248acd | 2017-10-30 17:48:49 | [diff] [blame] | 219 | tls_current_notification.Set(previous_notification); |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 220 | } |
| 221 | |
François Degros | d6e2d7dd | 2017-11-22 05:37:02 | [diff] [blame] | 222 | const ObserverListPolicy policy_ = ObserverListPolicy::ALL; |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 223 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 224 | // Synchronizes access to |observers_|. |
| 225 | mutable Lock lock_; |
tsergeant | 0091f85 | 2017-02-01 00:47:41 | [diff] [blame] | 226 | |
fdoray | 6aad314 | 2017-04-10 18:58:21 | [diff] [blame] | 227 | // Keys are observers. Values are the SequencedTaskRunners on which they must |
| 228 | // be notified. |
| 229 | std::unordered_map<ObserverType*, scoped_refptr<SequencedTaskRunner>> |
| 230 | observers_; |
| 231 | |
[email protected] | fc29bc70 | 2010-06-04 16:13:51 | [diff] [blame] | 232 | DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 233 | }; |
| 234 | |
brettw | 5a1613dc | 2015-06-02 05:34:43 | [diff] [blame] | 235 | } // namespace base |
| 236 | |
[email protected] | 503631c | 2008-10-22 23:09:21 | [diff] [blame] | 237 | #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |