Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch add lazy init #236

Merged
merged 3 commits into from
Apr 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 9 additions & 11 deletions kernel/src/libs/lazy_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,7 @@ use core::sync::atomic::{AtomicBool, Ordering};
use super::spinlock::SpinLock;

/// A wrapper around a value that is initialized lazily.
/// The value is initialized the first time it is accessed.
/// This is useful for initializing values that are expensive to initialize.
/// The value is initialized using the provided closure.
/// The closure is only called once, and the result is cached.
/// The value is not initialized if it is never accessed.
pub struct Lazy<T: Sync> {
pub struct Lazy<T> {
/// The lock that is used to ensure that only one thread calls the init function at the same time.
init_lock: SpinLock<()>,
/// The value that is initialized lazily.
Expand All @@ -39,7 +34,7 @@ pub struct Lazy<T: Sync> {
initialized: AtomicBool,
}

impl<T: Sync> Lazy<T> {
impl<T> Lazy<T> {
/// Creates a new `Lazy` value that will be initialized with the
/// result of the closure `init`.
pub const fn new() -> Lazy<T> {
Expand Down Expand Up @@ -122,7 +117,7 @@ impl<T: Sync> Lazy<T> {
}
}

impl<T: Sync> Deref for Lazy<T> {
impl<T> Deref for Lazy<T> {
type Target = T;

#[inline(always)]
Expand All @@ -131,14 +126,14 @@ impl<T: Sync> Deref for Lazy<T> {
}
}

impl<T: Sync> DerefMut for Lazy<T> {
impl<T> DerefMut for Lazy<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut T {
return self.get_mut();
}
}

impl<T: Sync + Debug> Debug for Lazy<T> {
impl<T: Debug> Debug for Lazy<T> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if let Some(value) = self.try_get() {
return write!(f, "Lazy({:?})", value);
Expand All @@ -148,7 +143,7 @@ impl<T: Sync + Debug> Debug for Lazy<T> {
}
}

impl<T: Sync> Drop for Lazy<T> {
impl<T> Drop for Lazy<T> {
fn drop(&mut self) {
if self.initialized() {
unsafe {
Expand All @@ -157,3 +152,6 @@ impl<T: Sync> Drop for Lazy<T> {
}
}
}

unsafe impl<T: Send + Sync> Sync for Lazy<T> {}
unsafe impl<T: Send> Send for Lazy<T> {}