summaryrefslogtreecommitdiffstats
path: root/base/thread_local_storage.h
diff options
context:
space:
mode:
authorevanm@google.com <evanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-20 21:47:40 +0000
committerevanm@google.com <evanm@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2008-08-20 21:47:40 +0000
commitc3ec35638ea6594b6bdbac7ff92c38add8984623 (patch)
treec51541ac2451e66c410b3d244e1f59ac2218c57e /base/thread_local_storage.h
parente537c3515e270527ba2fa54fcfc4a1ed49cc975e (diff)
downloadchromium_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.h')
-rw-r--r--base/thread_local_storage.h81
1 files changed, 52 insertions, 29 deletions
diff --git a/base/thread_local_storage.h b/base/thread_local_storage.h
index 245a73f..fba90ae 100644
--- a/base/thread_local_storage.h
+++ b/base/thread_local_storage.h
@@ -32,43 +32,62 @@
#include "base/basictypes.h"
-#if defined(OS_WIN)
-typedef int TLSSlot;
-#elif defined(OS_POSIX)
+#if defined(OS_POSIX)
#include <pthread.h>
-typedef pthread_key_t TLSSlot;
-#endif // OS_*
+#endif
-// Wrapper for thread local storage. This class doesn't
-// do much except provide an API for portability later.
+// Wrapper for thread local storage. This class doesn't do much except provide
+// an API for portability.
class ThreadLocalStorage {
public:
- // Prototype for the TLS destructor function, which can be
- // optionally used to cleanup thread local storage on
- // thread exit. 'value' is the data that is stored
- // in thread local storage.
+
+ // Prototype for the TLS destructor function, which can be optionally used to
+ // cleanup thread local storage on thread exit. 'value' is the data that is
+ // stored in thread local storage.
typedef void (*TLSDestructorFunc)(void* value);
- // Allocate a TLS 'slot'.
- // 'destructor' is a pointer to a function to perform
- // per-thread cleanup of this object. If set to NULL,
- // no cleanup is done for this TLS slot.
- // Returns an index > 0 on success, or -1 on failure.
- static TLSSlot Alloc(TLSDestructorFunc destructor = NULL);
+ // A key representing one value stored in TLS.
+ class Slot {
+ public:
+ Slot(TLSDestructorFunc destructor = NULL);
+
+ // This constructor should be used for statics.
+ // It returns an uninitialized Slot.
+ explicit Slot(base::LinkerInitialized x) {}
+
+ // Set up the TLS slot. Called by the constructor.
+ // 'destructor' is a pointer to a function to perform per-thread cleanup of
+ // this object. If set to NULL, no cleanup is done for this TLS slot.
+ // Returns false on error.
+ bool Initialize(TLSDestructorFunc destructor);
+
+ // Free a previously allocated TLS 'slot'.
+ // If a destructor was set for this slot, removes
+ // the destructor so that remaining threads exiting
+ // will not free data.
+ void Free();
- // Free a previously allocated TLS 'slot'.
- // If a destructor was set for this slot, removes
- // the destructor so that remaining threads exiting
- // will not free data.
- static void Free(TLSSlot slot);
+ // Get the thread-local value stored in slot 'slot'.
+ // Values are guaranteed to initially be zero.
+ void* Get() const;
- // Get the thread-local value stored in slot 'slot'.
- // Values are guaranteed to initially be zero.
- static void* Get(TLSSlot slot);
+ // Set the thread-local value stored in slot 'slot' to
+ // value 'value'.
+ void Set(void* value);
- // Set the thread-local value stored in slot 'slot' to
- // value 'value'.
- static void Set(TLSSlot slot, void* value);
+ bool initialized() const { return initialized_; }
+
+ private:
+ // The internals of this struct should be considered private.
+ bool initialized_;
+#if defined(OS_WIN)
+ int slot_;
+#elif defined(OS_POSIX)
+ pthread_key_t key_;
+#endif
+
+ DISALLOW_COPY_AND_ASSIGN(Slot);
+ };
#if defined(OS_WIN)
// Function called when on thread exit to call TLS
@@ -90,7 +109,11 @@ class ThreadLocalStorage {
static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize];
#endif // OS_WIN
- DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage);
+ DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
};
+// Temporary backwards-compatible name.
+// TODO(evanm): replace all usage of TLSSlot.
+typedef ThreadLocalStorage::Slot TLSSlot;
+
#endif // BASE_THREAD_LOCAL_STORAGE_H_