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
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
21 __declspec(dllimport
) void* __stdcall
TlsGetValue(unsigned long);
22 __declspec(dllimport
) int __stdcall
TlsSetValue(unsigned long, void*);
23 __declspec(dllimport
) unsigned long __stdcall
TlsAlloc();
30 #include "mozilla/Assertions.h"
31 #include "mozilla/Attributes.h"
32 #include "mozilla/NullPtr.h"
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.
40 typedef unsigned long sig_safe_t
;
42 typedef sig_atomic_t sig_safe_t
;
46 * Thread Local Storage helpers.
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.
57 * // Create a TLS item.
59 * // Note that init() should be invoked exactly once, before any usage of set()
61 * mozilla::ThreadLocal<int> tlsKey;
62 * if (!tlsKey.init()) {
63 * // deal with the error
66 * // Set the TLS value
69 * // Get the TLS value
70 * int value = tlsKey.get();
76 typedef unsigned long key_t
;
78 typedef pthread_key_t key_t
;
88 MOZ_WARN_UNUSED_RESULT
inline bool init();
92 inline void set(const T aValue
);
94 bool initialized() const { return mInited
; }
103 ThreadLocal
<T
>::init()
105 static_assert(sizeof(T
) <= sizeof(void*),
106 "mozilla::ThreadLocal can't be used for types larger than "
108 MOZ_ASSERT(!initialized());
111 mInited
= mKey
!= 0xFFFFFFFFUL
; // TLS_OUT_OF_INDEXES
113 mInited
= !pthread_key_create(&mKey
, nullptr);
120 ThreadLocal
<T
>::get() const
122 MOZ_ASSERT(initialized());
125 h
.mPtr
= TlsGetValue(mKey
);
127 h
.mPtr
= pthread_getspecific(mKey
);
134 ThreadLocal
<T
>::set(const T aValue
)
136 MOZ_ASSERT(initialized());
140 bool succeeded
= TlsSetValue(mKey
, h
.mPtr
);
142 bool succeeded
= !pthread_setspecific(mKey
, h
.mPtr
);
149 } // namespace mozilla
151 #endif /* mozilla_ThreadLocal_h */