2012-06-04 07:36:43 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-10 00:54:33 +04:00
|
|
|
/* 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/. */
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
/* Cross-platform lightweight thread local data wrappers. */
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2012-12-18 00:40:50 +04:00
|
|
|
#ifndef mozilla_ThreadLocal_h_
|
|
|
|
#define mozilla_ThreadLocal_h_
|
2012-05-10 00:54:33 +04:00
|
|
|
|
|
|
|
#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" {
|
2012-12-18 00:40:50 +04:00
|
|
|
__declspec(dllimport) void* __stdcall TlsGetValue(unsigned long);
|
|
|
|
__declspec(dllimport) int __stdcall TlsSetValue(unsigned long, void*);
|
2012-05-10 00:54:33 +04:00
|
|
|
__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();
|
|
|
|
*/
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-05-10 00:54:33 +04:00
|
|
|
class ThreadLocal
|
|
|
|
{
|
|
|
|
#if defined(XP_WIN)
|
|
|
|
typedef unsigned long key_t;
|
|
|
|
#else
|
|
|
|
typedef pthread_key_t key_t;
|
|
|
|
#endif
|
|
|
|
|
2012-05-22 10:43:55 +04:00
|
|
|
union Helper {
|
2012-06-04 07:36:43 +04:00
|
|
|
void* ptr;
|
2012-05-22 10:43:55 +04:00
|
|
|
T value;
|
|
|
|
};
|
|
|
|
|
2012-05-10 00:54:33 +04:00
|
|
|
public:
|
|
|
|
MOZ_WARN_UNUSED_RESULT inline bool init();
|
|
|
|
|
2012-05-22 10:43:55 +04:00
|
|
|
inline T get() const;
|
2012-05-10 00:54:33 +04:00
|
|
|
|
2012-12-18 00:40:50 +04:00
|
|
|
inline void set(const T value);
|
2012-05-10 00:54:33 +04:00
|
|
|
|
|
|
|
bool initialized() const {
|
|
|
|
return inited;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
key_t key;
|
|
|
|
bool inited;
|
|
|
|
};
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-05-10 00:54:33 +04:00
|
|
|
inline bool
|
2012-06-04 07:36:43 +04:00
|
|
|
ThreadLocal<T>::init()
|
|
|
|
{
|
2012-12-18 00:40:50 +04:00
|
|
|
MOZ_STATIC_ASSERT(sizeof(T) <= sizeof(void*),
|
2012-06-04 07:36:43 +04:00
|
|
|
"mozilla::ThreadLocal can't be used for types larger than "
|
|
|
|
"a pointer");
|
2012-05-10 00:54:33 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-05-22 10:43:55 +04:00
|
|
|
inline T
|
2012-06-04 07:36:43 +04:00
|
|
|
ThreadLocal<T>::get() const
|
|
|
|
{
|
2012-05-10 00:54:33 +04:00
|
|
|
MOZ_ASSERT(initialized());
|
2012-05-22 10:43:55 +04:00
|
|
|
Helper h;
|
2012-05-10 00:54:33 +04:00
|
|
|
#ifdef XP_WIN
|
2012-05-22 10:43:55 +04:00
|
|
|
h.ptr = TlsGetValue(key);
|
2012-05-10 00:54:33 +04:00
|
|
|
#else
|
2012-05-22 10:43:55 +04:00
|
|
|
h.ptr = pthread_getspecific(key);
|
2012-05-10 00:54:33 +04:00
|
|
|
#endif
|
2012-05-22 10:43:55 +04:00
|
|
|
return h.value;
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
2012-06-04 07:36:43 +04:00
|
|
|
template<typename T>
|
2012-12-18 00:40:50 +04:00
|
|
|
inline void
|
2012-06-04 07:36:43 +04:00
|
|
|
ThreadLocal<T>::set(const T value)
|
|
|
|
{
|
2012-05-10 00:54:33 +04:00
|
|
|
MOZ_ASSERT(initialized());
|
2012-05-22 10:43:55 +04:00
|
|
|
Helper h;
|
|
|
|
h.value = value;
|
2012-12-18 00:40:50 +04:00
|
|
|
bool succeeded;
|
2012-05-10 00:54:33 +04:00
|
|
|
#ifdef XP_WIN
|
2012-12-18 00:40:50 +04:00
|
|
|
succeeded = TlsSetValue(key, h.ptr);
|
2012-05-10 00:54:33 +04:00
|
|
|
#else
|
2012-12-18 00:40:50 +04:00
|
|
|
succeeded = !pthread_setspecific(key, h.ptr);
|
2012-05-10 00:54:33 +04:00
|
|
|
#endif
|
2012-12-18 00:40:50 +04:00
|
|
|
if (!succeeded)
|
|
|
|
MOZ_CRASH();
|
2012-05-10 00:54:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2012-12-18 00:40:50 +04:00
|
|
|
#endif // mozilla_ThreadLocal_h_
|