From fbf460bf9931a7104621c4091fde212f8ec0f631 Mon Sep 17 00:00:00 2001 From: Stefan Lankes Date: Mon, 7 Aug 2023 15:10:09 +0200 Subject: [PATCH] a recursive mutex isn't part of the hermit-abi --- src/synch/mod.rs | 1 - src/synch/recmutex.rs | 89 ---------------------------------------- src/syscalls/mod.rs | 2 - src/syscalls/recmutex.rs | 74 --------------------------------- 4 files changed, 166 deletions(-) delete mode 100644 src/synch/recmutex.rs delete mode 100644 src/syscalls/recmutex.rs diff --git a/src/synch/mod.rs b/src/synch/mod.rs index 08ebe096c1..b80f87acef 100644 --- a/src/synch/mod.rs +++ b/src/synch/mod.rs @@ -1,5 +1,4 @@ //! Synchronization primitives pub mod futex; -pub mod recmutex; pub mod semaphore; diff --git a/src/synch/recmutex.rs b/src/synch/recmutex.rs deleted file mode 100644 index 885fa9fdd1..0000000000 --- a/src/synch/recmutex.rs +++ /dev/null @@ -1,89 +0,0 @@ -use hermit_sync::TicketMutex; - -use crate::arch::core_local::*; -use crate::scheduler::task::{TaskHandlePriorityQueue, TaskId}; - -struct RecursiveMutexState { - current_tid: Option, - count: usize, - queue: TaskHandlePriorityQueue, -} - -pub struct RecursiveMutex { - state: TicketMutex, -} - -impl RecursiveMutex { - pub const fn new() -> Self { - Self { - state: TicketMutex::new(RecursiveMutexState { - current_tid: None, - count: 0, - queue: TaskHandlePriorityQueue::new(), - }), - } - } - - pub fn acquire(&self) { - // Get information about the current task. - let core_scheduler = core_scheduler(); - let tid = core_scheduler.get_current_task_id(); - - loop { - { - let mut locked_state = self.state.lock(); - - // Is the mutex currently acquired? - if let Some(current_tid) = locked_state.current_tid { - // Has it been acquired by the same task? - if current_tid == tid { - // Yes, so just increment the counter (recursive mutex behavior). - locked_state.count += 1; - return; - } - } else { - // The mutex is currently not acquired, so we become its new owner. - locked_state.current_tid = Some(tid); - locked_state.count = 1; - return; - } - - // The mutex is currently acquired by another task. - // Block the current task and add it to the wakeup queue. - core_scheduler.block_current_task(None); - locked_state - .queue - .push(core_scheduler.get_current_task_handle()); - } - - // Switch to the next task. - core_scheduler.reschedule(); - } - } - - pub fn release(&self) { - if let Some(task) = { - let mut locked_state = self.state.lock(); - - // We could do a sanity check here whether the RecursiveMutex is actually held by the current task. - // But let's just trust our code using this function for the sake of simplicity and performance. - - // Decrement the counter (recursive mutex behavior). - if locked_state.count > 0 { - locked_state.count -= 1; - } - - if locked_state.count == 0 { - // Release the entire recursive mutex. - locked_state.current_tid = None; - - locked_state.queue.pop() - } else { - None - } - } { - // Wake up any task that has been waiting for this mutex. - core_scheduler().custom_wakeup(task); - } - } -} diff --git a/src/syscalls/mod.rs b/src/syscalls/mod.rs index 914e2adcb1..958d74f62f 100644 --- a/src/syscalls/mod.rs +++ b/src/syscalls/mod.rs @@ -8,7 +8,6 @@ pub use self::condvar::*; pub use self::entropy::*; pub use self::futex::*; pub use self::processor::*; -pub use self::recmutex::*; pub use self::semaphore::*; pub use self::spinlock::*; pub use self::system::*; @@ -30,7 +29,6 @@ mod lwip; #[cfg(all(feature = "tcp", not(feature = "newlib")))] pub mod net; mod processor; -mod recmutex; mod semaphore; mod spinlock; mod system; diff --git a/src/syscalls/recmutex.rs b/src/syscalls/recmutex.rs deleted file mode 100644 index 639e2dd1ae..0000000000 --- a/src/syscalls/recmutex.rs +++ /dev/null @@ -1,74 +0,0 @@ -use alloc::boxed::Box; - -use crate::errno::*; -use crate::synch::recmutex::RecursiveMutex; - -extern "C" fn __sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 { - if recmutex.is_null() { - return -EINVAL; - } - - // Create a new boxed recursive mutex and return a pointer to the raw memory. - let boxed_mutex = Box::new(RecursiveMutex::new()); - unsafe { - *recmutex = Box::into_raw(boxed_mutex); - } - - 0 -} - -#[no_mangle] -pub extern "C" fn sys_recmutex_init(recmutex: *mut *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_init(recmutex)) -} - -extern "C" fn __sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 { - if recmutex.is_null() { - return -EINVAL; - } - - // Consume the pointer to the raw memory into a Box again - // and drop the Box to free the associated memory. - unsafe { - drop(Box::from_raw(recmutex)); - } - - 0 -} - -#[no_mangle] -pub extern "C" fn sys_recmutex_destroy(recmutex: *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_destroy(recmutex)) -} - -extern "C" fn __sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 { - if recmutex.is_null() { - return -EINVAL; - } - - let mutex = unsafe { &*recmutex }; - mutex.acquire(); - - 0 -} - -#[no_mangle] -pub extern "C" fn sys_recmutex_lock(recmutex: *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_lock(recmutex)) -} - -extern "C" fn __sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 { - if recmutex.is_null() { - return -EINVAL; - } - - let mutex = unsafe { &*recmutex }; - mutex.release(); - - 0 -} - -#[no_mangle] -pub extern "C" fn sys_recmutex_unlock(recmutex: *mut RecursiveMutex) -> i32 { - kernel_function!(__sys_recmutex_unlock(recmutex)) -}