mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-21 09:14:34 +00:00
Merge pull request #425 from betrusted-io/ticktimer-refactor
Ticktimer refactor
This commit is contained in:
commit
0ec13e7d18
5 changed files with 458 additions and 293 deletions
|
|
@ -14,7 +14,7 @@ mod platform;
|
||||||
use platform::implementation::*;
|
use platform::implementation::*;
|
||||||
use platform::*;
|
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;
|
use susres::SuspendOrder;
|
||||||
|
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
|
|
@ -52,15 +52,15 @@ fn main() -> ! {
|
||||||
ticktimer.reset(); // reset the time to 0
|
ticktimer.reset(); // reset the time to 0
|
||||||
|
|
||||||
// register a suspend/resume listener
|
// 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();
|
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 =
|
let sr_cid =
|
||||||
xous::connect(ticktimer_server).expect("couldn't create suspend callback connection");
|
xous::connect(ticktimer_server).expect("couldn't create suspend callback connection");
|
||||||
|
|
||||||
#[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 mut susres = susres::Susres::new(
|
let mut susres = susres::Susres::new(
|
||||||
Some(SuspendOrder::Last),
|
Some(SuspendOrder::Last),
|
||||||
&xns,
|
&xns,
|
||||||
api::Opcode::SuspendResume as u32,
|
api::Opcode::SuspendResume as u32,
|
||||||
|
|
@ -73,29 +73,23 @@ fn main() -> ! {
|
||||||
// is currently at 900, the Request will be `1900`.
|
// is currently at 900, the Request will be `1900`.
|
||||||
let mut sleep_heap: BTreeMap<TimeoutExpiry, TimerRequest> = BTreeMap::new();
|
let mut sleep_heap: BTreeMap<TimeoutExpiry, TimerRequest> = BTreeMap::new();
|
||||||
|
|
||||||
// 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<Option<xous::PID>, HashMap<usize, VecDeque<xous::MessageSender>>> =
|
|
||||||
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<T> within the standard library. Keep track of any excess `notify_one()` or
|
|
||||||
// `notify_all()` messages for when the Mutex<T> is successfully locked.
|
|
||||||
let mut immedaite_notifications: HashMap<Option<xous::PID>, HashMap<usize, usize>> =
|
|
||||||
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<TimerRequest> = VecDeque::new();
|
|
||||||
|
|
||||||
// A list of mutexes that should be allowed to run immediately, because the
|
// 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
|
// 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
|
// 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.
|
// before it has a chance to send the `LockMutex` message to us.
|
||||||
let mut mutex_ready_hash: HashMap<Option<xous::PID>, HashSet<usize>> = HashMap::new();
|
let mut mutex_ready_table: HashMap<Option<xous::PID>, HashSet<usize>> = HashMap::new();
|
||||||
|
|
||||||
|
// 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_table: HashMap<
|
||||||
|
Option<xous::PID>,
|
||||||
|
HashMap<usize, VecDeque<xous::MessageSender>>,
|
||||||
|
> = 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<usize> = HashSet::new();
|
||||||
|
|
||||||
// A list of message IDs that are waiting to lock a Mutex. These are processes
|
// 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`
|
// that have attempted to lock a Mutex and failed, and have sent us the `LockMutex`
|
||||||
|
|
@ -114,10 +108,10 @@ fn main() -> ! {
|
||||||
xous::reply_and_receive_next_legacy(ticktimer_server, &mut msg_opt, &mut return_type)
|
xous::reply_and_receive_next_legacy(ticktimer_server, &mut msg_opt, &mut return_type)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
let msg = msg_opt.as_mut().unwrap();
|
let msg = msg_opt.as_mut().unwrap();
|
||||||
log::trace!("msg: {:x?}", msg);
|
let opcode = num_traits::FromPrimitive::from_usize(msg.body.id())
|
||||||
match num_traits::FromPrimitive::from_usize(msg.body.id())
|
.unwrap_or(api::Opcode::InvalidCall);
|
||||||
.unwrap_or(api::Opcode::InvalidCall)
|
log::trace!("msg {:?}: {:x?}", opcode, msg);
|
||||||
{
|
match opcode {
|
||||||
api::Opcode::ElapsedMs => {
|
api::Opcode::ElapsedMs => {
|
||||||
if let Some(scalar) = msg.body.scalar_message_mut() {
|
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||||
let time = ticktimer.elapsed_ms() as i64;
|
let time = ticktimer.elapsed_ms() as i64;
|
||||||
|
|
@ -154,7 +148,7 @@ fn main() -> ! {
|
||||||
ticktimer.recalculate_sleep(
|
ticktimer.recalculate_sleep(
|
||||||
&mut sleep_heap,
|
&mut sleep_heap,
|
||||||
Some(TimerRequest {
|
Some(TimerRequest {
|
||||||
msec: ms,
|
msec: ms.into(),
|
||||||
sender,
|
sender,
|
||||||
kind: RequestKind::Sleep,
|
kind: RequestKind::Sleep,
|
||||||
data: 0,
|
data: 0,
|
||||||
|
|
@ -167,62 +161,59 @@ fn main() -> ! {
|
||||||
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()
|
||||||
{
|
{
|
||||||
ticktimer.recalculate_sleep(&mut sleep_heap, None);
|
log::error!("got a RecalculateSleep message from a process other than the ticktimer server");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
let Some(args) = msg.body.scalar_message() else {
|
let Some(args) = msg.body.scalar_message() else {
|
||||||
continue
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// If this is a Timeout message that fired, remove it from the Notification list
|
// If this is a Timeout message that fired, remove it from the Notification list
|
||||||
let sender = args.arg1;
|
let sender_id = args.arg1;
|
||||||
let request_kind = args.arg2;
|
let sender = xous::MessageSender::from_usize(sender_id);
|
||||||
let condvar = args.arg3;
|
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
|
// It should, however, still be in the notify hash. Remove it.
|
||||||
// message from the `entries` list.
|
let awaiting = notify_table
|
||||||
if (request_kind == RequestKind::Timeout as usize) && (sender != 0) {
|
.entry(sender.pid())
|
||||||
let mut expired_values = 0;
|
|
||||||
let awaiting = notify_hash
|
|
||||||
.entry(sender_pid)
|
|
||||||
.or_default()
|
.or_default()
|
||||||
.entry(condvar)
|
.entry(condvar)
|
||||||
.or_default();
|
.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| {
|
// Note multiple events elapsing since the last recalculate. Theorized to be harmless.
|
||||||
if e.to_usize() == sender {
|
if ticktimer.last_response() != sender {
|
||||||
expired_values += 1;
|
log::warn!("Multiple events triggered before we could recalculate sleep: ticktimer.last_response() {:?} != sender {:?}; sleep_heap.len(): {}",
|
||||||
false
|
ticktimer.last_response(), sender, sleep_heap.len()
|
||||||
} 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));
|
|
||||||
}
|
}
|
||||||
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, _, _, _, {
|
api::Opcode::SuspendResume => xous::msg_scalar_unpack!(msg, _token, _, _, _, {
|
||||||
ticktimer.suspend();
|
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
|
susres
|
||||||
.suspend_until_resume(_token)
|
.suspend_until_resume(_token)
|
||||||
.expect("couldn't execute suspend/resume");
|
.expect("couldn't execute suspend/resume");
|
||||||
|
|
@ -230,7 +221,7 @@ fn main() -> ! {
|
||||||
}),
|
}),
|
||||||
|
|
||||||
api::Opcode::PingWdt => {
|
api::Opcode::PingWdt => {
|
||||||
#[cfg(feature="watchdog")]
|
#[cfg(feature = "watchdog")]
|
||||||
ticktimer.reset_wdt();
|
ticktimer.reset_wdt();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,23 +249,24 @@ fn main() -> ! {
|
||||||
};
|
};
|
||||||
|
|
||||||
let pid = msg.sender.pid();
|
let pid = msg.sender.pid();
|
||||||
|
let mutex_id = scalar.arg1;
|
||||||
|
|
||||||
let ready = mutex_ready_hash.entry(pid).or_default();
|
// If this item is in the Ready list, then someone unlocked this Mutex after the
|
||||||
|
// sender locked it but before they got a chance to send this message. Return
|
||||||
// If this item is in the Ready list, return right away without blocking
|
// immediately without blocking.
|
||||||
if ready.remove(&scalar.arg1) {
|
if mutex_ready_table.entry(pid).or_default().remove(&mutex_id) {
|
||||||
scalar.id = 0;
|
scalar.id = 0;
|
||||||
continue;
|
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
|
// Add this to the end of the list of entries to call so that when `UnlockMutex` is sent
|
||||||
// the message will get a response.
|
// the message will get a response.
|
||||||
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
|
mutex_hash
|
||||||
mutex_entry.push_back(msg.sender);
|
.entry(pid)
|
||||||
|
.or_default()
|
||||||
|
.entry(mutex_id)
|
||||||
|
.or_default()
|
||||||
|
.push_back(msg.sender);
|
||||||
|
|
||||||
// We've saved the `msg.sender` above and will return the lock as part of the mutex
|
// 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.
|
// work thread. Forget the contents of `msg_opt` without running its destructor.
|
||||||
|
|
@ -282,10 +274,10 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::UnlockMutex => {
|
api::Opcode::UnlockMutex => {
|
||||||
if msg.body.is_blocking() {
|
// if msg.body.is_blocking() {
|
||||||
log::error!("sender made UnlockMutex request that was blocking");
|
// log::error!("sender made UnlockMutex request that was blocking");
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
let Some(scalar) = msg.body.scalar_message() else {
|
let Some(scalar) = msg.body.scalar_message() else {
|
||||||
log::error!("made a call to UnlockMutex with a non-scalar message");
|
log::error!("made a call to UnlockMutex with a non-scalar message");
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -293,18 +285,34 @@ fn main() -> ! {
|
||||||
|
|
||||||
let pid = msg.sender.pid();
|
let pid = msg.sender.pid();
|
||||||
|
|
||||||
// Get a list of awaiting mutexes for this process
|
|
||||||
let awaiting = mutex_hash.entry(pid).or_default();
|
|
||||||
|
|
||||||
// Get the vector of awaiting mutex entries.
|
|
||||||
let mutex_entry = awaiting.entry(scalar.arg1).or_default();
|
|
||||||
|
|
||||||
// If there's something waiting in the queue, respond to that message
|
// If there's something waiting in the queue, respond to that message
|
||||||
if let Some(sender) = mutex_entry.pop_front() {
|
let mut returned = false;
|
||||||
|
if let Some(sender) = mutex_hash
|
||||||
|
.entry(pid)
|
||||||
|
.or_default()
|
||||||
|
.entry(scalar.arg1)
|
||||||
|
.or_default()
|
||||||
|
.pop_front()
|
||||||
|
{
|
||||||
xous::return_scalar(sender, 0).unwrap();
|
xous::return_scalar(sender, 0).unwrap();
|
||||||
|
returned = true;
|
||||||
} else {
|
} else {
|
||||||
// Otherwise, mark this scalar as being ready to run
|
log::warn!(
|
||||||
mutex_ready_hash.entry(pid).or_default().insert(scalar.arg1);
|
"Process {} Attempted to unlock a mutex {:08x} that has not yet been locked",
|
||||||
|
pid.map(|v| v.get()).unwrap_or_default(),
|
||||||
|
scalar.arg1
|
||||||
|
);
|
||||||
|
if !mutex_ready_table
|
||||||
|
.entry(pid)
|
||||||
|
.or_default()
|
||||||
|
.insert(scalar.arg1)
|
||||||
|
{
|
||||||
|
log::error!("attempted to ready mutex {:08x} for PID {:?}, but there was already one waiting", scalar.arg1, pid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(scalar) = msg.body.scalar_message_mut() {
|
||||||
|
scalar.id = if returned { 1 } else { 0 };
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -314,17 +322,25 @@ fn main() -> ! {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
// log::info!(
|
let pid = msg.sender.pid();
|
||||||
// "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
|
if let Some(responders) = mutex_hash.entry(pid).or_default().remove(&scalar.arg1) {
|
||||||
.entry(msg.sender.pid())
|
if responders.len() != 0 {
|
||||||
|
log::error!("When freeing mutex {:08x}, there were {} mutexes awaiting to be unlocked", scalar.arg1, responders.len());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if mutex_ready_table
|
||||||
|
.entry(pid)
|
||||||
.or_default()
|
.or_default()
|
||||||
.remove(&scalar.arg1);
|
.remove(&scalar.arg1)
|
||||||
|
{
|
||||||
|
log::error!(
|
||||||
|
"When freeing mutex {:08x}, there was one pending unlock request that hasn't been answered",
|
||||||
|
scalar.arg1,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::WaitForCondition => {
|
api::Opcode::WaitForCondition => {
|
||||||
|
|
@ -346,42 +362,40 @@ fn main() -> ! {
|
||||||
timeout
|
timeout
|
||||||
);
|
);
|
||||||
|
|
||||||
// If there's a condition waiting already, decrement the total list
|
// If there's a `timeout` argument, schedule a response. Also add the response
|
||||||
// and return immediately.
|
// to a list of notifications that have timeouts, in order to speed up
|
||||||
if let Some(excess) = immedaite_notifications
|
// recalculations in the future and to avoid sending duplicate responses.
|
||||||
.entry(pid)
|
if timeout != 0 {
|
||||||
.or_default()
|
ticktimer.stop_sleep(&mut sleep_heap);
|
||||||
.get_mut(&condvar)
|
notifications_with_timeouts.insert(msg.sender.to_usize());
|
||||||
{
|
|
||||||
if *excess > 0 {
|
|
||||||
*excess -= 1;
|
|
||||||
scalar.id = 0;
|
|
||||||
|
|
||||||
// Rust libstd expects a `Scalar1` return type for this call
|
// Safety: The ticktimer must be stopped, and we have done that above.
|
||||||
return_type = 1;
|
unsafe {
|
||||||
continue;
|
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.
|
// Add this sender to the global notification table. This table is used
|
||||||
notify_hash
|
// when notifying senders, which will unblock them and wake them up.
|
||||||
|
notify_table
|
||||||
.entry(pid)
|
.entry(pid)
|
||||||
.or_default()
|
.or_default()
|
||||||
.entry(condvar)
|
.entry(condvar)
|
||||||
.or_default()
|
.or_default()
|
||||||
.push_back(msg.sender);
|
.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 {
|
if timeout != 0 {
|
||||||
ticktimer.recalculate_sleep(
|
ticktimer.start_sleep(&mut sleep_heap);
|
||||||
&mut sleep_heap,
|
|
||||||
Some(TimerRequest {
|
|
||||||
msec: timeout as i64,
|
|
||||||
sender: msg.sender,
|
|
||||||
kind: RequestKind::Timeout,
|
|
||||||
data: condvar,
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar));
|
// log::trace!("New waiting senders: {:?}", notify_hash.get(&pid).unwrap().get(&condvar));
|
||||||
|
|
@ -395,66 +409,77 @@ 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 {
|
||||||
log::error!(
|
log::error!("sender made NotifyCondition request that wasn't Scalar");
|
||||||
"sender made WaitForCondition request that wasn't a Scalar or BlockingScalar Message"
|
|
||||||
);
|
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
if msg.body.is_blocking() {
|
|
||||||
return_type = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
let condvar = scalar.arg1;
|
let condvar = scalar.arg1;
|
||||||
|
let mut requested_count: usize = scalar.arg2;
|
||||||
|
|
||||||
log::trace!(
|
let awaiting = notify_table
|
||||||
"sender in pid {:?} is notifying {} entries for condition {:08x}",
|
|
||||||
pid,
|
|
||||||
scalar.arg2,
|
|
||||||
condvar,
|
|
||||||
);
|
|
||||||
|
|
||||||
let awaiting = notify_hash
|
|
||||||
.entry(pid)
|
.entry(pid)
|
||||||
.or_default()
|
.or_default()
|
||||||
.entry(condvar)
|
.entry(condvar)
|
||||||
.or_default();
|
.or_default();
|
||||||
|
|
||||||
// Wake threads, ensuring we don't run off the end.
|
// As a special case, if 0 conditions are requested to wake, notify all
|
||||||
let requested_count = scalar.arg2;
|
// waiting conditions.
|
||||||
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");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if requested_count == 0 {
|
if requested_count == 0 {
|
||||||
log::error!("requested to wake no entries!");
|
requested_count = awaiting.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
ticktimer.stop_sleep(&mut sleep_heap);
|
// Wake threads, ensuring we don't run off the end.
|
||||||
for entry in awaiting.drain(..available_count) {
|
let count_to_notify = core::cmp::min(requested_count, awaiting.len());
|
||||||
// Remove each entry in the timeout set
|
|
||||||
sleep_heap.retain(|_, v| v.sender != entry);
|
|
||||||
|
|
||||||
if let Some(last_response) = ticktimer.last_response() {
|
log::trace!(
|
||||||
if last_response.sender == entry {
|
"sender in pid {:?} is notifying {} entries for condition {:08x} -- there are {} entries waiting, so we'll wake up {}",
|
||||||
log::error!(
|
pid,
|
||||||
"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",
|
requested_count,
|
||||||
condvar, entry.to_usize());
|
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;
|
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) {
|
match xous::return_scalar(entry, 0) {
|
||||||
Ok(_) => {}
|
Ok(_) => {
|
||||||
|
notified_count += 1;
|
||||||
|
}
|
||||||
Err(xous::Error::ProcessNotFound) => {
|
Err(xous::Error::ProcessNotFound) => {
|
||||||
log::error!(
|
log::error!(
|
||||||
"process {} exited -- removing remaining entries for PID {:?}/condvar {:08x}",
|
"process {} exited -- removing remaining entries for PID {:?}/condvar {:08x}",
|
||||||
|
|
@ -469,24 +494,14 @@ 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
|
// Resume sleeping, which re-enables interrupts and queues the
|
||||||
// next timer event to fire.
|
// next timer event to fire.
|
||||||
ticktimer.start_sleep(&mut sleep_heap);
|
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.id = notified_count;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
api::Opcode::FreeCondition => {
|
api::Opcode::FreeCondition => {
|
||||||
|
|
@ -504,7 +519,7 @@ fn main() -> ! {
|
||||||
|
|
||||||
let condvar = scalar.arg1;
|
let condvar = scalar.arg1;
|
||||||
|
|
||||||
let awaiting = notify_hash
|
let awaiting = notify_table
|
||||||
.entry(pid)
|
.entry(pid)
|
||||||
.or_default()
|
.or_default()
|
||||||
.entry(condvar)
|
.entry(condvar)
|
||||||
|
|
@ -521,12 +536,23 @@ fn main() -> ! {
|
||||||
|
|
||||||
// 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
|
||||||
ticktimer.stop_sleep(&mut sleep_heap);
|
ticktimer.stop_sleep(&mut sleep_heap);
|
||||||
|
let mut altered_sleep_heap = false;
|
||||||
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);
|
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 the lengths are not equal, then our entry was in the sleep heap.
|
||||||
if last_response.sender == entry {
|
// 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!(
|
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",
|
"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());
|
condvar, entry.to_usize());
|
||||||
|
|
@ -534,23 +560,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();
|
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_table.entry(pid).or_default().remove(&condvar);
|
||||||
|
|
||||||
let immediate_notifications = immedaite_notifications
|
// If we removed entries from the sleep heap, recalculate it before resuming
|
||||||
.entry(pid)
|
// sleep.
|
||||||
.or_default()
|
if altered_sleep_heap {
|
||||||
.remove(&condvar)
|
unsafe { ticktimer.recalculate_sleep_offline(&mut sleep_heap, None) };
|
||||||
.unwrap_or_default();
|
|
||||||
if immediate_notifications != 0 {
|
|
||||||
log::error!(
|
|
||||||
"there were {} threads that were notified without first waiting",
|
|
||||||
immediate_notifications
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resume sleeping
|
// Resume sleeping
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,16 @@
|
||||||
use crate::RequestKind;
|
use crate::RequestKind;
|
||||||
use crate::TimerRequest;
|
|
||||||
use crate::TimeoutExpiry;
|
use crate::TimeoutExpiry;
|
||||||
|
use crate::TimerRequest;
|
||||||
|
|
||||||
use num_traits::ToPrimitive;
|
use num_traits::ToPrimitive;
|
||||||
use std::convert::TryInto;
|
|
||||||
use std::collections::BTreeMap;
|
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)]
|
#[derive(Debug)]
|
||||||
enum SleepComms {
|
enum SleepComms {
|
||||||
|
|
@ -26,17 +32,19 @@ impl XousTickTimer {
|
||||||
let (sleep_sender, sleep_receiver) = std::sync::mpsc::channel();
|
let (sleep_sender, sleep_receiver) = std::sync::mpsc::channel();
|
||||||
let (time_remaining_sender, time_remaining_receiver) = std::sync::mpsc::channel();
|
let (time_remaining_sender, time_remaining_receiver) = std::sync::mpsc::channel();
|
||||||
xous::create_thread(move || {
|
xous::create_thread(move || {
|
||||||
|
use std::sync::mpsc::RecvTimeoutError;
|
||||||
let mut timeout = None;
|
let mut timeout = None;
|
||||||
let mut current_response: Option<TimerRequest> = None;
|
let mut current_response: Option<TimerRequest> = None;
|
||||||
loop {
|
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 {
|
let result = match timeout {
|
||||||
None => sleep_receiver
|
None => sleep_receiver
|
||||||
.recv()
|
.recv()
|
||||||
.map_err(|_| std::sync::mpsc::RecvTimeoutError::Disconnected),
|
.map_err(|_| RecvTimeoutError::Disconnected),
|
||||||
Some(s) => sleep_receiver.recv_timeout(s),
|
Some(s) => sleep_receiver.recv_timeout(s),
|
||||||
};
|
};
|
||||||
match result {
|
match result {
|
||||||
Err(std::sync::mpsc::RecvTimeoutError::Timeout) => {
|
Err(RecvTimeoutError::Timeout) => {
|
||||||
let response = current_response.take().unwrap();
|
let response = current_response.take().unwrap();
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!("Returning scalar to {}", response.sender);
|
log::info!("Returning scalar to {}", response.sender);
|
||||||
|
|
@ -54,10 +62,11 @@ impl XousTickTimer {
|
||||||
arg4: 0,
|
arg4: 0,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
LAST_RESPONDER.store(response.sender.to_usize(), Ordering::Relaxed);
|
||||||
timeout = None;
|
timeout = None;
|
||||||
}
|
}
|
||||||
Err(std::sync::mpsc::RecvTimeoutError::Disconnected) => {
|
Err(RecvTimeoutError::Disconnected) => {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Ok(SleepComms::InterruptSleep) => {
|
Ok(SleepComms::InterruptSleep) => {
|
||||||
|
|
@ -69,25 +78,25 @@ impl XousTickTimer {
|
||||||
if duration > 0 {
|
if duration > 0 {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!(
|
log::info!(
|
||||||
"Starting sleep for {} ms, returning to {}",
|
"Starting sleep for {} ms, returning to {}",
|
||||||
duration,
|
duration,
|
||||||
new_sender
|
new_sender
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!(
|
log::info!(
|
||||||
"Clamping duration to 0 (was: {})m returning to {}",
|
"Clamping duration to 0 (was: {})m returning to {}",
|
||||||
duration,
|
duration,
|
||||||
new_sender
|
new_sender
|
||||||
);
|
);
|
||||||
duration = 0;
|
duration = 0;
|
||||||
}
|
}
|
||||||
timeout = Some(std::time::Duration::from_millis(
|
timeout = Some(Duration::from_millis(
|
||||||
duration.try_into().unwrap(),
|
duration.try_into().unwrap(),
|
||||||
));
|
));
|
||||||
current_response = Some(TimerRequest {
|
current_response = Some(TimerRequest {
|
||||||
sender: new_sender,
|
sender: new_sender,
|
||||||
msec: expiry,
|
msec: expiry.into(),
|
||||||
kind: RequestKind::Sleep,
|
kind: RequestKind::Sleep,
|
||||||
data: 0,
|
data: 0,
|
||||||
});
|
});
|
||||||
|
|
@ -95,20 +104,20 @@ impl XousTickTimer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
XousTickTimer {
|
XousTickTimer {
|
||||||
start: std::time::Instant::now(),
|
start: Instant::now(),
|
||||||
time_remaining_receiver,
|
time_remaining_receiver,
|
||||||
sleep_comms: sleep_sender,
|
sleep_comms: sleep_sender,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn last_response(&self) -> &Option<TimerRequest> {
|
/// Used for sanity-checking to make sure we're not double-responding
|
||||||
&None
|
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) {
|
pub fn reset(&mut self) {
|
||||||
self.start = std::time::Instant::now();
|
self.start = std::time::Instant::now();
|
||||||
}
|
}
|
||||||
|
|
@ -125,15 +134,15 @@ impl XousTickTimer {
|
||||||
pub fn schedule_response(&mut self, request: TimerRequest) {
|
pub fn schedule_response(&mut self, request: TimerRequest) {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!(
|
log::info!(
|
||||||
"request.msec: {} self.elapsed_ms: {} returning to: {}",
|
"request.msec: {} self.elapsed_ms: {} returning to: {}",
|
||||||
request.msec,
|
request.msec,
|
||||||
self.elapsed_ms(),
|
self.elapsed_ms(),
|
||||||
request.sender
|
request.sender
|
||||||
);
|
);
|
||||||
self.sleep_comms
|
self.sleep_comms
|
||||||
.send(SleepComms::StartSleep(
|
.send(SleepComms::StartSleep(
|
||||||
request.sender,
|
request.sender,
|
||||||
request.msec as i64,
|
request.msec.to_i64(),
|
||||||
self.elapsed_ms(),
|
self.elapsed_ms(),
|
||||||
))
|
))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
@ -157,7 +166,7 @@ impl XousTickTimer {
|
||||||
/// Disable the sleep interrupt and remove the currently-pending sleep item.
|
/// 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
|
/// If the sleep item has fired, then there will be no existing sleep item
|
||||||
/// remaining.
|
/// remaining.
|
||||||
pub fn stop_sleep(
|
pub(crate) fn stop_sleep(
|
||||||
&mut self,
|
&mut self,
|
||||||
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
||||||
) {
|
) {
|
||||||
|
|
@ -172,7 +181,7 @@ impl XousTickTimer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn start_sleep(
|
pub(crate) fn start_sleep(
|
||||||
&mut self,
|
&mut self,
|
||||||
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
||||||
) {
|
) {
|
||||||
|
|
@ -181,7 +190,9 @@ impl XousTickTimer {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!(
|
log::info!(
|
||||||
"scheduling a response at {} to {} (heap: {:?})",
|
"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);
|
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
|
/// Note that interrupts are always enabled, which is why we must stop the timer prior to
|
||||||
/// reordering the list.
|
/// reordering the list.
|
||||||
pub fn recalculate_sleep(
|
pub(crate) unsafe fn recalculate_sleep_offline(
|
||||||
&mut self,
|
&mut self,
|
||||||
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
||||||
new: Option<TimerRequest>,
|
new: Option<TimerRequest>,
|
||||||
) {
|
) {
|
||||||
self.stop_sleep(sleep_heap);
|
|
||||||
log::trace!("Elapsed: {}", self.elapsed_ms());
|
log::trace!("Elapsed: {}", self.elapsed_ms());
|
||||||
self.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 {
|
||||||
|
|
@ -227,7 +236,15 @@ impl XousTickTimer {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!("No new sleep request");
|
log::info!("No new sleep request");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn recalculate_sleep(
|
||||||
|
&mut self,
|
||||||
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
||||||
|
new: Option<TimerRequest>,
|
||||||
|
) {
|
||||||
|
self.stop_sleep(sleep_heap);
|
||||||
|
unsafe { self.recalculate_sleep_offline(sleep_heap, new) }
|
||||||
self.start_sleep(sleep_heap);
|
self.start_sleep(sleep_heap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,31 +1,92 @@
|
||||||
#[cfg(any(feature="precursor", feature="renode"))]
|
#[cfg(any(feature = "precursor", feature = "renode"))]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod precursor;
|
pub mod precursor;
|
||||||
#[cfg(any(feature="precursor", feature="renode"))]
|
use core::ops::{Add, AddAssign};
|
||||||
|
|
||||||
|
#[cfg(any(feature = "precursor", feature = "renode"))]
|
||||||
pub use precursor::*;
|
pub use precursor::*;
|
||||||
|
|
||||||
#[cfg(any(not(target_os = "xous"),
|
#[cfg(any(
|
||||||
not(any(feature="precursor", feature="renode", feature="atsama5d27", feature="cramium-fpga", feature="cramium-soc", not(target_os = "xous")))
|
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;
|
pub mod hosted;
|
||||||
#[cfg(any(not(target_os = "xous"),
|
#[cfg(any(
|
||||||
not(any(feature="precursor", feature="renode", feature="atsama5d27", feature="cramium-fpga", feature="cramium-soc", not(target_os = "xous")))
|
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::*;
|
pub use hosted::*;
|
||||||
|
|
||||||
#[cfg(any(feature="atsama5d27"))]
|
#[cfg(any(feature = "atsama5d27"))]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod atsama5d2;
|
pub mod atsama5d2;
|
||||||
#[cfg(any(feature="atsama5d27"))]
|
#[cfg(any(feature = "atsama5d27"))]
|
||||||
pub use atsama5d2::*;
|
pub use atsama5d2::*;
|
||||||
|
|
||||||
#[cfg(any(feature="cramium-fpga", feature="cramium-soc"))]
|
#[cfg(any(feature = "cramium-fpga", feature = "cramium-soc"))]
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
pub mod cramium;
|
pub mod cramium;
|
||||||
#[cfg(any(feature="cramium-fpga", feature="cramium-soc"))]
|
#[cfg(any(feature = "cramium-fpga", feature = "cramium-soc"))]
|
||||||
pub use cramium::*;
|
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<i64> for TimeoutExpiry {
|
||||||
|
type Output = TimeoutExpiry;
|
||||||
|
fn add(self, rhs: i64) -> Self::Output {
|
||||||
|
TimeoutExpiry(self.0 + rhs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AddAssign<i64> 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<i64> for TimeoutExpiry {
|
||||||
|
fn from(item: i64) -> Self {
|
||||||
|
TimeoutExpiry(item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<usize> for TimeoutExpiry {
|
||||||
|
fn from(item: usize) -> Self {
|
||||||
|
TimeoutExpiry(item as i64)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
#[derive(PartialEq, Eq, PartialOrd, Ord, Copy, Clone)]
|
||||||
pub enum RequestKind {
|
pub enum RequestKind {
|
||||||
|
|
|
||||||
|
|
@ -1,29 +1,52 @@
|
||||||
const TICKS_PER_MS: u64 = 1;
|
const TICKS_PER_MS: u64 = 1;
|
||||||
|
|
||||||
|
use core::sync::atomic::{AtomicU32, AtomicUsize, Ordering};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
use crate::TimerRequest;
|
|
||||||
use crate::TimeoutExpiry;
|
use crate::TimeoutExpiry;
|
||||||
|
use crate::TimerRequest;
|
||||||
|
|
||||||
use susres::{RegManager, RegOrField, SuspendResume};
|
use susres::{RegManager, RegOrField, SuspendResume};
|
||||||
use utralib::generated::*;
|
use utralib::generated::*;
|
||||||
|
|
||||||
|
use xous::definitions::MessageSender;
|
||||||
|
|
||||||
|
/// Latency slack may be necessary for hardware implementations that can't handle
|
||||||
|
/// events that happened in the past. However, for Precursor hardware, alarms will
|
||||||
|
/// trigger based on a "less than or equal to current time" basis, so even if an
|
||||||
|
/// alarm happens in the "past" due to slippage between the current time reading
|
||||||
|
/// and the alarm setting, the alarm should still trigger.
|
||||||
|
///
|
||||||
|
/// Note that setting this number larger than 0 will degrade scheduler performance.
|
||||||
|
const LATENCY_SLACK_MS: i64 = 0;
|
||||||
|
|
||||||
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,
|
|
||||||
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 }>,
|
||||||
wdt: utralib::CSR<u32>,
|
wdt: utralib::CSR<u32>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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) {
|
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);
|
// println!("In IRQ, connection: {}", xtt.connection);
|
||||||
|
|
||||||
// Safe because we're in an interrupt, and this interrupt is only
|
// 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();
|
let response = xtt.current_response.take().unwrap();
|
||||||
xous::return_scalar(response.sender, response.kind as usize).ok();
|
xous::return_scalar(response.sender, response.kind as usize).ok();
|
||||||
|
|
||||||
|
|
@ -35,7 +58,7 @@ fn handle_irq(_irq_no: usize, arg: *mut usize) {
|
||||||
// Which is fine, because the queue is always recalculated any time a message arrives.
|
// Which is fine, because the queue is always recalculated any time a message arrives.
|
||||||
use num_traits::ToPrimitive;
|
use num_traits::ToPrimitive;
|
||||||
xous::try_send_message(
|
xous::try_send_message(
|
||||||
xtt.connection,
|
connection,
|
||||||
xous::Message::Scalar(xous::ScalarMessage {
|
xous::Message::Scalar(xous::ScalarMessage {
|
||||||
id: crate::api::Opcode::RecalculateSleep.to_usize().unwrap(),
|
id: crate::api::Opcode::RecalculateSleep.to_usize().unwrap(),
|
||||||
arg1: response.sender.to_usize(),
|
arg1: response.sender.to_usize(),
|
||||||
|
|
@ -44,10 +67,14 @@ fn handle_irq(_irq_no: usize, arg: *mut usize) {
|
||||||
arg4: 0,
|
arg4: 0,
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
// Save the response so we can be sure we don't double-return messages.
|
// Remember what the last message was that we responded to. This will prevent
|
||||||
xtt.last_response = Some(response);
|
// 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 {
|
impl XousTickTimer {
|
||||||
|
|
@ -59,14 +86,14 @@ impl XousTickTimer {
|
||||||
4096,
|
4096,
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
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(
|
let wdt = xous::syscall::map_memory(
|
||||||
xous::MemoryAddress::new(utra::wdt::HW_WDT_BASE),
|
xous::MemoryAddress::new(utra::wdt::HW_WDT_BASE),
|
||||||
None,
|
None,
|
||||||
4096,
|
4096,
|
||||||
xous::MemoryFlags::R | xous::MemoryFlags::W,
|
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 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);
|
let wdt_sr_manager = RegManager::new(wdt.as_mut_ptr() as *mut u32);
|
||||||
|
|
@ -74,8 +101,6 @@ impl XousTickTimer {
|
||||||
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,
|
|
||||||
ticktimer_sr_manager,
|
ticktimer_sr_manager,
|
||||||
wdt_sr_manager,
|
wdt_sr_manager,
|
||||||
wdt: CSR::new(wdt.as_mut_ptr() as *mut u32),
|
wdt: CSR::new(wdt.as_mut_ptr() as *mut u32),
|
||||||
|
|
@ -89,23 +114,33 @@ impl XousTickTimer {
|
||||||
// xtt.wdt_sr_manager.push(RegOrField::Field(utra::wdt::WATCHDOG_ENABLE), None);
|
// 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
|
xtt
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn last_response(&self) -> &Option<TimerRequest> {
|
pub fn last_response(&self) -> MessageSender {
|
||||||
&self.last_response
|
MessageSender::from_usize(LAST_RESPONDER.load(Ordering::Relaxed))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_last_response(&mut self) {
|
/// Return the number of times the ticktimer IRQ has fired.
|
||||||
self.last_response = None;
|
///
|
||||||
|
/// # 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) {
|
pub fn init(&mut self) {
|
||||||
xous::claim_interrupt(
|
xous::claim_interrupt(
|
||||||
utra::ticktimer::TICKTIMER_IRQ,
|
utra::ticktimer::TICKTIMER_IRQ,
|
||||||
handle_irq,
|
handle_irq,
|
||||||
self as *mut XousTickTimer as *mut usize,
|
self as *mut XousTickTimer as *mut usize,
|
||||||
)
|
)
|
||||||
.expect("couldn't claim irq");
|
.expect("couldn't claim irq");
|
||||||
|
|
||||||
self.ticktimer_sr_manager
|
self.ticktimer_sr_manager
|
||||||
.push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET0), None);
|
.push(RegOrField::Reg(utra::ticktimer::MSLEEP_TARGET0), None);
|
||||||
|
|
@ -116,6 +151,7 @@ impl XousTickTimer {
|
||||||
self.ticktimer_sr_manager
|
self.ticktimer_sr_manager
|
||||||
.push(RegOrField::Reg(utra::ticktimer::EV_ENABLE), None);
|
.push(RegOrField::Reg(utra::ticktimer::EV_ENABLE), 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
|
||||||
|
|
@ -143,10 +179,10 @@ impl XousTickTimer {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
{
|
{
|
||||||
log::info!(
|
log::info!(
|
||||||
"Stopping currently-running timer sr.msec: {} elapsed_ms: {}",
|
"Stopping currently-running timer sr.msec: {} elapsed_ms: {}",
|
||||||
sr.msec,
|
sr.msec,
|
||||||
self.elapsed_ms()
|
self.elapsed_ms()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
Some(sr)
|
Some(sr)
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -155,12 +191,12 @@ impl XousTickTimer {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn schedule_response(&mut self, request: TimerRequest) {
|
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!(
|
log::trace!(
|
||||||
"setting a response at {} ms (current time: {} ms)",
|
"setting a response at {} ms (current time: {} ms)",
|
||||||
irq_target,
|
irq_target,
|
||||||
self.elapsed_ms()
|
self.elapsed_ms()
|
||||||
);
|
);
|
||||||
|
|
||||||
// Disable the timer interrupt
|
// Disable the timer interrupt
|
||||||
assert!(self.csr.rf(utra::ticktimer::EV_ENABLE_ALARM) == 0);
|
assert!(self.csr.rf(utra::ticktimer::EV_ENABLE_ALARM) == 0);
|
||||||
|
|
@ -192,10 +228,10 @@ impl XousTickTimer {
|
||||||
let state = self.wdt.r(utra::wdt::STATE);
|
let state = self.wdt.r(utra::wdt::STATE);
|
||||||
if state & self.wdt.ms(utra::wdt::STATE_DISARMED, 1) == 0 {
|
if state & self.wdt.ms(utra::wdt::STATE_DISARMED, 1) == 0 {
|
||||||
log::info!(
|
log::info!(
|
||||||
"{} WDT is not disarmed, state: 0x{:x}",
|
"{} WDT is not disarmed, state: 0x{:x}",
|
||||||
self.elapsed_ms(),
|
self.elapsed_ms(),
|
||||||
state
|
state
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -209,6 +245,7 @@ impl XousTickTimer {
|
||||||
// by writing this after suspend(), resume will get the prior value
|
// by writing this after suspend(), resume will get the prior value
|
||||||
self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0);
|
self.csr.wfo(utra::ticktimer::EV_ENABLE_ALARM, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn resume(&mut self) {
|
pub fn resume(&mut self) {
|
||||||
// this is a write-once bit that's later erased, so it can't be managed automatically
|
// 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
|
// thus we have to restore in manually on a resume
|
||||||
|
|
@ -224,24 +261,28 @@ impl XousTickTimer {
|
||||||
self.ticktimer_sr_manager.resume();
|
self.ticktimer_sr_manager.resume();
|
||||||
|
|
||||||
log::trace!(
|
log::trace!(
|
||||||
"ticktimer enable: {}",
|
"ticktimer enable: {}",
|
||||||
self.csr.r(utra::ticktimer::EV_ENABLE)
|
self.csr.r(utra::ticktimer::EV_ENABLE)
|
||||||
);
|
);
|
||||||
log::trace!(
|
log::trace!(
|
||||||
"ticktimer time/target: {}/{}",
|
"ticktimer time/target: {}/{}",
|
||||||
self.csr.r(utra::ticktimer::TIME0),
|
self.csr.r(utra::ticktimer::TIME0),
|
||||||
self.csr.r(utra::ticktimer::MSLEEP_TARGET0)
|
self.csr.r(utra::ticktimer::MSLEEP_TARGET0)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Disable the sleep interrupt and remove the currently-pending sleep item.
|
/// 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
|
/// If the sleep item has fired, then there will be no existing sleep item
|
||||||
/// remaining.
|
/// 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,
|
&mut self,
|
||||||
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
||||||
) {
|
) -> usize {
|
||||||
// If there's a sleep request ongoing now, grab it.
|
// If there's a sleep request ongoing now, grab it.
|
||||||
if let Some(current) = self.stop_interrupt() {
|
if let Some(current) = self.stop_interrupt() {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
|
|
@ -251,9 +292,10 @@ impl XousTickTimer {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!("There was no existing sleep() request");
|
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,
|
&mut self,
|
||||||
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
||||||
) {
|
) {
|
||||||
|
|
@ -262,7 +304,9 @@ impl XousTickTimer {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!(
|
log::info!(
|
||||||
"scheduling a response at {} to {} (heap: {:?})",
|
"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);
|
self.schedule_response(next_response);
|
||||||
|
|
@ -275,20 +319,17 @@ impl XousTickTimer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Recalculate the sleep timer, optionally adding a new Request to the list of available
|
/// Recalculate sleep without starting and stopping the sleep timer.
|
||||||
/// 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
|
/// # Safety
|
||||||
/// reordering the list.
|
///
|
||||||
pub fn recalculate_sleep(
|
/// This must be called with the sleep timer already stopped
|
||||||
|
pub(crate) unsafe fn recalculate_sleep_offline(
|
||||||
&mut self,
|
&mut self,
|
||||||
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>, // min-heap with Reverse
|
sleep_heap: &mut BTreeMap<TimeoutExpiry, TimerRequest>,
|
||||||
new: Option<TimerRequest>,
|
new: Option<TimerRequest>,
|
||||||
) {
|
) {
|
||||||
self.stop_sleep(sleep_heap);
|
|
||||||
log::trace!("Elapsed: {}", self.elapsed_ms());
|
log::trace!("Elapsed: {}", self.elapsed_ms());
|
||||||
self.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 {
|
||||||
|
|
@ -308,7 +349,21 @@ impl XousTickTimer {
|
||||||
#[cfg(feature = "debug-print")]
|
#[cfg(feature = "debug-print")]
|
||||||
log::info!("No new sleep request");
|
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<TimeoutExpiry, TimerRequest>,
|
||||||
|
new: Option<TimerRequest>,
|
||||||
|
) {
|
||||||
|
self.stop_sleep(sleep_heap);
|
||||||
|
unsafe { self.recalculate_sleep_offline(sleep_heap, new) }
|
||||||
self.start_sleep(sleep_heap);
|
self.start_sleep(sleep_heap);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,21 @@
|
||||||
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
|
|
||||||
|
static CACHED_PID: AtomicU32 = AtomicU32::new(0);
|
||||||
|
|
||||||
pub fn id() -> u32 {
|
pub fn id() -> u32 {
|
||||||
|
let pid = CACHED_PID.load(Ordering::Relaxed);
|
||||||
|
if pid != 0 {
|
||||||
|
return pid;
|
||||||
|
}
|
||||||
|
|
||||||
if let Ok(pid) = crate::syscall::current_pid() {
|
if let Ok(pid) = crate::syscall::current_pid() {
|
||||||
pid.get() as u32
|
CACHED_PID.store(pid.get() as u32, Ordering::Relaxed);
|
||||||
|
pid.get() as u32;
|
||||||
} else {
|
} else {
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_id(_id: u32) {}
|
pub fn set_id(id: u32) {
|
||||||
|
CACHED_PID.store(id, Ordering::Relaxed);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue