csutil/threading/win32_tls.h
00001 /* 00002 Copyright (C) 2008 by Michael Gist 00003 00004 This library is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU Lesser General Public 00006 License as published by the Free Software Foundation; either 00007 version 2 of the License, or (at your option) any later version. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public 00015 License along with this library; if not, write to the Free 00016 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 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 // Register for later cleanup 00043 if (dtor != 0) 00044 ThreadBase::RegisterTlsInstance (this); 00045 } 00046 00047 ~ThreadLocalBase() 00048 { 00049 // Remove from cleanup set 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 } // Threading 00083 } // CS 00084 00085 #endif // !defined(CS_PLATFORM_WIN32) 00086 00087 #endif // __CS_CSUTIL_THREADING_WIN32_TLS_H__
Generated for Crystal Space 2.0 by doxygen 1.6.1
