#ifndef WTF_ThreadSpecific_h
#define WTF_ThreadSpecific_h
#include <wtf/Noncopyable.h>
#if USE(PTHREADS)
#include <pthread.h>
#elif PLATFORM(QT)
#include <QThreadStorage>
#elif PLATFORM(GTK)
#include <glib.h>
#elif OS(WINDOWS)
#include <windows.h>
#endif
namespace WTF {
#if !USE(PTHREADS) && !PLATFORM(QT) && !PLATFORM(GTK) && OS(WINDOWS)
void ThreadSpecificThreadExit();
#endif
template<typename T> class ThreadSpecific {
WTF_MAKE_NONCOPYABLE(ThreadSpecific);
public:
ThreadSpecific();
T* operator->();
operator T*();
T& operator*();
private:
#if !USE(PTHREADS) && !PLATFORM(QT) && !PLATFORM(GTK) && OS(WINDOWS)
friend void ThreadSpecificThreadExit();
#endif
~ThreadSpecific();
T* get();
void set(T*);
void static destroy(void* ptr);
#if USE(PTHREADS) || PLATFORM(QT) || PLATFORM(GTK) || OS(WINDOWS)
struct Data {
WTF_MAKE_NONCOPYABLE(Data);
public:
Data(T* value, ThreadSpecific<T>* owner) : value(value), owner(owner) {}
#if PLATFORM(QT)
~Data() { owner->destroy(this); }
#endif
T* value;
ThreadSpecific<T>* owner;
#if !USE(PTHREADS) && !PLATFORM(QT) && !PLATFORM(GTK)
void (*destructor)(void*);
#endif
};
#endif
#if ENABLE(SINGLE_THREADED)
T* m_value;
#else
#if USE(PTHREADS)
pthread_key_t m_key;
#elif PLATFORM(QT)
QThreadStorage<Data*> m_key;
#elif PLATFORM(GTK)
GStaticPrivate m_key;
#elif OS(WINDOWS)
int m_index;
#endif
#endif
};
#if ENABLE(SINGLE_THREADED)
template<typename T>
inline ThreadSpecific<T>::ThreadSpecific()
: m_value(0)
{
}
template<typename T>
inline T* ThreadSpecific<T>::get()
{
return m_value;
}
template<typename T>
inline void ThreadSpecific<T>::set(T* ptr)
{
ASSERT(!get());
m_value = ptr;
}
#else
#if USE(PTHREADS)
template<typename T>
inline ThreadSpecific<T>::ThreadSpecific()
{
int error = pthread_key_create(&m_key, destroy);
if (error)
CRASH();
}
template<typename T>
inline T* ThreadSpecific<T>::get()
{
Data* data = static_cast<Data*>(pthread_getspecific(m_key));
return data ? data->value : 0;
}
template<typename T>
inline void ThreadSpecific<T>::set(T* ptr)
{
ASSERT(!get());
pthread_setspecific(m_key, new Data(ptr, this));
}
#elif PLATFORM(QT)
template<typename T>
inline ThreadSpecific<T>::ThreadSpecific()
{
}
template<typename T>
inline T* ThreadSpecific<T>::get()
{
Data* data = static_cast<Data*>(m_key.localData());
return data ? data->value : 0;
}
template<typename T>
inline void ThreadSpecific<T>::set(T* ptr)
{
ASSERT(!get());
Data* data = new Data(ptr, this);
m_key.setLocalData(data);
}
#elif PLATFORM(GTK)
template<typename T>
inline ThreadSpecific<T>::ThreadSpecific()
{
g_static_private_init(&m_key);
}
template<typename T>
inline T* ThreadSpecific<T>::get()
{
Data* data = static_cast<Data*>(g_static_private_get(&m_key));
return data ? data->value : 0;
}
template<typename T>
inline void ThreadSpecific<T>::set(T* ptr)
{
ASSERT(!get());
Data* data = new Data(ptr, this);
g_static_private_set(&m_key, data, destroy);
}
#elif OS(WINDOWS)
#ifndef TLS_OUT_OF_INDEXES
#define TLS_OUT_OF_INDEXES 0xffffffff
#endif
const int kMaxTlsKeySize = 256;
long& tlsKeyCount();
DWORD* tlsKeys();
template<typename T>
inline ThreadSpecific<T>::ThreadSpecific()
: m_index(-1)
{
DWORD tlsKey = TlsAlloc();
if (tlsKey == TLS_OUT_OF_INDEXES)
CRASH();
m_index = InterlockedIncrement(&tlsKeyCount()) - 1;
if (m_index >= kMaxTlsKeySize)
CRASH();
tlsKeys()[m_index] = tlsKey;
}
template<typename T>
inline ThreadSpecific<T>::~ThreadSpecific()
{
TlsFree(tlsKeys()[m_index]);
}
template<typename T>
inline T* ThreadSpecific<T>::get()
{
Data* data = static_cast<Data*>(TlsGetValue(tlsKeys()[m_index]));
return data ? data->value : 0;
}
template<typename T>
inline void ThreadSpecific<T>::set(T* ptr)
{
ASSERT(!get());
Data* data = new Data(ptr, this);
data->destructor = &ThreadSpecific<T>::destroy;
TlsSetValue(tlsKeys()[m_index], data);
}
#else
#error ThreadSpecific is not implemented for this platform.
#endif
#endif
template<typename T>
inline void ThreadSpecific<T>::destroy(void* ptr)
{
#if !ENABLE(SINGLE_THREADED)
Data* data = static_cast<Data*>(ptr);
#if USE(PTHREADS)
pthread_setspecific(data->owner->m_key, ptr);
#elif PLATFORM(GTK)
g_static_private_set(&data->owner->m_key, data, 0);
#endif
#if PLATFORM(QT)
data->owner->m_key.setLocalData(data);
#endif
data->value->~T();
fastFree(data->value);
#if USE(PTHREADS)
pthread_setspecific(data->owner->m_key, 0);
#elif PLATFORM(QT)
#elif PLATFORM(GTK)
g_static_private_set(&data->owner->m_key, 0, 0);
#elif OS(WINDOWS)
TlsSetValue(tlsKeys()[data->owner->m_index], 0);
#else
#error ThreadSpecific is not implemented for this platform.
#endif
#if !PLATFORM(QT)
delete data;
#endif
#endif
}
template<typename T>
inline ThreadSpecific<T>::operator T*()
{
T* ptr = static_cast<T*>(get());
if (!ptr) {
ptr = static_cast<T*>(fastZeroedMalloc(sizeof(T)));
set(ptr);
new (ptr) T;
}
return ptr;
}
template<typename T>
inline T* ThreadSpecific<T>::operator->()
{
return operator T*();
}
template<typename T>
inline T& ThreadSpecific<T>::operator*()
{
return *operator T*();
}
}
#endif