Dedicated ThreadLocalPtr header

This commit is contained in:
Jack Andersen 2016-03-31 18:24:05 -10:00
parent 679ba36c4c
commit 83f55a54d9
3 changed files with 33 additions and 27 deletions

View File

@ -193,6 +193,7 @@ add_library(boo
include/boo/audiodev/IAudioVoiceEngine.hpp
include/boo/IWindow.hpp
include/boo/IApplication.hpp
include/boo/ThreadLocalPtr.hpp
include/boo/System.hpp
include/boo/boo.hpp
InputDeviceClasses.cpp

View File

@ -0,0 +1,31 @@
#ifndef BOO_THREADLOCALPTR_HPP
#define BOO_THREADLOCALPTR_HPP
#if _WIN32
#else
#include <pthread.h>
#endif
/** Multiplatform TLS-pointer wrapper (for compilers without proper thread_local support) */
template <class T>
class ThreadLocalPtr
{
#if _WIN32
DWORD m_key;
public:
ThreadLocalPtr() {m_key = TlsAlloc();}
~ThreadLocalPtr() {TlsFree(m_key);}
T* get() {return static_cast<T*>(TlsGetValue(m_key));}
void reset(T* v=nullptr) {TlsSetValue(m_key, v);}
#else
pthread_key_t m_key;
public:
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);}
#endif
T* operator->() {return get();}
};
#endif // BOO_THREADLOCALPTR_HPP

View File

@ -5,11 +5,7 @@
#include <functional>
#include <stdint.h>
#include "boo/System.hpp"
#if _WIN32
#else
#include <pthread.h>
#endif
#include "boo/ThreadLocalPtr.hpp"
namespace boo
{
@ -255,28 +251,6 @@ private:
using FactoryCommitFunc = std::function<bool(IGraphicsDataFactory::Context& ctx)>;
/** Multiplatform TLS-pointer wrapper (for compilers without proper thread_local support) */
template <class T>
class ThreadLocalPtr
{
#if _WIN32
DWORD m_key;
public:
ThreadLocalPtr() {m_key = TlsAlloc();}
~ThreadLocalPtr() {TlsFree(m_key);}
T* get() {return static_cast<T*>(TlsGetValue(m_key));}
void reset(T* v=nullptr) {TlsSetValue(m_key, v);}
#else
pthread_key_t m_key;
public:
ThreadLocalPtr() {pthread_key_create(&m_key, nullptr);}
~ThreadLocalPtr() {pthread_key_delete(m_key);}
T* get() {return static_cast<T*>(pthread_getspecific(m_key));}
void reset(T* v=nullptr) {pthread_setspecific(m_key, v);}
#endif
T* operator->() {return get();}
};
/** Ownership token for maintaining lifetime of factory-created resources
* deletion of this token triggers mass-deallocation of the factory's
* IGraphicsData (please don't delete and draw contained resources in the same frame). */