From 6077e19029e2a7e64f863cbd26c11026a6ee5773 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Mon, 7 Apr 2014 14:11:11 +0000 Subject: [PATCH] From Marcel Pursche, "The problem is that when OpenThreads is build with the Linux pthreads implementation all threads inherit the processor affinity from their parent thread. This behavior is also described in the pthreads man page (http://man7.org/linux/man-pages/man3/pthread_create.3.html): > > Linux-specific details > The new thread inherits copies of the calling thread's capability > sets (see capabilities(7)) and CPU affinity mask (see > sched_setaffinity(2)). > To prevent this behaviour I wrote a patch that explicitly sets the affinity mask to all cores of the system, if no specific affinity was defined with PThread::setProcessorAffinity(unsigned int) . Thank you! " --- src/OpenThreads/pthreads/PThread.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/OpenThreads/pthreads/PThread.cpp b/src/OpenThreads/pthreads/PThread.cpp index 60d1dde51..9858877da 100644 --- a/src/OpenThreads/pthreads/PThread.cpp +++ b/src/OpenThreads/pthreads/PThread.cpp @@ -149,7 +149,30 @@ private: #endif #endif } +#if defined(HAVE_PTHREAD_SETAFFINITY_NP) || defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) || defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) + else + { + // BUG-fix for linux: + // Each thread inherits the processor affinity mask from its parent thread. + // We need to explicitly set it to all CPUs, if no affinity was specified. + cpu_set_t cpumask; + CPU_ZERO( &cpumask ); + + for (int i = 0; i < OpenThreads::GetNumberOfProcessors(); ++i) + { + CPU_SET( i, &cpumask ); + } + +#if defined(HAVE_PTHREAD_SETAFFINITY_NP) + pthread_setaffinity_np( pthread_self(), sizeof(cpumask), &cpumask); +#elif defined(HAVE_THREE_PARAM_SCHED_SETAFFINITY) + sched_setaffinity( 0, sizeof(cpumask), &cpumask ); +#elif defined(HAVE_TWO_PARAM_SCHED_SETAFFINITY) + sched_setaffinity( 0, &cpumask ); +#endif + } +#endif ThreadCleanupStruct tcs; tcs.thread = thread;