You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
160 lines
5.0 KiB
Rust
160 lines
5.0 KiB
Rust
5 years ago
|
// SPDX-License-Identifier: MIT OR Apache-2.0
|
||
|
//
|
||
4 years ago
|
// Copyright (c) 2020-2021 Andre Richter <andre.o.richter@gmail.com>
|
||
5 years ago
|
|
||
|
//! Synchronization primitives.
|
||
4 years ago
|
//!
|
||
4 years ago
|
//! # 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>
|
||
5 years ago
|
|
||
|
use core::cell::UnsafeCell;
|
||
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Public Definitions
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
/// Synchronization interfaces.
|
||
|
pub mod interface {
|
||
|
|
||
4 years ago
|
/// Any object implementing this trait guarantees exclusive access to the data wrapped within
|
||
5 years ago
|
/// the Mutex for the duration of the provided closure.
|
||
|
pub trait Mutex {
|
||
4 years ago
|
/// The type of the data that is wrapped by this mutex.
|
||
5 years ago
|
type Data;
|
||
|
|
||
4 years ago
|
/// 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;
|
||
5 years ago
|
}
|
||
|
|
||
|
/// A reader-writer exclusion type.
|
||
|
///
|
||
|
/// The implementing object allows either a number of readers or at most one writer at any point
|
||
|
/// in time.
|
||
|
pub trait ReadWriteEx {
|
||
|
/// The type of encapsulated data.
|
||
|
type Data;
|
||
|
|
||
|
/// Grants temporary mutable access to the encapsulated data.
|
||
4 years ago
|
fn write<R>(&self, f: impl FnOnce(&mut Self::Data) -> R) -> R;
|
||
5 years ago
|
|
||
|
/// Grants temporary immutable access to the encapsulated data.
|
||
4 years ago
|
fn read<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R;
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
|
/// 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 on a single core.
|
||
4 years ago
|
pub struct IRQSafeNullLock<T>
|
||
|
where
|
||
|
T: ?Sized,
|
||
|
{
|
||
5 years ago
|
data: UnsafeCell<T>,
|
||
|
}
|
||
|
|
||
|
/// A pseudo-lock that is RW during the single-core kernel init phase and RO afterwards.
|
||
|
///
|
||
|
/// Intended to encapsulate data that is populated during kernel init when no concurrency exists.
|
||
4 years ago
|
pub struct InitStateLock<T>
|
||
|
where
|
||
|
T: ?Sized,
|
||
|
{
|
||
5 years ago
|
data: UnsafeCell<T>,
|
||
|
}
|
||
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Public Code
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
4 years ago
|
unsafe impl<T> Send for IRQSafeNullLock<T> where T: ?Sized + Send {}
|
||
|
unsafe impl<T> Sync for IRQSafeNullLock<T> where T: ?Sized + Send {}
|
||
5 years ago
|
|
||
|
impl<T> IRQSafeNullLock<T> {
|
||
4 years ago
|
/// Create an instance.
|
||
5 years ago
|
pub const fn new(data: T) -> Self {
|
||
|
Self {
|
||
|
data: UnsafeCell::new(data),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
unsafe impl<T> Send for InitStateLock<T> where T: ?Sized + Send {}
|
||
|
unsafe impl<T> Sync for InitStateLock<T> where T: ?Sized + Send {}
|
||
5 years ago
|
|
||
|
impl<T> InitStateLock<T> {
|
||
4 years ago
|
/// Create an instance.
|
||
5 years ago
|
pub const fn new(data: T) -> Self {
|
||
|
Self {
|
||
|
data: UnsafeCell::new(data),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
//------------------------------------------------------------------------------
|
||
|
// OS Interface Code
|
||
|
//------------------------------------------------------------------------------
|
||
|
use crate::{exception, state};
|
||
|
|
||
4 years ago
|
impl<T> interface::Mutex for IRQSafeNullLock<T> {
|
||
5 years ago
|
type Data = T;
|
||
|
|
||
4 years ago
|
fn lock<R>(&self, f: impl FnOnce(&mut Self::Data) -> R) -> R {
|
||
5 years ago
|
// 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() };
|
||
|
|
||
|
// Execute the closure while IRQs are masked.
|
||
|
exception::asynchronous::exec_with_irq_masked(|| f(data))
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
impl<T> interface::ReadWriteEx for InitStateLock<T> {
|
||
5 years ago
|
type Data = T;
|
||
|
|
||
4 years ago
|
fn write<R>(&self, f: impl FnOnce(&mut Self::Data) -> R) -> R {
|
||
5 years ago
|
assert!(
|
||
4 years ago
|
state::state_manager().is_init(),
|
||
5 years ago
|
"InitStateLock::write called after kernel init phase"
|
||
|
);
|
||
|
assert!(
|
||
|
!exception::asynchronous::is_local_irq_masked(),
|
||
|
"InitStateLock::write called with IRQs unmasked"
|
||
|
);
|
||
|
|
||
|
let data = unsafe { &mut *self.data.get() };
|
||
|
|
||
|
f(data)
|
||
|
}
|
||
|
|
||
4 years ago
|
fn read<R>(&self, f: impl FnOnce(&Self::Data) -> R) -> R {
|
||
5 years ago
|
let data = unsafe { &*self.data.get() };
|
||
|
|
||
|
f(data)
|
||
|
}
|
||
|
}
|
||
4 years ago
|
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
// Testing
|
||
|
//--------------------------------------------------------------------------------------------------
|
||
|
|
||
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
use test_macros::kernel_test;
|
||
|
|
||
|
/// InitStateLock must be transparent.
|
||
|
#[kernel_test]
|
||
|
fn init_state_lock_is_transparent() {
|
||
|
use core::mem::size_of;
|
||
|
|
||
|
assert_eq!(size_of::<InitStateLock<u64>>(), size_of::<u64>());
|
||
|
}
|
||
|
}
|