[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
// Step one:
let mut value = *val;
if value > 0 {
let mut pending_alters = self.pending_alters.lock();
pending_alters.retain_mut(|op| {
if *val == 0 {
return true;
let mut cursor = pending_alters.cursor_front_mut();
while let Some(op) = cursor.current() {
if value == 0 {
break;
}
// Check if the process alive.
if op.process.upgrade().is_none() {
return false;
cursor.remove_current().unwrap();
continue;
}
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!(
"Found removable pending op, op: {:?}, pid:{:?}",
op.sem_buf.sem_op,
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.update_otime();
op.status.set_status(Status::Normal);
op.waker.wake_up();
false
cursor.remove_current().unwrap();
} else {
true
cursor.move_next();
}
}
}
});
// Step two:
if *val == 0 {
if value == 0 {
let mut pending_const = self.pending_const.lock();
pending_const.iter().for_each(|op| {
op.status.set_status(Status::Normal);
@ -299,7 +305,8 @@ impl Semaphore {
});
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_upcasting)]
#![feature(linked_list_retain)]
#![feature(linked_list_cursors)]
#![register_tool(component_access_control)]
use core::sync::atomic::Ordering;