1 // Copyright 2022 The ChromiumOS Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 use std::mem::MaybeUninit; 6 7 use once_cell::sync::Lazy; 8 use winapi::um::processthreadsapi::GetCurrentProcessId; 9 use winapi::um::sysinfoapi::GetNativeSystemInfo; 10 use winapi::um::sysinfoapi::SYSTEM_INFO; 11 12 use super::Pid; 13 use crate::Result; 14 15 struct SystemInfo { 16 pagesize: usize, 17 number_of_logical_cores: usize, 18 allocation_granularity: u64, 19 } 20 21 static SYSTEM_INFO: Lazy<SystemInfo> = Lazy::new(|| { 22 // SAFETY: 23 // Safe because this is a universally available call on modern Windows systems. 24 let sysinfo = unsafe { 25 let mut sysinfo = MaybeUninit::<SYSTEM_INFO>::uninit(); 26 GetNativeSystemInfo(sysinfo.as_mut_ptr()); 27 sysinfo.assume_init() 28 }; 29 30 SystemInfo { 31 pagesize: sysinfo.dwPageSize as usize, 32 number_of_logical_cores: sysinfo.dwNumberOfProcessors as usize, 33 allocation_granularity: sysinfo.dwAllocationGranularity.into(), 34 } 35 }); 36 37 /// Returns the system page size in bytes. pagesize() -> usize38pub fn pagesize() -> usize { 39 SYSTEM_INFO.pagesize 40 } 41 42 /// Returns the number of online logical cores on the system. number_of_logical_cores() -> Result<usize>43pub fn number_of_logical_cores() -> Result<usize> { 44 Ok(SYSTEM_INFO.number_of_logical_cores) 45 } 46 47 /// Returns the minimum memory allocation granularity in bytes. allocation_granularity() -> u6448pub fn allocation_granularity() -> u64 { 49 SYSTEM_INFO.allocation_granularity 50 } 51 52 /// Cross-platform wrapper around getting the current process id. 53 #[inline(always)] getpid() -> Pid54pub fn getpid() -> Pid { 55 // SAFETY: 56 // Safe because we only use the return value. 57 unsafe { GetCurrentProcessId() } 58 } 59 60 /// Set the name of the thread. set_thread_name(_name: &str) -> Result<()>61pub fn set_thread_name(_name: &str) -> Result<()> { 62 todo!(); 63 } 64