Add TLS class wrapper.

Fix AutoPtr operator=.
Fix typo.
pull/216/head
castano 16 years ago
parent 6db5cffca6
commit 91eb30667f

@ -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

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

@ -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
Loading…
Cancel
Save