diff options
author | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-08 02:07:29 +0000 |
---|---|---|
committer | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-08 02:07:29 +0000 |
commit | 4bc4f127afcbbd22dec6eb3f7109e422d54ab6d4 (patch) | |
tree | 5eb0922f938a7ed2ec314e0cba416cbbdfe1de6c | |
parent | f79bec58adbbeeb055ddb221c1e9b6a6fc13eb93 (diff) | |
download | chromium_src-4bc4f127afcbbd22dec6eb3f7109e422d54ab6d4.zip chromium_src-4bc4f127afcbbd22dec6eb3f7109e422d54ab6d4.tar.gz chromium_src-4bc4f127afcbbd22dec6eb3f7109e422d54ab6d4.tar.bz2 |
IDMap derives from NonThreadSafe.
During a code review I noticed that RenderViewHost::FromID was being called from the IO thread, but there were no automated assertions that fired.
BUG=None
TEST=DCHECK fires when I add bad code. Otherwise Chrome works without crashing in Debug build.
Review URL: http://codereview.chromium.org/6591108
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@77232 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | base/id_map.h | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/base/id_map.h b/base/id_map.h index 9a41d74..5aea98e 100644 --- a/base/id_map.h +++ b/base/id_map.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -11,6 +11,7 @@ #include "base/basictypes.h" #include "base/hash_tables.h" #include "base/logging.h" +#include "base/threading/non_thread_safe.h" // Ownership semantics - own pointer means the pointer is deleted in Remove() // & during destruction @@ -31,16 +32,23 @@ enum IDMapOwnershipSemantics { // This class does not have a virtual destructor, do not inherit from it when // ownership semantics are set to own because pointers will leak. template<typename T, IDMapOwnershipSemantics OS = IDMapExternalPointer> -class IDMap { +class IDMap : public base::NonThreadSafe { private: typedef int32 KeyType; typedef base::hash_map<KeyType, T*> HashTable; public: IDMap() : iteration_depth_(0), next_id_(1), check_on_null_data_(false) { + // A number of consumers of IDMap create it on one thread but always access + // it from a different, but consitent, thread post-construction. + DetachFromThread(); } ~IDMap() { + // Many IDMap's are static, and hence will be destroyed on the main thread. + // However, all the accesses may take place on another thread, such as the + // IO thread. Detaching again to clean this up. + DetachFromThread(); Releaser<OS, 0>::release_all(&data_); } @@ -49,6 +57,7 @@ class IDMap { // Adds a view with an automatically generated unique ID. See AddWithID. KeyType Add(T* data) { + DCHECK(CalledOnValidThread()); CHECK(!check_on_null_data_ || data); KeyType this_id = next_id_; DCHECK(data_.find(this_id) == data_.end()) << "Inserting duplicate item"; @@ -62,12 +71,14 @@ class IDMap { // this function, or allow this object to generate IDs and call Add. These // two methods may not be mixed, or duplicate IDs may be generated void AddWithID(T* data, KeyType id) { + DCHECK(CalledOnValidThread()); CHECK(!check_on_null_data_ || data); DCHECK(data_.find(id) == data_.end()) << "Inserting duplicate item"; data_[id] = data; } void Remove(KeyType id) { + DCHECK(CalledOnValidThread()); typename HashTable::iterator i = data_.find(id); if (i == data_.end()) { NOTREACHED() << "Attempting to remove an item not in the list"; @@ -83,10 +94,12 @@ class IDMap { } bool IsEmpty() const { + DCHECK(CalledOnValidThread()); return size() == 0u; } T* Lookup(KeyType id) const { + DCHECK(CalledOnValidThread()); typename HashTable::const_iterator i = data_.find(id); if (i == data_.end()) return NULL; @@ -94,6 +107,7 @@ class IDMap { } size_t size() const { + DCHECK(CalledOnValidThread()); return data_.size() - removed_ids_.size(); } @@ -105,28 +119,34 @@ class IDMap { Iterator(IDMap<T, OS>* map) : map_(map), iter_(map_->data_.begin()) { + DCHECK(map->CalledOnValidThread()); ++map_->iteration_depth_; SkipRemovedEntries(); } ~Iterator() { + DCHECK(map_->CalledOnValidThread()); if (--map_->iteration_depth_ == 0) map_->Compact(); } bool IsAtEnd() const { + DCHECK(map_->CalledOnValidThread()); return iter_ == map_->data_.end(); } KeyType GetCurrentKey() const { + DCHECK(map_->CalledOnValidThread()); return iter_->first; } ReturnType* GetCurrentValue() const { + DCHECK(map_->CalledOnValidThread()); return iter_->second; } void Advance() { + DCHECK(map_->CalledOnValidThread()); ++iter_; SkipRemovedEntries(); } |