blob: 0dc50a183e3a03d5feddcbffae2c955fc2fffe3a [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit3f4a7322008-07-27 06:49:38 +09004
avi@google.com4be2f9b2008-08-08 05:12:28 +09005#ifndef BASE_THREAD_LOCAL_STORAGE_H_
6#define BASE_THREAD_LOCAL_STORAGE_H_
initial.commit3f4a7322008-07-27 06:49:38 +09007
8#include "base/basictypes.h"
9
evanm@google.comf26fd3a2008-08-21 07:54:52 +090010#if defined(OS_POSIX)
avi@google.com4be2f9b2008-08-08 05:12:28 +090011#include <pthread.h>
evanm@google.comf26fd3a2008-08-21 07:54:52 +090012#endif
initial.commit3f4a7322008-07-27 06:49:38 +090013
evanm@google.comf26fd3a2008-08-21 07:54:52 +090014// Wrapper for thread local storage. This class doesn't do much except provide
15// an API for portability.
initial.commit3f4a7322008-07-27 06:49:38 +090016class ThreadLocalStorage {
17 public:
evanm@google.comf26fd3a2008-08-21 07:54:52 +090018
19 // Prototype for the TLS destructor function, which can be optionally used to
20 // cleanup thread local storage on thread exit. 'value' is the data that is
21 // stored in thread local storage.
initial.commit3f4a7322008-07-27 06:49:38 +090022 typedef void (*TLSDestructorFunc)(void* value);
23
evanm@google.comf26fd3a2008-08-21 07:54:52 +090024 // A key representing one value stored in TLS.
25 class Slot {
26 public:
27 Slot(TLSDestructorFunc destructor = NULL);
initial.commit3f4a7322008-07-27 06:49:38 +090028
evanm@google.comf26fd3a2008-08-21 07:54:52 +090029 // This constructor should be used for statics.
30 // It returns an uninitialized Slot.
31 explicit Slot(base::LinkerInitialized x) {}
initial.commit3f4a7322008-07-27 06:49:38 +090032
evanm@google.comf26fd3a2008-08-21 07:54:52 +090033 // Set up the TLS slot. Called by the constructor.
34 // 'destructor' is a pointer to a function to perform per-thread cleanup of
35 // this object. If set to NULL, no cleanup is done for this TLS slot.
36 // Returns false on error.
37 bool Initialize(TLSDestructorFunc destructor);
initial.commit3f4a7322008-07-27 06:49:38 +090038
evanm@google.comf26fd3a2008-08-21 07:54:52 +090039 // Free a previously allocated TLS 'slot'.
40 // If a destructor was set for this slot, removes
41 // the destructor so that remaining threads exiting
42 // will not free data.
43 void Free();
44
45 // Get the thread-local value stored in slot 'slot'.
46 // Values are guaranteed to initially be zero.
47 void* Get() const;
48
49 // Set the thread-local value stored in slot 'slot' to
50 // value 'value'.
51 void Set(void* value);
52
53 bool initialized() const { return initialized_; }
54
55 private:
56 // The internals of this struct should be considered private.
57 bool initialized_;
58#if defined(OS_WIN)
59 int slot_;
60#elif defined(OS_POSIX)
61 pthread_key_t key_;
62#endif
63
64 DISALLOW_COPY_AND_ASSIGN(Slot);
65 };
initial.commit3f4a7322008-07-27 06:49:38 +090066
darin@google.com38100322008-08-07 06:27:02 +090067#if defined(OS_WIN)
initial.commit3f4a7322008-07-27 06:49:38 +090068 // Function called when on thread exit to call TLS
69 // destructor functions. This function is used internally.
70 static void ThreadExit();
71
72 private:
73 // Function to lazily initialize our thread local storage.
74 static void **Initialize();
75
76 private:
77 // The maximum number of 'slots' in our thread local storage stack.
78 // For now, this is fixed. We could either increase statically, or
79 // we could make it dynamic in the future.
80 static const int kThreadLocalStorageSize = 64;
81
82 static long tls_key_;
83 static long tls_max_;
84 static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize];
darin@google.com38100322008-08-07 06:27:02 +090085#endif // OS_WIN
initial.commit3f4a7322008-07-27 06:49:38 +090086
evanm@google.comf26fd3a2008-08-21 07:54:52 +090087 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
initial.commit3f4a7322008-07-27 06:49:38 +090088};
89
evanm@google.comf26fd3a2008-08-21 07:54:52 +090090// Temporary backwards-compatible name.
91// TODO(evanm): replace all usage of TLSSlot.
92typedef ThreadLocalStorage::Slot TLSSlot;
93
avi@google.com4be2f9b2008-08-08 05:12:28 +090094#endif // BASE_THREAD_LOCAL_STORAGE_H_
license.botf003cfe2008-08-24 09:55:55 +090095