implement Drop on Com object

This commit is contained in:
bunnie 2021-04-02 19:01:11 +08:00
parent d4e1de7cf9
commit c972666f41
3 changed files with 57 additions and 17 deletions

View file

@ -122,10 +122,21 @@ impl Com {
impl Drop for Com {
fn drop(&mut self) {
// tell my handler thread to quit
xous::send_message(self.conn,
Message::new_scalar(api::Callback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
// now de-allocate myself
self.battstats_sid = None;
// if we have callbacks, destroy the battstats callback server
if let Some(sid) = self.battstats_sid.take() {
// no need to tell the COM server we're quitting: the next time a callback processes,
// it will automatically remove my entry as it will receive a ServerNotFound error.
// tell my handler thread to quit
let cid = xous::connect(sid).unwrap();
xous::send_message(cid,
Message::new_blocking_scalar(api::Callback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
unsafe{xous::disconnect(cid).unwrap();}
xous::destroy_server(sid).unwrap();
}
// now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection.
unsafe{xous::disconnect(self.conn).unwrap();}
}
}

View file

@ -127,21 +127,31 @@ mod implementation {
self.txrx(tx)
}
pub fn process_queue(&mut self) {
pub fn process_queue(&mut self) -> Option<xous::CID> {
if !self.workqueue.is_empty() && !self.busy {
self.busy = true;
let work_descriptor = self.workqueue.swap_remove(0); // not quite FIFO, but Vec does not support FIFO (best we can do with "heapless")
if work_descriptor.work.verb == ComState::STAT.verb {
let ret = if work_descriptor.work.verb == ComState::STAT.verb {
let stats = self.get_battstats();
return_battstats(work_descriptor.sender, stats)
.expect("Could not return BattStatsNb value");
match return_battstats(work_descriptor.sender, stats) {
Err(xous::Error::ServerNotFound) => {
// the callback target has quit, so de-allocate it from our list
Some(work_descriptor.sender)
},
Ok(()) => None,
_ => panic!("unhandled error in callback process_queue"),
}
} else {
error!(
"unimplemented work queue responder 0x{:x}",
work_descriptor.work.verb
);
}
None
};
self.busy = false;
ret
} else {
None
}
}
@ -207,21 +217,31 @@ mod implementation {
}
}
pub fn process_queue(&mut self) {
pub fn process_queue(&mut self) -> Option<xous::CID> {
if !self.workqueue.is_empty() && !self.busy {
self.busy = true;
let work_descriptor = self.workqueue.swap_remove(0); // not quite FIFO, but Vec does not support FIFO (best we can do with "heapless")
if work_descriptor.work.verb == ComState::STAT.verb {
let ret = if work_descriptor.work.verb == ComState::STAT.verb {
let stats = self.get_battstats();
return_battstats(work_descriptor.sender, stats)
.expect("Could not return BattStatsNb value");
match return_battstats(work_descriptor.sender, stats) {
Err(xous::Error::ServerNotFound) => {
// the callback target has quit, so de-allocate it from our list
Some(work_descriptor.sender)
},
Ok(()) => None,
_ => panic!("unhandled error in callback process_queue"),
}
} else {
error!(
"unimplemented work queue responder 0x{:x}",
work_descriptor.work.verb
);
}
None
};
self.busy = false;
ret
} else {
None
}
}
}
@ -341,6 +361,15 @@ fn xmain() -> ! {
None => error!("unknown opcode"),
}
com.process_queue();
if let Some(dropped_cid) = com.process_queue() {
for entry in battstats_conns.iter_mut() {
if let Some(cid) = *entry {
if cid == dropped_cid {
*entry = None;
break;
}
}
}
}
}
}

View file

@ -41,7 +41,7 @@ fn try_main() -> Result<(), DynError> {
//"shellchat",
//"ime-frontend",
//"ime-plugin-shell",
"graphics-server",
//"graphics-server",
"ticktimer-server",
"log-server",
"com",