Chromium Code Reviews| 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/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/callback_old.h" | 14 #include "base/callback_old.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
| 19 #include "base/message_loop_proxy.h" | 19 #include "base/message_loop_proxy.h" |
| 20 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
| 21 #include "base/task.h" | 21 #include "base/task.h" |
| 22 #include "base/threading/platform_thread.h" | |
| 22 | 23 |
| 23 /////////////////////////////////////////////////////////////////////////////// | 24 /////////////////////////////////////////////////////////////////////////////// |
| 24 // | 25 // |
| 25 // OVERVIEW: | 26 // OVERVIEW: |
| 26 // | 27 // |
| 27 // A thread-safe container for a list of observers. | 28 // A thread-safe container for a list of observers. |
| 28 // This is similar to the observer_list (see observer_list.h), but it | 29 // This is similar to the observer_list (see observer_list.h), but it |
| 29 // is more robust for multi-threaded situations. | 30 // is more robust for multi-threaded situations. |
| 30 // | 31 // |
| 31 // The following use cases are supported: | 32 // The following use cases are supported: |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 NotificationType; | 84 NotificationType; |
| 84 | 85 |
| 85 ObserverListThreadSafe() | 86 ObserverListThreadSafe() |
| 86 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} | 87 : type_(ObserverListBase<ObserverType>::NOTIFY_ALL) {} |
| 87 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} | 88 explicit ObserverListThreadSafe(NotificationType type) : type_(type) {} |
| 88 | 89 |
| 89 // Add an observer to the list. An observer should not be added to | 90 // Add an observer to the list. An observer should not be added to |
| 90 // the same list more than once. | 91 // the same list more than once. |
| 91 void AddObserver(ObserverType* obs) { | 92 void AddObserver(ObserverType* obs) { |
| 92 ObserverList<ObserverType>* list = NULL; | 93 ObserverList<ObserverType>* list = NULL; |
| 93 MessageLoop* loop = MessageLoop::current(); | 94 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
| 94 // TODO(mbelshe): Get rid of this check. Its needed right now because | |
| 95 // Time currently triggers usage of the ObserverList. | |
| 96 // And unittests use time without a MessageLoop. | |
| 97 if (!loop) | |
| 98 return; // Some unittests may access this without a message loop. | |
| 99 { | 95 { |
| 100 base::AutoLock lock(list_lock_); | 96 base::AutoLock lock(list_lock_); |
| 101 if (observer_lists_.find(loop) == observer_lists_.end()) | 97 if (observer_lists_.find(thread_id) == observer_lists_.end()) |
| 102 observer_lists_[loop] = new ObserverListContext(type_); | 98 observer_lists_[thread_id] = new ObserverListContext(type_); |
|
willchan no longer on Chromium
2011/11/22 19:15:23
It's not clear to me that this is safe. Won't Obse
Robert Sesek
2011/11/22 19:24:39
You're right that it may crash if the current loop
willchan no longer on Chromium
2011/11/22 19:31:24
Yep, it looks like the time implementation changed
Robert Sesek
2011/11/22 20:48:51
Done.
I also tried to add a test for the broken b
| |
| 103 list = &(observer_lists_[loop]->list); | 99 list = &(observer_lists_[thread_id]->list); |
| 104 } | 100 } |
| 105 list->AddObserver(obs); | 101 list->AddObserver(obs); |
| 106 } | 102 } |
| 107 | 103 |
| 108 // Remove an observer from the list if it is in the list. | 104 // Remove an observer from the list if it is in the list. |
| 109 // If there are pending notifications in-transit to the observer, they will | 105 // If there are pending notifications in-transit to the observer, they will |
| 110 // be aborted. | 106 // be aborted. |
| 111 // If the observer to be removed is in the list, RemoveObserver MUST | 107 // If the observer to be removed is in the list, RemoveObserver MUST |
| 112 // be called from the same thread which called AddObserver. | 108 // be called from the same thread which called AddObserver. |
| 113 void RemoveObserver(ObserverType* obs) { | 109 void RemoveObserver(ObserverType* obs) { |
| 114 ObserverListContext* context = NULL; | 110 ObserverListContext* context = NULL; |
| 115 ObserverList<ObserverType>* list = NULL; | 111 ObserverList<ObserverType>* list = NULL; |
| 116 MessageLoop* loop = MessageLoop::current(); | 112 base::PlatformThreadId thread_id = base::PlatformThread::CurrentId(); |
| 117 if (!loop) | |
| 118 return; // On shutdown, it is possible that current() is already null. | |
| 119 { | 113 { |
| 120 base::AutoLock lock(list_lock_); | 114 base::AutoLock lock(list_lock_); |
| 121 typename ObserversListMap::iterator it = observer_lists_.find(loop); | 115 typename ObserversListMap::iterator it = observer_lists_.find(thread_id); |
| 122 if (it == observer_lists_.end()) { | 116 if (it == observer_lists_.end()) { |
| 123 // This will happen if we try to remove an observer on a thread | 117 // This will happen if we try to remove an observer on a thread |
| 124 // we never added an observer for. | 118 // we never added an observer for. |
| 125 return; | 119 return; |
| 126 } | 120 } |
| 127 context = it->second; | 121 context = it->second; |
| 128 list = &context->list; | 122 list = &context->list; |
| 129 | 123 |
| 130 // If we're about to remove the last observer from the list, | 124 // If we're about to remove the last observer from the list, |
| 131 // then we can remove this observer_list entirely. | 125 // then we can remove this observer_list entirely. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 221 // ObserverList. This function MUST be called on the thread which owns | 215 // ObserverList. This function MUST be called on the thread which owns |
| 222 // the unsafe ObserverList. | 216 // the unsafe ObserverList. |
| 223 template <class Method, class Params> | 217 template <class Method, class Params> |
| 224 void NotifyWrapper(ObserverListContext* context, | 218 void NotifyWrapper(ObserverListContext* context, |
| 225 const UnboundMethod<ObserverType, Method, Params>& method) { | 219 const UnboundMethod<ObserverType, Method, Params>& method) { |
| 226 | 220 |
| 227 // Check that this list still needs notifications. | 221 // Check that this list still needs notifications. |
| 228 { | 222 { |
| 229 base::AutoLock lock(list_lock_); | 223 base::AutoLock lock(list_lock_); |
| 230 typename ObserversListMap::iterator it = | 224 typename ObserversListMap::iterator it = |
| 231 observer_lists_.find(MessageLoop::current()); | 225 observer_lists_.find(base::PlatformThread::CurrentId()); |
| 232 | 226 |
| 233 // The ObserverList could have been removed already. In fact, it could | 227 // The ObserverList could have been removed already. In fact, it could |
| 234 // have been removed and then re-added! If the master list's loop | 228 // have been removed and then re-added! If the master list's loop |
| 235 // does not match this one, then we do not need to finish this | 229 // does not match this one, then we do not need to finish this |
| 236 // notification. | 230 // notification. |
| 237 if (it == observer_lists_.end() || it->second != context) | 231 if (it == observer_lists_.end() || it->second != context) |
| 238 return; | 232 return; |
| 239 } | 233 } |
| 240 | 234 |
| 241 { | 235 { |
| 242 typename ObserverList<ObserverType>::Iterator it(context->list); | 236 typename ObserverList<ObserverType>::Iterator it(context->list); |
| 243 ObserverType* obs; | 237 ObserverType* obs; |
| 244 while ((obs = it.GetNext()) != NULL) | 238 while ((obs = it.GetNext()) != NULL) |
| 245 method.Run(obs); | 239 method.Run(obs); |
| 246 } | 240 } |
| 247 | 241 |
| 248 // If there are no more observers on the list, we can now delete it. | 242 // If there are no more observers on the list, we can now delete it. |
| 249 if (context->list.size() == 0) { | 243 if (context->list.size() == 0) { |
| 250 { | 244 { |
| 251 base::AutoLock lock(list_lock_); | 245 base::AutoLock lock(list_lock_); |
| 252 // Remove |list| if it's not already removed. | 246 // Remove |list| if it's not already removed. |
| 253 // This can happen if multiple observers got removed in a notification. | 247 // This can happen if multiple observers got removed in a notification. |
| 254 // See http://crbug.com/55725. | 248 // See http://crbug.com/55725. |
| 255 typename ObserversListMap::iterator it = | 249 typename ObserversListMap::iterator it = |
| 256 observer_lists_.find(MessageLoop::current()); | 250 observer_lists_.find(base::PlatformThread::CurrentId()); |
| 257 if (it != observer_lists_.end() && it->second == context) | 251 if (it != observer_lists_.end() && it->second == context) |
| 258 observer_lists_.erase(it); | 252 observer_lists_.erase(it); |
| 259 } | 253 } |
| 260 delete context; | 254 delete context; |
| 261 } | 255 } |
| 262 } | 256 } |
| 263 | 257 |
| 264 typedef std::map<MessageLoop*, ObserverListContext*> ObserversListMap; | 258 typedef std::map<base::PlatformThreadId, ObserverListContext*> |
| 259 ObserversListMap; | |
| 265 | 260 |
| 266 base::Lock list_lock_; // Protects the observer_lists_. | 261 base::Lock list_lock_; // Protects the observer_lists_. |
| 267 ObserversListMap observer_lists_; | 262 ObserversListMap observer_lists_; |
| 268 const NotificationType type_; | 263 const NotificationType type_; |
| 269 | 264 |
| 270 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); | 265 DISALLOW_COPY_AND_ASSIGN(ObserverListThreadSafe); |
| 271 }; | 266 }; |
| 272 | 267 |
| 273 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ | 268 #endif // BASE_OBSERVER_LIST_THREADSAFE_H_ |
| OLD | NEW |