Browse Source

添加简单的cpumask (#533)

LoGin 1 year ago
parent
commit
7d66c3134c
2 changed files with 86 additions and 0 deletions
  1. 85 0
      kernel/src/libs/cpumask.rs
  2. 1 0
      kernel/src/libs/mod.rs

+ 85 - 0
kernel/src/libs/cpumask.rs

@@ -0,0 +1,85 @@
+use bitmap::{traits::BitMapOps, AllocBitmap};
+
+use crate::mm::percpu::PerCpu;
+
+pub struct CpuMask {
+    bmp: AllocBitmap,
+}
+
+#[allow(dead_code)]
+impl CpuMask {
+    pub fn new() -> Self {
+        let bmp = AllocBitmap::new(PerCpu::MAX_CPU_NUM);
+        Self { bmp }
+    }
+
+    /// 获取CpuMask中的第一个cpu
+    pub fn first(&self) -> Option<usize> {
+        self.bmp.first_index()
+    }
+
+    /// 获取CpuMask中第一个未被置位的cpu
+    pub fn first_zero(&self) -> Option<usize> {
+        self.bmp.first_false_index()
+    }
+
+    /// 获取CpuMask中的最后一个被置位的cpu
+    pub fn last(&self) -> Option<usize> {
+        self.bmp.last_index()
+    }
+
+    /// 获取指定cpu之后第一个为1的位的cpu
+    pub fn next_index(&self, cpu: usize) -> Option<usize> {
+        self.bmp.next_index(cpu)
+    }
+
+    /// 获取指定cpu之后第一个为未被置位的cpu
+    pub fn next_zero_index(&self, cpu: usize) -> Option<usize> {
+        self.bmp.next_false_index(cpu)
+    }
+
+    /// 迭代所有被置位的cpu
+    pub fn iter_cpu(&self) -> CpuMaskIter {
+        CpuMaskIter {
+            mask: self,
+            index: 0,
+            set: true,
+        }
+    }
+
+    /// 迭代所有未被置位的cpu
+    pub fn iter_zero_cpu(&self) -> CpuMaskIter {
+        CpuMaskIter {
+            mask: self,
+            index: 0,
+            set: false,
+        }
+    }
+}
+
+pub struct CpuMaskIter<'a> {
+    mask: &'a CpuMask,
+    index: usize,
+    set: bool,
+}
+
+impl<'a> Iterator for CpuMaskIter<'a> {
+    type Item = usize;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        if self.index == 0 {
+            if self.set {
+                self.index = self.mask.first()?;
+            } else {
+                self.index = self.mask.first_zero()?;
+            }
+        }
+
+        if self.set {
+            self.index = self.mask.next_index(self.index)?;
+        } else {
+            self.index = self.mask.next_zero_index(self.index)?;
+        }
+        Some(self.index)
+    }
+}

+ 1 - 0
kernel/src/libs/mod.rs

@@ -1,5 +1,6 @@
 pub mod align;
 pub mod casting;
+pub mod cpumask;
 pub mod elf;
 pub mod ffi_convert;
 #[macro_use]