Home | History | Annotate | Download | only in threading
      1 // Copyright (c) 2012 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.
      4 
      5 #ifndef BASE_THREADING_THREAD_LOCAL_STORAGE_H_
      6 #define BASE_THREADING_THREAD_LOCAL_STORAGE_H_
      7 
      8 #include "base/base_export.h"
      9 #include "base/basictypes.h"
     10 
     11 #if defined(OS_POSIX)
     12 #include <pthread.h>
     13 #endif
     14 
     15 namespace base {
     16 
     17 // Wrapper for thread local storage.  This class doesn't do much except provide
     18 // an API for portability.
     19 class BASE_EXPORT ThreadLocalStorage {
     20  public:
     21 
     22   // Prototype for the TLS destructor function, which can be optionally used to
     23   // cleanup thread local storage on thread exit.  'value' is the data that is
     24   // stored in thread local storage.
     25   typedef void (*TLSDestructorFunc)(void* value);
     26 
     27   // StaticSlot uses its own struct initializer-list style static
     28   // initialization, as base's LINKER_INITIALIZED requires a constructor and on
     29   // some compilers (notably gcc 4.4) this still ends up needing runtime
     30   // initialization.
     31   #define TLS_INITIALIZER {0}
     32 
     33   // A key representing one value stored in TLS.
     34   // Initialize like
     35   //   ThreadLocalStorage::StaticSlot my_slot = TLS_INITIALIZER;
     36   // If you're not using a static variable, use the convenience class
     37   // ThreadLocalStorage::Slot (below) instead.
     38   struct BASE_EXPORT StaticSlot {
     39     // Set up the TLS slot.  Called by the constructor.
     40     // 'destructor' is a pointer to a function to perform per-thread cleanup of
     41     // this object.  If set to NULL, no cleanup is done for this TLS slot.
     42     // Returns false on error.
     43     bool Initialize(TLSDestructorFunc destructor);
     44 
     45     // Free a previously allocated TLS 'slot'.
     46     // If a destructor was set for this slot, removes
     47     // the destructor so that remaining threads exiting
     48     // will not free data.
     49     void Free();
     50 
     51     // Get the thread-local value stored in slot 'slot'.
     52     // Values are guaranteed to initially be zero.
     53     void* Get() const;
     54 
     55     // Set the thread-local value stored in slot 'slot' to
     56     // value 'value'.
     57     void Set(void* value);
     58 
     59     bool initialized() const { return initialized_; }
     60 
     61     // The internals of this struct should be considered private.
     62     bool initialized_;
     63 #if defined(OS_WIN)
     64     int slot_;
     65 #elif defined(OS_POSIX)
     66     pthread_key_t key_;
     67 #endif
     68 
     69   };
     70 
     71   // A convenience wrapper around StaticSlot with a constructor. Can be used
     72   // as a member variable.
     73   class BASE_EXPORT Slot : public StaticSlot {
     74    public:
     75     // Calls StaticSlot::Initialize().
     76     explicit Slot(TLSDestructorFunc destructor = NULL);
     77 
     78    private:
     79     using StaticSlot::initialized_;
     80 #if defined(OS_WIN)
     81     using StaticSlot::slot_;
     82 #elif defined(OS_POSIX)
     83     using StaticSlot::key_;
     84 #endif
     85     DISALLOW_COPY_AND_ASSIGN(Slot);
     86   };
     87 
     88   DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
     89 };
     90 
     91 }  // namespace base
     92 
     93 #endif  // BASE_THREADING_THREAD_LOCAL_STORAGE_H_
     94