mirror of https://github.com/AxioDL/boo.git
Dedicated ThreadLocalPtr header
This commit is contained in:
parent
679ba36c4c
commit
83f55a54d9
|
@ -193,6 +193,7 @@ add_library(boo
|
||||||
include/boo/audiodev/IAudioVoiceEngine.hpp
|
include/boo/audiodev/IAudioVoiceEngine.hpp
|
||||||
include/boo/IWindow.hpp
|
include/boo/IWindow.hpp
|
||||||
include/boo/IApplication.hpp
|
include/boo/IApplication.hpp
|
||||||
|
include/boo/ThreadLocalPtr.hpp
|
||||||
include/boo/System.hpp
|
include/boo/System.hpp
|
||||||
include/boo/boo.hpp
|
include/boo/boo.hpp
|
||||||
InputDeviceClasses.cpp
|
InputDeviceClasses.cpp
|
||||||
|
|
|
@ -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
|
|
@ -5,11 +5,7 @@
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include "boo/System.hpp"
|
#include "boo/System.hpp"
|
||||||
|
#include "boo/ThreadLocalPtr.hpp"
|
||||||
#if _WIN32
|
|
||||||
#else
|
|
||||||
#include <pthread.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
namespace boo
|
namespace boo
|
||||||
{
|
{
|
||||||
|
@ -255,28 +251,6 @@ private:
|
||||||
|
|
||||||
using FactoryCommitFunc = std::function<bool(IGraphicsDataFactory::Context& ctx)>;
|
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
|
/** Ownership token for maintaining lifetime of factory-created resources
|
||||||
* deletion of this token triggers mass-deallocation of the factory's
|
* deletion of this token triggers mass-deallocation of the factory's
|
||||||
* IGraphicsData (please don't delete and draw contained resources in the same frame). */
|
* IGraphicsData (please don't delete and draw contained resources in the same frame). */
|
||||||
|
|
Loading…
Reference in New Issue