rust-raspberrypi-OS-tutorials/10_virtual_mem_part1_identity_mapping/src/synchronization.rs

78 lines
2.6 KiB
Rust
Raw Normal View History

2020-03-28 12:26:48 +00:00
// SPDX-License-Identifier: MIT OR Apache-2.0
//
2022-01-15 20:50:11 +00:00
// Copyright (c) 2020-2022 Andre Richter <andre.o.richter@gmail.com>
2020-03-28 12:26:48 +00:00
//! Synchronization primitives.
2020-11-20 21:05:14 +00:00
//!
2021-01-29 21:30:02 +00:00
//! # 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>
2020-03-28 12:26:48 +00:00
use core::cell::UnsafeCell;
//--------------------------------------------------------------------------------------------------
// Public Definitions
//--------------------------------------------------------------------------------------------------
/// Synchronization interfaces.
pub mod interface {
2020-11-20 21:05:14 +00:00
/// Any object implementing this trait guarantees exclusive access to the data wrapped within
2020-03-28 12:26:48 +00:00
/// the Mutex for the duration of the provided closure.
pub trait Mutex {
2020-11-20 21:05:14 +00:00
/// The type of the data that is wrapped by this mutex.
2020-03-28 12:26:48 +00:00
type Data;
2020-11-20 21:05:14 +00:00
/// Locks the mutex and grants the closure temporary mutable access to the wrapped data.
fn lock<R>(&self, f: impl FnOnce(&mut Self::Data) -> R) -> R;
2020-03-28 12:26:48 +00:00
}
}
/// A pseudo-lock for teaching 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 lessons.
///
/// 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.
2020-11-20 21:05:14 +00:00
pub struct NullLock<T>
where
T: ?Sized,
{
2020-03-28 12:26:48 +00:00
data: UnsafeCell<T>,
}
//--------------------------------------------------------------------------------------------------
// Public Code
//--------------------------------------------------------------------------------------------------
2020-11-20 21:05:14 +00:00
unsafe impl<T> Send for NullLock<T> where T: ?Sized + Send {}
unsafe impl<T> Sync for NullLock<T> where T: ?Sized + Send {}
2020-03-28 12:26:48 +00:00
impl<T> NullLock<T> {
2020-11-20 21:05:14 +00:00
/// Create an instance.
2020-03-28 12:26:48 +00:00
pub const fn new(data: T) -> Self {
Self {
data: UnsafeCell::new(data),
}
}
}
//------------------------------------------------------------------------------
// OS Interface Code
//------------------------------------------------------------------------------
2020-11-20 21:05:14 +00:00
impl<T> interface::Mutex for NullLock<T> {
2020-03-28 12:26:48 +00:00
type Data = T;
2020-11-20 21:05:14 +00:00
fn lock<R>(&self, f: impl FnOnce(&mut Self::Data) -> R) -> R {
2020-03-28 12:26:48 +00:00
// 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)
}
}