ipc/chromium/src/base/thread_local_storage.h

Wed, 31 Dec 2014 13:27:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 13:27:57 +0100
branch
TOR_BUG_3246
changeset 6
8bccb770b82d
permissions
-rw-r--r--

Ignore runtime configuration files generated during quality assurance.

michael@0 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
michael@0 2 // Use of this source code is governed by a BSD-style license that can be
michael@0 3 // found in the LICENSE file.
michael@0 4
michael@0 5 #ifndef BASE_THREAD_LOCAL_STORAGE_H_
michael@0 6 #define BASE_THREAD_LOCAL_STORAGE_H_
michael@0 7
michael@0 8 #include "base/basictypes.h"
michael@0 9
michael@0 10 #if defined(OS_POSIX)
michael@0 11 #include <pthread.h>
michael@0 12 #endif
michael@0 13
michael@0 14 // Wrapper for thread local storage. This class doesn't do much except provide
michael@0 15 // an API for portability.
michael@0 16 class ThreadLocalStorage {
michael@0 17 public:
michael@0 18
michael@0 19 // Prototype for the TLS destructor function, which can be optionally used to
michael@0 20 // cleanup thread local storage on thread exit. 'value' is the data that is
michael@0 21 // stored in thread local storage.
michael@0 22 typedef void (*TLSDestructorFunc)(void* value);
michael@0 23
michael@0 24 // A key representing one value stored in TLS.
michael@0 25 class Slot {
michael@0 26 public:
michael@0 27 Slot(TLSDestructorFunc destructor = NULL);
michael@0 28
michael@0 29 // This constructor should be used for statics.
michael@0 30 // It returns an uninitialized Slot.
michael@0 31 explicit Slot(base::LinkerInitialized x) {}
michael@0 32
michael@0 33 // Set up the TLS slot. Called by the constructor.
michael@0 34 // 'destructor' is a pointer to a function to perform per-thread cleanup of
michael@0 35 // this object. If set to NULL, no cleanup is done for this TLS slot.
michael@0 36 // Returns false on error.
michael@0 37 bool Initialize(TLSDestructorFunc destructor);
michael@0 38
michael@0 39 // Free a previously allocated TLS 'slot'.
michael@0 40 // If a destructor was set for this slot, removes
michael@0 41 // the destructor so that remaining threads exiting
michael@0 42 // will not free data.
michael@0 43 void Free();
michael@0 44
michael@0 45 // Get the thread-local value stored in slot 'slot'.
michael@0 46 // Values are guaranteed to initially be zero.
michael@0 47 void* Get() const;
michael@0 48
michael@0 49 // Set the thread-local value stored in slot 'slot' to
michael@0 50 // value 'value'.
michael@0 51 void Set(void* value);
michael@0 52
michael@0 53 bool initialized() const { return initialized_; }
michael@0 54
michael@0 55 private:
michael@0 56 // The internals of this struct should be considered private.
michael@0 57 bool initialized_;
michael@0 58 #if defined(OS_WIN)
michael@0 59 int slot_;
michael@0 60 #elif defined(OS_POSIX)
michael@0 61 pthread_key_t key_;
michael@0 62 #endif
michael@0 63
michael@0 64 DISALLOW_COPY_AND_ASSIGN(Slot);
michael@0 65 };
michael@0 66
michael@0 67 #if defined(OS_WIN)
michael@0 68 // Function called when on thread exit to call TLS
michael@0 69 // destructor functions. This function is used internally.
michael@0 70 static void ThreadExit();
michael@0 71
michael@0 72 private:
michael@0 73 // Function to lazily initialize our thread local storage.
michael@0 74 static void **Initialize();
michael@0 75
michael@0 76 private:
michael@0 77 // The maximum number of 'slots' in our thread local storage stack.
michael@0 78 // For now, this is fixed. We could either increase statically, or
michael@0 79 // we could make it dynamic in the future.
michael@0 80 static const int kThreadLocalStorageSize = 64;
michael@0 81
michael@0 82 static long tls_key_;
michael@0 83 static long tls_max_;
michael@0 84 static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize];
michael@0 85 #endif // OS_WIN
michael@0 86
michael@0 87 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
michael@0 88 };
michael@0 89
michael@0 90 // Temporary backwards-compatible name.
michael@0 91 // TODO(evanm): replace all usage of TLSSlot.
michael@0 92 typedef ThreadLocalStorage::Slot TLSSlot;
michael@0 93
michael@0 94 #endif // BASE_THREAD_LOCAL_STORAGE_H_

mercurial