[Semaphore] Optimize the update_pending_ops

This commit is contained in:
Yuke Peng
2024-08-30 13:47:40 +08:00
committed by Tate, Hongliang Tian
parent a8970daec2
commit 3297976700
2 changed files with 35 additions and 27 deletions

View File

@ -258,37 +258,43 @@ impl Semaphore {
// 2. If val is equal to 0, then clear pending_const // 2. If val is equal to 0, then clear pending_const
// Step one: // Step one:
let mut value = *val;
if value > 0 {
let mut pending_alters = self.pending_alters.lock(); let mut pending_alters = self.pending_alters.lock();
pending_alters.retain_mut(|op| { let mut cursor = pending_alters.cursor_front_mut();
if *val == 0 { while let Some(op) = cursor.current() {
return true; if value == 0 {
break;
} }
// Check if the process alive. // Check if the process alive.
if op.process.upgrade().is_none() { if op.process.upgrade().is_none() {
return false; cursor.remove_current().unwrap();
continue;
} }
debug_assert!(op.sem_buf.sem_op < 0); debug_assert!(op.sem_buf.sem_op < 0);
if op.sem_buf.sem_op.abs() as i32 <= *val { if op.sem_buf.sem_op.abs() as i32 <= value {
trace!( trace!(
"Found removable pending op, op: {:?}, pid:{:?}", "Found removable pending op, op: {:?}, pid:{:?}",
op.sem_buf.sem_op, op.sem_buf.sem_op,
op.pid op.pid
); );
*val += i32::from(op.sem_buf.sem_op); value += i32::from(op.sem_buf.sem_op);
self.latest_modified_pid.store(op.pid, Ordering::Relaxed); self.latest_modified_pid.store(op.pid, Ordering::Relaxed);
self.update_otime(); self.update_otime();
op.status.set_status(Status::Normal); op.status.set_status(Status::Normal);
op.waker.wake_up(); op.waker.wake_up();
false cursor.remove_current().unwrap();
} else { } else {
true cursor.move_next();
}
}
} }
});
// Step two: // Step two:
if *val == 0 { if value == 0 {
let mut pending_const = self.pending_const.lock(); let mut pending_const = self.pending_const.lock();
pending_const.iter().for_each(|op| { pending_const.iter().for_each(|op| {
op.status.set_status(Status::Normal); op.status.set_status(Status::Normal);
@ -299,7 +305,8 @@ impl Semaphore {
}); });
pending_const.clear(); pending_const.clear();
} }
trace!("Updated pending ops, semaphore after: {:?}", *val); *val = value;
trace!("Updated pending ops, semaphore after: {:?}", value);
} }
} }

View File

@ -28,6 +28,7 @@
#![feature(trait_alias)] #![feature(trait_alias)]
#![feature(trait_upcasting)] #![feature(trait_upcasting)]
#![feature(linked_list_retain)] #![feature(linked_list_retain)]
#![feature(linked_list_cursors)]
#![register_tool(component_access_control)] #![register_tool(component_access_control)]
use core::sync::atomic::Ordering; use core::sync::atomic::Ordering;