-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: safe globals implemented via NullLock<T>
- Loading branch information
Showing
5 changed files
with
180 additions
and
16 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
//! Synchronization primitives. | ||
//! | ||
//! # Resources | ||
//! | ||
//! - <https://doc.rust-lang.org/book/ch16-04-extensible-concurrency-sync-and-send.html> | ||
//! - <https://stackoverflow.com/questions/59428096/understanding-the-send-trait> | ||
//! - <https://doc.rust-lang.org/std/cell/index.html> | ||
//---------------------------------------------------------------------------- | ||
// Public Definitions | ||
//---------------------------------------------------------------------------- | ||
|
||
use core::cell::UnsafeCell; | ||
|
||
/// Synchronization interfaces. | ||
pub mod interface { | ||
|
||
/// Any object implementing this trait guarantees exclusive access to the data wrapped within | ||
/// the Mutex for the duration of the provided closure. | ||
pub trait Mutex { | ||
/// The type of the data that is wrapped by this mutex. | ||
type Data; | ||
|
||
/// Locks the mutex and grants the closure temporary mutable access to the wrapped data. | ||
fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut Self::Data) -> R) -> R; | ||
} | ||
} | ||
|
||
/// A pseudo-lock for experimental purposes. | ||
/// | ||
/// In contrast to a real Mutex implementation, does not protect against concurrent access from | ||
/// other cores to the contained data. This part is preserved for later inspections. | ||
/// | ||
/// The lock will only be used as long as it is safe to do so, i.e. as long as the kernel is | ||
/// executing single-threaded, aka only running on a single core with interrupts disabled. | ||
pub struct NullLock<T> | ||
where | ||
T: ?Sized, | ||
{ | ||
data: UnsafeCell<T>, | ||
} | ||
|
||
//---------------------------------------------------------------------------- | ||
// Public Code | ||
//---------------------------------------------------------------------------- | ||
|
||
unsafe impl<T> Send for NullLock<T> where T: ?Sized + Send {} | ||
unsafe impl<T> Sync for NullLock<T> where T: ?Sized + Send {} | ||
|
||
impl<T> NullLock<T> { | ||
/// Create an instance | ||
pub const fn new(data: T) -> Self { | ||
Self { | ||
data: UnsafeCell::new(data), | ||
} | ||
} | ||
} | ||
|
||
//---------------------------------------------------------------------------- | ||
// Operating System Interface Code | ||
//---------------------------------------------------------------------------- | ||
|
||
impl<T> interface::Mutex for NullLock<T> { | ||
type Data = T; | ||
|
||
fn lock<'a, R>(&'a self, f: impl FnOnce(&'a mut Self::Data) -> R) -> R { | ||
// In a real lock, there would be code encapsulating this line that ensures that this | ||
// mutable reference will ever only be given out once at a time. | ||
let data = unsafe { &mut *self.data.get() }; | ||
|
||
f(data) | ||
} | ||
} |