mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +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 {
|
pub struct XousTickTimer {
|
||||||
csr: utralib::CSR<u32>,
|
csr: utralib::CSR<u32>,
|
||||||
current_response: Option<TimerRequest>,
|
current_response: Option<TimerRequest>,
|
||||||
|
last_response: Option<TimerRequest>,
|
||||||
connection: xous::CID,
|
connection: xous::CID,
|
||||||
ticktimer_sr_manager: RegManager<{ utra::ticktimer::TICKTIMER_NUMREGS }>,
|
ticktimer_sr_manager: RegManager<{ utra::ticktimer::TICKTIMER_NUMREGS }>,
|
||||||
wdt_sr_manager: RegManager<{ utra::wdt::WDT_NUMREGS }>,
|
wdt_sr_manager: RegManager<{ utra::wdt::WDT_NUMREGS }>,
|
||||||
|
|
@ -105,6 +106,9 @@ mod implementation {
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
|
// Save the response so we can be sure we don't double-return messages.
|
||||||
|
xtt.last_response = Some(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XousTickTimer {
|
impl XousTickTimer {
|
||||||
|
|
@ -131,6 +135,7 @@ mod implementation {
|
||||||
let mut xtt = XousTickTimer {
|
let mut xtt = XousTickTimer {
|
||||||
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
csr: CSR::new(csr.as_mut_ptr() as *mut u32),
|
||||||
current_response: None,
|
current_response: None,
|
||||||
|
last_response: None,
|
||||||
connection,
|
connection,
|
||||||
ticktimer_sr_manager,
|
ticktimer_sr_manager,
|
||||||
wdt_sr_manager,
|
wdt_sr_manager,
|
||||||
|
|
@ -164,6 +169,14 @@ mod implementation {
|
||||||
xtt
|
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) {
|
pub fn reset(&mut self) {
|
||||||
self.csr.wfo(utra::ticktimer::CONTROL_RESET, 0b1);
|
self.csr.wfo(utra::ticktimer::CONTROL_RESET, 0b1);
|
||||||
self.csr.wo(utra::ticktimer::CONTROL, 0); // not paused, not reset -> free-run
|
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) {
|
pub fn reset(&mut self) {
|
||||||
self.start = std::time::Instant::now();
|
self.start = std::time::Instant::now();
|
||||||
}
|
}
|
||||||
|
|
@ -494,6 +513,8 @@ fn recalculate_sleep(
|
||||||
) {
|
) {
|
||||||
stop_sleep(ticktimer, sleep_heap);
|
stop_sleep(ticktimer, sleep_heap);
|
||||||
|
|
||||||
|
ticktimer.clear_last_response();
|
||||||
|
|
||||||
// If we have a new sleep request, add it to the heap.
|
// If we have a new sleep request, add it to the heap.
|
||||||
if let Some(mut request) = new {
|
if let Some(mut request) = new {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
|
|
@ -621,6 +642,7 @@ fn main() -> ! {
|
||||||
return_type = 2;
|
return_type = 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::SleepMs => {
|
api::Opcode::SleepMs => {
|
||||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||||
let ms = scalar.arg1 as i64;
|
let ms = scalar.arg1 as i64;
|
||||||
|
|
@ -654,6 +676,7 @@ fn main() -> ! {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::RecalculateSleep => {
|
api::Opcode::RecalculateSleep => {
|
||||||
if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32
|
if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32
|
||||||
!= xous::process::id()
|
!= xous::process::id()
|
||||||
|
|
@ -674,7 +697,7 @@ fn main() -> ! {
|
||||||
|
|
||||||
// If we're being asked to recalculate due to a timeout expiring, drop the sent
|
// If we're being asked to recalculate due to a timeout expiring, drop the sent
|
||||||
// message from the `entries` list.
|
// 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 mut expired_values = 0;
|
||||||
let awaiting = notify_hash
|
let awaiting = notify_hash
|
||||||
.entry(sender_pid)
|
.entry(sender_pid)
|
||||||
|
|
@ -710,6 +733,7 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
recalculate_sleep(&mut ticktimer, &mut sleep_heap, None);
|
recalculate_sleep(&mut ticktimer, &mut sleep_heap, None);
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, token, _, _, _, {
|
||||||
ticktimer.suspend();
|
ticktimer.suspend();
|
||||||
susres
|
susres
|
||||||
|
|
@ -717,9 +741,11 @@ fn main() -> ! {
|
||||||
.expect("couldn't execute suspend/resume");
|
.expect("couldn't execute suspend/resume");
|
||||||
ticktimer.resume();
|
ticktimer.resume();
|
||||||
}),
|
}),
|
||||||
|
|
||||||
api::Opcode::PingWdt => {
|
api::Opcode::PingWdt => {
|
||||||
ticktimer.reset_wdt();
|
ticktimer.reset_wdt();
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::GetVersion => {
|
api::Opcode::GetVersion => {
|
||||||
let mut buf = unsafe {
|
let mut buf = unsafe {
|
||||||
xous_ipc::Buffer::from_memory_message_mut(
|
xous_ipc::Buffer::from_memory_message_mut(
|
||||||
|
|
@ -736,63 +762,76 @@ fn main() -> ! {
|
||||||
buf.replace(v).unwrap();
|
buf.replace(v).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::LockMutex => {
|
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();
|
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
|
let ready = mutex_ready_hash.entry(pid).or_default();
|
||||||
if ready.remove(&scalar.arg1) {
|
|
||||||
scalar.id = 0;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// This item is not in the Ready list, so add our sender to the list of processes
|
// If this item is in the Ready list, return right away without blocking
|
||||||
// to get called when UnlockMutex is invoked
|
if ready.remove(&scalar.arg1) {
|
||||||
let awaiting = mutex_hash.entry(pid).or_default();
|
scalar.id = 0;
|
||||||
|
continue;
|
||||||
// Add this to the end of the list of entries to call so that when `UnlockMutex` is sent
|
|
||||||
// the message will get a response.
|
|
||||||
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
|
|
||||||
mutex_entry.push_back(msg.sender);
|
|
||||||
|
|
||||||
// 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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This item is not in the Ready list, so add our sender to the list of processes
|
||||||
|
// to get called when UnlockMutex is invoked
|
||||||
|
let awaiting = mutex_hash.entry(pid).or_default();
|
||||||
|
|
||||||
|
// Add this to the end of the list of entries to call so that when `UnlockMutex` is sent
|
||||||
|
// the message will get a response.
|
||||||
|
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
|
||||||
|
mutex_entry.push_back(msg.sender);
|
||||||
|
|
||||||
|
// 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());
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::UnlockMutex => {
|
api::Opcode::UnlockMutex => {
|
||||||
let pid = msg.sender.pid();
|
|
||||||
if msg.body.is_blocking() {
|
if msg.body.is_blocking() {
|
||||||
info!("sender made UnlockMutex request that was blocking");
|
log::error!("sender made UnlockMutex request that was blocking");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(scalar) = msg.body.scalar_message() {
|
let Some(scalar) = msg.body.scalar_message() else {
|
||||||
// Get a list of awaiting mutexes for this process
|
log::error!("made a call to UnlockMutex with a non-scalar message");
|
||||||
let awaiting = mutex_hash.entry(pid).or_default();
|
continue;
|
||||||
|
};
|
||||||
|
|
||||||
// Get the vector of awaiting mutex entries.
|
let pid = msg.sender.pid();
|
||||||
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
|
|
||||||
|
|
||||||
// If there's something waiting in the queue, respond to that message
|
// Get a list of awaiting mutexes for this process
|
||||||
if let Some(sender) = mutex_entry.pop_front() {
|
let awaiting = mutex_hash.entry(pid).or_default();
|
||||||
xous::return_scalar(sender, 0).unwrap();
|
|
||||||
} else {
|
// Get the vector of awaiting mutex entries.
|
||||||
// Otherwise, mark this scalar as being ready to run
|
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
|
||||||
mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1);
|
|
||||||
}
|
// If there's something waiting in the queue, respond to that message
|
||||||
|
if let Some(sender) = mutex_entry.pop_front() {
|
||||||
|
xous::return_scalar(sender, 0).unwrap();
|
||||||
|
} else {
|
||||||
|
// Otherwise, mark this scalar as being ready to run
|
||||||
|
mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::FreeMutex => {
|
api::Opcode::FreeMutex => {
|
||||||
let Some(scalar) = msg.body.scalar_message() else {
|
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;
|
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
|
// Remove all instances of this mutex from the mutex hash
|
||||||
mutex_hash
|
mutex_hash
|
||||||
.entry(msg.sender.pid())
|
.entry(msg.sender.pid())
|
||||||
|
|
@ -803,7 +842,7 @@ fn main() -> ! {
|
||||||
api::Opcode::WaitForCondition => {
|
api::Opcode::WaitForCondition => {
|
||||||
let pid = msg.sender.pid();
|
let pid = msg.sender.pid();
|
||||||
let Some(scalar) = msg.body.scalar_message_mut() else {
|
let Some(scalar) = msg.body.scalar_message_mut() else {
|
||||||
info!(
|
log::trace!(
|
||||||
"sender made WaitForCondition request that wasn't a BlockingScalar Message"
|
"sender made WaitForCondition request that wasn't a BlockingScalar Message"
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -869,7 +908,7 @@ fn main() -> ! {
|
||||||
api::Opcode::NotifyCondition => {
|
api::Opcode::NotifyCondition => {
|
||||||
let pid = msg.sender.pid();
|
let pid = msg.sender.pid();
|
||||||
let Some(scalar) = msg.body.scalar_message() else {
|
let Some(scalar) = msg.body.scalar_message() else {
|
||||||
info!(
|
log::error!(
|
||||||
"sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message"
|
"sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message"
|
||||||
);
|
);
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -897,17 +936,34 @@ fn main() -> ! {
|
||||||
// Wake threads, ensuring we don't run off the end.
|
// Wake threads, ensuring we don't run off the end.
|
||||||
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());
|
||||||
|
let remaining_count = awaiting.len() - available_count;
|
||||||
|
|
||||||
if requested_count > awaiting.len() {
|
if remaining_count > 0 {
|
||||||
log::trace!("requested to wake {} entries, which is more than the current {} waiting entries", requested_count, awaiting.len());
|
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 {
|
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);
|
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| 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
|
// If there's an error waking up the sender, deal with it
|
||||||
match xous::return_scalar(entry, 0) {
|
match xous::return_scalar(entry, 0) {
|
||||||
|
|
@ -967,16 +1023,49 @@ fn main() -> ! {
|
||||||
.entry(condvar)
|
.entry(condvar)
|
||||||
.or_default();
|
.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
|
// Free all entries in the sleep heap that are waiting for this condition
|
||||||
stop_sleep(&mut ticktimer, &mut sleep_heap);
|
stop_sleep(&mut ticktimer, &mut sleep_heap);
|
||||||
for entry in awaiting.drain(..) {
|
for entry in awaiting.drain(..) {
|
||||||
// Remove each entry in the timeout set
|
// Remove each entry in the timeout set
|
||||||
sleep_heap.retain(|_, v| v.sender != entry);
|
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
|
// Remove all instances of this condvar
|
||||||
notify_hash.entry(pid).or_default().remove(&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
|
// Resume sleeping
|
||||||
start_sleep(&mut ticktimer, &mut sleep_heap);
|
start_sleep(&mut ticktimer, &mut sleep_heap);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue