Permalink
Switch branches/tags
unstable trunk testing_temp stable stable-with-mods <rail@mozilla.com> last-mozilla-central gloda-milestone-1 gloda-milestone-0 chromium-import-r15462 chromium-import-latest bsmedberg-static-xpcom-registration-base UPDATE_PACKAGING_R16 UPDATE_PACKAGING_R15 UPDATE_PACKAGING_R14 UPDATE_PACKAGING_R13 UPDATE_PACKAGING_R12 UPDATE_PACKAGING_R11_1_MU UPDATE_PACKAGING_R11 UPDATE_PACKAGING_R10 UPDATE_PACKAGING_R9 UPDATE_PACKAGING_R8 UPDATE_PACKAGING_R7 UPDATE_PACKAGING_R6 UPDATE_PACKAGING_R5 THUNDERBIRD_26_0b1_RELEASE THUNDERBIRD_26_0b1_BUILD1 THUNDERBIRD_25_0b1_RELEASE THUNDERBIRD_25_0b1_BUILD1 THUNDERBIRD_24_2_0_RELEASE THUNDERBIRD_24_2_0_BUILD1 THUNDERBIRD_24_1_1_RELEASE THUNDERBIRD_24_1_1_BUILD1 THUNDERBIRD_24_1_0_RELEASE THUNDERBIRD_24_1_0_BUILD1 THUNDERBIRD_24_0_1_RELEASE THUNDERBIRD_24_0_1_BUILD1 THUNDERBIRD_24_0b3_RELEASE THUNDERBIRD_24_0b3_BUILD1 THUNDERBIRD_24_0b2_RELEASE THUNDERBIRD_24_0b2_BUILD1 THUNDERBIRD_24_0b1_RELEASE THUNDERBIRD_24_0b1_BUILD4 THUNDERBIRD_24_0b1_BUILD3 THUNDERBIRD_24_0b1_BUILD2 THUNDERBIRD_24_0b1_BUILD1 THUNDERBIRD_24_0_RELEASE THUNDERBIRD_24_0_BUILD1 THUNDERBIRD_23_0b1_RELEASE THUNDERBIRD_23_0b1_BUILD1 THUNDERBIRD_22_0b1_RELEASE THUNDERBIRD_22_0b1_BUILD2 THUNDERBIRD_22_0b1_BUILD1 THUNDERBIRD_21_0b1_RELEASE THUNDERBIRD_21_0b1_BUILD1 THUNDERBIRD_20_0b1_RELEASE THUNDERBIRD_20_0b1_BUILD2 THUNDERBIRD_20_0b1_BUILD1 THUNDERBIRD_19_0b1_RELEASE THUNDERBIRD_19_0b1_BUILD1 THUNDERBIRD_18_0b1_RELEASE THUNDERBIRD_18_0b1_BUILD1 THUNDERBIRD_17_0_11esr_RELEASE THUNDERBIRD_17_0_11esr_BUILD1 THUNDERBIRD_17_0_10esr_RELEASE THUNDERBIRD_17_0_10esr_BUILD1 THUNDERBIRD_17_0_9esr_RELEASE THUNDERBIRD_17_0_9esr_BUILD1 THUNDERBIRD_17_0_8esr_RELEASE THUNDERBIRD_17_0_8esr_BUILD1 THUNDERBIRD_17_0_8_RELEASE THUNDERBIRD_17_0_8_BUILD1 THUNDERBIRD_17_0_7esr_RELEASE THUNDERBIRD_17_0_7esr_BUILD1 THUNDERBIRD_17_0_7_RELEASE THUNDERBIRD_17_0_7_BUILD1 THUNDERBIRD_17_0_6esr_RELEASE THUNDERBIRD_17_0_6esr_BUILD1 THUNDERBIRD_17_0_6_RELEASE THUNDERBIRD_17_0_6_BUILD1 THUNDERBIRD_17_0_5esr_RELEASE THUNDERBIRD_17_0_5esr_BUILD1 THUNDERBIRD_17_0_5_RELEASE THUNDERBIRD_17_0_5_BUILD1 THUNDERBIRD_17_0_4esr_RELEASE THUNDERBIRD_17_0_4esr_BUILD1 THUNDERBIRD_17_0_4_RELEASE THUNDERBIRD_17_0_4_BUILD1 THUNDERBIRD_17_0_3esr_RELEASE THUNDERBIRD_17_0_3esr_BUILD1 THUNDERBIRD_17_0_3_RELEASE THUNDERBIRD_17_0_3_BUILD1 THUNDERBIRD_17_0_2esr_RELEASE THUNDERBIRD_17_0_2esr_BUILD2 THUNDERBIRD_17_0_2esr_BUILD1 THUNDERBIRD_17_0_2_RELEASE THUNDERBIRD_17_0_2_BUILD2 THUNDERBIRD_17_0_2_BUILD1 THUNDERBIRD_17_0esr_RELEASE THUNDERBIRD_17_0esr_BUILD4
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
145 lines (126 sloc) 3.4 KB
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* Cross-platform lightweight thread local data wrappers. */
#ifndef mozilla_TLS_h_
#define mozilla_TLS_h_
#if defined(XP_WIN)
// This file will get included in any file that wants to add a profiler mark.
// In order to not bring <windows.h> together we could include windef.h and
// winbase.h which are sufficient to get the prototypes for the Tls* functions.
// # include <windef.h>
// # include <winbase.h>
// Unfortunately, even including these headers causes us to add a bunch of ugly
// stuff to our namespace e.g #define CreateEvent CreateEventW
extern "C" {
__declspec(dllimport) void * __stdcall TlsGetValue(unsigned long);
__declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void *);
__declspec(dllimport) unsigned long __stdcall TlsAlloc();
}
#else
# include <pthread.h>
# include <signal.h>
#endif
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
namespace mozilla {
// sig_safe_t denotes an atomic type which can be read or stored in a single
// instruction. This means that data of this type is safe to be manipulated
// from a signal handler, or other similar asynchronous execution contexts.
#if defined(XP_WIN)
typedef unsigned long sig_safe_t;
#else
typedef sig_atomic_t sig_safe_t;
#endif
/*
* Thread Local Storage helpers.
*
* Usage:
*
* Only static-storage-duration (e.g. global variables, or static class members)
* objects of this class should be instantiated. This class relies on
* zero-initialization, which is implicit for static-storage-duration objects.
* It doesn't have a custom default constructor, to avoid static initializers.
*
* API usage:
*
* // Create a TLS item
* mozilla::ThreadLocal<int> tlsKey;
* if (!tlsKey.init()) {
* // deal with the error
* }
*
* // Set the TLS value
* tlsKey.set(123);
*
* // Get the TLS value
* int value = tlsKey.get();
*/
template<typename T>
class ThreadLocal
{
#if defined(XP_WIN)
typedef unsigned long key_t;
#else
typedef pthread_key_t key_t;
#endif
union Helper {
void* ptr;
T value;
};
public:
MOZ_WARN_UNUSED_RESULT inline bool init();
inline T get() const;
inline bool set(const T value);
bool initialized() const {
return inited;
}
private:
key_t key;
bool inited;
};
template<typename T>
inline bool
ThreadLocal<T>::init()
{
MOZ_STATIC_ASSERT(sizeof(T) <= sizeof(void *),
"mozilla::ThreadLocal can't be used for types larger than "
"a pointer");
MOZ_ASSERT(!initialized());
#ifdef XP_WIN
key = TlsAlloc();
inited = key != 0xFFFFFFFFUL; // TLS_OUT_OF_INDEXES
#else
inited = !pthread_key_create(&key, NULL);
#endif
return inited;
}
template<typename T>
inline T
ThreadLocal<T>::get() const
{
MOZ_ASSERT(initialized());
Helper h;
#ifdef XP_WIN
h.ptr = TlsGetValue(key);
#else
h.ptr = pthread_getspecific(key);
#endif
return h.value;
}
template<typename T>
inline bool
ThreadLocal<T>::set(const T value)
{
MOZ_ASSERT(initialized());
Helper h;
h.value = value;
#ifdef XP_WIN
return TlsSetValue(key, h.ptr);
#else
return !pthread_setspecific(key, h.ptr);
#endif
}
} // namespace mozilla
#endif // mozilla_TLS_h_