From 91eb30667f0d5ebc2a8451d27f28138830b5d5e1 Mon Sep 17 00:00:00 2001 From: castano Date: Thu, 17 Apr 2008 18:39:01 +0000 Subject: [PATCH] Add TLS class wrapper. Fix AutoPtr operator=. Fix typo. --- src/nvcore/Prefetch.h | 2 +- src/nvcore/Ptr.h | 7 +++- src/nvcore/ThreadLocalStorage.h | 73 +++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 3 deletions(-) create mode 100755 src/nvcore/ThreadLocalStorage.h diff --git a/src/nvcore/Prefetch.h b/src/nvcore/Prefetch.h index aa0bcfc..71bd0ed 100644 --- a/src/nvcore/Prefetch.h +++ b/src/nvcore/Prefetch.h @@ -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 diff --git a/src/nvcore/Ptr.h b/src/nvcore/Ptr.h index 4b35718..b5e9fe7 100644 --- a/src/nvcore/Ptr.h +++ b/src/nvcore/Ptr.h @@ -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. */ diff --git a/src/nvcore/ThreadLocalStorage.h b/src/nvcore/ThreadLocalStorage.h new file mode 100755 index 0000000..f71a07d --- /dev/null +++ b/src/nvcore/ThreadLocalStorage.h @@ -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 + +// ThreadLocal context; +// +// context.allocate(); +// +// context = new Context(); +// context->member(); +// context = NULL; +// +// context.free(); + +#if NV_CC_GNUC + +#elif NV_CC_MSVC + +template +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(TlsGetValue(index)); + } + + DWORD index; +}; + + +#endif // NV_CC_MSVC + +#endif // NV_CORE_THREADLOCALSTORAGE_H