mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
xous-ticktimer: overhaul condvars
The previous condvar implementation was unsound, and didn't do the right thing. Overhaul condvars so that they now pass the libstd tests. As part of this, function calls were added to free the mutex and condvar objects that may have been allocated. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
2cb44cdfc2
commit
5185b2ee07
1 changed files with 132 additions and 43 deletions
|
|
@ -72,6 +72,7 @@ mod implementation {
|
|||
pub struct XousTickTimer {
|
||||
csr: utralib::CSR<u32>,
|
||||
current_response: Option<TimerRequest>,
|
||||
last_response: Option<TimerRequest>,
|
||||
connection: xous::CID,
|
||||
ticktimer_sr_manager: RegManager<{ utra::ticktimer::TICKTIMER_NUMREGS }>,
|
||||
wdt_sr_manager: RegManager<{ utra::wdt::WDT_NUMREGS }>,
|
||||
|
|
@ -105,6 +106,9 @@ mod implementation {
|
|||
}),
|
||||
)
|
||||
.ok();
|
||||
|
||||
// Save the response so we can be sure we don't double-return messages.
|
||||
xtt.last_response = Some(response);
|
||||
}
|
||||
|
||||
impl XousTickTimer {
|
||||
|
|
@ -131,6 +135,7 @@ mod implementation {
|
|||
let mut xtt = XousTickTimer {
|
||||
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
||||
current_response: None,
|
||||
last_response: None,
|
||||
connection,
|
||||
ticktimer_sr_manager,
|
||||
wdt_sr_manager,
|
||||
|
|
@ -164,6 +169,14 @@ mod implementation {
|
|||
xtt
|
||||
}
|
||||
|
||||
pub fn last_response(&self) -> &Option<TimerRequest> {
|
||||
&self.last_response
|
||||
}
|
||||
|
||||
pub fn clear_last_response(&mut self) {
|
||||
self.last_response = None;
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.csr.wfo(utra::ticktimer::CONTROL_RESET, 0b1);
|
||||
self.csr.wo(utra::ticktimer::CONTROL, 0); // not paused, not reset -> free-run
|
||||
|
|
@ -393,6 +406,12 @@ mod implementation {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn last_response(&self) -> &Option<TimerRequest> {
|
||||
&None
|
||||
}
|
||||
|
||||
pub fn clear_last_response(&mut self) {}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.start = std::time::Instant::now();
|
||||
}
|
||||
|
|
@ -494,6 +513,8 @@ fn recalculate_sleep(
|
|||
) {
|
||||
stop_sleep(ticktimer, sleep_heap);
|
||||
|
||||
ticktimer.clear_last_response();
|
||||
|
||||
// If we have a new sleep request, add it to the heap.
|
||||
if let Some(mut request) = new {
|
||||
#[cfg(feature = "debug-print")]
|
||||
|
|
@ -621,6 +642,7 @@ fn main() -> ! {
|
|||
return_type = 2;
|
||||
}
|
||||
}
|
||||
|
||||
api::Opcode::SleepMs => {
|
||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||
let ms = scalar.arg1 as i64;
|
||||
|
|
@ -654,6 +676,7 @@ fn main() -> ! {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
api::Opcode::RecalculateSleep => {
|
||||
if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32
|
||||
!= xous::process::id()
|
||||
|
|
@ -674,7 +697,7 @@ fn main() -> ! {
|
|||
|
||||
// If we're being asked to recalculate due to a timeout expiring, drop the sent
|
||||
// message from the `entries` list.
|
||||
if (request_kind == RequestKind::Timeout as usize) && (sender > 0) {
|
||||
if (request_kind == RequestKind::Timeout as usize) && (sender != 0) {
|
||||
let mut expired_values = 0;
|
||||
let awaiting = notify_hash
|
||||
.entry(sender_pid)
|
||||
|
|
@ -710,6 +733,7 @@ fn main() -> ! {
|
|||
}
|
||||
recalculate_sleep(&mut ticktimer, &mut sleep_heap, None);
|
||||
}
|
||||
|
||||
api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
||||
ticktimer.suspend();
|
||||
susres
|
||||
|
|
@ -717,9 +741,11 @@ fn main() -> ! {
|
|||
.expect("couldn't execute suspend/resume");
|
||||
ticktimer.resume();
|
||||
}),
|
||||
|
||||
api::Opcode::PingWdt => {
|
||||
ticktimer.reset_wdt();
|
||||
}
|
||||
|
||||
api::Opcode::GetVersion => {
|
||||
let mut buf = unsafe {
|
||||
xous_ipc::Buffer::from_memory_message_mut(
|
||||
|
|
@ -736,9 +762,15 @@ fn main() -> ! {
|
|||
buf.replace(v).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
api::Opcode::LockMutex => {
|
||||
let Some(scalar) = msg.body.scalar_message_mut() else {
|
||||
log::error!("sender made LockMutex request that was not blocking");
|
||||
continue;
|
||||
};
|
||||
|
||||
let pid = msg.sender.pid();
|
||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||
|
||||
let ready = mutex_ready_hash.entry(pid).or_default();
|
||||
|
||||
// If this item is in the Ready list, return right away without blocking
|
||||
|
|
@ -759,18 +791,20 @@ fn main() -> ! {
|
|||
// We've saved the `msg.sender` above and will return the lock as part of the mutex
|
||||
// work thread. Forget the contents of `msg_opt` without running its destructor.
|
||||
core::mem::forget(msg_opt.take());
|
||||
} else {
|
||||
info!("sender made LockMutex request that was not blocking");
|
||||
}
|
||||
}
|
||||
|
||||
api::Opcode::UnlockMutex => {
|
||||
let pid = msg.sender.pid();
|
||||
if msg.body.is_blocking() {
|
||||
info!("sender made UnlockMutex request that was blocking");
|
||||
log::error!("sender made UnlockMutex request that was blocking");
|
||||
continue;
|
||||
}
|
||||
if let Some(scalar) = msg.body.scalar_message() {
|
||||
let Some(scalar) = msg.body.scalar_message() else {
|
||||
log::error!("made a call to UnlockMutex with a non-scalar message");
|
||||
continue;
|
||||
};
|
||||
|
||||
let pid = msg.sender.pid();
|
||||
|
||||
// Get a list of awaiting mutexes for this process
|
||||
let awaiting = mutex_hash.entry(pid).or_default();
|
||||
|
||||
|
|
@ -785,14 +819,19 @@ fn main() -> ! {
|
|||
mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
api::Opcode::FreeMutex => {
|
||||
let Some(scalar) = msg.body.scalar_message() else {
|
||||
info!("sender tried to free a mutex using a non-scalar message");
|
||||
log::error!("sender tried to free a mutex using a non-scalar message");
|
||||
continue;
|
||||
};
|
||||
|
||||
// log::info!(
|
||||
// "PID {}: freeing mutex {}",
|
||||
// msg.sender.pid().map(|v| v.get()).unwrap_or_default(),
|
||||
// scalar.arg1
|
||||
// );
|
||||
|
||||
// Remove all instances of this mutex from the mutex hash
|
||||
mutex_hash
|
||||
.entry(msg.sender.pid())
|
||||
|
|
@ -803,7 +842,7 @@ fn main() -> ! {
|
|||
api::Opcode::WaitForCondition => {
|
||||
let pid = msg.sender.pid();
|
||||
let Some(scalar) = msg.body.scalar_message_mut() else {
|
||||
info!(
|
||||
log::trace!(
|
||||
"sender made WaitForCondition request that wasn't a BlockingScalar Message"
|
||||
);
|
||||
continue;
|
||||
|
|
@ -869,7 +908,7 @@ fn main() -> ! {
|
|||
api::Opcode::NotifyCondition => {
|
||||
let pid = msg.sender.pid();
|
||||
let Some(scalar) = msg.body.scalar_message() else {
|
||||
info!(
|
||||
log::error!(
|
||||
"sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message"
|
||||
);
|
||||
continue;
|
||||
|
|
@ -897,17 +936,34 @@ fn main() -> ! {
|
|||
// Wake threads, ensuring we don't run off the end.
|
||||
let requested_count = scalar.arg2;
|
||||
let available_count = core::cmp::min(requested_count, awaiting.len());
|
||||
let remaining_count = awaiting.len() - available_count;
|
||||
|
||||
if requested_count > awaiting.len() {
|
||||
log::trace!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len());
|
||||
if remaining_count > 0 {
|
||||
log::error!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len());
|
||||
if let Some(entry) = ticktimer.last_response() {
|
||||
log::error!("but there is a last_response present");
|
||||
if entry.data == condvar {
|
||||
log::error!("...which matches the condvar that was being unlocked");
|
||||
}
|
||||
}
|
||||
}
|
||||
if requested_count == 0 {
|
||||
log::trace!("requested to wake no entries! weird, huh?");
|
||||
log::error!("requested to wake no entries!");
|
||||
}
|
||||
|
||||
stop_sleep(&mut ticktimer, &mut sleep_heap);
|
||||
for entry in awaiting.drain(..available_count) {
|
||||
// Remove each entry in the timeout set
|
||||
sleep_heap.retain(|_, v| if v.sender == entry { false } else { true });
|
||||
sleep_heap.retain(|_, v| v.sender != entry);
|
||||
|
||||
if let Some(last_response) = ticktimer.last_response() {
|
||||
if last_response.sender == entry {
|
||||
log::error!(
|
||||
"got a request to wake a condvar ({:08x}) that we just responded to with entry {:08x} -- suspect there's a thread waiting in the server queue to clean it up",
|
||||
condvar, entry.to_usize());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If there's an error waking up the sender, deal with it
|
||||
match xous::return_scalar(entry, 0) {
|
||||
|
|
@ -967,16 +1023,49 @@ fn main() -> ! {
|
|||
.entry(condvar)
|
||||
.or_default();
|
||||
|
||||
if !awaiting.is_empty() {
|
||||
log::info!(
|
||||
"PID {}: freeing condvar {:08x} -- {} entries waiting",
|
||||
pid.map(|v| v.get()).unwrap_or_default(),
|
||||
condvar,
|
||||
awaiting.len()
|
||||
);
|
||||
}
|
||||
|
||||
// Free all entries in the sleep heap that are waiting for this condition
|
||||
stop_sleep(&mut ticktimer, &mut sleep_heap);
|
||||
for entry in awaiting.drain(..) {
|
||||
// Remove each entry in the timeout set
|
||||
sleep_heap.retain(|_, v| v.sender != entry);
|
||||
|
||||
if let Some(last_response) = ticktimer.last_response() {
|
||||
if last_response.sender == entry {
|
||||
log::error!(
|
||||
"got a request to destroy a condvar ({:08x}) that we just responded to with entry {:08x} -- suspect there's a thread waiting in the server queue to clean it up",
|
||||
condvar, entry.to_usize());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// If there's an error waking up the sender, deal with it
|
||||
xous::return_scalar(entry, 0).ok();
|
||||
}
|
||||
|
||||
// Remove all instances of this condvar
|
||||
notify_hash.entry(pid).or_default().remove(&condvar);
|
||||
|
||||
let immediate_notifications = immedaite_notifications
|
||||
.entry(pid)
|
||||
.or_default()
|
||||
.remove(&condvar)
|
||||
.unwrap_or_default();
|
||||
if immediate_notifications != 0 {
|
||||
log::error!(
|
||||
"there were {} threads that were notified without first waiting",
|
||||
immediate_notifications
|
||||
);
|
||||
}
|
||||
|
||||
// Resume sleeping
|
||||
start_sleep(&mut ticktimer, &mut sleep_heap);
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue