CpuCount.cpp (792B)
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include <unistd.h> 7 8 #include "threading/CpuCount.h" 9 10 uint32_t js::GetCPUCount() { 11 static uint32_t ncpus = 0; 12 13 // _SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN are common, but not 14 // standard. 15 if (ncpus == 0) { 16 #if defined(_SC_NPROCESSORS_CONF) 17 long n = sysconf(_SC_NPROCESSORS_CONF); 18 ncpus = (n > 0) ? uint32_t(n) : 1; 19 #elif defined(_SC_NPROCESSORS_ONLN) 20 long n = sysconf(_SC_NPROCESSORS_ONLN); 21 ncpus = (n > 0) ? uint32_t(n) : 1; 22 #else 23 ncpus = 1; 24 #endif 25 } 26 27 return ncpus; 28 }