1 // Copyright (c) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //! num_cpu windows wrapping
15 use std::os::raw::c_long;
16
17 // Get the current number of available cpu cores on Windows platform
get_cpu_num_online() -> c_long18 pub(crate) fn get_cpu_num_online() -> c_long {
19 #[repr(C)]
20 struct SYSTEM_INFO {
21 w_processor_architecture: u16,
22 w_reserved: u16,
23 dw_page_size: u32,
24 lp_minimum_application_address: *mut u8,
25 lp_maximum_application_address: *mut u8,
26 dw_active_processor_mask: *mut u8,
27 dw_number_of_processors: u32,
28 dw_processor_type: u32,
29 dw_allocation_granularity: u32,
30 w_processor_level: u16,
31 w_processor_revision: u16,
32 }
33
34 extern "system" {
35 fn GetSystemInfo(lpSystemInfo: *mut SYSTEM_INFO);
36 }
37
38 unsafe {
39 let mut sysinfo: SYSTEM_INFO = std::mem::zeroed();
40 GetSystemInfo(&mut sysinfo);
41 sysinfo.dw_number_of_processors as c_long
42 }
43 }
44