ticktimer: improve condvar handling

Improve condvar so that it is more reliable.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2023-01-29 18:04:06 +08:00
parent bd530f7a74
commit e7983aef87

View file

@ -787,7 +787,13 @@ fn main() -> ! {
} }
api::Opcode::WaitForCondition => { api::Opcode::WaitForCondition => {
let pid = msg.sender.pid(); let pid = msg.sender.pid();
if let Some(scalar) = msg.body.scalar_message_mut() { let Some(scalar) = msg.body.scalar_message_mut() else {
info!(
"sender made WaitForCondition request that wasn't a BlockingScalar Message"
);
continue;
};
let condvar = scalar.arg1; let condvar = scalar.arg1;
let timeout = scalar.arg2; let timeout = scalar.arg2;
@ -815,6 +821,14 @@ fn main() -> ! {
} }
} }
// Add this to the list of entries waiting for a response.
notify_hash
.entry(pid)
.or_default()
.entry(condvar)
.or_default()
.push_back(msg.sender);
// If there's a `timeout` argument, schedule a response. // If there's a `timeout` argument, schedule a response.
if timeout != 0 { if timeout != 0 {
recalculate_sleep( recalculate_sleep(
@ -829,33 +843,27 @@ fn main() -> ! {
) )
} }
// Add this to the list of entries waiting for a response.
notify_hash
.entry(pid)
.or_default()
.entry(condvar)
.or_default()
.push_back(msg.sender);
// log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar)); // log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar));
// The message will be responded to as part of the notification hash // The message will be responded to as part of the notification hash
// when the condvar is unlocked. Forget the contents of the message // when the condvar is unlocked. Forget the contents of the message
// in order to prevent it from being responded to early. // in order to prevent it from being responded to early.
core::mem::forget(msg_opt.take()); core::mem::forget(msg_opt.take());
} else {
info!(
"sender made WaitForCondition request that wasn't a BlockingScalar Message"
);
}
} }
api::Opcode::NotifyCondition => { api::Opcode::NotifyCondition => {
let pid = msg.sender.pid(); let pid = msg.sender.pid();
if msg.body.is_blocking() { let Some(scalar) = msg.body.scalar_message() else {
info!("sender made NotifyCondition request that was blocking"); info!(
"sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message"
);
continue; continue;
};
if msg.body.is_blocking() {
return_type = 0;
} }
if let Some(scalar) = msg.body.scalar_message() {
let condvar = scalar.arg1; let condvar = scalar.arg1;
log::trace!( log::trace!(
@ -875,11 +883,32 @@ fn main() -> ! {
let requested_count = scalar.arg2; let requested_count = scalar.arg2;
let available_count = core::cmp::min(requested_count, awaiting.len()); let available_count = core::cmp::min(requested_count, awaiting.len());
if requested_count > awaiting.len() {
log::trace!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len());
}
if requested_count == 0 {
log::trace!("requested to wake no entries! weird, huh?");
}
stop_sleep(&mut ticktimer, &mut sleep_heap); stop_sleep(&mut ticktimer, &mut sleep_heap);
for entry in awaiting.drain(..available_count) { for entry in awaiting.drain(..available_count) {
// Remove each entry in the timeout set // Remove each entry in the timeout set
sleep_heap.retain(|_, v| if v.sender == entry { false } else { true }); sleep_heap.retain(|_, v| if v.sender == entry { false } else { true });
xous::return_scalar(entry, 0).expect("couldn't send response");
// If there's an error waking up the sender, deal with it
match xous::return_scalar(entry, 0) {
Ok(_) => {}
Err(xous::Error::ProcessNotFound) => {
log::error!(
"process {} exited -- removing remaining entries for PID {:?}/condvar {:08x}",
pid.map(|v| v.get()).unwrap_or_default(), pid, condvar
);
mutex_hash.remove(&pid);
}
Err(xous::Error::DoubleFree) => {
panic!("tried to wake up a thread twice");
}
Err(e) => panic!("unexpected error responding to scalar: {:?}", e),
}
} }
// If there are leftover requested, add them to the list of // If there are leftover requested, add them to the list of
@ -900,11 +929,6 @@ fn main() -> ! {
// Resume sleeping, which re-enables interrupts and queues the // Resume sleeping, which re-enables interrupts and queues the
// next timer event to fire. // next timer event to fire.
start_sleep(&mut ticktimer, &mut sleep_heap); start_sleep(&mut ticktimer, &mut sleep_heap);
} else {
info!(
"sender made WaitForCondition request that wasn't a BlockingScalar Message"
);
}
} }
api::Opcode::InvalidCall => { api::Opcode::InvalidCall => {
error!("couldn't convert opcode"); error!("couldn't convert opcode");