1 /* 2 * Copyright 2012 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef _GNU_SOURCE 9 #define _GNU_SOURCE //for pthread_setaffinity_np 10 #endif 11 12 #include "SkThreadUtils.h" 13 #include "SkThreadUtils_pthread.h" 14 15 #include <pthread.h> 16 17 static int nth_set_cpu(unsigned int n, cpu_set_t* cpuSet) { 18 n %= CPU_COUNT(cpuSet); 19 for (unsigned int setCpusSeen = 0, currentCpu = 0; true; ++currentCpu) { 20 if (CPU_ISSET(currentCpu, cpuSet)) { 21 ++setCpusSeen; 22 if (setCpusSeen > n) { 23 return currentCpu; 24 } 25 } 26 } 27 } 28 29 bool SkThread::setProcessorAffinity(unsigned int processor) { 30 SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(fData); 31 if (!pthreadData->fValidPThread) { 32 return false; 33 } 34 35 cpu_set_t parentCpuset; 36 if (0 != pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &parentCpuset)) { 37 return false; 38 } 39 40 cpu_set_t cpuset; 41 CPU_ZERO(&cpuset); 42 CPU_SET(nth_set_cpu(processor, &parentCpuset), &cpuset); 43 return 0 == pthread_setaffinity_np(pthreadData->fPThread, 44 sizeof(cpu_set_t), 45 &cpuset); 46 } 47