Bug 785860 - fix sts preload list tests to skip private mode tests if private browsin...
[gecko.git] / mfbt / ThreadLocal.h
blob712f1f12d404eb775b926780f4cc47b12a3bc0cb
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /* Cross-platform lightweight thread local data wrappers. */
8 #ifndef mozilla_TLS_h_
9 #define mozilla_TLS_h_
11 #if defined(XP_WIN)
12 // This file will get included in any file that wants to add a profiler mark.
13 // In order to not bring <windows.h> together we could include windef.h and
14 // winbase.h which are sufficient to get the prototypes for the Tls* functions.
15 // # include <windef.h>
16 // # include <winbase.h>
17 // Unfortunately, even including these headers causes us to add a bunch of ugly
18 // stuff to our namespace e.g #define CreateEvent CreateEventW
19 extern "C" {
20 __declspec(dllimport) void * __stdcall TlsGetValue(unsigned long);
21 __declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void *);
22 __declspec(dllimport) unsigned long __stdcall TlsAlloc();
24 #else
25 # include <pthread.h>
26 # include <signal.h>
27 #endif
29 #include "mozilla/Assertions.h"
30 #include "mozilla/Attributes.h"
32 namespace mozilla {
34 // sig_safe_t denotes an atomic type which can be read or stored in a single
35 // instruction. This means that data of this type is safe to be manipulated
36 // from a signal handler, or other similar asynchronous execution contexts.
37 #if defined(XP_WIN)
38 typedef unsigned long sig_safe_t;
39 #else
40 typedef sig_atomic_t sig_safe_t;
41 #endif
44 * Thread Local Storage helpers.
46 * Usage:
48 * Only static-storage-duration (e.g. global variables, or static class members)
49 * objects of this class should be instantiated. This class relies on
50 * zero-initialization, which is implicit for static-storage-duration objects.
51 * It doesn't have a custom default constructor, to avoid static initializers.
53 * API usage:
55 * // Create a TLS item
56 * mozilla::ThreadLocal<int> tlsKey;
57 * if (!tlsKey.init()) {
58 * // deal with the error
59 * }
61 * // Set the TLS value
62 * tlsKey.set(123);
64 * // Get the TLS value
65 * int value = tlsKey.get();
67 template<typename T>
68 class ThreadLocal
70 #if defined(XP_WIN)
71 typedef unsigned long key_t;
72 #else
73 typedef pthread_key_t key_t;
74 #endif
76 union Helper {
77 void* ptr;
78 T value;
81 public:
82 MOZ_WARN_UNUSED_RESULT inline bool init();
84 inline T get() const;
86 inline bool set(const T value);
88 bool initialized() const {
89 return inited;
92 private:
93 key_t key;
94 bool inited;
97 template<typename T>
98 inline bool
99 ThreadLocal<T>::init()
101 MOZ_STATIC_ASSERT(sizeof(T) <= sizeof(void *),
102 "mozilla::ThreadLocal can't be used for types larger than "
103 "a pointer");
104 MOZ_ASSERT(!initialized());
105 #ifdef XP_WIN
106 key = TlsAlloc();
107 inited = key != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES
108 #else
109 inited = !pthread_key_create(&key, NULL);
110 #endif
111 return inited;
114 template<typename T>
115 inline T
116 ThreadLocal<T>::get() const
118 MOZ_ASSERT(initialized());
119 Helper h;
120 #ifdef XP_WIN
121 h.ptr = TlsGetValue(key);
122 #else
123 h.ptr = pthread_getspecific(key);
124 #endif
125 return h.value;
128 template<typename T>
129 inline bool
130 ThreadLocal<T>::set(const T value)
132 MOZ_ASSERT(initialized());
133 Helper h;
134 h.value = value;
135 #ifdef XP_WIN
136 return TlsSetValue(key, h.ptr);
137 #else
138 return !pthread_setspecific(key, h.ptr);
139 #endif
142 } // namespace mozilla
144 #endif // mozilla_TLS_h_