mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-25 02:13:24 +00:00
Strict TLB coherence
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
30ec0be210
commit
4f0acddfd4
@ -106,14 +106,25 @@ pub fn all_cpus() -> impl Iterator<Item = CpuId> {
|
||||
/// The implementor must ensure that the current task is pinned to the current
|
||||
/// CPU while any one of the instances of the implemented structure exists.
|
||||
pub unsafe trait PinCurrentCpu {
|
||||
/// Returns the number of the current CPU.
|
||||
/// Returns the ID of the current CPU.
|
||||
fn current_cpu(&self) -> CpuId {
|
||||
let id = CURRENT_CPU.load();
|
||||
debug_assert_ne!(id, u32::MAX, "This CPU is not initialized");
|
||||
CpuId(id)
|
||||
current_cpu_racy()
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the ID of the current CPU.
|
||||
///
|
||||
/// This function is safe to call, but is vulnerable to races. The returned CPU
|
||||
/// ID may be outdated if the task migrates to another CPU.
|
||||
///
|
||||
/// To ensure that the CPU ID is up-to-date, do it under any guards that
|
||||
/// implements the [`PinCurrentCpu`] trait.
|
||||
pub fn current_cpu_racy() -> CpuId {
|
||||
let id = CURRENT_CPU.load();
|
||||
debug_assert_ne!(id, u32::MAX, "This CPU is not initialized");
|
||||
CpuId(id)
|
||||
}
|
||||
|
||||
// SAFETY: When IRQs are disabled, the task cannot be passively preempted and
|
||||
// migrates to another CPU. If the task actively calls `yield`, it will not be
|
||||
// successful either.
|
||||
|
Reference in New Issue
Block a user