Add TLS class wrapper.

Fix AutoPtr operator=.
Fix typo.
This commit is contained in:
castano 2008-04-17 18:39:01 +00:00
parent 6db5cffca6
commit 91eb30667f
3 changed files with 79 additions and 3 deletions

View File

@ -24,7 +24,7 @@ __forceinline void nvPrefetch(const void * mem)
#else // NV_CC_MSVC
// do nothing in other case.
#define piPrefetch(ptr)
#define nvPrefetch(ptr)
#endif // NV_CC_MSVC

View File

@ -48,9 +48,12 @@ public:
/** Delete owned pointer and assign new one. */
void operator=( T * p ) {
if (p != m_ptr)
{
delete m_ptr;
m_ptr = p;
}
}
/** Member access. */
T * operator -> () const {

73
src/nvcore/ThreadLocalStorage.h Executable file
View File

@ -0,0 +1,73 @@
// This code is in the public domain -- castanyo@yahoo.es
#ifndef NV_CORE_THREADLOCALSTORAGE_H
#define NV_CORE_THREADLOCALSTORAGE_H
#include <nvcore/nvcore.h>
// ThreadLocal<Context> context;
//
// context.allocate();
//
// context = new Context();
// context->member();
// context = NULL;
//
// context.free();
#if NV_CC_GNUC
#elif NV_CC_MSVC
template <class T>
class ThreadLocal
{
public:
ThreadLocal() : index(0) {}
~ThreadLocal() { nvCheck(index == 0); }
void allocate()
{
index = TlsAlloc();
}
void free()
{
delete ptr();
TlsFree(index);
index = 0;
}
bool isValid()
{
return index != 0;
}
void operator=( T * p )
{
if (p != ptr())
{
delete ptr();
TlsSetValue(index, p);
}
}
T * operator -> () const
{
return ptr();
}
T & operator*() const
{
return *ptr();
}
T * ptr() const {
return static_cast<T *>(TlsGetValue(index));
}
DWORD index;
};
#endif // NV_CC_MSVC
#endif // NV_CORE_THREADLOCALSTORAGE_H