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