michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set ts=8 sts=2 et sw=2 tw=80: */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* Cross-platform lightweight thread local data wrappers. */ michael@0: michael@0: #ifndef mozilla_ThreadLocal_h michael@0: #define mozilla_ThreadLocal_h michael@0: michael@0: #if defined(XP_WIN) michael@0: // This file will get included in any file that wants to add a profiler mark. michael@0: // In order to not bring together we could include windef.h and michael@0: // winbase.h which are sufficient to get the prototypes for the Tls* functions. michael@0: // # include michael@0: // # include michael@0: // Unfortunately, even including these headers causes us to add a bunch of ugly michael@0: // stuff to our namespace e.g #define CreateEvent CreateEventW michael@0: extern "C" { michael@0: __declspec(dllimport) void* __stdcall TlsGetValue(unsigned long); michael@0: __declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void*); michael@0: __declspec(dllimport) unsigned long __stdcall TlsAlloc(); michael@0: } michael@0: #else michael@0: # include michael@0: # include michael@0: #endif michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/Attributes.h" michael@0: #include "mozilla/NullPtr.h" michael@0: michael@0: namespace mozilla { michael@0: michael@0: // sig_safe_t denotes an atomic type which can be read or stored in a single michael@0: // instruction. This means that data of this type is safe to be manipulated michael@0: // from a signal handler, or other similar asynchronous execution contexts. michael@0: #if defined(XP_WIN) michael@0: typedef unsigned long sig_safe_t; michael@0: #else michael@0: typedef sig_atomic_t sig_safe_t; michael@0: #endif michael@0: michael@0: /* michael@0: * Thread Local Storage helpers. michael@0: * michael@0: * Usage: michael@0: * michael@0: * Only static-storage-duration (e.g. global variables, or static class members) michael@0: * objects of this class should be instantiated. This class relies on michael@0: * zero-initialization, which is implicit for static-storage-duration objects. michael@0: * It doesn't have a custom default constructor, to avoid static initializers. michael@0: * michael@0: * API usage: michael@0: * michael@0: * // Create a TLS item. michael@0: * // michael@0: * // Note that init() should be invoked exactly once, before any usage of set() michael@0: * // or get(). michael@0: * mozilla::ThreadLocal tlsKey; michael@0: * if (!tlsKey.init()) { michael@0: * // deal with the error michael@0: * } michael@0: * michael@0: * // Set the TLS value michael@0: * tlsKey.set(123); michael@0: * michael@0: * // Get the TLS value michael@0: * int value = tlsKey.get(); michael@0: */ michael@0: template michael@0: class ThreadLocal michael@0: { michael@0: #if defined(XP_WIN) michael@0: typedef unsigned long key_t; michael@0: #else michael@0: typedef pthread_key_t key_t; michael@0: #endif michael@0: michael@0: union Helper { michael@0: void* ptr; michael@0: T value; michael@0: }; michael@0: michael@0: public: michael@0: MOZ_WARN_UNUSED_RESULT inline bool init(); michael@0: michael@0: inline T get() const; michael@0: michael@0: inline void set(const T value); michael@0: michael@0: bool initialized() const { michael@0: return inited; michael@0: } michael@0: michael@0: private: michael@0: key_t key; michael@0: bool inited; michael@0: }; michael@0: michael@0: template michael@0: inline bool michael@0: ThreadLocal::init() michael@0: { michael@0: static_assert(sizeof(T) <= sizeof(void*), michael@0: "mozilla::ThreadLocal can't be used for types larger than " michael@0: "a pointer"); michael@0: MOZ_ASSERT(!initialized()); michael@0: #ifdef XP_WIN michael@0: key = TlsAlloc(); michael@0: inited = key != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES michael@0: #else michael@0: inited = !pthread_key_create(&key, nullptr); michael@0: #endif michael@0: return inited; michael@0: } michael@0: michael@0: template michael@0: inline T michael@0: ThreadLocal::get() const michael@0: { michael@0: MOZ_ASSERT(initialized()); michael@0: Helper h; michael@0: #ifdef XP_WIN michael@0: h.ptr = TlsGetValue(key); michael@0: #else michael@0: h.ptr = pthread_getspecific(key); michael@0: #endif michael@0: return h.value; michael@0: } michael@0: michael@0: template michael@0: inline void michael@0: ThreadLocal::set(const T value) michael@0: { michael@0: MOZ_ASSERT(initialized()); michael@0: Helper h; michael@0: h.value = value; michael@0: bool succeeded; michael@0: #ifdef XP_WIN michael@0: succeeded = TlsSetValue(key, h.ptr); michael@0: #else michael@0: succeeded = !pthread_setspecific(key, h.ptr); michael@0: #endif michael@0: if (!succeeded) michael@0: MOZ_CRASH(); michael@0: } michael@0: michael@0: } // namespace mozilla michael@0: michael@0: #endif /* mozilla_ThreadLocal_h */