1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include "hal_cpu.h" 17 #ifdef _WIN32 18 #include <windows.h> 19 #elif defined __LINUX__ 20 #include <unistd.h> 21 #endif 22 23 #ifdef __cplusplus 24 #if __cplusplus 25 extern "C" { 26 #endif 27 #endif 28 HalGetCpuCoreNum()29uint32_t HalGetCpuCoreNum() 30 { 31 static uint32_t cpuCoreNum = 0; 32 if (cpuCoreNum) { 33 return cpuCoreNum; 34 } 35 #ifdef _WIN32 36 SYSTEM_INFO sysInfo; 37 GetSystemInfo(&sysInfo); 38 cpuCoreNum = sysInfo.dwNumberOfProcessors; 39 #elif defined __LINUX__ 40 cpuCoreNum = sysconf(_SC_NPROCESSORS_ONLN); 41 #elif ((defined (HAL_CPU_NUM)) && (HAL_CPU_NUM < 8)) 42 cpuCoreNum = HAL_CPU_NUM; 43 #else 44 cpuCoreNum = 1; 45 #endif 46 return cpuCoreNum; 47 } 48 49 #ifdef __cplusplus 50 #if __cplusplus 51 } 52 #endif /* __cplusplus */ 53 #endif /* __cplusplus */ 54