-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements wrapper type to store per-CPU value (#983)
- Loading branch information
1 parent
aa362f7
commit c1dcf43
Showing
10 changed files
with
90 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub const PAGE_SIZE: usize = 0x4000; | ||
use core::num::NonZero; | ||
|
||
pub const PAGE_SIZE: NonZero<usize> = unsafe { NonZero::new_unchecked(0x4000) }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
pub const PAGE_SIZE: usize = 0x1000; | ||
use core::num::NonZero; | ||
|
||
pub const PAGE_SIZE: NonZero<usize> = unsafe { NonZero::new_unchecked(0x1000) }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use super::{Context, PinnedContext}; | ||
use crate::config::config; | ||
use alloc::vec::Vec; | ||
use core::ops::Deref; | ||
|
||
/// Encapsulates per-CPU value. | ||
pub struct CpuLocal<T>(Vec<T>); | ||
|
||
impl<T> CpuLocal<T> { | ||
pub fn new(mut f: impl FnMut(usize) -> T) -> Self { | ||
let len = config().max_cpu.get(); | ||
let mut vec = Vec::with_capacity(len); | ||
|
||
for i in 0..len { | ||
vec.push(f(i)); | ||
} | ||
|
||
Self(vec) | ||
} | ||
|
||
pub fn lock(&self) -> CpuLock<T> { | ||
let pin = Context::pin(); | ||
let val = &self.0[unsafe { pin.cpu() }]; | ||
|
||
CpuLock { val, pin } | ||
} | ||
} | ||
|
||
/// RAII struct to access per-CPU value in [`CpuLocal`]. | ||
pub struct CpuLock<'a, T> { | ||
val: &'a T, | ||
#[allow(dead_code)] | ||
pin: PinnedContext, // Must be dropped last. | ||
} | ||
|
||
impl<'a, T> Deref for CpuLock<'a, T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.val | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters