mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-09 05:16:47 +00:00
Allow dyn InAtomicMode
as AsAtomicModeGuard
This commit is contained in:
parent
6dc0189e43
commit
72fb0752ae
@ -17,7 +17,10 @@ use non_null::NonNullPtr;
|
|||||||
use spin::once::Once;
|
use spin::once::Once;
|
||||||
|
|
||||||
use self::monitor::RcuMonitor;
|
use self::monitor::RcuMonitor;
|
||||||
use crate::task::{atomic_mode::AsAtomicModeGuard, disable_preempt, DisabledPreemptGuard};
|
use crate::task::{
|
||||||
|
atomic_mode::{AsAtomicModeGuard, InAtomicMode},
|
||||||
|
disable_preempt, DisabledPreemptGuard,
|
||||||
|
};
|
||||||
|
|
||||||
mod monitor;
|
mod monitor;
|
||||||
pub mod non_null;
|
pub mod non_null;
|
||||||
@ -168,19 +171,16 @@ impl<P: NonNullPtr + Send> RcuInner<P> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn read_with<'a>(&'a self, guard: &'a dyn AsAtomicModeGuard) -> Option<P::Ref<'a>> {
|
fn read_with<'a>(&'a self, _guard: &'a dyn InAtomicMode) -> Option<P::Ref<'a>> {
|
||||||
// Ensure that a real atomic-mode guard is obtained.
|
|
||||||
let _atomic_mode_guard = guard.as_atomic_mode_guard();
|
|
||||||
|
|
||||||
let obj_ptr = self.ptr.load(Acquire);
|
let obj_ptr = self.ptr.load(Acquire);
|
||||||
if obj_ptr.is_null() {
|
if obj_ptr.is_null() {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
// SAFETY:
|
// SAFETY:
|
||||||
// 1. This pointer is not NULL.
|
// 1. This pointer is not NULL.
|
||||||
// 2. The `_atomic_mode_guard` guarantees atomic mode for the duration of
|
// 2. The `_guard` guarantees atomic mode for the duration of lifetime
|
||||||
// lifetime `'a`, the pointer is valid because other writers won't release
|
// `'a`, the pointer is valid because other writers won't release the
|
||||||
// the allocation until this task passes the quiescent state.
|
// allocation until this task passes the quiescent state.
|
||||||
NonNull::new(obj_ptr).map(|ptr| unsafe { P::raw_as_ref(ptr) })
|
NonNull::new(obj_ptr).map(|ptr| unsafe { P::raw_as_ref(ptr) })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -286,8 +286,8 @@ impl<P: NonNullPtr + Send> Rcu<P> {
|
|||||||
/// Unlike [`Self::read`], this function does not return a read guard, so
|
/// Unlike [`Self::read`], this function does not return a read guard, so
|
||||||
/// you cannot use [`RcuReadGuard::compare_exchange`] to synchronize the
|
/// you cannot use [`RcuReadGuard::compare_exchange`] to synchronize the
|
||||||
/// writers. You may do it via a [`super::SpinLock`].
|
/// writers. You may do it via a [`super::SpinLock`].
|
||||||
pub fn read_with<'a>(&'a self, guard: &'a dyn AsAtomicModeGuard) -> P::Ref<'a> {
|
pub fn read_with<'a, G: AsAtomicModeGuard + ?Sized>(&'a self, guard: &'a G) -> P::Ref<'a> {
|
||||||
self.0.read_with(guard).unwrap()
|
self.0.read_with(guard.as_atomic_mode_guard()).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -341,8 +341,11 @@ impl<P: NonNullPtr + Send> RcuOption<P> {
|
|||||||
/// Unlike [`Self::read`], this function does not return a read guard, so
|
/// Unlike [`Self::read`], this function does not return a read guard, so
|
||||||
/// you cannot use [`RcuOptionReadGuard::compare_exchange`] to synchronize the
|
/// you cannot use [`RcuOptionReadGuard::compare_exchange`] to synchronize the
|
||||||
/// writers. You may do it via a [`super::SpinLock`].
|
/// writers. You may do it via a [`super::SpinLock`].
|
||||||
pub fn read_with<'a>(&'a self, guard: &'a dyn AsAtomicModeGuard) -> Option<P::Ref<'a>> {
|
pub fn read_with<'a, G: AsAtomicModeGuard + ?Sized>(
|
||||||
self.0.read_with(guard)
|
&'a self,
|
||||||
|
guard: &'a G,
|
||||||
|
) -> Option<P::Ref<'a>> {
|
||||||
|
self.0.read_with(guard.as_atomic_mode_guard())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -73,3 +73,9 @@ impl<G: InAtomicMode> AsAtomicModeGuard for G {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl AsAtomicModeGuard for dyn InAtomicMode + '_ {
|
||||||
|
fn as_atomic_mode_guard(&self) -> &dyn InAtomicMode {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user