summaryrefslogtreecommitdiffstats
path: root/base/thread_local_storage.h
diff options
context:
space:
mode:
authorinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 21:49:38 +0000
committerinitial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98>2008-07-26 21:49:38 +0000
commitd7cae12696b96500c05dd2d430f6238922c20c96 (patch)
treeecff27b367735535b2a66477f8cd89d3c462a6c0 /base/thread_local_storage.h
parentee2815e28d408216cf94e874825b6bcf76c69083 (diff)
downloadchromium_src-d7cae12696b96500c05dd2d430f6238922c20c96.zip
chromium_src-d7cae12696b96500c05dd2d430f6238922c20c96.tar.gz
chromium_src-d7cae12696b96500c05dd2d430f6238922c20c96.tar.bz2
Add base to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/thread_local_storage.h')
-rw-r--r--base/thread_local_storage.h95
1 files changed, 95 insertions, 0 deletions
diff --git a/base/thread_local_storage.h b/base/thread_local_storage.h
new file mode 100644
index 0000000..e27edb8
--- /dev/null
+++ b/base/thread_local_storage.h
@@ -0,0 +1,95 @@
+// Copyright 2008, Google Inc.
+// All rights reserved.
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are
+// met:
+//
+// * Redistributions of source code must retain the above copyright
+// notice, this list of conditions and the following disclaimer.
+// * Redistributions in binary form must reproduce the above
+// copyright notice, this list of conditions and the following disclaimer
+// in the documentation and/or other materials provided with the
+// distribution.
+// * Neither the name of Google Inc. nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#ifndef BASE_THREAD_LOCAL_STORAGE_H__
+#define BASE_THREAD_LOCAL_STORAGE_H__
+
+#include "base/basictypes.h"
+
+#ifdef WIN32
+typedef int TLSSlot;
+#else
+typedef pthread_key_t TLSSlot;
+#endif
+
+// Wrapper for thread local storage. This class doesn't
+// do much except provide an API for portability later.
+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.
+ 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);
+
+ // 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.
+ static void* Get(TLSSlot slot);
+
+ // Set the thread-local value stored in slot 'slot' to
+ // value 'value'.
+ static void Set(TLSSlot slot, void* value);
+
+#ifdef WIN32
+ // Function called when on thread exit to call TLS
+ // destructor functions. This function is used internally.
+ static void ThreadExit();
+
+ private:
+ // Function to lazily initialize our thread local storage.
+ static void **Initialize();
+
+ private:
+ // The maximum number of 'slots' in our thread local storage stack.
+ // For now, this is fixed. We could either increase statically, or
+ // we could make it dynamic in the future.
+ static const int kThreadLocalStorageSize = 64;
+
+ static long tls_key_;
+ static long tls_max_;
+ static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize];
+#endif
+
+ DISALLOW_EVIL_CONSTRUCTORS(ThreadLocalStorage);
+};
+
+#endif // BASE_THREAD_LOCAL_STORAGE_H__