mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-20 09:03:28 +00:00
eliminate static mut unsafe pattern in callback function storage
since xous 0.9.5, we've had a Box and we've had threads, so now we can put the callback in a Box and pass it to a thread, instead of relying on an `unsafe` work-around to get the callback function pointer into the thread's environment.
This commit is contained in:
parent
e4db83e59e
commit
960745ca61
2 changed files with 31 additions and 50 deletions
|
|
@ -4,12 +4,10 @@ use xous_ipc::Buffer;
|
||||||
use num_traits::{ToPrimitive, FromPrimitive};
|
use num_traits::{ToPrimitive, FromPrimitive};
|
||||||
use crate::*;
|
use crate::*;
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Rtc {
|
pub struct Rtc {
|
||||||
conn: CID,
|
conn: CID,
|
||||||
callback_sid: Option<xous::SID>,
|
callback_sid: Option<xous::SID>,
|
||||||
}
|
}
|
||||||
static mut RTC_CB: Option<fn(DateTime)> = None;
|
|
||||||
impl Rtc {
|
impl Rtc {
|
||||||
pub fn new(xns: &xous_names::XousNames) -> Self {
|
pub fn new(xns: &xous_names::XousNames) -> Self {
|
||||||
REFCOUNT.fetch_add(1, Ordering::Relaxed);
|
REFCOUNT.fetch_add(1, Ordering::Relaxed);
|
||||||
|
|
@ -24,8 +22,7 @@ impl Rtc {
|
||||||
buf.lend(self.conn, RtcOpcode::SetDateTime.to_u32().unwrap()).map(|_| ())
|
buf.lend(self.conn, RtcOpcode::SetDateTime.to_u32().unwrap()).map(|_| ())
|
||||||
}
|
}
|
||||||
pub fn unhook_rtc_callback(&mut self) -> Result<(), xous::Error> {
|
pub fn unhook_rtc_callback(&mut self) -> Result<(), xous::Error> {
|
||||||
unsafe{RTC_CB = None};
|
if let Some(sid) = self.callback_sid.take() {
|
||||||
if let Some(sid) = self.callback_sid {
|
|
||||||
let sid_tuple = sid.to_u32();
|
let sid_tuple = sid.to_u32();
|
||||||
xous::send_message(self.conn,
|
xous::send_message(self.conn,
|
||||||
Message::new_scalar(RtcOpcode::UnregisterDateTimeCallback.to_usize().unwrap(),
|
Message::new_scalar(RtcOpcode::UnregisterDateTimeCallback.to_usize().unwrap(),
|
||||||
|
|
@ -34,21 +31,39 @@ impl Rtc {
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn hook_rtc_callback(&mut self, cb: fn(DateTime)) -> Result<(), xous::Error> {
|
pub fn hook_rtc_callback(&mut self, cb: impl Fn(DateTime) + 'static + Send) -> Result<(), xous::Error> {
|
||||||
log::trace!("hooking rtc callback");
|
log::trace!("hooking rtc callback");
|
||||||
if unsafe{RTC_CB}.is_some() {
|
if self.callback_sid.is_some() {
|
||||||
return Err(xous::Error::MemoryInUse)
|
return Err(xous::Error::MemoryInUse)
|
||||||
}
|
}
|
||||||
unsafe{RTC_CB = Some(cb)};
|
let sid = xous::create_server().expect("Couldn't create RTC callback server");
|
||||||
let sid_tuple: (u32, u32, u32, u32);
|
self.callback_sid = Some(sid);
|
||||||
if let Some(sid) = self.callback_sid {
|
let sid_tuple = sid.to_u32();
|
||||||
sid_tuple = sid.to_u32();
|
std::thread::spawn({
|
||||||
} else {
|
let sid = sid.clone();
|
||||||
let sid = xous::create_server().expect("Couldn't create RTC callback server");
|
let rtc_cb = Box::new(cb);
|
||||||
self.callback_sid = Some(sid);
|
move || {
|
||||||
sid_tuple = sid.to_u32();
|
loop {
|
||||||
xous::create_thread_4(rtc_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
let msg = xous::receive_message(sid).unwrap();
|
||||||
}
|
log::trace!("rtc callback got msg: {:?}", msg);
|
||||||
|
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();
|
||||||
|
rtc_cb.as_ref()(dt);
|
||||||
|
}
|
||||||
|
Some(Return::Drop) => {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
log::error!("got unrecognized message in rtc CB server, ignoring");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log::trace!("rtc callback server exiting");
|
||||||
|
xous::destroy_server(sid).expect("can't destroy my server on exit!"); }
|
||||||
|
});
|
||||||
|
//xous::create_thread_4(rtc_cb_server, sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize).unwrap();
|
||||||
xous::send_message(self.conn,
|
xous::send_message(self.conn,
|
||||||
Message::new_scalar(RtcOpcode::RegisterDateTimeCallback.to_usize().unwrap(),
|
Message::new_scalar(RtcOpcode::RegisterDateTimeCallback.to_usize().unwrap(),
|
||||||
sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize
|
sid_tuple.0 as usize, sid_tuple.1 as usize, sid_tuple.2 as usize, sid_tuple.3 as usize
|
||||||
|
|
@ -106,36 +121,3 @@ impl Drop for Rtc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rtc_cb_server(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
|
|
||||||
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);
|
|
||||||
log::trace!("rtc callback server started");
|
|
||||||
loop {
|
|
||||||
let msg = xous::receive_message(sid).unwrap();
|
|
||||||
log::trace!("rtc callback got msg: {:?}", msg);
|
|
||||||
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)
|
|
||||||
} else {
|
|
||||||
// callback happened after we unregistered
|
|
||||||
// this is a race condition, but it's also a harmless side effect
|
|
||||||
// we handle it by just ignoring the message
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Some(Return::Drop) => {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
log::error!("got unrecognized message in rtc CB server, ignoring");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
log::trace!("rtc callback server exiting");
|
|
||||||
xous::destroy_server(sid).expect("can't destroy my server on exit!");
|
|
||||||
}
|
|
||||||
|
|
@ -39,7 +39,6 @@ macro_rules! cmd_api {
|
||||||
|
|
||||||
use trng::*;
|
use trng::*;
|
||||||
/////////////////////////// Command shell integration
|
/////////////////////////// Command shell integration
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct CommonEnv {
|
pub struct CommonEnv {
|
||||||
llio: llio::Llio,
|
llio: llio::Llio,
|
||||||
i2c: llio::I2c,
|
i2c: llio::I2c,
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue