relocate the destroy callback call to after the server exits

This commit is contained in:
bunnie 2021-04-09 18:51:43 +08:00
parent 162bf01819
commit 2916eb3775
7 changed files with 33 additions and 39 deletions

View file

@ -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.

View file

@ -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();}
}
}

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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,
}

View file

@ -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::<Return,_>().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::<DateTime,_>().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!");
}

View file

@ -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::<DateTime, _>().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;