2018-10-06 20:36:44 -07:00
|
|
|
#pragma once
|
2016-03-31 21:24:05 -07:00
|
|
|
|
2018-10-06 19:49:22 -07:00
|
|
|
#ifndef __SWITCH__
|
|
|
|
|
2016-03-31 21:24:05 -07:00
|
|
|
#if _WIN32
|
|
|
|
#else
|
|
|
|
#include <pthread.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** Multiplatform TLS-pointer wrapper (for compilers without proper thread_local support) */
|
|
|
|
template <class T>
|
2018-12-07 21:17:51 -08:00
|
|
|
class ThreadLocalPtr {
|
2016-03-31 21:24:05 -07:00
|
|
|
#if _WIN32
|
2018-12-07 21:17:51 -08:00
|
|
|
DWORD m_key;
|
|
|
|
|
2016-03-31 21:24:05 -07:00
|
|
|
public:
|
2018-12-07 21:17:51 -08:00
|
|
|
ThreadLocalPtr() { m_key = TlsAlloc(); }
|
|
|
|
~ThreadLocalPtr() { TlsFree(m_key); }
|
|
|
|
T* get() const { return static_cast<T*>(TlsGetValue(m_key)); }
|
|
|
|
void reset(T* v = nullptr) { TlsSetValue(m_key, LPVOID(v)); }
|
2016-03-31 21:24:05 -07:00
|
|
|
#else
|
2018-12-07 21:17:51 -08:00
|
|
|
pthread_key_t m_key;
|
|
|
|
|
2016-03-31 21:24:05 -07:00
|
|
|
public:
|
2018-12-07 21:17:51 -08:00
|
|
|
ThreadLocalPtr() { pthread_key_create(&m_key, nullptr); }
|
|
|
|
~ThreadLocalPtr() { pthread_key_delete(m_key); }
|
|
|
|
T* get() const { return static_cast<T*>(pthread_getspecific(m_key)); }
|
|
|
|
void reset(T* v = nullptr) { pthread_setspecific(m_key, v); }
|
2016-03-31 21:24:05 -07:00
|
|
|
#endif
|
2018-12-07 21:17:51 -08:00
|
|
|
T* operator->() { return get(); }
|
2016-03-31 21:24:05 -07:00
|
|
|
};
|
|
|
|
|
2018-10-06 19:49:22 -07:00
|
|
|
#endif
|