1e2841179SLoGin use core::sync::atomic::AtomicU32; 2c3dad001SLoGin 3c3dad001SLoGin use alloc::vec::Vec; 4c3dad001SLoGin 5c3dad001SLoGin use crate::{ 6338f6903SLoGin libs::lazy_init::Lazy, 7*8cb2e9b3SLoGin smp::{ 8*8cb2e9b3SLoGin core::smp_get_processor_id, 9*8cb2e9b3SLoGin cpu::{smp_cpu_manager, ProcessorId}, 10*8cb2e9b3SLoGin }, 11c3dad001SLoGin }; 12c3dad001SLoGin 13c3dad001SLoGin /// 系统中的CPU数量 14c3dad001SLoGin /// 15c3dad001SLoGin /// todo: 待smp模块重构后,从smp模块获取CPU数量。 16c3dad001SLoGin /// 目前由于smp模块初始化时机较晚,导致大部分内核模块无法在早期初始化PerCpu变量。 17e2841179SLoGin const CPU_NUM: AtomicU32 = AtomicU32::new(PerCpu::MAX_CPU_NUM); 18c3dad001SLoGin 19c3dad001SLoGin #[derive(Debug)] 20c3dad001SLoGin pub struct PerCpu; 21c3dad001SLoGin 22c3dad001SLoGin impl PerCpu { 23e2841179SLoGin pub const MAX_CPU_NUM: u32 = 128; 24c3dad001SLoGin /// # 初始化PerCpu 25c3dad001SLoGin /// 26c3dad001SLoGin /// 该函数应该在内核初始化时调用一次。 27c3dad001SLoGin /// 28c3dad001SLoGin /// 该函数会调用`smp_get_total_cpu()`获取CPU数量,然后将其存储在`CPU_NUM`中。 29c3dad001SLoGin #[allow(dead_code)] 30c3dad001SLoGin pub fn init() { 31c3dad001SLoGin if CPU_NUM.load(core::sync::atomic::Ordering::SeqCst) != 0 { 32c3dad001SLoGin panic!("PerCpu::init() called twice"); 33c3dad001SLoGin } 34*8cb2e9b3SLoGin let cpus = smp_cpu_manager().present_cpus_count(); 35*8cb2e9b3SLoGin assert!(cpus > 0, "PerCpu::init(): present_cpus_count() returned 0"); 36*8cb2e9b3SLoGin 37c3dad001SLoGin CPU_NUM.store(cpus, core::sync::atomic::Ordering::SeqCst); 38c3dad001SLoGin } 39c3dad001SLoGin } 40c3dad001SLoGin 41c3dad001SLoGin /// PerCpu变量 42c3dad001SLoGin /// 43c3dad001SLoGin /// 该结构体的每个实例都是线程安全的,因为每个CPU都有自己的变量。 44c3dad001SLoGin /// 45c3dad001SLoGin /// 一种简单的使用方法是:使用该结构体提供的`define_lazy`方法定义一个全局变量, 46c3dad001SLoGin /// 然后在内核初始化时调用`init`、`new`方法去初始化它。 47c3dad001SLoGin /// 48c3dad001SLoGin /// 当然,由于Lazy<T>有运行时开销,所以也可以直接全局声明一个Option, 49c3dad001SLoGin /// 然后手动初始化然后赋值到Option中。(这样需要在初始化的时候,手动确保并发安全) 50c3dad001SLoGin #[derive(Debug)] 51c3dad001SLoGin #[allow(dead_code)] 52c3dad001SLoGin pub struct PerCpuVar<T> { 53c3dad001SLoGin inner: Vec<T>, 54c3dad001SLoGin } 55c3dad001SLoGin 56c3dad001SLoGin #[allow(dead_code)] 57c3dad001SLoGin impl<T> PerCpuVar<T> { 58c3dad001SLoGin /// # 初始化PerCpu变量 59c3dad001SLoGin /// 60c3dad001SLoGin /// ## 参数 61c3dad001SLoGin /// 62c3dad001SLoGin /// - `data` - 每个CPU的数据的初始值。 传入的Vec的长度必须等于CPU的数量,否则返回None。 63c3dad001SLoGin pub fn new(data: Vec<T>) -> Option<Self> { 64c3dad001SLoGin let cpu_num = CPU_NUM.load(core::sync::atomic::Ordering::SeqCst); 65c3dad001SLoGin if cpu_num == 0 { 66c3dad001SLoGin panic!("PerCpu::init() not called"); 67c3dad001SLoGin } 68c3dad001SLoGin 69e2841179SLoGin if data.len() != cpu_num.try_into().unwrap() { 70c3dad001SLoGin return None; 71c3dad001SLoGin } 72c3dad001SLoGin 73c3dad001SLoGin return Some(Self { inner: data }); 74c3dad001SLoGin } 75c3dad001SLoGin 76c3dad001SLoGin /// 定义一个Lazy的PerCpu变量,稍后再初始化 77c3dad001SLoGin pub const fn define_lazy() -> Lazy<Self> { 78c3dad001SLoGin Lazy::<Self>::new() 79c3dad001SLoGin } 80c3dad001SLoGin 81c3dad001SLoGin pub fn get(&self) -> &T { 82c3dad001SLoGin let cpu_id = smp_get_processor_id(); 83e2841179SLoGin &self.inner[cpu_id.data() as usize] 84c3dad001SLoGin } 85c3dad001SLoGin 86*8cb2e9b3SLoGin pub fn get_mut(&self) -> &mut T { 87c3dad001SLoGin let cpu_id = smp_get_processor_id(); 88*8cb2e9b3SLoGin unsafe { 89*8cb2e9b3SLoGin &mut (self as *const Self as *mut Self).as_mut().unwrap().inner[cpu_id.data() as usize] 90*8cb2e9b3SLoGin } 91c3dad001SLoGin } 92338f6903SLoGin 93338f6903SLoGin pub unsafe fn force_get(&self, cpu_id: ProcessorId) -> &T { 94338f6903SLoGin &self.inner[cpu_id.data() as usize] 95338f6903SLoGin } 96338f6903SLoGin 97*8cb2e9b3SLoGin pub unsafe fn force_get_mut(&self, cpu_id: ProcessorId) -> &mut T { 98*8cb2e9b3SLoGin &mut (self as *const Self as *mut Self).as_mut().unwrap().inner[cpu_id.data() as usize] 99338f6903SLoGin } 100c3dad001SLoGin } 101c3dad001SLoGin 102c3dad001SLoGin /// PerCpu变量是线程安全的,因为每个CPU都有自己的变量。 103c3dad001SLoGin unsafe impl<T> Sync for PerCpuVar<T> {} 104c3dad001SLoGin unsafe impl<T> Send for PerCpuVar<T> {} 105