Bug 777574 - Skip quickCheckAPI-B2.html on Linux. r=bjacob, a=test-only
[gecko.git] / mfbt / ThreadLocal.h
blob6df109821f88aed055ca68465361fc86e68255ae
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* Cross-platform lightweight thread local data wrappers. */
9 #ifndef mozilla_ThreadLocal_h
10 #define mozilla_ThreadLocal_h
12 #if defined(XP_WIN)
13 // This file will get included in any file that wants to add a profiler mark.
14 // In order to not bring <windows.h> together we could include windef.h and
15 // winbase.h which are sufficient to get the prototypes for the Tls* functions.
16 // # include <windef.h>
17 // # include <winbase.h>
18 // Unfortunately, even including these headers causes us to add a bunch of ugly
19 // stuff to our namespace e.g #define CreateEvent CreateEventW
20 extern "C" {
21 __declspec(dllimport) void* __stdcall TlsGetValue(unsigned long);
22 __declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void*);
23 __declspec(dllimport) unsigned long __stdcall TlsAlloc();
25 #else
26 # include <pthread.h>
27 # include <signal.h>
28 #endif
30 #include "mozilla/Assertions.h"
31 #include "mozilla/Attributes.h"
32 #include "mozilla/NullPtr.h"
34 namespace mozilla {
36 // sig_safe_t denotes an atomic type which can be read or stored in a single
37 // instruction. This means that data of this type is safe to be manipulated
38 // from a signal handler, or other similar asynchronous execution contexts.
39 #if defined(XP_WIN)
40 typedef unsigned long sig_safe_t;
41 #else
42 typedef sig_atomic_t sig_safe_t;
43 #endif
46 * Thread Local Storage helpers.
48 * Usage:
50 * Only static-storage-duration (e.g. global variables, or static class members)
51 * objects of this class should be instantiated. This class relies on
52 * zero-initialization, which is implicit for static-storage-duration objects.
53 * It doesn't have a custom default constructor, to avoid static initializers.
55 * API usage:
57 * // Create a TLS item
58 * mozilla::ThreadLocal<int> tlsKey;
59 * if (!tlsKey.init()) {
60 * // deal with the error
61 * }
63 * // Set the TLS value
64 * tlsKey.set(123);
66 * // Get the TLS value
67 * int value = tlsKey.get();
69 template<typename T>
70 class ThreadLocal
72 #if defined(XP_WIN)
73 typedef unsigned long key_t;
74 #else
75 typedef pthread_key_t key_t;
76 #endif
78 union Helper {
79 void* ptr;
80 T value;
83 public:
84 MOZ_WARN_UNUSED_RESULT inline bool init();
86 inline T get() const;
88 inline void set(const T value);
90 bool initialized() const {
91 return inited;
94 private:
95 key_t key;
96 bool inited;
99 template<typename T>
100 inline bool
101 ThreadLocal<T>::init()
103 static_assert(sizeof(T) <= sizeof(void*),
104 "mozilla::ThreadLocal can't be used for types larger than "
105 "a pointer");
106 MOZ_ASSERT(!initialized());
107 #ifdef XP_WIN
108 key = TlsAlloc();
109 inited = key != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES
110 #else
111 inited = !pthread_key_create(&key, nullptr);
112 #endif
113 return inited;
116 template<typename T>
117 inline T
118 ThreadLocal<T>::get() const
120 MOZ_ASSERT(initialized());
121 Helper h;
122 #ifdef XP_WIN
123 h.ptr = TlsGetValue(key);
124 #else
125 h.ptr = pthread_getspecific(key);
126 #endif
127 return h.value;
130 template<typename T>
131 inline void
132 ThreadLocal<T>::set(const T value)
134 MOZ_ASSERT(initialized());
135 Helper h;
136 h.value = value;
137 bool succeeded;
138 #ifdef XP_WIN
139 succeeded = TlsSetValue(key, h.ptr);
140 #else
141 succeeded = !pthread_setspecific(key, h.ptr);
142 #endif
143 if (!succeeded)
144 MOZ_CRASH();
147 } // namespace mozilla
149 #endif /* mozilla_ThreadLocal_h */