From c96514d36585ef85abc96e7fb0cbb7894f276340 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Mon, 9 Oct 2023 17:41:46 +0800 Subject: [PATCH] xous-ticktimer: rewrite mutex and condvar implementations This rewrites the Mutex and Condvar backends after gaining a better understanding of how they're supposed to work. Additionally, the Ticktimer backend implementation has changed: * There is now a `recalculate_sleep_offline()` call that skips starting and stopping the ticktimer. This is an unsafe function because the user must manually manage the ticktimer state. It is used to perform multiple operations while the ticktimer is unscheduled. * Ticktimers now deal in `TimerRequest` types rather than raw `i64` types. This is a newtype that is distinct from the previous `i64`, but should behave largely the same. * Waking a Condvar now reports the number of items that were awoken, provided the requester sends a blocking scalar value. This allows for backwards compatibility. * Attempting to wake 0 Condvars now wakes all pending Condvars. * We no longer keep track of lock-then-unlock for Mutexes, nor notify-then-wait for Condvars. This makes those cases undefined behavior, which puts us in line with Posix. Don't unlock mutexes that haven't been locked, or notify Condvar recipients when nobody is waiting. Signed-off-by: Sean Cross --- services/xous-ticktimer/src/main.rs | 362 +++++++++--------- .../src/platform/hosted/implementation.rs | 83 ++-- services/xous-ticktimer/src/platform/mod.rs | 83 +++- .../src/platform/precursor/implementation.rs | 154 +++++--- 4 files changed, 398 insertions(+), 284 deletions(-) diff --git a/services/xous-ticktimer/src/main.rs b/services/xous-ticktimer/src/main.rs index f49045aff..bda6b6a48 100644 --- a/services/xous-ticktimer/src/main.rs +++ b/services/xous-ticktimer/src/main.rs @@ -14,7 +14,7 @@ mod platform; use platform::implementation::*; use platform::*; -#[cfg(not(any(target_arch = "arm", feature="cramium-soc", feature="cramium-fpga")))] +#[cfg(not(any(target_arch = "arm", feature = "cramium-soc", feature = "cramium-fpga")))] use susres::SuspendOrder; fn main() -> ! { @@ -52,15 +52,15 @@ fn main() -> ! { ticktimer.reset(); // reset the time to 0 // register a suspend/resume listener - #[cfg(not(any(target_arch = "arm", feature="cramium-soc", feature="cramium-fpga")))] + #[cfg(not(any(target_arch = "arm", feature = "cramium-soc", feature = "cramium-fpga")))] let xns = xous_names::XousNames::new().unwrap(); - #[cfg(not(any(target_arch = "arm", feature="cramium-soc", feature="cramium-fpga")))] + #[cfg(not(any(target_arch = "arm", feature = "cramium-soc", feature = "cramium-fpga")))] let sr_cid = xous::connect(ticktimer_server).expect("couldn't create suspend callback connection"); - #[cfg(not(any(target_arch = "arm", feature="cramium-soc", feature="cramium-fpga")))] - let mut susres = susres::Susres::new( + #[cfg(not(any(target_arch = "arm", feature = "cramium-soc", feature = "cramium-fpga")))] + let mut susres = susres::Susres::new( Some(SuspendOrder::Last), &xns, api::Opcode::SuspendResume as u32, @@ -75,27 +75,15 @@ fn main() -> ! { // A list of message IDs that are waiting to receive a Notification. This queue is drained // by threads sending `NotifyCondition` to us, or by a condvar timing out. - let mut notify_hash: HashMap, HashMap>> = - HashMap::new(); + let mut notify_table: HashMap< + Option, + HashMap>, + > = HashMap::new(); - // There is a small chance that a client sends a `notify_one()` or `notify_all()` before - // the other threads have a chance to recover. This is due to a non-threadsafe use of - // Mutex within the standard library. Keep track of any excess `notify_one()` or - // `notify_all()` messages for when the Mutex is successfully locked. - let mut immedaite_notifications: HashMap, HashMap> = - HashMap::new(); - - // // A list of timeouts for a given condvar. This list serves two purposes: - // // 1. When a Notification is sent to a condvar, it is removed from this hash (based on the Message ID) - // // 2. When the timer event hits, a response is sent to this condvar - // // Therefore, this must be indexable by two keys: Message ID and - // let mut timeout_heap: std::collections::VecDeque = VecDeque::new(); - - // A list of mutexes that should be allowed to run immediately, because the - // thread they were waiting on has already unlocked the mutex. This occurs - // when a thread attempts to Lock a Mutex and fails, then gets preempted - // before it has a chance to send the `LockMutex` message to us. - let mut mutex_ready_hash: HashMap, HashSet> = HashMap::new(); + // Keep track of which notifications have timeouts. This is used to determine which + // notifications have already been responded to by the interrupt, as well as to + // improve performance when recalculating timeouts. + let mut notifications_with_timeouts: HashSet = HashSet::new(); // A list of message IDs that are waiting to lock a Mutex. These are processes // that have attempted to lock a Mutex and failed, and have sent us the `LockMutex` @@ -114,10 +102,10 @@ fn main() -> ! { xous::reply_and_receive_next_legacy(ticktimer_server, &mut msg_opt, &mut return_type) .unwrap(); let msg = msg_opt.as_mut().unwrap(); - log::trace!("msg: {:x?}", msg); - match num_traits::FromPrimitive::from_usize(msg.body.id()) - .unwrap_or(api::Opcode::InvalidCall) - { + let opcode = num_traits::FromPrimitive::from_usize(msg.body.id()) + .unwrap_or(api::Opcode::InvalidCall); + log::trace!("msg {:?}: {:x?}", opcode, msg); + match opcode { api::Opcode::ElapsedMs => { if let Some(scalar) = msg.body.scalar_message_mut() { let time = ticktimer.elapsed_ms() as i64; @@ -154,7 +142,7 @@ fn main() -> ! { ticktimer.recalculate_sleep( &mut sleep_heap, Some(TimerRequest { - msec: ms, + msec: ms.into(), sender, kind: RequestKind::Sleep, data: 0, @@ -167,62 +155,55 @@ fn main() -> ! { if msg.sender.pid().map(|p| p.get()).unwrap_or_default() as u32 != xous::process::id() { - ticktimer.recalculate_sleep(&mut sleep_heap, None); + log::error!("got a RecalculateSleep message from a process other than the ticktimer server"); continue; } let Some(args) = msg.body.scalar_message() else { - continue + continue; }; // If this is a Timeout message that fired, remove it from the Notification list - let sender = args.arg1; - let request_kind = args.arg2; + let sender_id = args.arg1; + let sender = xous::MessageSender::from_usize(sender_id); let condvar = args.arg3; - let sender_pid = xous::MessageSender::from_usize(sender).pid(); + ticktimer.stop_sleep(&mut sleep_heap); + if notifications_with_timeouts.remove(&sender_id) { + // Check to make sure this isn't in the sleep heap. It shouldn't be, + // since the timer just fired. + let len_before = sleep_heap.len(); + sleep_heap.retain(|_, v| v.sender != sender); + let len_after = sleep_heap.len(); + assert!(len_before == len_after); - // 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) { - let mut expired_values = 0; - let awaiting = notify_hash - .entry(sender_pid) + // It should, however, still be in the notify hash. Remove it. + let awaiting = notify_table + .entry(sender.pid()) .or_default() .entry(condvar) .or_default(); + let len_before = awaiting.len(); + awaiting.retain(|v| *v != sender); + let len_after = awaiting.len(); + assert!(len_before != len_after); - awaiting.retain(|e| { - if e.to_usize() == sender { - expired_values += 1; - false - } else { - true - } - }); - - // Remove each entry in the timeout set - for (_msecs, timer_request) in sleep_heap.iter() { - if timer_request.sender.to_usize() == sender { - log::error!("we were just notified of PID {:?}/condvar {:08x} expiring, yet it's still in the sleep heap with sender {:08x}", sender_pid, condvar, timer_request.sender.to_usize()); - } - } - - // log::info!( - // "removed {} entries for PID {:?}/condvar {:08x}, {} to start, {} remain", - // expired_values, - // sender_pid, - // condvar, - // len_before, - // awaiting.len(), - // ); - // log::trace!("new entries for PID {:?}/condvar {:08x}: {:?}", sender_pid, condvar, notify_hash.get(&sender_pid).unwrap().get(&condvar)); + // As a final check, make sure the last notification is from us. + assert!(ticktimer.last_response() == sender); } - ticktimer.recalculate_sleep(&mut sleep_heap, None); + + // Recalculate sleep with the newly-adjusted hash and re-enable + // the sleep interrupt. + unsafe { ticktimer.recalculate_sleep_offline(&mut sleep_heap, None) }; + ticktimer.start_sleep(&mut sleep_heap); } api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, _token, _, _, _, { ticktimer.suspend(); - #[cfg(not(any(target_arch = "arm", feature="cramium-soc", feature="cramium-fpga")))] + #[cfg(not(any( + target_arch = "arm", + feature = "cramium-soc", + feature = "cramium-fpga" + )))] susres .suspend_until_resume(_token) .expect("couldn't execute suspend/resume"); @@ -230,7 +211,7 @@ fn main() -> ! { }), api::Opcode::PingWdt => { - #[cfg(feature="watchdog")] + #[cfg(feature = "watchdog")] ticktimer.reset_wdt(); } @@ -259,22 +240,16 @@ fn main() -> ! { let pid = msg.sender.pid(); - let ready = mutex_ready_hash.entry(pid).or_default(); - - // If this item is in the Ready list, return right away without blocking - 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 // 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); + awaiting + .entry(scalar.arg1) + .or_default() + .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. @@ -294,17 +269,24 @@ fn main() -> ! { let pid = msg.sender.pid(); // Get a list of awaiting mutexes for this process - let awaiting = mutex_hash.entry(pid).or_default(); + let awaiting = mutex_hash + .entry(pid) + .or_default() + .entry(scalar.arg1) + .or_default(); // Get the vector of awaiting mutex entries. - let mutex_entry = awaiting.entry(scalar.arg1).or_default(); + let mutex_entry = awaiting; // 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); + log::warn!( + "Process {} Attempted to unlock a mutex {:08x} that has not yet been locked", + pid.map(|v| v.get()).unwrap_or_default(), + scalar.arg1 + ); } } @@ -314,17 +296,16 @@ fn main() -> ! { 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 + if let Some(responders) = mutex_hash .entry(msg.sender.pid()) .or_default() - .remove(&scalar.arg1); + .remove(&scalar.arg1) + { + if responders.len() != 0 { + log::error!("When freeing mutex {:08x}, there were {} mutexes awaiting to be unlocked", scalar.arg1, responders.len()); + } + } } api::Opcode::WaitForCondition => { @@ -346,42 +327,40 @@ fn main() -> ! { timeout ); - // If there's a condition waiting already, decrement the total list - // and return immediately. - if let Some(excess) = immedaite_notifications - .entry(pid) - .or_default() - .get_mut(&condvar) - { - if *excess > 0 { - *excess -= 1; - scalar.id = 0; + // If there's a `timeout` argument, schedule a response. Also add the response + // to a list of notifications that have timeouts, in order to speed up + // recalculations in the future and to avoid sending duplicate responses. + if timeout != 0 { + ticktimer.stop_sleep(&mut sleep_heap); + notifications_with_timeouts.insert(msg.sender.to_usize()); - // Rust libstd expects a `Scalar1` return type for this call - return_type = 1; - continue; + // Safety: The ticktimer must be stopped, and we have done that above. + unsafe { + ticktimer.recalculate_sleep_offline( + &mut sleep_heap, + Some(TimerRequest { + msec: timeout.into(), + sender: msg.sender, + kind: RequestKind::Timeout, + data: condvar, + }), + ); } } - // Add this to the list of entries waiting for a response. - notify_hash + // Add this sender to the global notification table. This table is used + // when notifying senders, which will unblock them and wake them up. + notify_table .entry(pid) .or_default() .entry(condvar) .or_default() .push_back(msg.sender); - // If there's a `timeout` argument, schedule a response. + // If there is a timeout, then the ticktimer was paused above. Resume + // the ticktimer now that everything is settled. if timeout != 0 { - ticktimer.recalculate_sleep( - &mut sleep_heap, - Some(TimerRequest { - msec: timeout as i64, - sender: msg.sender, - kind: RequestKind::Timeout, - data: condvar, - }), - ) + ticktimer.start_sleep(&mut sleep_heap); } // log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar)); @@ -395,66 +374,77 @@ fn main() -> ! { api::Opcode::NotifyCondition => { let pid = msg.sender.pid(); let Some(scalar) = msg.body.scalar_message() else { - log::error!( - "sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message" - ); + log::error!("sender made NotifyCondition request that wasn't Scalar"); continue; }; - if msg.body.is_blocking() { - return_type = 0; - } - let condvar = scalar.arg1; + let mut requested_count: usize = scalar.arg2; - log::trace!( - "sender in pid {:?} is notifying {} entries for condition {:08x}", - pid, - scalar.arg2, - condvar, - ); - - let awaiting = notify_hash + let awaiting = notify_table .entry(pid) .or_default() .entry(condvar) .or_default(); - // 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 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"); - } - } - } + // As a special case, if 0 conditions are requested to wake, notify all + // waiting conditions. if requested_count == 0 { - log::error!("requested to wake no entries!"); + requested_count = awaiting.len(); } - ticktimer.stop_sleep(&mut sleep_heap); - for entry in awaiting.drain(..available_count) { - // Remove each entry in the timeout set - sleep_heap.retain(|_, v| v.sender != entry); + // Wake threads, ensuring we don't run off the end. + let count_to_notify = core::cmp::min(requested_count, awaiting.len()); - 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()); + log::trace!( + "sender in pid {:?} is notifying {} entries for condition {:08x} -- there are {} entries waiting, so we'll wake up {}", + pid, + requested_count, + condvar, + awaiting.len(), + count_to_notify, + ); + + // Stop the ticktimer interrupt, in case the condvar that we're waking up + // had a timeout associated with it. + ticktimer.stop_sleep(&mut sleep_heap); + + let mut notified_count = 0; + for entry in awaiting.drain(..count_to_notify) { + // If this entry had a timeout associated with it, see if it needs to + // be removed from the sleep heap. + if notifications_with_timeouts.remove(&entry.to_usize()) { + // Remove the entries from the sleep heap. There will either be no entries + // removed, in which case we're waiting for a `RecalculateSleep` message + // to be processed, or there will be exactly one entry missing. + + let len_before = sleep_heap.len(); + sleep_heap.retain(|_, v| v.sender != entry); + let len_after = sleep_heap.len(); + assert!((len_before == len_after) || (len_before == len_after + 1)); + + // If the lengths are not equal, then our entry was in the sleep heap. + // Presumably, it was responded to in the interrim, we just haven't + // recalculated the sleep heap since then. + if len_before != len_after { + // assert!(ticktimer.last_response() != entry); + // We removed an item from the sleep heap. This means that the entry + // didn't time out, and we should respond to it here. + } else { + // assert!(ticktimer.last_response() == entry); + // If no entry was removed, then the entry wasn't in the sleep + // heap because it was queued to fire, and has already fired. + // Don't respond to it here a second time. continue; } } - // If there's an error waking up the sender, deal with it + // Wake up the process that was waiting on this entry. Be sure to send + // a value of `0`, which indicates success. match xous::return_scalar(entry, 0) { - Ok(_) => {} + Ok(_) => { + notified_count += 1; + } Err(xous::Error::ProcessNotFound) => { log::error!( "process {} exited -- removing remaining entries for PID {:?}/condvar {:08x}", @@ -469,24 +459,15 @@ fn main() -> ! { } } - // If there are leftover requested, add them to the list of - // notofications that will be responded to immediately. - if available_count - requested_count > 0 { - #[cfg(feature = "debug-print")] - log::trace!( - "Adding {} spare sleep requests to immediate_notifications list", - available_count - requested_count - ); - *immedaite_notifications - .entry(pid) - .or_default() - .entry(condvar) - .or_default() += available_count - requested_count; - } - // Resume sleeping, which re-enables interrupts and queues the // next timer event to fire. ticktimer.start_sleep(&mut sleep_heap); + + // Return the number of conditions that were notified + if let Some(return_value) = msg.body.scalar_message_mut() { + return_value.arg1 = notified_count; + } + return_type = 1; // Return a Scalar1 } api::Opcode::FreeCondition => { @@ -504,7 +485,7 @@ fn main() -> ! { let condvar = scalar.arg1; - let awaiting = notify_hash + let awaiting = notify_table .entry(pid) .or_default() .entry(condvar) @@ -521,12 +502,23 @@ fn main() -> ! { // Free all entries in the sleep heap that are waiting for this condition ticktimer.stop_sleep(&mut sleep_heap); + let mut altered_sleep_heap = false; for entry in awaiting.drain(..) { // Remove each entry in the timeout set - sleep_heap.retain(|_, v| v.sender != entry); + if notifications_with_timeouts.remove(&entry.to_usize()) { + let len_before = sleep_heap.len(); + sleep_heap.retain(|_, v| v.sender != entry); + let len_after = sleep_heap.len(); + assert!((len_before == len_after) || (len_before == len_after + 1)); - if let Some(last_response) = ticktimer.last_response() { - if last_response.sender == entry { + // If the lengths are not equal, then our entry was in the sleep heap. + // Presumably, it was responded to in the interrim, we just haven't + // recalculated the sleep heap since then. + if len_before != len_after { + assert!(ticktimer.last_response() != entry); + altered_sleep_heap = true; + } else { + assert!(ticktimer.last_response() == 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()); @@ -534,23 +526,17 @@ fn main() -> ! { } } - // If there's an error waking up the sender, deal with it + // Wake up any responders that are waiting on the condvar xous::return_scalar(entry, 0).ok(); } // Remove all instances of this condvar - notify_hash.entry(pid).or_default().remove(&condvar); + notify_table.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 - ); + // If we removed entries from the sleep heap, recalculate it before resuming + // sleep. + if altered_sleep_heap { + unsafe { ticktimer.recalculate_sleep_offline(&mut sleep_heap, None) }; } // Resume sleeping diff --git a/services/xous-ticktimer/src/platform/hosted/implementation.rs b/services/xous-ticktimer/src/platform/hosted/implementation.rs index 661eb3746..2e04702c9 100644 --- a/services/xous-ticktimer/src/platform/hosted/implementation.rs +++ b/services/xous-ticktimer/src/platform/hosted/implementation.rs @@ -1,10 +1,16 @@ use crate::RequestKind; -use crate::TimerRequest; use crate::TimeoutExpiry; +use crate::TimerRequest; use num_traits::ToPrimitive; -use std::convert::TryInto; use std::collections::BTreeMap; +use std::convert::TryInto; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::{Duration, Instant}; +use xous::definitions::MessageSender; + +/// The Message ID of the last message we responded to +static LAST_RESPONDER: AtomicUsize = AtomicUsize::new(0); #[derive(Debug)] enum SleepComms { @@ -26,17 +32,19 @@ impl XousTickTimer { let (sleep_sender, sleep_receiver) = std::sync::mpsc::channel(); let (time_remaining_sender, time_remaining_receiver) = std::sync::mpsc::channel(); xous::create_thread(move || { + use std::sync::mpsc::RecvTimeoutError; let mut timeout = None; let mut current_response: Option = None; loop { + log::trace!("waiting for{} for an event", timeout.map(|d: Duration| format!(" {} ms", d.as_millis())).unwrap_or("ever".to_string())); let result = match timeout { None => sleep_receiver .recv() - .map_err(|_| std::sync::mpsc::RecvTimeoutError::Disconnected), + .map_err(|_| RecvTimeoutError::Disconnected), Some(s) => sleep_receiver.recv_timeout(s), }; match result { - Err(std::sync::mpsc::RecvTimeoutError::Timeout) => { + Err(RecvTimeoutError::Timeout) => { let response = current_response.take().unwrap(); #[cfg(feature = "debug-print")] log::info!("Returning scalar to {}", response.sender); @@ -54,10 +62,11 @@ impl XousTickTimer { arg4: 0, }), ) - .unwrap(); + .unwrap(); + LAST_RESPONDER.store(response.sender.to_usize(), Ordering::Relaxed); timeout = None; } - Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => { + Err(RecvTimeoutError::Disconnected) => { return; } Ok(SleepComms::InterruptSleep) => { @@ -69,25 +78,25 @@ impl XousTickTimer { if duration > 0 { #[cfg(feature = "debug-print")] log::info!( - "Starting sleep for {} ms, returning to {}", - duration, - new_sender - ); + "Starting sleep for {} ms, returning to {}", + duration, + new_sender + ); } else { #[cfg(feature = "debug-print")] log::info!( - "Clamping duration to 0 (was: {})m returning to {}", - duration, - new_sender - ); + "Clamping duration to 0 (was: {})m returning to {}", + duration, + new_sender + ); duration = 0; } - timeout = Some(std::time::Duration::from_millis( + timeout = Some(Duration::from_millis( duration.try_into().unwrap(), )); current_response = Some(TimerRequest { sender: new_sender, - msec: expiry, + msec: expiry.into(), kind: RequestKind::Sleep, data: 0, }); @@ -95,20 +104,20 @@ impl XousTickTimer { } } }) - .unwrap(); + .unwrap(); XousTickTimer { - start: std::time::Instant::now(), + start: Instant::now(), time_remaining_receiver, sleep_comms: sleep_sender, } } - pub fn last_response(&self) -> &Option { - &None + /// Used for sanity-checking to make sure we're not double-responding + pub fn last_response(&self) -> MessageSender { + MessageSender::from_usize(LAST_RESPONDER.load(Ordering::Relaxed)) } - pub fn clear_last_response(&mut self) {} pub fn reset(&mut self) { self.start = std::time::Instant::now(); } @@ -125,15 +134,15 @@ impl XousTickTimer { pub fn schedule_response(&mut self, request: TimerRequest) { #[cfg(feature = "debug-print")] log::info!( - "request.msec: {} self.elapsed_ms: {} returning to: {}", - request.msec, - self.elapsed_ms(), - request.sender - ); + "request.msec: {} self.elapsed_ms: {} returning to: {}", + request.msec, + self.elapsed_ms(), + request.sender + ); self.sleep_comms .send(SleepComms::StartSleep( request.sender, - request.msec as i64, + request.msec.to_i64(), self.elapsed_ms(), )) .unwrap(); @@ -157,7 +166,7 @@ impl XousTickTimer { /// Disable the sleep interrupt and remove the currently-pending sleep item. /// If the sleep item has fired, then there will be no existing sleep item /// remaining. - pub fn stop_sleep( + pub(crate) fn stop_sleep( &mut self, sleep_heap: &mut BTreeMap, // min-heap with Reverse ) { @@ -172,7 +181,7 @@ impl XousTickTimer { } } - pub fn start_sleep( + pub(crate) fn start_sleep( &mut self, sleep_heap: &mut BTreeMap, // min-heap with Reverse ) { @@ -181,7 +190,9 @@ impl XousTickTimer { #[cfg(feature = "debug-print")] log::info!( "scheduling a response at {} to {} (heap: {:?})", - next_response.msec, next_response.sender, sleep_heap + next_response.msec, + next_response.sender, + sleep_heap ); self.schedule_response(next_response); @@ -200,14 +211,12 @@ impl XousTickTimer { /// /// Note that interrupts are always enabled, which is why we must stop the timer prior to /// reordering the list. - pub fn recalculate_sleep( + pub(crate) unsafe fn recalculate_sleep_offline( &mut self, sleep_heap: &mut BTreeMap, // min-heap with Reverse new: Option, ) { - self.stop_sleep(sleep_heap); log::trace!("Elapsed: {}", self.elapsed_ms()); - self.clear_last_response(); // If we have a new sleep request, add it to the heap. if let Some(mut request) = new { @@ -227,7 +236,15 @@ impl XousTickTimer { #[cfg(feature = "debug-print")] log::info!("No new sleep request"); } + } + pub(crate) fn recalculate_sleep( + &mut self, + sleep_heap: &mut BTreeMap, // min-heap with Reverse + new: Option, + ) { + self.stop_sleep(sleep_heap); + unsafe { self.recalculate_sleep_offline(sleep_heap, new) } self.start_sleep(sleep_heap); } } diff --git a/services/xous-ticktimer/src/platform/mod.rs b/services/xous-ticktimer/src/platform/mod.rs index e8fe7a460..c44d4ee01 100644 --- a/services/xous-ticktimer/src/platform/mod.rs +++ b/services/xous-ticktimer/src/platform/mod.rs @@ -1,31 +1,92 @@ -#[cfg(any(feature="precursor", feature="renode"))] +#[cfg(any(feature = "precursor", feature = "renode"))] #[macro_use] pub mod precursor; -#[cfg(any(feature="precursor", feature="renode"))] +use core::ops::{Add, AddAssign}; + +#[cfg(any(feature = "precursor", feature = "renode"))] pub use precursor::*; -#[cfg(any(not(target_os = "xous"), - not(any(feature="precursor", feature="renode", feature="atsama5d27", feature="cramium-fpga", feature="cramium-soc", not(target_os = "xous"))) +#[cfg(any( + not(target_os = "xous"), + not(any( + feature = "precursor", + feature = "renode", + feature = "atsama5d27", + feature = "cramium-fpga", + feature = "cramium-soc", + not(target_os = "xous") + )) ))] pub mod hosted; -#[cfg(any(not(target_os = "xous"), - not(any(feature="precursor", feature="renode", feature="atsama5d27", feature="cramium-fpga", feature="cramium-soc", not(target_os = "xous"))) +#[cfg(any( + not(target_os = "xous"), + not(any( + feature = "precursor", + feature = "renode", + feature = "atsama5d27", + feature = "cramium-fpga", + feature = "cramium-soc", + not(target_os = "xous") + )) ))] pub use hosted::*; -#[cfg(any(feature="atsama5d27"))] +#[cfg(any(feature = "atsama5d27"))] #[macro_use] pub mod atsama5d2; -#[cfg(any(feature="atsama5d27"))] +#[cfg(any(feature = "atsama5d27"))] pub use atsama5d2::*; -#[cfg(any(feature="cramium-fpga", feature="cramium-soc"))] +#[cfg(any(feature = "cramium-fpga", feature = "cramium-soc"))] #[macro_use] pub mod cramium; -#[cfg(any(feature="cramium-fpga", feature="cramium-soc"))] +#[cfg(any(feature = "cramium-fpga", feature = "cramium-soc"))] pub use cramium::*; -pub(crate) type TimeoutExpiry = i64; +#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] +pub(crate) struct TimeoutExpiry(i64); +impl TimeoutExpiry { + pub fn to_i64(&self) -> i64 { + self.0 + } +} + +impl Add for TimeoutExpiry { + type Output = TimeoutExpiry; + fn add(self, rhs: i64) -> Self::Output { + TimeoutExpiry(self.0 + rhs) + } +} + +impl AddAssign for TimeoutExpiry { + fn add_assign(&mut self, rhs: i64) { + self.0 += rhs; + } +} + +impl core::fmt::Display for TimeoutExpiry { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}ms", self.0) + } +} + +impl core::fmt::Debug for TimeoutExpiry { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "{}ms", self.0) + } +} + +impl From for TimeoutExpiry { + fn from(item: i64) -> Self { + TimeoutExpiry(item) + } +} + +impl From for TimeoutExpiry { + fn from(item: usize) -> Self { + TimeoutExpiry(item as i64) + } +} #[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)] pub enum RequestKind { diff --git a/services/xous-ticktimer/src/platform/precursor/implementation.rs b/services/xous-ticktimer/src/platform/precursor/implementation.rs index fe16ea2e0..807fe5c25 100644 --- a/services/xous-ticktimer/src/platform/precursor/implementation.rs +++ b/services/xous-ticktimer/src/platform/precursor/implementation.rs @@ -1,29 +1,47 @@ const TICKS_PER_MS: u64 = 1; +use core::sync::atomic::{AtomicU32, AtomicUsize, Ordering}; use std::collections::BTreeMap; -use crate::TimerRequest; use crate::TimeoutExpiry; +use crate::TimerRequest; use susres::{RegManager, RegOrField, SuspendResume}; use utralib::generated::*; +use xous::definitions::MessageSender; + +/// Add some slack to ensure that when we set the timeout, it +/// doesn't end up in the past due to processing latencies. +const LATENCY_SLACK_MS: i64 = 3; + pub struct XousTickTimer { csr: utralib::CSR, current_response: Option, - last_response: Option, - connection: xous::CID, ticktimer_sr_manager: RegManager<{ utra::ticktimer::TICKTIMER_NUMREGS }>, wdt_sr_manager: RegManager<{ utra::wdt::WDT_NUMREGS }>, wdt: utralib::CSR, } +/// Connection ID to the ticktimer server, for use in interrupt contexts. +static TICKTIMER_CONNECTION: AtomicU32 = AtomicU32::new(0); + +/// The Message ID of the last message we responded to +static LAST_RESPONDER: AtomicUsize = AtomicUsize::new(0); + +/// This is incremented every time the ticktimer IRQ fires. It's used to +/// determine how many events were missed during processing of new +/// ticktimer events. +static TICKTIMER_SEQUENCE_NUMBER: AtomicUsize = AtomicUsize::new(0); + fn handle_irq(_irq_no: usize, arg: *mut usize) { - let xtt = unsafe { &mut *(arg as *mut XousTickTimer) }; + let xtt: &mut XousTickTimer = unsafe { &mut *(arg as *mut XousTickTimer) }; + let connection = TICKTIMER_CONNECTION.load(Ordering::Relaxed); // println!("In IRQ, connection: {}", xtt.connection); // Safe because we're in an interrupt, and this interrupt is only - // enabled when this value is not None. + // enabled when this value is not None. Furthermore, the value is + // only ever updated when interrupts are disabled. let response = xtt.current_response.take().unwrap(); xous::return_scalar(response.sender, response.kind as usize).ok(); @@ -35,7 +53,7 @@ fn handle_irq(_irq_no: usize, arg: *mut usize) { // Which is fine, because the queue is always recalculated any time a message arrives. use num_traits::ToPrimitive; xous::try_send_message( - xtt.connection, + connection, xous::Message::Scalar(xous::ScalarMessage { id: crate::api::Opcode::RecalculateSleep.to_usize().unwrap(), arg1: response.sender.to_usize(), @@ -44,10 +62,14 @@ fn handle_irq(_irq_no: usize, arg: *mut usize) { arg4: 0, }), ) - .ok(); + .ok(); - // Save the response so we can be sure we don't double-return messages. - xtt.last_response = Some(response); + // Remember what the last message was that we responded to. This will prevent + // double-responding to messages. + LAST_RESPONDER.store(response.sender.to_usize(), Ordering::Relaxed); + + // Note that we've handled another IRQ event. + TICKTIMER_SEQUENCE_NUMBER.fetch_add(1, Ordering::Relaxed); } impl XousTickTimer { @@ -59,14 +81,14 @@ impl XousTickTimer { 4096, xous::MemoryFlags::R | xous::MemoryFlags::W, ) - .expect("couldn't map Tick Timer CSR range"); + .expect("couldn't map Tick Timer CSR range"); let wdt = xous::syscall::map_memory( xous::MemoryAddress::new(utra::wdt::HW_WDT_BASE), None, 4096, xous::MemoryFlags::R | xous::MemoryFlags::W, ) - .expect("couldn't map Watchdog timer CSR range"); + .expect("couldn't map Watchdog timer CSR range"); let ticktimer_sr_manager = RegManager::new(csr.as_mut_ptr() as *mut u32); let wdt_sr_manager = RegManager::new(wdt.as_mut_ptr() as *mut u32); @@ -74,8 +96,6 @@ impl XousTickTimer { 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, wdt: CSR::new(wdt.as_mut_ptr() as *mut u32), @@ -89,23 +109,33 @@ impl XousTickTimer { // xtt.wdt_sr_manager.push(RegOrField::Field(utra::wdt::WATCHDOG_ENABLE), None); } + // Keep a copy of the connection around so we can use it in the interrupt. + TICKTIMER_CONNECTION.store(connection.into(), Ordering::Relaxed); + xtt } - pub fn last_response(&self) -> &Option { - &self.last_response + pub fn last_response(&self) -> MessageSender { + MessageSender::from_usize(LAST_RESPONDER.load(Ordering::Relaxed)) } - pub fn clear_last_response(&mut self) { - self.last_response = None; + /// Return the number of times the ticktimer IRQ has fired. + /// + /// # Safety + /// + /// This function is safe to call only if interrupts are disabled. Otherwise, + /// the value may change during processing. + pub unsafe fn sequence_number() -> usize { + TICKTIMER_SEQUENCE_NUMBER.load(Ordering::Relaxed) } + pub fn init(&mut self) { xous::claim_interrupt( utra::ticktimer::TICKTIMER_IRQ, handle_irq, self as *mut XousTickTimer as *mut usize, ) - .expect("couldn't claim irq"); + .expect("couldn't claim irq"); self.ticktimer_sr_manager .push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET0), None); @@ -116,6 +146,7 @@ impl XousTickTimer { self.ticktimer_sr_manager .push(RegOrField::Reg(utra::ticktimer::EV_ENABLE), 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 @@ -143,10 +174,10 @@ impl XousTickTimer { #[cfg(feature = "debug-print")] { log::info!( - "Stopping currently-running timer sr.msec: {} elapsed_ms: {}", - sr.msec, - self.elapsed_ms() - ); + "Stopping currently-running timer sr.msec: {} elapsed_ms: {}", + sr.msec, + self.elapsed_ms() + ); } Some(sr) } else { @@ -155,12 +186,12 @@ impl XousTickTimer { } pub fn schedule_response(&mut self, request: TimerRequest) { - let irq_target = request.msec; + let irq_target = request.msec.to_i64().wrapping_add(LATENCY_SLACK_MS); log::trace!( - "setting a response at {} ms (current time: {} ms)", - irq_target, - self.elapsed_ms() - ); + "setting a response at {} ms (current time: {} ms)", + irq_target, + self.elapsed_ms() + ); // Disable the timer interrupt assert!(self.csr.rf(utra::ticktimer::EV_ENABLE_ALARM) == 0); @@ -192,10 +223,10 @@ impl XousTickTimer { let state = self.wdt.r(utra::wdt::STATE); if state & self.wdt.ms(utra::wdt::STATE_DISARMED, 1) == 0 { log::info!( - "{} WDT is not disarmed, state: 0x{:x}", - self.elapsed_ms(), - state - ); + "{} WDT is not disarmed, state: 0x{:x}", + self.elapsed_ms(), + state + ); } } @@ -209,6 +240,7 @@ impl XousTickTimer { // by writing this after suspend(), resume will get the prior value self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0); } + pub fn resume(&mut self) { // this is a write-once bit that's later erased, so it can't be managed automatically // thus we have to restore in manually on a resume @@ -224,24 +256,28 @@ impl XousTickTimer { self.ticktimer_sr_manager.resume(); log::trace!( - "ticktimer enable: {}", - self.csr.r(utra::ticktimer::EV_ENABLE) - ); + "ticktimer enable: {}", + self.csr.r(utra::ticktimer::EV_ENABLE) + ); log::trace!( - "ticktimer time/target: {}/{}", - self.csr.r(utra::ticktimer::TIME0), - self.csr.r(utra::ticktimer::MSLEEP_TARGET0) - ); + "ticktimer time/target: {}/{}", + self.csr.r(utra::ticktimer::TIME0), + self.csr.r(utra::ticktimer::MSLEEP_TARGET0) + ); } - /// Disable the sleep interrupt and remove the currently-pending sleep item. /// If the sleep item has fired, then there will be no existing sleep item /// remaining. - pub fn stop_sleep( + /// + /// # Returns + /// + /// The serial number of the ticktimer handler at the point where + /// interrupts were disabled. + pub(crate) fn stop_sleep( &mut self, sleep_heap: &mut BTreeMap, // min-heap with Reverse - ) { + ) -> usize { // If there's a sleep request ongoing now, grab it. if let Some(current) = self.stop_interrupt() { #[cfg(feature = "debug-print")] @@ -251,9 +287,10 @@ impl XousTickTimer { #[cfg(feature = "debug-print")] log::info!("There was no existing sleep() request"); } + TICKTIMER_SEQUENCE_NUMBER.load(Ordering::Relaxed) } - pub fn start_sleep( + pub(crate) fn start_sleep( &mut self, sleep_heap: &mut BTreeMap, // min-heap with Reverse ) { @@ -262,7 +299,9 @@ impl XousTickTimer { #[cfg(feature = "debug-print")] log::info!( "scheduling a response at {} to {} (heap: {:?})", - next_response.msec, next_response.sender, sleep_heap + next_response.msec, + next_response.sender, + sleep_heap ); self.schedule_response(next_response); @@ -275,20 +314,17 @@ impl XousTickTimer { } } - /// Recalculate the sleep timer, optionally adding a new Request to the list of available - /// sleep events. This involves stopping the timer, recalculating the newest item, then - /// restarting the timer. + /// Recalculate sleep without starting and stopping the sleep timer. /// - /// Note that interrupts are always enabled, which is why we must stop the timer prior to - /// reordering the list. - pub fn recalculate_sleep( + /// # Safety + /// + /// This must be called with the sleep timer already stopped + pub(crate) unsafe fn recalculate_sleep_offline( &mut self, - sleep_heap: &mut BTreeMap, // min-heap with Reverse + sleep_heap: &mut BTreeMap, new: Option, ) { - self.stop_sleep(sleep_heap); log::trace!("Elapsed: {}", self.elapsed_ms()); - self.clear_last_response(); // If we have a new sleep request, add it to the heap. if let Some(mut request) = new { @@ -308,7 +344,21 @@ impl XousTickTimer { #[cfg(feature = "debug-print")] log::info!("No new sleep request"); } + } + /// Recalculate the sleep timer, optionally adding a new Request to the list of available + /// sleep events. This involves stopping the timer, recalculating the newest item, then + /// restarting the timer. + /// + /// Note that interrupts are always enabled, which is why we must stop the timer prior to + /// reordering the list. + pub(crate) fn recalculate_sleep( + &mut self, + sleep_heap: &mut BTreeMap, + new: Option, + ) { + self.stop_sleep(sleep_heap); + unsafe { self.recalculate_sleep_offline(sleep_heap, new) } self.start_sleep(sleep_heap); } }