OS X thread-local fixes

This commit is contained in:
Jack Andersen
2015-12-18 11:33:53 -10:00
parent 68a1e9150e
commit 8a33d74c13
5 changed files with 115 additions and 64 deletions

View File

@@ -15,7 +15,7 @@ class GLDataFactory : public IGraphicsDataFactory
{
friend struct GLCommandQueue;
IGraphicsContext* m_parent;
static thread_local struct GLData* m_deferredData;
static ThreadLocalPtr<struct GLData> m_deferredData;
std::unordered_set<struct GLData*> m_committedData;
std::mutex m_committedMutex;
std::vector<int> m_texUnis;

View File

@@ -3,6 +3,7 @@
#include <memory>
#include <stdint.h>
#include <pthread.h>
#include "boo/System.hpp"
namespace boo
@@ -226,6 +227,28 @@ private:
virtual void destroyAllData()=0;
};
/** 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). */

View File

@@ -11,6 +11,7 @@
#include "IGraphicsCommandQueue.hpp"
#include "boo/IGraphicsContext.hpp"
#include <vector>
#include <mutex>
#include <unordered_set>
#include <unordered_map>
@@ -22,8 +23,9 @@ class MetalDataFactory : public IGraphicsDataFactory
{
friend struct MetalCommandQueue;
IGraphicsContext* m_parent;
struct MetalData* m_deferredData = nullptr;
static ThreadLocalPtr<struct MetalData> m_deferredData;
std::unordered_set<struct MetalData*> m_committedData;
std::mutex m_committedMutex;
struct MetalContext* m_ctx;
void destroyData(IGraphicsData*);
@@ -64,7 +66,7 @@ public:
size_t texCount, ITexture** texs);
void reset();
IGraphicsDataToken commit();
GraphicsDataToken commit();
};
}