jhawkins@chromium.org | 4a31021 | 2012-01-04 04:36:57 +0900 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [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 | |
erg@chromium.org | 6f206e7 | 2010-07-15 03:58:17 +0900 | [diff] [blame] | 8 | #include <algorithm> |
erg@chromium.org | a752852 | 2010-07-16 02:23:23 +0900 | [diff] [blame] | 9 | #include <map> |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 10 | |
| 11 | #include "base/basictypes.h" |
jhawkins@chromium.org | 22d6756 | 2011-10-13 05:26:50 +0900 | [diff] [blame] | 12 | #include "base/bind.h" |
ajwong@chromium.org | 8e2e300 | 2011-09-22 03:05:41 +0900 | [diff] [blame] | 13 | #include "base/location.h" |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 14 | #include "base/logging.h" |
levin@chromium.org | 5c52868 | 2011-03-28 10:54:15 +0900 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
avi@chromium.org | a043a86 | 2013-07-18 17:12:40 +0900 | [diff] [blame] | 16 | #include "base/message_loop/message_loop.h" |
brettw@chromium.org | ab2678f | 2013-06-11 05:56:28 +0900 | [diff] [blame] | 17 | #include "base/message_loop/message_loop_proxy.h" |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 18 | #include "base/observer_list.h" |
tfarina@chromium.org | e2903a5 | 2013-03-17 14:52:05 +0900 | [diff] [blame] | 19 | #include "base/stl_util.h" |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 20 | #include "base/threading/platform_thread.h" |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 21 | |
| 22 | /////////////////////////////////////////////////////////////////////////////// |
| 23 | // |
| 24 | // OVERVIEW: |
| 25 | // |
| 26 | // A thread-safe container for a list of observers. |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 27 | // This is similar to the observer_list (see observer_list.h), but it |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 28 | // is more robust for multi-threaded situations. |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 29 | // |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 30 | // The following use cases are supported: |
| 31 | // * Observers can register for notifications from any thread. |
| 32 | // Callbacks to the observer will occur on the same thread where |
| 33 | // the observer initially called AddObserver() from. |
pkasting@chromium.org | aed4e58 | 2010-06-26 06:30:38 +0900 | [diff] [blame] | 34 | // * Any thread may trigger a notification via Notify(). |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 35 | // * Observers can remove themselves from the observer list inside |
| 36 | // of a callback. |
| 37 | // * If one thread is notifying observers concurrently with an observer |
| 38 | // removing itself from the observer list, the notifications will |
| 39 | // be silently dropped. |
| 40 | // |
| 41 | // The drawback of the threadsafe observer list is that notifications |
| 42 | // are not as real-time as the non-threadsafe version of this class. |
| 43 | // Notifications will always be done via PostTask() to another thread, |
| 44 | // whereas with the non-thread-safe observer_list, notifications happen |
| 45 | // synchronously and immediately. |
| 46 | // |
| 47 | // IMPLEMENTATION NOTES |
| 48 | // The ObserverListThreadSafe maintains an ObserverList for each thread |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 49 | // which uses the ThreadSafeObserver. When Notifying the observers, |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 50 | // we simply call PostTask to each registered thread, and then each thread |
| 51 | // will notify its regular ObserverList. |
| 52 | // |
| 53 | /////////////////////////////////////////////////////////////////////////////// |
akalin@chromium.org | e43be3d | 2011-01-12 06:19:54 +0900 | [diff] [blame] | 54 | |
| 55 | // Forward declaration for ObserverListThreadSafeTraits. |
| 56 | template <class ObserverType> |
| 57 | class ObserverListThreadSafe; |
| 58 | |
jhawkins@chromium.org | 4a31021 | 2012-01-04 04:36:57 +0900 | [diff] [blame] | 59 | // An UnboundMethod is a wrapper for a method where the actual object is |
| 60 | // provided at Run dispatch time. |
| 61 | template <class T, class Method, class Params> |
| 62 | class UnboundMethod { |
| 63 | public: |
| 64 | UnboundMethod(Method m, const Params& p) : m_(m), p_(p) { |
| 65 | COMPILE_ASSERT( |
| 66 | (base::internal::ParamsUseScopedRefptrCorrectly<Params>::value), |
| 67 | badunboundmethodparams); |
| 68 | } |
| 69 | void Run(T* obj) const { |
| 70 | DispatchToMethod(obj, m_, p_); |
| 71 | } |
| 72 | private: |
| 73 | Method m_; |
| 74 | Params p_; |
| 75 | }; |
| 76 | |
akalin@chromium.org | e43be3d | 2011-01-12 06:19:54 +0900 | [diff] [blame] | 77 | // This class is used to work around VS2005 not accepting: |
| 78 | // |
| 79 | // friend class |
| 80 | // base::RefCountedThreadSafe<ObserverListThreadSafe<ObserverType> >; |
| 81 | // |
| 82 | // Instead of friending the class, we could friend the actual function |
| 83 | // which calls delete. However, this ends up being |
| 84 | // RefCountedThreadSafe::DeleteInternal(), which is private. So we |
| 85 | // define our own templated traits class so we can friend it. |
| 86 | template <class T> |
| 87 | struct ObserverListThreadSafeTraits { |
| 88 | static void Destruct(const ObserverListThreadSafe<T>* x) { |
| 89 | delete x; |
| 90 | } |
| 91 | }; |
| 92 | |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 93 | template <class ObserverType> |
maruel@chromium.org | 8fe7adc | 2009-03-04 00:01:12 +0900 | [diff] [blame] | 94 | class ObserverListThreadSafe |
akalin@chromium.org | e43be3d | 2011-01-12 06:19:54 +0900 | [diff] [blame] | 95 | : public base::RefCountedThreadSafe< |
| 96 | ObserverListThreadSafe<ObserverType>, |
| 97 | ObserverListThreadSafeTraits<ObserverType> > { |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 98 | public: |
willchan@chromium.org | 4c7f03e | 2010-09-01 07:54:21 +0900 | [diff] [blame] | 99 | typedef typename ObserverList<ObserverType>::NotificationType |
| 100 | NotificationType; |
| 101 | |
| 102 | ObserverListThreadSafe() |
| 103 | : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} |
| 104 | explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 105 | |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 106 | // Add an observer to the list. An observer should not be added to |
| 107 | // the same list more than once. |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 108 | void AddObserver(ObserverType* obs) { |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 109 | // If there is not a current MessageLoop, it is impossible to notify on it, |
| 110 | // so do not add the observer. |
xhwang@chromium.org | ff47fa5 | 2013-05-04 22:57:01 +0900 | [diff] [blame] | 111 | if (!base::MessageLoop::current()) |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 112 | return; |
| 113 | |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 114 | ObserverList<ObserverType>* list = NULL; |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 115 | base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 116 | { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 117 | base::AutoLock lock(list_lock_); |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 118 | if (observer_lists_.find(thread_id) == observer_lists_.end()) |
| 119 | observer_lists_[thread_id] = new ObserverListContext(type_); |
| 120 | list = &(observer_lists_[thread_id]->list); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 121 | } |
| 122 | list->AddObserver(obs); |
| 123 | } |
| 124 | |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 125 | // Remove an observer from the list if it is in the list. |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 126 | // If there are pending notifications in-transit to the observer, they will |
| 127 | // be aborted. |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 128 | // If the observer to be removed is in the list, RemoveObserver MUST |
| 129 | // be called from the same thread which called AddObserver. |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 130 | void RemoveObserver(ObserverType* obs) { |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 131 | ObserverListContext* context = NULL; |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 132 | ObserverList<ObserverType>* list = NULL; |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 133 | base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 134 | { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 135 | base::AutoLock lock(list_lock_); |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 136 | typename ObserversListMap::iterator it = observer_lists_.find(thread_id); |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 137 | if (it == observer_lists_.end()) { |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 138 | // This will happen if we try to remove an observer on a thread |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 139 | // we never added an observer for. |
mbelshe@google.com | 2d95cab | 2008-11-04 02:15:07 +0900 | [diff] [blame] | 140 | return; |
| 141 | } |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 142 | context = it->second; |
| 143 | list = &context->list; |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 144 | |
| 145 | // If we're about to remove the last observer from the list, |
| 146 | // then we can remove this observer_list entirely. |
akalin@chromium.org | cbec5fb | 2011-06-05 16:07:12 +0900 | [diff] [blame] | 147 | if (list->HasObserver(obs) && list->size() == 1) |
| 148 | observer_lists_.erase(it); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 149 | } |
| 150 | list->RemoveObserver(obs); |
| 151 | |
| 152 | // If RemoveObserver is called from a notification, the size will be |
| 153 | // nonzero. Instead of deleting here, the NotifyWrapper will delete |
| 154 | // when it finishes iterating. |
| 155 | if (list->size() == 0) |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 156 | delete context; |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 157 | } |
| 158 | |
sergeyu@chromium.org | 86992ef | 2012-02-08 09:22:11 +0900 | [diff] [blame] | 159 | // Verifies that the list is currently empty (i.e. there are no observers). |
| 160 | void AssertEmpty() const { |
| 161 | base::AutoLock lock(list_lock_); |
| 162 | DCHECK(observer_lists_.empty()); |
| 163 | } |
| 164 | |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 165 | // Notify methods. |
| 166 | // Make a thread-safe callback to each Observer in the list. |
| 167 | // Note, these calls are effectively asynchronous. You cannot assume |
| 168 | // that at the completion of the Notify call that all Observers have |
| 169 | // been Notified. The notification may still be pending delivery. |
thakis | d5c8587 | 2014-12-23 11:02:57 +0900 | [diff] [blame] | 170 | template <class Method, class... Params> |
reillyg | 57b1bc5 | 2015-02-10 05:18:33 +0900 | [diff] [blame] | 171 | void Notify(const tracked_objects::Location& from_here, |
| 172 | Method m, |
| 173 | const Params&... params) { |
thakis | d5c8587 | 2014-12-23 11:02:57 +0900 | [diff] [blame] | 174 | UnboundMethod<ObserverType, Method, Tuple<Params...>> method( |
| 175 | m, MakeTuple(params...)); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 176 | |
thakis | d5c8587 | 2014-12-23 11:02:57 +0900 | [diff] [blame] | 177 | base::AutoLock lock(list_lock_); |
| 178 | for (const auto& entry : observer_lists_) { |
| 179 | ObserverListContext* context = entry.second; |
| 180 | context->loop->PostTask( |
reillyg | 57b1bc5 | 2015-02-10 05:18:33 +0900 | [diff] [blame] | 181 | from_here, |
thakis | d5c8587 | 2014-12-23 11:02:57 +0900 | [diff] [blame] | 182 | base::Bind( |
| 183 | &ObserverListThreadSafe<ObserverType>::template NotifyWrapper< |
| 184 | Method, Tuple<Params...>>, |
| 185 | this, context, method)); |
| 186 | } |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 187 | } |
| 188 | |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 189 | private: |
akalin@chromium.org | e43be3d | 2011-01-12 06:19:54 +0900 | [diff] [blame] | 190 | // See comment above ObserverListThreadSafeTraits' definition. |
| 191 | friend struct ObserverListThreadSafeTraits<ObserverType>; |
| 192 | |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 193 | struct ObserverListContext { |
| 194 | explicit ObserverListContext(NotificationType type) |
nduca@chromium.org | ba04861 | 2011-08-16 05:33:46 +0900 | [diff] [blame] | 195 | : loop(base::MessageLoopProxy::current()), |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 196 | list(type) { |
| 197 | } |
| 198 | |
| 199 | scoped_refptr<base::MessageLoopProxy> loop; |
| 200 | ObserverList<ObserverType> list; |
| 201 | |
danakj | de6142a | 2015-03-08 08:04:23 +0900 | [diff] [blame^] | 202 | private: |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 203 | DISALLOW_COPY_AND_ASSIGN(ObserverListContext); |
| 204 | }; |
| 205 | |
akalin@chromium.org | 982d9d5 | 2011-01-11 05:40:36 +0900 | [diff] [blame] | 206 | ~ObserverListThreadSafe() { |
tfarina@chromium.org | e2903a5 | 2013-03-17 14:52:05 +0900 | [diff] [blame] | 207 | STLDeleteValues(&observer_lists_); |
akalin@chromium.org | 982d9d5 | 2011-01-11 05:40:36 +0900 | [diff] [blame] | 208 | } |
| 209 | |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 210 | // Wrapper which is called to fire the notifications for each thread's |
| 211 | // ObserverList. This function MUST be called on the thread which owns |
| 212 | // the unsafe ObserverList. |
| 213 | template <class Method, class Params> |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 214 | void NotifyWrapper(ObserverListContext* context, |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 215 | const UnboundMethod<ObserverType, Method, Params>& method) { |
| 216 | |
| 217 | // Check that this list still needs notifications. |
| 218 | { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 219 | base::AutoLock lock(list_lock_); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 220 | typename ObserversListMap::iterator it = |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 221 | observer_lists_.find(base::PlatformThread::CurrentId()); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 222 | |
| 223 | // The ObserverList could have been removed already. In fact, it could |
| 224 | // have been removed and then re-added! If the master list's loop |
| 225 | // does not match this one, then we do not need to finish this |
| 226 | // notification. |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 227 | if (it == observer_lists_.end() || it->second != context) |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 228 | return; |
| 229 | } |
| 230 | |
| 231 | { |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 232 | typename ObserverList<ObserverType>::Iterator it(context->list); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 233 | ObserverType* obs; |
| 234 | while ((obs = it.GetNext()) != NULL) |
| 235 | method.Run(obs); |
| 236 | } |
| 237 | |
| 238 | // If there are no more observers on the list, we can now delete it. |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 239 | if (context->list.size() == 0) { |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 240 | { |
brettw@chromium.org | abe477a | 2011-01-21 13:55:52 +0900 | [diff] [blame] | 241 | base::AutoLock lock(list_lock_); |
willchan@chromium.org | 2777ca5 | 2010-09-18 04:33:06 +0900 | [diff] [blame] | 242 | // Remove |list| if it's not already removed. |
| 243 | // This can happen if multiple observers got removed in a notification. |
| 244 | // See http://crbug.com/55725. |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 245 | typename ObserversListMap::iterator it = |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 246 | observer_lists_.find(base::PlatformThread::CurrentId()); |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 247 | if (it != observer_lists_.end() && it->second == context) |
willchan@chromium.org | 2777ca5 | 2010-09-18 04:33:06 +0900 | [diff] [blame] | 248 | observer_lists_.erase(it); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 249 | } |
adamk@chromium.org | 2bc73e7 | 2011-08-10 06:29:59 +0900 | [diff] [blame] | 250 | delete context; |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
rsesek@chromium.org | 1aab77c | 2011-11-24 05:34:04 +0900 | [diff] [blame] | 254 | // Key by PlatformThreadId because in tests, clients can attempt to remove |
| 255 | // observers without a MessageLoop. If this were keyed by MessageLoop, that |
| 256 | // operation would be silently ignored, leaving garbage in the ObserverList. |
| 257 | typedef std::map<base::PlatformThreadId, ObserverListContext*> |
| 258 | ObserversListMap; |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 259 | |
sergeyu@chromium.org | 86992ef | 2012-02-08 09:22:11 +0900 | [diff] [blame] | 260 | mutable base::Lock list_lock_; // Protects the observer_lists_. |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 261 | ObserversListMap observer_lists_; |
willchan@chromium.org | 4c7f03e | 2010-09-01 07:54:21 +0900 | [diff] [blame] | 262 | const NotificationType type_; |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 263 | |
tfarina@chromium.org | 0aad842 | 2010-06-05 01:13:51 +0900 | [diff] [blame] | 264 | DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
mbelshe@google.com | 877d203 | 2008-10-23 08:09:21 +0900 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |