diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index a93b3c774..36bc5e762 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -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();} + } } diff --git a/services/com/src/main.rs b/services/com/src/main.rs index 94b404048..85f979067 100644 --- a/services/com/src/main.rs +++ b/services/com/src/main.rs @@ -127,21 +127,31 @@ mod implementation { self.txrx(tx) } - pub fn process_queue(&mut self) { + pub fn process_queue(&mut self) -> Option { 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 { 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; + } + } + } + } } } diff --git a/xtask/src/main.rs b/xtask/src/main.rs index d6470e2e3..dfb2e1974 100644 --- a/xtask/src/main.rs +++ b/xtask/src/main.rs @@ -41,7 +41,7 @@ fn try_main() -> Result<(), DynError> { //"shellchat", //"ime-frontend", //"ime-plugin-shell", - "graphics-server", + //"graphics-server", "ticktimer-server", "log-server", "com",