From b36f7e1fe79476e33dc73400f79254b31b686e2a Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 23 Jan 2009 10:29:09 +0000 Subject: [PATCH] From Mattias Froehlich, "To pass the Microsoft application verifier we have tried to get osgviewer working with that thing. This is what was missing so far: Make win32 threads behave like the posix implementation when setting thread cpu affinity. That includes avoid setting thread affininty on a non running thread. Set that once it is running." --- src/OpenThreads/win32/Win32Thread.cpp | 12 ++++++++++ .../win32/Win32ThreadPrivateData.h | 24 ++++++++++--------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/OpenThreads/win32/Win32Thread.cpp b/src/OpenThreads/win32/Win32Thread.cpp index 5b21d90cc..02b9af9c5 100644 --- a/src/OpenThreads/win32/Win32Thread.cpp +++ b/src/OpenThreads/win32/Win32Thread.cpp @@ -109,6 +109,9 @@ namespace OpenThreads { // release the thread that created this thread. pd->threadStartedBlock.release(); + if (0 <= pd->cpunum) + thread->setProcessorAffinity(pd->cpunum); + try{ thread->run(); } @@ -248,6 +251,7 @@ Thread::Thread() { pd->threadPolicy = Thread::THREAD_SCHEDULE_DEFAULT; pd->detached = false; pd->cancelEvent.set(CreateEvent(NULL,TRUE,FALSE,NULL)); + pd->cpunum = -1; _prvData = static_cast(pd); } @@ -567,6 +571,14 @@ size_t Thread::getStackSize() { int Thread::setProcessorAffinity( unsigned int cpunum ) { Win32ThreadPrivateData *pd = static_cast (_prvData); + pd->cpunum = cpunum; + if (!pd->isRunning) + return 0; + + if (pd->tid.get() == INVALID_HANDLE_VALUE) + return -1; + + DWORD affinityMask = 0x1 << cpunum ; // thread affinity mask DWORD_PTR res = SetThreadAffinityMask diff --git a/src/OpenThreads/win32/Win32ThreadPrivateData.h b/src/OpenThreads/win32/Win32ThreadPrivateData.h index b59d2426a..e26b65b19 100644 --- a/src/OpenThreads/win32/Win32ThreadPrivateData.h +++ b/src/OpenThreads/win32/Win32ThreadPrivateData.h @@ -29,7 +29,7 @@ namespace OpenThreads { - class Win32ThreadPrivateData { + class Win32ThreadPrivateData { //------------------------------------------------------------------------- // We're friendly to Thread, so it can use our data. // @@ -62,20 +62,22 @@ private: int uniqueId; + int cpunum; + public: - HandleHolder cancelEvent; + HandleHolder cancelEvent; - struct TlsHolder{ // thread local storage slot - DWORD ID; - TlsHolder(): ID(TlsAlloc()){ - } - ~TlsHolder(){ - TlsFree(ID); - } - }; + struct TlsHolder{ // thread local storage slot + DWORD ID; + TlsHolder(): ID(TlsAlloc()){ + } + ~TlsHolder(){ + TlsFree(ID); + } + }; - static TlsHolder TLS; + static TlsHolder TLS; };