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
CrOS-Libchrome-Original-Commit: c3ec35638ea6594b6bdbac7ff92c38add8984623
diff --git a/base/thread_local_storage_posix.cc b/base/thread_local_storage_posix.cc
index bcc982f..7c24e59 100644
--- a/base/thread_local_storage_posix.cc
+++ b/base/thread_local_storage_posix.cc
@@ -31,23 +31,39 @@
#include "base/logging.h"
-TLSSlot ThreadLocalStorage::Alloc(TLSDestructorFunc destructor) {
- TLSSlot key;
- int error = pthread_key_create(&key, destructor);
+ThreadLocalStorage::Slot::Slot(TLSDestructorFunc destructor)
+ : initialized_(false) {
+ Initialize(destructor);
+}
+
+bool ThreadLocalStorage::Slot::Initialize(TLSDestructorFunc destructor) {
+ DCHECK(!initialized_);
+ int error = pthread_key_create(&key_, destructor);
+ if (error) {
+ NOTREACHED();
+ return false;
+ }
+
+ initialized_ = true;
+ return true;
+}
+
+void ThreadLocalStorage::Slot::Free() {
+ DCHECK(initialized_);
+ int error = pthread_key_delete(key_);
if (error)
NOTREACHED();
-
- return key;
+ initialized_ = false;
}
-void ThreadLocalStorage::Free(TLSSlot slot) {
- pthread_key_delete(slot);
+void* ThreadLocalStorage::Slot::Get() const {
+ DCHECK(initialized_);
+ return pthread_getspecific(key_);
}
-void* ThreadLocalStorage::Get(TLSSlot slot) {
- return pthread_getspecific(slot);
-}
-
-void ThreadLocalStorage::Set(TLSSlot slot, void* value) {
- pthread_setspecific(slot, value);
+void ThreadLocalStorage::Slot::Set(void* value) {
+ DCHECK(initialized_);
+ int error = pthread_setspecific(key_, value);
+ if (error)
+ NOTREACHED();
}