mirror of
https://github.com/asterinas/asterinas.git
synced 2025-06-25 02:13:24 +00:00
[Semaphore] Optimize the update_pending_ops
This commit is contained in:
committed by
Tate, Hongliang Tian
parent
a8970daec2
commit
3297976700
@ -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 pending_alters = self.pending_alters.lock();
|
let mut value = *val;
|
||||||
pending_alters.retain_mut(|op| {
|
if value > 0 {
|
||||||
if *val == 0 {
|
let mut pending_alters = self.pending_alters.lock();
|
||||||
return true;
|
let mut cursor = pending_alters.cursor_front_mut();
|
||||||
}
|
while let Some(op) = cursor.current() {
|
||||||
// Check if the process alive.
|
if value == 0 {
|
||||||
if op.process.upgrade().is_none() {
|
break;
|
||||||
return false;
|
}
|
||||||
}
|
// Check if the process alive.
|
||||||
debug_assert!(op.sem_buf.sem_op < 0);
|
if op.process.upgrade().is_none() {
|
||||||
|
cursor.remove_current().unwrap();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if op.sem_buf.sem_op.abs() as i32 <= *val {
|
debug_assert!(op.sem_buf.sem_op < 0);
|
||||||
trace!(
|
|
||||||
"Found removable pending op, op: {:?}, pid:{:?}",
|
|
||||||
op.sem_buf.sem_op,
|
|
||||||
op.pid
|
|
||||||
);
|
|
||||||
|
|
||||||
*val += i32::from(op.sem_buf.sem_op);
|
if op.sem_buf.sem_op.abs() as i32 <= value {
|
||||||
self.latest_modified_pid.store(op.pid, Ordering::Relaxed);
|
trace!(
|
||||||
self.update_otime();
|
"Found removable pending op, op: {:?}, pid:{:?}",
|
||||||
op.status.set_status(Status::Normal);
|
op.sem_buf.sem_op,
|
||||||
op.waker.wake_up();
|
op.pid
|
||||||
false
|
);
|
||||||
} else {
|
|
||||||
true
|
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();
|
||||||
|
cursor.remove_current().unwrap();
|
||||||
|
} else {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Reference in New Issue
Block a user