diff options
author | evanm@google.com <evanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-20 21:47:40 +0000 |
---|---|---|
committer | evanm@google.com <evanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-08-20 21:47:40 +0000 |
commit | c3ec35638ea6594b6bdbac7ff92c38add8984623 (patch) | |
tree | c51541ac2451e66c410b3d244e1f59ac2218c57e /base/thread_local_storage_win.cc | |
parent | e537c3515e270527ba2fa54fcfc4a1ed49cc975e (diff) | |
download | chromium_src-c3ec35638ea6594b6bdbac7ff92c38add8984623.zip chromium_src-c3ec35638ea6594b6bdbac7ff92c38add8984623.tar.gz chromium_src-c3ec35638ea6594b6bdbac7ff92c38add8984623.tar.bz2 |
TrackedObjects assumes you can use a "TLS slot" of -1 to indicate uninitialized. This isn't true for the pthread_key_t type, which is unsigned on Linux and reportedly a struct on Macs. This change modifies the Slot type to be a struct containing an "initialized" flag.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@1113 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/thread_local_storage_win.cc')
-rw-r--r-- | base/thread_local_storage_win.cc | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/base/thread_local_storage_win.cc b/base/thread_local_storage_win.cc index 8157e88..154935a 100644 --- a/base/thread_local_storage_win.cc +++ b/base/thread_local_storage_win.cc @@ -55,7 +55,7 @@ long ThreadLocalStorage::tls_max_ = 1; ThreadLocalStorage::TLSDestructorFunc ThreadLocalStorage::tls_destructors_[kThreadLocalStorageSize]; -void **ThreadLocalStorage::Initialize() { +void** ThreadLocalStorage::Initialize() { if (tls_key_ == TLS_OUT_OF_INDEXES) { long value = TlsAlloc(); DCHECK(value != TLS_OUT_OF_INDEXES); @@ -73,52 +73,59 @@ void **ThreadLocalStorage::Initialize() { DCHECK(TlsGetValue(tls_key_) == NULL); // Create an array to store our data. - void **tls_data = new void*[kThreadLocalStorageSize]; + void** tls_data = new void*[kThreadLocalStorageSize]; memset(tls_data, 0, sizeof(void*[kThreadLocalStorageSize])); TlsSetValue(tls_key_, tls_data); return tls_data; } -TLSSlot ThreadLocalStorage::Alloc(TLSDestructorFunc destructor) { +ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor) + : initialized_(false) { + Initialize(destructor); +} + +bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) { if (tls_key_ == TLS_OUT_OF_INDEXES || !TlsGetValue(tls_key_)) - Initialize(); + ThreadLocalStorage::Initialize(); // Grab a new slot. - int slot = InterlockedIncrement(&tls_max_) - 1; - if (slot >= kThreadLocalStorageSize) { + slot_ = InterlockedIncrement(&tls_max_) - 1; + if (slot_ >= kThreadLocalStorageSize) { NOTREACHED(); - return -1; + return false; } // Setup our destructor. - tls_destructors_[slot] = destructor; - return slot; + tls_destructors_[slot_] = destructor; + initialized_ = true; + return true; } -void ThreadLocalStorage::Free(TLSSlot slot) { +void ThreadLocalStorage::Slot::Free() { // At this time, we don't reclaim old indices for TLS slots. // So all we need to do is wipe the destructor. - tls_destructors_[slot] = NULL; + tls_destructors_[slot_] = NULL; + initialized_ = false; } -void* ThreadLocalStorage::Get(TLSSlot slot) { - void **tls_data = static_cast<void**>(TlsGetValue(tls_key_)); +void* ThreadLocalStorage::Slot::Get() const { + void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); if (!tls_data) - tls_data = Initialize(); - DCHECK(slot >= 0 && slot < kThreadLocalStorageSize); - return tls_data[slot]; + tls_data = ThreadLocalStorage::Initialize(); + DCHECK(slot_ >= 0 && slot_ < kThreadLocalStorageSize); + return tls_data[slot_]; } -void ThreadLocalStorage::Set(TLSSlot slot, void* value) { - void **tls_data = static_cast<void**>(TlsGetValue(tls_key_)); +void ThreadLocalStorage::Slot::Set(void* value) { + void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); if (!tls_data) - tls_data = Initialize(); - DCHECK(slot >= 0 && slot < kThreadLocalStorageSize); - tls_data[slot] = value; + tls_data = ThreadLocalStorage::Initialize(); + DCHECK(slot_ >= 0 && slot_ < kThreadLocalStorageSize); + tls_data[slot_] = value; } void ThreadLocalStorage::ThreadExit() { - void **tls_data = static_cast<void**>(TlsGetValue(tls_key_)); + void** tls_data = static_cast<void**>(TlsGetValue(tls_key_)); // Maybe we have never initialized TLS for this thread. if (!tls_data) @@ -126,7 +133,7 @@ void ThreadLocalStorage::ThreadExit() { for (int slot = 0; slot < tls_max_; slot++) { if (tls_destructors_[slot] != NULL) { - void *value = tls_data[slot]; + void* value = tls_data[slot]; tls_destructors_[slot](value); } } |