#pragma once
#include <wtf/MainThread.h>
#include <wtf/Noncopyable.h>
#include <wtf/StdLibExtras.h>
#include <wtf/Threading.h>
namespace WTF {
enum class CanBeGCThread {
False,
True
};
template<typename T, CanBeGCThread canBeGCThread = CanBeGCThread::False> class ThreadSpecific {
WTF_MAKE_NONCOPYABLE(ThreadSpecific);
WTF_MAKE_FAST_ALLOCATED;
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 destroy(void* ptr);
#if USE(PTHREADS)
pthread_key_t m_key { };
#elif OS(WINDOWS)
int m_key;
#endif
};
#if USE(PTHREADS)
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)
template<typename T, CanBeGCThread canBeGCThread>
inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific()
: m_key(-1)
{
bool ok = Thread::SpecificStorage::allocateKey(m_key, destroy);
if (!ok)
CRASH();
}
template<typename T, CanBeGCThread canBeGCThread>
inline T* ThreadSpecific<T, canBeGCThread>::get()
{
auto data = static_cast<Data*>(Thread::current().specificStorage().get(m_key));
if (!data)
return nullptr;
return data->storagePointer();
}
template<typename T, CanBeGCThread canBeGCThread>
inline void ThreadSpecific<T, canBeGCThread>::setInTLS(Data* data)
{
return Thread::current().specificStorage().set(m_key, data);
}
#else
#error ThreadSpecific is not implemented for this platform.
#endif
template<typename T, CanBeGCThread canBeGCThread>
inline void 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 || !Thread::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;