ipc/chromium/src/base/thread_local_storage.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 // 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.
     5 #ifndef BASE_THREAD_LOCAL_STORAGE_H_
     6 #define BASE_THREAD_LOCAL_STORAGE_H_
     8 #include "base/basictypes.h"
    10 #if defined(OS_POSIX)
    11 #include <pthread.h>
    12 #endif
    14 // Wrapper for thread local storage.  This class doesn't do much except provide
    15 // an API for portability.
    16 class ThreadLocalStorage {
    17  public:
    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.
    22   typedef void (*TLSDestructorFunc)(void* value);
    24   // A key representing one value stored in TLS.
    25   class Slot {
    26    public:
    27     Slot(TLSDestructorFunc destructor = NULL);
    29     // This constructor should be used for statics.
    30     // It returns an uninitialized Slot.
    31     explicit Slot(base::LinkerInitialized x) {}
    33     // 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);
    39     // 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();
    45     // Get the thread-local value stored in slot 'slot'.
    46     // Values are guaranteed to initially be zero.
    47     void* Get() const;
    49     // Set the thread-local value stored in slot 'slot' to
    50     // value 'value'.
    51     void Set(void* value);
    53     bool initialized() const { return initialized_; }
    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
    64     DISALLOW_COPY_AND_ASSIGN(Slot);
    65   };
    67 #if defined(OS_WIN)
    68   // Function called when on thread exit to call TLS
    69   // destructor functions.  This function is used internally.
    70   static void ThreadExit();
    72  private:
    73   // Function to lazily initialize our thread local storage.
    74   static void **Initialize();
    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;
    82   static long tls_key_;
    83   static long tls_max_;
    84   static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize];
    85 #endif  // OS_WIN
    87   DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
    88 };
    90 // Temporary backwards-compatible name.
    91 // TODO(evanm): replace all usage of TLSSlot.
    92 typedef ThreadLocalStorage::Slot TLSSlot;
    94 #endif  // BASE_THREAD_LOCAL_STORAGE_H_

mercurial