00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef __CS_CSUTIL_THREADING_WIN32_TLS_H__
00020 #define __CS_CSUTIL_THREADING_WIN32_TLS_H__
00021
00022 #if !defined(CS_PLATFORM_WIN32)
00023 #error "This file is only for Windows and requires you to include csysdefs.h before"
00024 #else
00025
00026 #include "thread.h"
00027
00028 namespace CS
00029 {
00030 namespace Threading
00031 {
00032 namespace Implementation
00033 {
00034 class ThreadLocalBase
00035 {
00036 public:
00037 typedef void (* DestructorFn)(void*);
00038
00039 ThreadLocalBase (DestructorFn dtor = 0) : dtor (dtor)
00040 {
00041 threadIndex = TlsAlloc();
00042
00043 if (dtor != 0)
00044 ThreadBase::RegisterTlsInstance (this);
00045 }
00046
00047 ~ThreadLocalBase()
00048 {
00049
00050 if (dtor != 0)
00051 ThreadBase::UnregisterTlsInstance (this);
00052
00053 if(threadIndex != TLS_OUT_OF_INDEXES)
00054 {
00055 TlsFree(threadIndex);
00056 }
00057 }
00058
00059 void SetValue(void* data) const
00060 {
00061 TlsSetValue(threadIndex, data);
00062 }
00063
00064 void* GetValue() const
00065 {
00066 return TlsGetValue(threadIndex);
00067 }
00068
00069 protected:
00070 friend class ThreadBase;
00071
00072 void CleanupInstance ()
00073 {
00074 void* p = TlsGetValue (threadIndex);
00075 if (p != 0) dtor (p);
00076 }
00077
00078 DWORD threadIndex;
00079 DestructorFn dtor;
00080 };
00081 }
00082 }
00083 }
00084
00085 #endif // !defined(CS_PLATFORM_WIN32)
00086
00087 #endif // __CS_CSUTIL_THREADING_WIN32_TLS_H__