michael@0: /* michael@0: * Copyright 2012 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: michael@0: #ifndef _GNU_SOURCE michael@0: #define _GNU_SOURCE //for pthread_setaffinity_np michael@0: #endif michael@0: michael@0: #include "SkThreadUtils.h" michael@0: #include "SkThreadUtils_pthread.h" michael@0: michael@0: #include michael@0: michael@0: static int nth_set_cpu(unsigned int n, cpu_set_t* cpuSet) { michael@0: n %= CPU_COUNT(cpuSet); michael@0: for (unsigned int setCpusSeen = 0, currentCpu = 0; true; ++currentCpu) { michael@0: if (CPU_ISSET(currentCpu, cpuSet)) { michael@0: ++setCpusSeen; michael@0: if (setCpusSeen > n) { michael@0: return currentCpu; michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool SkThread::setProcessorAffinity(unsigned int processor) { michael@0: SkThread_PThreadData* pthreadData = static_cast(fData); michael@0: if (!pthreadData->fValidPThread) { michael@0: return false; michael@0: } michael@0: michael@0: cpu_set_t parentCpuset; michael@0: if (0 != pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &parentCpuset)) { michael@0: return false; michael@0: } michael@0: michael@0: cpu_set_t cpuset; michael@0: CPU_ZERO(&cpuset); michael@0: CPU_SET(nth_set_cpu(processor, &parentCpuset), &cpuset); michael@0: return 0 == pthread_setaffinity_np(pthreadData->fPThread, michael@0: sizeof(cpu_set_t), michael@0: &cpuset); michael@0: }