Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
11 7 : js::GetCPUCount()
12 : {
13 : static uint32_t ncpus = 0;
14 :
15 : // _SC_NPROCESSORS_CONF and _SC_NPROCESSORS_ONLN are common, but not
16 : // standard.
17 7 : if (ncpus == 0) {
18 : #if defined(_SC_NPROCESSORS_CONF)
19 3 : long n = sysconf(_SC_NPROCESSORS_CONF);
20 3 : ncpus = (n > 0) ? uint32_t(n) : 1;
21 : #elif defined(_SC_NPROCESSORS_ONLN)
22 : long n = sysconf(_SC_NPROCESSORS_ONLN);
23 : ncpus = (n > 0) ? uint32_t(n) : 1;
24 : #else
25 : ncpus = 1;
26 : #endif
27 : }
28 :
29 7 : return ncpus;
30 : }
|