[Semaphore] Replace Mutex with SpinLock

This commit is contained in:
Yuke Peng
2024-08-30 19:23:53 +08:00
committed by Tate, Hongliang Tian
parent b17e3e24d8
commit db16caf28c

View File

@ -5,7 +5,7 @@ use core::{
time::Duration, time::Duration,
}; };
use ostd::sync::{Mutex, Waiter, Waker}; use ostd::sync::{PreemptDisabled, Waiter, Waker};
use super::sem_set::SEMVMX; use super::sem_set::SEMVMX;
use crate::{ use crate::{
@ -84,16 +84,16 @@ impl Debug for PendingOp {
#[derive(Debug)] #[derive(Debug)]
pub struct Semaphore { pub struct Semaphore {
val: Mutex<i32>, val: SpinLock<i32>,
/// PID of the process that last modified semaphore. /// PID of the process that last modified semaphore.
/// - through semop with op != 0 /// - through semop with op != 0
/// - through semctl with SETVAL and SETALL /// - through semctl with SETVAL and SETALL
/// - through SEM_UNDO when task exit /// - through SEM_UNDO when task exit
latest_modified_pid: AtomicU32, latest_modified_pid: AtomicU32,
/// Pending alter operations. For each pending operation, it has `sem_op < 0`. /// Pending alter operations. For each pending operation, it has `sem_op < 0`.
pending_alters: Mutex<LinkedList<PendingOp>>, pending_alters: SpinLock<LinkedList<PendingOp>>,
/// Pending zeros operations. For each pending operation, it has `sem_op = 0`. /// Pending zeros operations. For each pending operation, it has `sem_op = 0`.
pending_const: Mutex<LinkedList<PendingOp>>, pending_const: SpinLock<LinkedList<PendingOp>>,
/// Last semop time. /// Last semop time.
sem_otime: AtomicU64, sem_otime: AtomicU64,
} }
@ -150,10 +150,10 @@ impl Semaphore {
pub(super) fn new(val: i32) -> Self { pub(super) fn new(val: i32) -> Self {
Self { Self {
val: Mutex::new(val), val: SpinLock::new(val),
latest_modified_pid: AtomicU32::new(current!().pid()), latest_modified_pid: AtomicU32::new(current!().pid()),
pending_alters: Mutex::new(LinkedList::new()), pending_alters: SpinLock::new(LinkedList::new()),
pending_const: Mutex::new(LinkedList::new()), pending_const: SpinLock::new(LinkedList::new()),
sem_otime: AtomicU64::new(0), sem_otime: AtomicU64::new(0),
} }
} }
@ -170,7 +170,7 @@ impl Semaphore {
let sem_op = sem_buf.sem_op; let sem_op = sem_buf.sem_op;
let current_pid = ctx.process.pid(); let current_pid = ctx.process.pid();
let flags = IpcFlags::from_bits(sem_buf.sem_flags as u32).unwrap(); let flags = IpcFlags::from_bits_truncate(sem_buf.sem_flags as u32);
if flags.contains(IpcFlags::SEM_UNDO) { if flags.contains(IpcFlags::SEM_UNDO) {
todo!() todo!()
} }
@ -243,7 +243,7 @@ impl Semaphore {
} }
/// Updates pending ops after the val changed. /// Updates pending ops after the val changed.
fn update_pending_ops(&self, mut val: MutexGuard<i32>, current_pid: Pid) { fn update_pending_ops(&self, mut val: SpinLockGuard<i32, PreemptDisabled>, current_pid: Pid) {
debug_assert!(*val >= 0); debug_assert!(*val >= 0);
trace!("Updating pending ops, semaphore before: {:?}", *val); trace!("Updating pending ops, semaphore before: {:?}", *val);