1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/utils/SkThreadUtils_pthread_linux.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,46 @@ 1.4 +/* 1.5 + * Copyright 2012 Google Inc. 1.6 + * 1.7 + * Use of this source code is governed by a BSD-style license that can be 1.8 + * found in the LICENSE file. 1.9 + */ 1.10 + 1.11 +#ifndef _GNU_SOURCE 1.12 +#define _GNU_SOURCE //for pthread_setaffinity_np 1.13 +#endif 1.14 + 1.15 +#include "SkThreadUtils.h" 1.16 +#include "SkThreadUtils_pthread.h" 1.17 + 1.18 +#include <pthread.h> 1.19 + 1.20 +static int nth_set_cpu(unsigned int n, cpu_set_t* cpuSet) { 1.21 + n %= CPU_COUNT(cpuSet); 1.22 + for (unsigned int setCpusSeen = 0, currentCpu = 0; true; ++currentCpu) { 1.23 + if (CPU_ISSET(currentCpu, cpuSet)) { 1.24 + ++setCpusSeen; 1.25 + if (setCpusSeen > n) { 1.26 + return currentCpu; 1.27 + } 1.28 + } 1.29 + } 1.30 +} 1.31 + 1.32 +bool SkThread::setProcessorAffinity(unsigned int processor) { 1.33 + SkThread_PThreadData* pthreadData = static_cast<SkThread_PThreadData*>(fData); 1.34 + if (!pthreadData->fValidPThread) { 1.35 + return false; 1.36 + } 1.37 + 1.38 + cpu_set_t parentCpuset; 1.39 + if (0 != pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &parentCpuset)) { 1.40 + return false; 1.41 + } 1.42 + 1.43 + cpu_set_t cpuset; 1.44 + CPU_ZERO(&cpuset); 1.45 + CPU_SET(nth_set_cpu(processor, &parentCpuset), &cpuset); 1.46 + return 0 == pthread_setaffinity_np(pthreadData->fPThread, 1.47 + sizeof(cpu_set_t), 1.48 + &cpuset); 1.49 +}