mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
use interior mutability to handle callbacks
this eases a mutability requirement for the PDDB object
This commit is contained in:
parent
59550978f4
commit
d9cda3a715
1 changed files with 27 additions and 12 deletions
|
|
@ -18,6 +18,7 @@ use std::thread::JoinHandle;
|
||||||
use core::sync::atomic::{AtomicU32, Ordering};
|
use core::sync::atomic::{AtomicU32, Ordering};
|
||||||
pub(crate) static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
pub(crate) static REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
||||||
pub(crate) static POLLER_REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
pub(crate) static POLLER_REFCOUNT: AtomicU32 = AtomicU32::new(0);
|
||||||
|
use std::cell::RefCell;
|
||||||
|
|
||||||
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
|
||||||
pub enum CbOp {
|
pub enum CbOp {
|
||||||
|
|
@ -73,7 +74,8 @@ impl Drop for PddbMountPoller {
|
||||||
pub struct Pddb {
|
pub struct Pddb {
|
||||||
conn: CID,
|
conn: CID,
|
||||||
/// a SID that we can directly share with the PDDB server for the purpose of handling key change callbacks
|
/// a SID that we can directly share with the PDDB server for the purpose of handling key change callbacks
|
||||||
cb: Option<(SID, JoinHandle::<()>)>,
|
cb: RefCell<Option<SID>>,
|
||||||
|
cb_handle: RefCell<Option<JoinHandle::<()>>>,
|
||||||
/// Handle key change updates. The general intent is that the closure implements a
|
/// Handle key change updates. The general intent is that the closure implements a
|
||||||
/// `send` of a message to the server to deal with a key change appropriately,
|
/// `send` of a message to the server to deal with a key change appropriately,
|
||||||
/// but no mutable data is allowed within the closure itself due to safety problems.
|
/// but no mutable data is allowed within the closure itself due to safety problems.
|
||||||
|
|
@ -95,13 +97,14 @@ impl Pddb {
|
||||||
let keys: Arc<Mutex<HashMap<ApiToken, Box<dyn Fn() + 'static + Send> >>> = Arc::new(Mutex::new(HashMap::new()));
|
let keys: Arc<Mutex<HashMap<ApiToken, Box<dyn Fn() + 'static + Send> >>> = Arc::new(Mutex::new(HashMap::new()));
|
||||||
Pddb {
|
Pddb {
|
||||||
conn,
|
conn,
|
||||||
cb: None,
|
cb: RefCell::new(None),
|
||||||
|
cb_handle: RefCell::new(None),
|
||||||
keys,
|
keys,
|
||||||
trng: trng::Trng::new(&xns).unwrap(),
|
trng: trng::Trng::new(&xns).unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn ensure_async_responder(&mut self) {
|
fn ensure_async_responder(&self) {
|
||||||
if self.cb.is_none() {
|
if self.cb.borrow().is_none() {
|
||||||
let sid = xous::create_server().unwrap();
|
let sid = xous::create_server().unwrap();
|
||||||
let handle = thread::spawn({
|
let handle = thread::spawn({
|
||||||
let keys = Arc::clone(&self.keys);
|
let keys = Arc::clone(&self.keys);
|
||||||
|
|
@ -128,7 +131,8 @@ impl Pddb {
|
||||||
xous::destroy_server(sid).unwrap();
|
xous::destroy_server(sid).unwrap();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
self.cb = Some((sid, handle));
|
self.cb.replace(Some(sid));
|
||||||
|
self.cb_handle.replace(Some(handle));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// This blocks until the PDDB is mounted by the end user. If `None` is specified for the poll_interval_ms,
|
/// This blocks until the PDDB is mounted by the end user. If `None` is specified for the poll_interval_ms,
|
||||||
|
|
@ -296,7 +300,7 @@ impl Pddb {
|
||||||
/// is perfectly fine, it just has a potential performance impact, especially for very large keys.
|
/// is perfectly fine, it just has a potential performance impact, especially for very large keys.
|
||||||
/// `key_changed_cb` is a static function meant to initiate a message to a server in case the key in question
|
/// `key_changed_cb` is a static function meant to initiate a message to a server in case the key in question
|
||||||
/// goes away due to a basis locking.
|
/// goes away due to a basis locking.
|
||||||
pub fn get(&mut self, dict_name: &str, key_name: &str, basis_name: Option<&str>,
|
pub fn get(&self, dict_name: &str, key_name: &str, basis_name: Option<&str>,
|
||||||
create_dict: bool, create_key: bool, alloc_hint: Option<usize>, key_changed_cb: Option<impl Fn() + 'static + Send>) -> Result<PddbKey> {
|
create_dict: bool, create_key: bool, alloc_hint: Option<usize>, key_changed_cb: Option<impl Fn() + 'static + Send>) -> Result<PddbKey> {
|
||||||
if key_name.len() > (KEY_NAME_LEN - 1) {
|
if key_name.len() > (KEY_NAME_LEN - 1) {
|
||||||
return Err(Error::new(ErrorKind::InvalidInput, "key name too long"));
|
return Err(Error::new(ErrorKind::InvalidInput, "key name too long"));
|
||||||
|
|
@ -316,11 +320,15 @@ impl Pddb {
|
||||||
if key_changed_cb.is_some() {
|
if key_changed_cb.is_some() {
|
||||||
self.ensure_async_responder();
|
self.ensure_async_responder();
|
||||||
}
|
}
|
||||||
let cb_sid = if let Some((sid, _handle)) = &self.cb {
|
|
||||||
|
let maybe_cb = self.cb.take();
|
||||||
|
let cb_sid = if let Some(sid) = maybe_cb {
|
||||||
Some(sid.to_array())
|
Some(sid.to_array())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
self.cb.replace(maybe_cb);
|
||||||
|
|
||||||
let request = PddbKeyRequest {
|
let request = PddbKeyRequest {
|
||||||
basis_specified: basis_name.is_some(),
|
basis_specified: basis_name.is_some(),
|
||||||
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
|
@ -375,7 +383,7 @@ impl Pddb {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// deletes a key within the dictionary
|
/// deletes a key within the dictionary
|
||||||
pub fn delete_key(&mut self, dict_name: &str, key_name: &str, basis_name: Option<&str>) -> Result<()> {
|
pub fn delete_key(&self, dict_name: &str, key_name: &str, basis_name: Option<&str>) -> Result<()> {
|
||||||
if key_name.len() > (KEY_NAME_LEN - 1) {
|
if key_name.len() > (KEY_NAME_LEN - 1) {
|
||||||
return Err(Error::new(ErrorKind::InvalidInput, "key name too long"));
|
return Err(Error::new(ErrorKind::InvalidInput, "key name too long"));
|
||||||
}
|
}
|
||||||
|
|
@ -391,11 +399,14 @@ impl Pddb {
|
||||||
xous_ipc::String::<BASIS_NAME_LEN>::new()
|
xous_ipc::String::<BASIS_NAME_LEN>::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let cb_sid = if let Some((sid, _handle)) = &self.cb {
|
let maybe_cb = self.cb.take();
|
||||||
|
let cb_sid = if let Some(sid) = maybe_cb {
|
||||||
Some(sid.to_array())
|
Some(sid.to_array())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
self.cb.replace(maybe_cb);
|
||||||
|
|
||||||
let request = PddbKeyRequest {
|
let request = PddbKeyRequest {
|
||||||
basis_specified: basis_name.is_some(),
|
basis_specified: basis_name.is_some(),
|
||||||
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
|
@ -421,7 +432,7 @@ impl Pddb {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// deletes the entire dictionary
|
/// deletes the entire dictionary
|
||||||
pub fn delete_dict(&mut self, dict_name: &str, basis_name: Option<&str>) -> Result<()> {
|
pub fn delete_dict(&self, dict_name: &str, basis_name: Option<&str>) -> Result<()> {
|
||||||
if dict_name.len() > (DICT_NAME_LEN - 1) {
|
if dict_name.len() > (DICT_NAME_LEN - 1) {
|
||||||
return Err(Error::new(ErrorKind::InvalidInput, "dictionary name too long"));
|
return Err(Error::new(ErrorKind::InvalidInput, "dictionary name too long"));
|
||||||
}
|
}
|
||||||
|
|
@ -434,11 +445,14 @@ impl Pddb {
|
||||||
xous_ipc::String::<BASIS_NAME_LEN>::new()
|
xous_ipc::String::<BASIS_NAME_LEN>::new()
|
||||||
};
|
};
|
||||||
|
|
||||||
let cb_sid = if let Some((sid, _handle)) = &self.cb {
|
let maybe_cb = self.cb.take();
|
||||||
|
let cb_sid = if let Some(sid) = maybe_cb {
|
||||||
Some(sid.to_array())
|
Some(sid.to_array())
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
self.cb.replace(maybe_cb);
|
||||||
|
|
||||||
let request = PddbKeyRequest {
|
let request = PddbKeyRequest {
|
||||||
basis_specified: basis_name.is_some(),
|
basis_specified: basis_name.is_some(),
|
||||||
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
basis: xous_ipc::String::<BASIS_NAME_LEN>::from_str(&bname),
|
||||||
|
|
@ -643,7 +657,8 @@ impl Pddb {
|
||||||
|
|
||||||
impl Drop for Pddb {
|
impl Drop for Pddb {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if let Some((cb_sid, handle)) = self.cb.take() {
|
if let Some(cb_sid) = self.cb.take() {
|
||||||
|
let handle = self.cb_handle.take().unwrap(); // we guarantee this is always set when cb is set
|
||||||
let cid = xous::connect(cb_sid).unwrap();
|
let cid = xous::connect(cb_sid).unwrap();
|
||||||
send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
|
||||||
unsafe{xous::disconnect(cid).ok();}
|
unsafe{xous::disconnect(cid).ok();}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue