Bug 1066410 - Serious color distortion happened when plays WebRTC. r=jesup, a=bajaj
[gecko.git] / mfbt / ThreadLocal.h
blob28015de22e221d34a7bed2b52fd07ac4b11c1771
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 * //
59 * // Note that init() should be invoked exactly once, before any usage of set()
60 * // or get().
61 * mozilla::ThreadLocal<int> tlsKey;
62 * if (!tlsKey.init()) {
63 * // deal with the error
64 * }
66 * // Set the TLS value
67 * tlsKey.set(123);
69 * // Get the TLS value
70 * int value = tlsKey.get();
72 template<typename T>
73 class ThreadLocal
75 #if defined(XP_WIN)
76 typedef unsigned long key_t;
77 #else
78 typedef pthread_key_t key_t;
79 #endif
81 union Helper
83 void* mPtr;
84 T mValue;
87 public:
88 MOZ_WARN_UNUSED_RESULT inline bool init();
90 inline T get() const;
92 inline void set(const T aValue);
94 bool initialized() const { return mInited; }
96 private:
97 key_t mKey;
98 bool mInited;
101 template<typename T>
102 inline bool
103 ThreadLocal<T>::init()
105 static_assert(sizeof(T) <= sizeof(void*),
106 "mozilla::ThreadLocal can't be used for types larger than "
107 "a pointer");
108 MOZ_ASSERT(!initialized());
109 #ifdef XP_WIN
110 mKey = TlsAlloc();
111 mInited = mKey != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES
112 #else
113 mInited = !pthread_key_create(&mKey, nullptr);
114 #endif
115 return mInited;
118 template<typename T>
119 inline T
120 ThreadLocal<T>::get() const
122 MOZ_ASSERT(initialized());
123 Helper h;
124 #ifdef XP_WIN
125 h.mPtr = TlsGetValue(mKey);
126 #else
127 h.mPtr = pthread_getspecific(mKey);
128 #endif
129 return h.mValue;
132 template<typename T>
133 inline void
134 ThreadLocal<T>::set(const T aValue)
136 MOZ_ASSERT(initialized());
137 Helper h;
138 h.mValue = aValue;
139 #ifdef XP_WIN
140 bool succeeded = TlsSetValue(mKey, h.mPtr);
141 #else
142 bool succeeded = !pthread_setspecific(mKey, h.mPtr);
143 #endif
144 if (!succeeded) {
145 MOZ_CRASH();
149 } // namespace mozilla
151 #endif /* mozilla_ThreadLocal_h */