From a6774396bdd973616acf9ae09626b697b9dfa6c0 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 2 Mar 2009 10:51:41 +0000 Subject: [PATCH] From Mathias Froehlich, "An other one: The TLS Varialbe is accessed before it is initialized. Attached is a change to rev 9791." Merged from svn/trunk using: svn merge -r 9831:9832 http://www.openscenegraph.org/svn/osg/OpenSceneGraph/trunk/src/OpenThreads/win32 --- src/OpenThreads/win32/Win32Thread.cpp | 5 +++-- src/OpenThreads/win32/Win32ThreadPrivateData.h | 12 ++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/OpenThreads/win32/Win32Thread.cpp b/src/OpenThreads/win32/Win32Thread.cpp index afa8dd70a..7e17f8362 100644 --- a/src/OpenThreads/win32/Win32Thread.cpp +++ b/src/OpenThreads/win32/Win32Thread.cpp @@ -98,7 +98,7 @@ namespace OpenThreads { if (thread->_prvData==0) return 0; - TlsSetValue(Win32ThreadPrivateData::TLS.ID ,data); + TlsSetValue(Win32ThreadPrivateData::TLS.getId(), data); //--------------------------------------------------------------------- // Set the proper scheduling priorities // @@ -127,6 +127,7 @@ namespace OpenThreads { // abnormal termination but must be caught in win32 anyway } + TlsSetValue(Win32ThreadPrivateData::TLS.getId(), 0); pd->isRunning = false; return 0; @@ -207,7 +208,7 @@ namespace OpenThreads { Thread* Thread::CurrentThread() { - return (Thread* )TlsGetValue(Win32ThreadPrivateData::TLS.ID); + return (Thread* )TlsGetValue(Win32ThreadPrivateData::TLS.getId()); }; //---------------------------------------------------------------------------- diff --git a/src/OpenThreads/win32/Win32ThreadPrivateData.h b/src/OpenThreads/win32/Win32ThreadPrivateData.h index e26b65b19..451c1af42 100644 --- a/src/OpenThreads/win32/Win32ThreadPrivateData.h +++ b/src/OpenThreads/win32/Win32ThreadPrivateData.h @@ -69,12 +69,20 @@ public: HandleHolder cancelEvent; struct TlsHolder{ // thread local storage slot - DWORD ID; - TlsHolder(): ID(TlsAlloc()){ + DWORD getId() + { + static bool initialized = false; + if (!initialized) { + ID = TlsAlloc(); + initialized = true; + } + return ID; } ~TlsHolder(){ TlsFree(ID); } + private: + DWORD ID; }; static TlsHolder TLS;