#pragma once
#include <wtf/MainThread.h>
#include <wtf/Noncopyable.h>
#include <wtf/StdLibExtras.h>
#if USE(PTHREADS)
#include <pthread.h>
#if OS(HURD)
#define PTHREAD_KEYS_MAX 1024
#else
#include <limits.h>
#endif
#elif OS(WINDOWS)
#include <windows.h>
#endif
namespace WTF {
#if OS(WINDOWS) && CPU(X86)
#define THREAD_SPECIFIC_CALL __stdcall
#else
#define THREAD_SPECIFIC_CALL
#endif
enum class CanBeGCThread {
False,
True
};
template<typename T, CanBeGCThread canBeGCThread = CanBeGCThread::False> class ThreadSpecific {
WTF_MAKE_NONCOPYABLE(ThreadSpecific);
public:
ThreadSpecific();
bool isSet(); T* operator->();
operator T*();
T& operator*();
private:
~ThreadSpecific();
struct Data {
WTF_MAKE_NONCOPYABLE(Data);
WTF_MAKE_FAST_ALLOCATED;
public:
using PointerType = typename std::remove_const<T>::type*;
Data(ThreadSpecific<T, canBeGCThread>* owner)
: owner(owner)
{
owner->setInTLS(this);
new (NotNull, storagePointer()) T();
}
~Data()
{
storagePointer()->~T();
owner->setInTLS(nullptr);
}
PointerType storagePointer() const { return const_cast<PointerType>(reinterpret_cast<const T*>(&m_storage)); }
typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type m_storage;
ThreadSpecific<T, canBeGCThread>* owner;
};
T* get();
T* set();
void setInTLS(Data*);
void static THREAD_SPECIFIC_CALL destroy(void* ptr);
#if USE(PTHREADS)
pthread_key_t m_key { };
#elif OS(WINDOWS)
int m_index;
#endif
};
#if USE(PTHREADS)
typedef pthread_key_t ThreadSpecificKey;
static const constexpr ThreadSpecificKey InvalidThreadSpecificKey = PTHREAD_KEYS_MAX;
inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (*destructor)(void *))
{
int error = pthread_key_create(key, destructor);
if (error)
CRASH();
}
inline void threadSpecificKeyDelete(ThreadSpecificKey key)
{
int error = pthread_key_delete(key);
if (error)
CRASH();
}
inline void threadSpecificSet(ThreadSpecificKey key, void* value)
{
pthread_setspecific(key, value);
}
inline void* threadSpecificGet(ThreadSpecificKey key)
{
return pthread_getspecific(key);
}
template<typename T, CanBeGCThread canBeGCThread>
inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific()
{
int error = pthread_key_create(&m_key, destroy);
if (error)
CRASH();
}
template<typename T, CanBeGCThread canBeGCThread>
inline T* ThreadSpecific<T, canBeGCThread>::get()
{
Data* data = static_cast<Data*>(pthread_getspecific(m_key));
if (data)
return data->storagePointer();
return nullptr;
}
template<typename T, CanBeGCThread canBeGCThread>
inline void ThreadSpecific<T, canBeGCThread>::setInTLS(Data* data)
{
pthread_setspecific(m_key, data);
}
#elif OS(WINDOWS)
const int kMaxFlsKeySize = 128;
WTF_EXPORT_PRIVATE long& flsKeyCount();
WTF_EXPORT_PRIVATE DWORD* flsKeys();
typedef DWORD ThreadSpecificKey;
static const constexpr ThreadSpecificKey InvalidThreadSpecificKey = FLS_OUT_OF_INDEXES;
inline void threadSpecificKeyCreate(ThreadSpecificKey* key, void (THREAD_SPECIFIC_CALL *destructor)(void *))
{
DWORD flsKey = FlsAlloc(destructor);
if (flsKey == FLS_OUT_OF_INDEXES)
CRASH();
*key = flsKey;
}
inline void threadSpecificKeyDelete(ThreadSpecificKey key)
{
FlsFree(key);
}
inline void threadSpecificSet(ThreadSpecificKey key, void* data)
{
FlsSetValue(key, data);
}
inline void* threadSpecificGet(ThreadSpecificKey key)
{
return FlsGetValue(key);
}
template<typename T, CanBeGCThread canBeGCThread>
inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific()
: m_index(-1)
{
DWORD flsKey = FlsAlloc(destroy);
if (flsKey == FLS_OUT_OF_INDEXES)
CRASH();
m_index = InterlockedIncrement(&flsKeyCount()) - 1;
if (m_index >= kMaxFlsKeySize)
CRASH();
flsKeys()[m_index] = flsKey;
}
template<typename T, CanBeGCThread canBeGCThread>
inline ThreadSpecific<T, canBeGCThread>::~ThreadSpecific()
{
FlsFree(flsKeys()[m_index]);
}
template<typename T, CanBeGCThread canBeGCThread>
inline T* ThreadSpecific<T, canBeGCThread>::get()
{
Data* data = static_cast<Data*>(FlsGetValue(flsKeys()[m_index]));
if (data)
return data->storagePointer();
return nullptr;
}
template<typename T, CanBeGCThread canBeGCThread>
inline void ThreadSpecific<T, canBeGCThread>::setInTLS(Data* data)
{
FlsSetValue(flsKeys()[m_index], data);
}
#else
#error ThreadSpecific is not implemented for this platform.
#endif
template<typename T, CanBeGCThread canBeGCThread>
inline void THREAD_SPECIFIC_CALL ThreadSpecific<T, canBeGCThread>::destroy(void* ptr)
{
Data* data = static_cast<Data*>(ptr);
#if USE(PTHREADS)
pthread_setspecific(data->owner->m_key, ptr);
#endif
delete data;
}
template<typename T, CanBeGCThread canBeGCThread>
inline T* ThreadSpecific<T, canBeGCThread>::set()
{
RELEASE_ASSERT(canBeGCThread == CanBeGCThread::True || !mayBeGCThread());
ASSERT(!get());
Data* data = new Data(this); ASSERT(get() == data->storagePointer());
return data->storagePointer();
}
template<typename T, CanBeGCThread canBeGCThread>
inline bool ThreadSpecific<T, canBeGCThread>::isSet()
{
return !!get();
}
template<typename T, CanBeGCThread canBeGCThread>
inline ThreadSpecific<T, canBeGCThread>::operator T*()
{
if (T* ptr = get())
return ptr;
return set();
}
template<typename T, CanBeGCThread canBeGCThread>
inline T* ThreadSpecific<T, canBeGCThread>::operator->()
{
return operator T*();
}
template<typename T, CanBeGCThread canBeGCThread>
inline T& ThreadSpecific<T, canBeGCThread>::operator*()
{
return *operator T*();
}
}
using WTF::ThreadSpecific;