diff --git a/kernel/src/ipc/semaphore/system_v/sem.rs b/kernel/src/ipc/semaphore/system_v/sem.rs index 53e6efd34..765f03c65 100644 --- a/kernel/src/ipc/semaphore/system_v/sem.rs +++ b/kernel/src/ipc/semaphore/system_v/sem.rs @@ -99,14 +99,14 @@ pub struct Semaphore { } impl Semaphore { - pub fn set_val(&self, val: i32) -> Result<()> { + pub fn set_val(&self, val: i32, current_pid: Pid) -> Result<()> { if !(0..SEMVMX).contains(&val) { return_errno!(Errno::ERANGE); } let mut current_val = self.val.lock(); *current_val = val; - *self.latest_modified_pid.write() = current!().pid(); + *self.latest_modified_pid.write() = current_pid; self.update_pending_ops(current_val); Ok(()) @@ -166,9 +166,10 @@ impl Semaphore { ); } - fn sem_op(&self, sem_buf: SemBuf, timeout: Option) -> Result<()> { + fn sem_op(&self, sem_buf: SemBuf, timeout: Option, ctx: &Context) -> Result<()> { let mut val = self.val.lock(); let sem_op = sem_buf.sem_op; + let current_pid = ctx.process.pid(); let flags = IpcFlags::from_bits(sem_buf.sem_flags as u32).unwrap(); if flags.contains(IpcFlags::SEM_UNDO) { @@ -189,7 +190,7 @@ impl Semaphore { } *val = new_val; - *self.latest_modified_pid.write() = current!().pid(); + *self.latest_modified_pid.write() = current_pid; self.update_otime(); self.update_pending_ops(val); @@ -206,14 +207,12 @@ impl Semaphore { // Add current to pending list let (waiter, waker) = Waiter::new_pair(); let status = Arc::new(AtomicStatus::new(Status::Pending)); - let current = current!(); - let pid = current.pid(); let pending_op = Box::new(PendingOp { sem_buf, status: status.clone(), waker: waker.clone(), - process: Arc::downgrade(¤t), - pid, + process: ctx.posix_thread.weak_process(), + pid: current_pid, }); if sem_op == 0 { self.pending_const.lock().push_back(pending_op); @@ -241,7 +240,7 @@ impl Semaphore { } else { self.pending_alters.lock() }; - pending_ops.retain(|op| op.pid != pid); + pending_ops.retain(|op| op.pid != current_pid); Err(Error::new(Errno::EAGAIN)) } } @@ -302,7 +301,12 @@ impl Semaphore { } } -pub fn sem_op(sem_id: key_t, sem_buf: SemBuf, timeout: Option) -> Result<()> { +pub fn sem_op( + sem_id: key_t, + sem_buf: SemBuf, + timeout: Option, + ctx: &Context, +) -> Result<()> { debug_assert!(sem_id > 0); debug!("[semop] sembuf: {:?}", sem_buf); @@ -318,5 +322,5 @@ pub fn sem_op(sem_id: key_t, sem_buf: SemBuf, timeout: Option) -> Resu .clone() }; - sem.sem_op(sem_buf, timeout) + sem.sem_op(sem_buf, timeout, ctx) } diff --git a/kernel/src/syscall/semctl.rs b/kernel/src/syscall/semctl.rs index 621570519..b399f8f58 100644 --- a/kernel/src/syscall/semctl.rs +++ b/kernel/src/syscall/semctl.rs @@ -58,7 +58,7 @@ pub fn sys_semctl( .get(semnum as usize) .ok_or(Error::new(Errno::EINVAL))?; - sem.set_val(val)?; + sem.set_val(val, ctx.process.pid())?; sem_set.update_ctime(); Ok(()) diff --git a/kernel/src/syscall/semop.rs b/kernel/src/syscall/semop.rs index 81115ec21..f8c97b083 100644 --- a/kernel/src/syscall/semop.rs +++ b/kernel/src/syscall/semop.rs @@ -13,12 +13,12 @@ use crate::{ time::timespec_t, }; -pub fn sys_semop(sem_id: i32, tsops: Vaddr, nsops: usize, _ctx: &Context) -> Result { +pub fn sys_semop(sem_id: i32, tsops: Vaddr, nsops: usize, ctx: &Context) -> Result { debug!( "[sys_semop] sem_id = {:?}, tsops_vaddr = {:x?}, nsops = {:?}", sem_id, tsops, nsops ); - do_sys_semtimedop(sem_id, tsops, nsops, None) + do_sys_semtimedop(sem_id, tsops, nsops, None, ctx) } pub fn sys_semtimedop( @@ -41,7 +41,7 @@ pub fn sys_semtimedop( )?) }; - do_sys_semtimedop(sem_id, tsops, nsops, timeout) + do_sys_semtimedop(sem_id, tsops, nsops, timeout, ctx) } fn do_sys_semtimedop( @@ -49,6 +49,7 @@ fn do_sys_semtimedop( tsops: Vaddr, nsops: usize, timeout: Option, + ctx: &Context, ) -> Result { if sem_id <= 0 || nsops == 0 { return_errno!(Errno::EINVAL); @@ -64,7 +65,7 @@ fn do_sys_semtimedop( check_sem(sem_id, None, PermissionMode::ALTER)?; } - sem_op(sem_id, sem_buf, timeout)?; + sem_op(sem_id, sem_buf, timeout, ctx)?; } Ok(SyscallReturn::Return(0))