From 171e4f0f29d8f17fe6497c596511d352617fa03b Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Tue, 27 Sep 2016 16:18:53 +0100 Subject: [PATCH] Implemented setting thread affinity for threads that aren't OpenThreads threads, such as main thread --- src/OpenThreads/win32/Win32Thread.cpp | 94 ++++++++++++--------------- 1 file changed, 42 insertions(+), 52 deletions(-) diff --git a/src/OpenThreads/win32/Win32Thread.cpp b/src/OpenThreads/win32/Win32Thread.cpp index 6ba17bb3e..87f8226cb 100644 --- a/src/OpenThreads/win32/Win32Thread.cpp +++ b/src/OpenThreads/win32/Win32Thread.cpp @@ -567,6 +567,45 @@ size_t Thread::getStackSize() { return pd->stackSize; } +static int SetThreadAffinity(HANDLE tid, const Affinity& affinity) +{ + unsigned int numprocessors = OpenThreads::GetNumberOfProcessors(); + std::cout << "setProcessorAffinity() : affinity.activeCPUs.size()=" << affinity.activeCPUs.size() << ", numprocessors=" << numprocessors << std::endl; + + DWORD affinityMask = 0x0; + if (affinity) + { + for (Affinity::ActiveCPUs::const_iterator itr = affinity.activeCPUs.begin(); + itr != affinity.activeCPUs.end(); + ++itr) + { + unsigned int cpunum = *itr; + if (cpunumtid.get(), // handle to thread - affinityMask // thread affinity mask - ); -/* - This one is funny. - This is "non-mandatory" affinity , windows will try to use dwIdealProcessor - whenever possible ( when Bill's account is over 50B, maybe :-) ). - - DWORD SetThreadIdealProcessor( - HANDLE hThread, // handle to the thread - DWORD dwIdealProcessor // ideal processor number - ); -*/ - // return value 1 means call is ignored ( 9x/ME/SE ) - if( res == 1 ) return -1; - // return value 0 is failure - return (res == 0) ? GetLastError() : 0 ; + return SetThreadAffinity(pd->tid.get(), affinity); } //----------------------------------------------------------------------------- @@ -718,7 +709,6 @@ int OpenThreads::SetProcessorAffinityOfCurrentThread(const Affinity& affinity) } else { - // non op right now, needs implementation. - return -1; + return SetThreadAffinity(GetCurrentThread(), affinity); } }