mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
refactor locking to use deferred responses
This commit is contained in:
parent
1261d73451
commit
360e50a8bb
2 changed files with 22 additions and 22 deletions
|
|
@ -12,7 +12,6 @@ use std::thread;
|
|||
pub struct Modals {
|
||||
conn: CID,
|
||||
token: [u32; 4],
|
||||
tt: ticktimer_server::Ticktimer,
|
||||
}
|
||||
impl Modals {
|
||||
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
||||
|
|
@ -24,7 +23,6 @@ impl Modals {
|
|||
Ok(Modals {
|
||||
conn,
|
||||
token,
|
||||
tt: ticktimer_server::Ticktimer::new().unwrap(),
|
||||
})
|
||||
}
|
||||
|
||||
|
|
@ -211,14 +209,8 @@ impl Modals {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// busy-wait until we have acquired a mutex on the Modals server
|
||||
/// Blocks until we have a lock on the modals server
|
||||
fn lock(&self) {
|
||||
while !self.try_get_mutex() {
|
||||
self.tt.sleep_ms(1000).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
fn try_get_mutex(&self) -> bool {
|
||||
match send_message(
|
||||
self.conn,
|
||||
Message::new_blocking_scalar(Opcode::GetMutex.to_usize().unwrap(),
|
||||
|
|
@ -228,18 +220,14 @@ impl Modals {
|
|||
self.token[3] as usize,
|
||||
)).expect("couldn't send mutex acquisition message") {
|
||||
xous::Result::Scalar1(code) => {
|
||||
if code == 1 {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
if code != 1 {
|
||||
log::warn!("Unexpected return from lock acquisition.");
|
||||
}
|
||||
},
|
||||
_ => {
|
||||
log::error!("Internal error trying to acquire mutex");
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -435,6 +435,8 @@ fn xmain() -> ! {
|
|||
let default_nonce = [
|
||||
trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(), trng.get_u32().unwrap(),
|
||||
];
|
||||
let mut work_queue = Vec::<(xous::MessageSender, [u32; 4])>::new();
|
||||
|
||||
loop {
|
||||
let mut msg = xous::receive_message(modals_sid).unwrap();
|
||||
log::debug!("message: {:?}", msg);
|
||||
|
|
@ -445,7 +447,7 @@ fn xmain() -> ! {
|
|||
if token == incoming_token {
|
||||
xous::return_scalar(msg.sender, 1).unwrap();
|
||||
} else {
|
||||
xous::return_scalar(msg.sender, 0).unwrap();
|
||||
work_queue.push((msg.sender, incoming_token));
|
||||
}
|
||||
} else {
|
||||
token_lock = Some(incoming_token);
|
||||
|
|
@ -470,7 +472,7 @@ fn xmain() -> ! {
|
|||
RendererState::RunRadio(_) => (),
|
||||
RendererState::ResponseRadio(item) => {
|
||||
buffer.replace(item).unwrap();
|
||||
token_lock = None;
|
||||
token_lock = next_lock(&mut work_queue);
|
||||
break;
|
||||
},
|
||||
_ => {
|
||||
|
|
@ -499,7 +501,7 @@ fn xmain() -> ! {
|
|||
RendererState::RunCheckBox(_) => (),
|
||||
RendererState::ResponseCheckBox(items) => {
|
||||
buffer.replace(items).unwrap();
|
||||
token_lock = None;
|
||||
token_lock = next_lock(&mut work_queue);
|
||||
break;
|
||||
},
|
||||
_ => {
|
||||
|
|
@ -528,7 +530,7 @@ fn xmain() -> ! {
|
|||
RendererState::RunText(_) => (),
|
||||
RendererState::ResponseText(text) => {
|
||||
buffer.replace(text).unwrap();
|
||||
token_lock = None;
|
||||
token_lock = next_lock(&mut work_queue);
|
||||
break;
|
||||
},
|
||||
_ => {
|
||||
|
|
@ -554,7 +556,7 @@ fn xmain() -> ! {
|
|||
loop {
|
||||
match *op.lock().unwrap() {
|
||||
RendererState::RunNotification(_) => (),
|
||||
RendererState::None => {token_lock = None; break},
|
||||
RendererState::None => {token_lock = next_lock(&mut work_queue); break},
|
||||
_ => {
|
||||
log::error!("Illegal state transition in renderer");
|
||||
panic!("Illegal state transition in renderer");
|
||||
|
|
@ -592,7 +594,7 @@ fn xmain() -> ! {
|
|||
renderer_cid,
|
||||
Message::new_scalar(RendererOp::FinishProgress.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't update progress bar");
|
||||
token_lock = None;
|
||||
token_lock = next_lock(&mut work_queue);
|
||||
}),
|
||||
Some(Opcode::AddModalItem) => {
|
||||
let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) };
|
||||
|
|
@ -640,7 +642,7 @@ fn xmain() -> ! {
|
|||
renderer_cid,
|
||||
Message::new_scalar(RendererOp::CloseDynamicNotification.to_usize().unwrap(), 0, 0, 0, 0)
|
||||
).expect("couldn't close dynamic notification");
|
||||
token_lock = None;
|
||||
token_lock = next_lock(&mut work_queue);
|
||||
}),
|
||||
Some(Opcode::Quit) => {
|
||||
log::warn!("Shared modal UX handler exiting.");
|
||||
|
|
@ -676,4 +678,14 @@ fn compute_checked_percentage(current: u32, start: u32, end: u32) -> u32 {
|
|||
(((current as u64 - start as u64) * 100) / (end as u64 - start as u64)) as u32
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn next_lock(work_queue: &mut Vec::<(xous::MessageSender, [u32; 4])>) -> Option<[u32; 4]> {
|
||||
if work_queue.len() > 0 {
|
||||
let (unblock_pid, token) = work_queue.remove(0);
|
||||
xous::return_scalar(unblock_pid, 1).unwrap();
|
||||
Some(token)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue