From 2916eb37757ed3a197718e49981bf2d7cafb69ff Mon Sep 17 00:00:00 2001 From: bunnie Date: Fri, 9 Apr 2021 18:51:43 +0800 Subject: [PATCH] relocate the destroy callback call to after the server exits --- RELEASE-v0.8.md | 2 +- services/com/src/lib.rs | 3 +-- services/keyboard/src/lib.rs | 4 ++-- services/llio/src/lib.rs | 6 +++++- services/rtc/src/api.rs | 4 ++-- services/rtc/src/lib.rs | 40 +++++++++++++++--------------------- services/rtc/src/main.rs | 13 ++++++------ 7 files changed, 33 insertions(+), 39 deletions(-) diff --git a/RELEASE-v0.8.md b/RELEASE-v0.8.md index f0cb924c2..1f0c9abc3 100644 --- a/RELEASE-v0.8.md +++ b/RELEASE-v0.8.md @@ -251,6 +251,7 @@ fn callback_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } impl Drop for MyServer { @@ -265,7 +266,6 @@ impl Drop for MyServer { 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. diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 20b16f8e4..62a7e402c 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -35,6 +35,7 @@ fn battstats_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } #[derive(Debug)] pub struct Com { @@ -132,11 +133,9 @@ impl Drop for Com { 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/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index 6d6fcf425..ac728cee1 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -83,7 +83,6 @@ impl Drop for Keyboard { 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(); } if let Some(sid) = self.raw_cb_sid.take() { @@ -95,7 +94,6 @@ impl Drop for Keyboard { 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. @@ -146,6 +144,7 @@ fn event_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } @@ -171,4 +170,5 @@ fn raw_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } \ No newline at end of file diff --git a/services/llio/src/lib.rs b/services/llio/src/lib.rs index 2f8c39be4..dff5713cb 100644 --- a/services/llio/src/lib.rs +++ b/services/llio/src/lib.rs @@ -280,7 +280,6 @@ fn drop_conn(sid: xous::SID) { xous::send_message(cid, Message::new_blocking_scalar(EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); unsafe{xous::disconnect(cid).unwrap();} - xous::destroy_server(sid).unwrap(); } impl Drop for Llio { fn drop(&mut self) { @@ -325,6 +324,7 @@ fn i2c_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } /// handles callback messages that indicate a USB interrupt has happened, in the library user's process space. @@ -345,6 +345,7 @@ fn usb_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } /// handles callback messages that indicate a RTC interrupt has happened, in the library user's process space. @@ -365,6 +366,7 @@ fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } /// handles callback messages that indicate a COM interrupt has happened, in the library user's process space. @@ -385,6 +387,7 @@ fn com_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } /// handles callback messages that indicate a GPIO interrupt has happened, in the library user's process space. @@ -405,4 +408,5 @@ fn gpio_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { None => (), } } + xous::destroy_server(sid).unwrap(); } \ No newline at end of file diff --git a/services/rtc/src/api.rs b/services/rtc/src/api.rs index b7d6ca071..272d2db9b 100644 --- a/services/rtc/src/api.rs +++ b/services/rtc/src/api.rs @@ -58,9 +58,9 @@ pub(crate) enum Opcode { ClearRtcAlarm, } -#[derive(Debug, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)] +#[derive(Debug, num_derive::FromPrimitive, num_derive::ToPrimitive)] pub(crate) enum Return { - ReturnDateTime(DateTime), + ReturnDateTime, Drop, } diff --git a/services/rtc/src/lib.rs b/services/rtc/src/lib.rs index 78ae17d96..2eb6ca2cc 100644 --- a/services/rtc/src/lib.rs +++ b/services/rtc/src/lib.rs @@ -6,7 +6,7 @@ pub use api::*; use api::{Return, Opcode}; use xous::{send_message, CID, Message}; use xous_ipc::Buffer; -use num_traits::ToPrimitive; +use num_traits::{ToPrimitive, FromPrimitive}; #[derive(Debug)] pub struct Rtc { @@ -34,23 +34,12 @@ impl Rtc { // tell my handler thread to quit log::trace!("connect for unhook"); let cid = xous::connect(sid).expect("can't connect to CB server for disconnect message"); - let msg = Return::Drop; - let buf = Buffer::into_buf(msg).expect("can't send convert drop message"); log::trace!("sending drop"); - buf.send(self.conn, 0).expect("can't send Drop message to CB server"); // there is only one message type, so ID field is disregarded + send_message(cid, + Message::new_scalar(Return::Drop.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); log::trace!("disconnecting"); unsafe{xous::disconnect(cid).expect("can't disconnect from CB server");} - log::trace!("destroying"); - let mut destroyed = false; - loop { - match xous::destroy_server(sid) { - Ok(_) => destroyed = true, - Err(e) => {log::trace!("destroy resulted in {:?}", e); xous::yield_slice()}, // wait a bit for the queue to empty - } - if destroyed { - break; - } - } } log::trace!("nullifying"); self.callback_sid = None; @@ -115,11 +104,10 @@ impl Drop for Rtc { if let Some(sid) = self.callback_sid.take() { // tell my handler thread to quit let cid = xous::connect(sid).unwrap(); - let msg = Return::Drop; - let buf = Buffer::into_buf(msg).unwrap(); - buf.lend(self.conn, 0).unwrap(); // there is only one message type, so ID field is disregarded + xous::send_message(cid, + xous::Message::new_scalar(Return::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. @@ -134,10 +122,10 @@ fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { loop { let msg = xous::receive_message(sid).unwrap(); log::trace!("rtc callback got msg: {:?}", msg); - let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; - let response = buffer.to_original::().unwrap(); - match response { - Return::ReturnDateTime(dt) => { + match FromPrimitive::from_usize(msg.body.id()) { + Some(Return::ReturnDateTime) => { + let buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; + let dt = buffer.to_original::().unwrap(); unsafe { if let Some(cb) = RTC_CB { cb(dt) @@ -146,9 +134,13 @@ fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { } } } - Return::Drop => { + Some(Return::Drop) => { break; } + None => { + log::error!("got unrecognized message in rtc CB server, ignoring"); + } } } + xous::destroy_server(sid).expect("can't destroy my server on exit!"); } \ No newline at end of file diff --git a/services/rtc/src/main.rs b/services/rtc/src/main.rs index b3aa917d2..322b05dbc 100644 --- a/services/rtc/src/main.rs +++ b/services/rtc/src/main.rs @@ -3,7 +3,7 @@ mod api; -use num_traits::FromPrimitive; +use num_traits::{FromPrimitive, ToPrimitive}; use xous_ipc::Buffer; use api::{Return, Opcode, DateTime}; use xous::{CID, msg_scalar_unpack}; @@ -599,12 +599,11 @@ fn xmain() -> ! { let incoming_buffer = unsafe { Buffer::from_memory_message(msg.body.memory_message().unwrap()) }; let dt = incoming_buffer.to_original::().unwrap(); log::trace!("ResponseDateTime received: {:?}", dt); - let ret = Return::ReturnDateTime(dt); - let outgoing_buf = Buffer::into_buf(ret).or(Err(xous::Error::InternalError)).unwrap(); + let outgoing_buf = Buffer::into_buf(dt).or(Err(xous::Error::InternalError)).unwrap(); for maybe_conn in dt_cb_conns.iter_mut() { if let Some(conn) = maybe_conn { //log::trace!("ResponeDateTime sending to {}", *conn); - match outgoing_buf.lend(*conn, 0) { // the ID field is ignored on the callback server + match outgoing_buf.lend(*conn, Return::ReturnDateTime.to_u32().unwrap()) { Err(xous::Error::ServerNotFound) => { *maybe_conn = None }, @@ -660,9 +659,9 @@ fn xmain() -> ! { } for entry in dt_cb_conns.iter_mut() { if let Some(conn) = entry { - let dropmsg = Return::Drop; - let buf = Buffer::into_buf(dropmsg).unwrap(); - buf.lend(*conn, 0).unwrap(); // the ID is ignored for this server + xous::send_message(*conn, + xous::Message::new_scalar(Return::Drop.to_usize().unwrap(), 0, 0, 0, 0) + ).unwrap(); unsafe{xous::disconnect(*conn).unwrap();} } *entry = None;