1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/base/thread_local_storage.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,94 @@ 1.4 +// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +#ifndef BASE_THREAD_LOCAL_STORAGE_H_ 1.9 +#define BASE_THREAD_LOCAL_STORAGE_H_ 1.10 + 1.11 +#include "base/basictypes.h" 1.12 + 1.13 +#if defined(OS_POSIX) 1.14 +#include <pthread.h> 1.15 +#endif 1.16 + 1.17 +// Wrapper for thread local storage. This class doesn't do much except provide 1.18 +// an API for portability. 1.19 +class ThreadLocalStorage { 1.20 + public: 1.21 + 1.22 + // Prototype for the TLS destructor function, which can be optionally used to 1.23 + // cleanup thread local storage on thread exit. 'value' is the data that is 1.24 + // stored in thread local storage. 1.25 + typedef void (*TLSDestructorFunc)(void* value); 1.26 + 1.27 + // A key representing one value stored in TLS. 1.28 + class Slot { 1.29 + public: 1.30 + Slot(TLSDestructorFunc destructor = NULL); 1.31 + 1.32 + // This constructor should be used for statics. 1.33 + // It returns an uninitialized Slot. 1.34 + explicit Slot(base::LinkerInitialized x) {} 1.35 + 1.36 + // Set up the TLS slot. Called by the constructor. 1.37 + // 'destructor' is a pointer to a function to perform per-thread cleanup of 1.38 + // this object. If set to NULL, no cleanup is done for this TLS slot. 1.39 + // Returns false on error. 1.40 + bool Initialize(TLSDestructorFunc destructor); 1.41 + 1.42 + // Free a previously allocated TLS 'slot'. 1.43 + // If a destructor was set for this slot, removes 1.44 + // the destructor so that remaining threads exiting 1.45 + // will not free data. 1.46 + void Free(); 1.47 + 1.48 + // Get the thread-local value stored in slot 'slot'. 1.49 + // Values are guaranteed to initially be zero. 1.50 + void* Get() const; 1.51 + 1.52 + // Set the thread-local value stored in slot 'slot' to 1.53 + // value 'value'. 1.54 + void Set(void* value); 1.55 + 1.56 + bool initialized() const { return initialized_; } 1.57 + 1.58 + private: 1.59 + // The internals of this struct should be considered private. 1.60 + bool initialized_; 1.61 +#if defined(OS_WIN) 1.62 + int slot_; 1.63 +#elif defined(OS_POSIX) 1.64 + pthread_key_t key_; 1.65 +#endif 1.66 + 1.67 + DISALLOW_COPY_AND_ASSIGN(Slot); 1.68 + }; 1.69 + 1.70 +#if defined(OS_WIN) 1.71 + // Function called when on thread exit to call TLS 1.72 + // destructor functions. This function is used internally. 1.73 + static void ThreadExit(); 1.74 + 1.75 + private: 1.76 + // Function to lazily initialize our thread local storage. 1.77 + static void **Initialize(); 1.78 + 1.79 + private: 1.80 + // The maximum number of 'slots' in our thread local storage stack. 1.81 + // For now, this is fixed. We could either increase statically, or 1.82 + // we could make it dynamic in the future. 1.83 + static const int kThreadLocalStorageSize = 64; 1.84 + 1.85 + static long tls_key_; 1.86 + static long tls_max_; 1.87 + static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize]; 1.88 +#endif // OS_WIN 1.89 + 1.90 + DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); 1.91 +}; 1.92 + 1.93 +// Temporary backwards-compatible name. 1.94 +// TODO(evanm): replace all usage of TLSSlot. 1.95 +typedef ThreadLocalStorage::Slot TLSSlot; 1.96 + 1.97 +#endif // BASE_THREAD_LOCAL_STORAGE_H_