wire up PDDB key unmount callbacks (oops)

This commit is contained in:
bunnie 2022-04-07 17:37:25 +08:00
parent 12ced30f40
commit 52d56f39f1
3 changed files with 114 additions and 49 deletions

View file

@ -194,7 +194,7 @@ pub struct PddbKeyRequest {
pub create_dict: bool, pub create_dict: bool,
pub create_key: bool, pub create_key: bool,
pub alloc_hint: Option<u64>, // this is a usize but for IPC we must have defined memory sizes, so we pick the big option. pub alloc_hint: Option<u64>, // this is a usize but for IPC we must have defined memory sizes, so we pick the big option.
pub cb_sid: [u32; 4], pub cb_sid: Option<[u32; 4]>,
pub result: PddbRequestCode, pub result: PddbRequestCode,
} }

View file

@ -20,7 +20,7 @@ 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);
#[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)]
enum CbOp { pub enum CbOp {
Change, Change,
Quit Quit
} }
@ -73,8 +73,7 @@ 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_sid: SID, cb: Option<(SID, JoinHandle::<()>)>,
cb_handle: 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.
@ -93,41 +92,45 @@ impl Pddb {
REFCOUNT.fetch_add(1, Ordering::Relaxed); REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_names::XousNames::new().unwrap(); let xns = xous_names::XousNames::new().unwrap();
let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("can't connect to Pddb server"); let conn = xns.request_connection_blocking(api::SERVER_NAME_PDDB).expect("can't connect to Pddb server");
let sid = xous::create_server().unwrap();
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()));
let handle = thread::spawn({
let keys = Arc::clone(&keys);
let sid = sid.clone();
move || {
loop {
let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(CbOp::Change) => msg_scalar_unpack!(msg, t0, t1, t2, _, {
let token: ApiToken = [t0 as u32, t1 as u32, t2 as u32];
if let Some(cb) = keys.lock().unwrap().get(&token) {
cb();
} else {
log::warn!("Key changed but no callback was hooked to receive it");
}
}),
Some(CbOp::Quit) => { // blocking scalar
xous::return_scalar(msg.sender, 0).unwrap();
break;
},
_ =>log::warn!("Got unknown opcode: {:?}", msg),
}
}
xous::destroy_server(sid).unwrap();
}
});
Pddb { Pddb {
conn, conn,
cb_sid: sid, cb: None,
cb_handle: Some(handle),
keys, keys,
trng: trng::Trng::new(&xns).unwrap(), trng: trng::Trng::new(&xns).unwrap(),
} }
} }
fn ensure_async_responder(&mut self) {
if self.cb.is_none() {
let sid = xous::create_server().unwrap();
let handle = thread::spawn({
let keys = Arc::clone(&self.keys);
let sid = sid.clone();
move || {
loop {
let msg = xous::receive_message(sid).unwrap();
match FromPrimitive::from_usize(msg.body.id()) {
Some(CbOp::Change) => msg_scalar_unpack!(msg, t0, t1, t2, _, {
let token: ApiToken = [t0 as u32, t1 as u32, t2 as u32];
if let Some(cb) = keys.lock().unwrap().get(&token) {
cb();
} else {
log::warn!("Key changed but no callback was hooked to receive it");
}
}),
Some(CbOp::Quit) => { // blocking scalar
xous::return_scalar(msg.sender, 0).unwrap();
break;
},
_ =>log::warn!("Got unknown opcode: {:?}", msg),
}
}
xous::destroy_server(sid).unwrap();
}
});
self.cb = Some((sid, 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,
/// A random interval between 1 and 2 seconds is chosen for the poll wait time. Randomizing the waiting time /// A random interval between 1 and 2 seconds is chosen for the poll wait time. Randomizing the waiting time
/// helps to level out the task scheduler in the case that many threads are waiting on the PDDB simultaneously. /// helps to level out the task scheduler in the case that many threads are waiting on the PDDB simultaneously.
@ -310,6 +313,14 @@ impl Pddb {
xous_ipc::String::<BASIS_NAME_LEN>::new() xous_ipc::String::<BASIS_NAME_LEN>::new()
}; };
if key_changed_cb.is_some() {
self.ensure_async_responder();
}
let cb_sid = if let Some((sid, _handle)) = &self.cb {
Some(sid.to_array())
} else {
None
};
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),
@ -319,7 +330,7 @@ impl Pddb {
create_key, create_key,
token: None, token: None,
result: PddbRequestCode::Uninit, result: PddbRequestCode::Uninit,
cb_sid: self.cb_sid.to_array(), cb_sid,
alloc_hint: if let Some(a) = alloc_hint {Some(a as u64)} else {None}, alloc_hint: if let Some(a) = alloc_hint {Some(a as u64)} else {None},
}; };
let mut buf = Buffer::into_buf(request) let mut buf = Buffer::into_buf(request)
@ -380,6 +391,11 @@ 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 {
Some(sid.to_array())
} else {
None
};
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),
@ -389,7 +405,7 @@ impl Pddb {
create_key: false, create_key: false,
token: None, token: None,
result: PddbRequestCode::Uninit, result: PddbRequestCode::Uninit,
cb_sid: self.cb_sid.to_array(), cb_sid,
alloc_hint: None, alloc_hint: None,
}; };
let mut buf = Buffer::into_buf(request) let mut buf = Buffer::into_buf(request)
@ -418,6 +434,11 @@ 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 {
Some(sid.to_array())
} else {
None
};
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),
@ -427,7 +448,7 @@ impl Pddb {
create_key: false, create_key: false,
token: None, token: None,
result: PddbRequestCode::Uninit, result: PddbRequestCode::Uninit,
cb_sid: self.cb_sid.to_array(), cb_sid,
alloc_hint: None, alloc_hint: None,
}; };
let mut buf = Buffer::into_buf(request) let mut buf = Buffer::into_buf(request)
@ -607,10 +628,10 @@ impl Pddb {
impl Drop for Pddb { impl Drop for Pddb {
fn drop(&mut self) { fn drop(&mut self) {
let cid = xous::connect(self.cb_sid).unwrap(); if let Some((cb_sid, handle)) = self.cb.take() {
send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); let cid = xous::connect(cb_sid).unwrap();
unsafe{xous::disconnect(cid).ok();} send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
if let Some(handle) = self.cb_handle.take() { unsafe{xous::disconnect(cid).ok();}
handle.join().expect("couldn't terminate callback helper thread"); handle.join().expect("couldn't terminate callback helper thread");
} }

View file

@ -410,7 +410,7 @@ struct TokenRecord {
pub key: String, pub key: String,
pub basis: Option<String>, pub basis: Option<String>,
pub alloc_hint: Option<usize>, pub alloc_hint: Option<usize>,
pub conn: xous::CID, // callback connection pub conn: Option<xous::CID>, // callback connection, if one was specified
} }
#[xous::xous_main] #[xous::xous_main]
@ -683,6 +683,7 @@ fn xmain() -> ! {
Some(Opcode::CloseBasis) => { Some(Opcode::CloseBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
notify_of_disconnect(&mut pddb_os, &token_dict, &mut basis_cache);
match mgmt.code { match mgmt.code {
PddbRequestCode::Close => { PddbRequestCode::Close => {
match basis_cache.basis_unmount(&mut pddb_os, mgmt.name.as_str().expect("name is not valid utf-8")) { match basis_cache.basis_unmount(&mut pddb_os, mgmt.name.as_str().expect("name is not valid utf-8")) {
@ -702,6 +703,7 @@ fn xmain() -> ! {
Some(Opcode::DeleteBasis) => { Some(Opcode::DeleteBasis) => {
let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) };
let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap(); let mut mgmt = buffer.to_original::<PddbBasisRequest, _>().unwrap();
notify_of_disconnect(&mut pddb_os, &token_dict, &mut basis_cache);
match mgmt.code { match mgmt.code {
PddbRequestCode::Delete => { PddbRequestCode::Delete => {
match basis_cache.basis_delete(&mut pddb_os, mgmt.name.as_str().expect("name is not valid utf-8")) { match basis_cache.basis_delete(&mut pddb_os, mgmt.name.as_str().expect("name is not valid utf-8")) {
@ -771,7 +773,11 @@ fn xmain() -> ! {
} }
// at this point, we have established a basis/dict/key tuple. // at this point, we have established a basis/dict/key tuple.
let token: ApiToken = [pddb_os.trng_u32(), pddb_os.trng_u32(), pddb_os.trng_u32()]; let token: ApiToken = [pddb_os.trng_u32(), pddb_os.trng_u32(), pddb_os.trng_u32()];
let cid = xous::connect(xous::SID::from_array(req.cb_sid)).expect("couldn't connect for callback"); let cid = if let Some(cb_sid) = req.cb_sid {
Some(xous::connect(xous::SID::from_array(cb_sid)).expect("couldn't connect for callback"))
} else {
None
};
let token_record = TokenRecord { let token_record = TokenRecord {
dict: String::from(dict), dict: String::from(dict),
key: String::from(key), key: String::from(key),
@ -789,15 +795,23 @@ fn xmain() -> ! {
if let Some(rec) = token_dict.remove(&token) { if let Some(rec) = token_dict.remove(&token) {
// now check if we can safely disconnect and recycle our connection number. // now check if we can safely disconnect and recycle our connection number.
// This is important because we can only have 32 outgoing connections... // This is important because we can only have 32 outgoing connections...
let mut has_cid = false; if let Some(conn_to_remove) = rec.conn {
for r in token_dict.values() { let mut still_needs_cid = false;
if r.conn == rec.conn { for r in token_dict.values() {
has_cid = true; // check through the remaining dictionary values to see if they have a connection that is the same as our number
break; if let Some(existing_conn) = r.conn {
if existing_conn == conn_to_remove {
still_needs_cid = true;
break;
}
}
} }
} // if nobody else had my connection number, disconnect it.
if !has_cid { if !still_needs_cid {
unsafe{xous::disconnect(rec.conn).expect("couldn't disconnect from callback server")}; unsafe{xous::disconnect(conn_to_remove).expect("couldn't disconnect from callback server")};
}
} else {
// if there was no/never a connection allocated, there's no connection to remove. do nothing.
} }
} }
xous::return_scalar(msg.sender, 1).expect("couldn't ack KeyDrop"); xous::return_scalar(msg.sender, 1).expect("couldn't ack KeyDrop");
@ -1392,3 +1406,33 @@ pub(crate) fn hw_testcase(pddb_os: &mut PddbOs) {
pddb_os.dbg_dump(Some("manual".to_string()), None); pddb_os.dbg_dump(Some("manual".to_string()), None);
} }
fn notify_of_disconnect(pddb_os: &mut PddbOs, token_dict: &HashMap::<ApiToken, TokenRecord>, basis_cache: &mut BasisCache) {
// 1. search to see if any of the active tokens are are in our token_dict
// 2. notify them of the disconnect, if there is a callback set.
for (api_key, entry) in token_dict.iter() {
if let Some(cb) = entry.conn {
match basis_cache.key_attributes(pddb_os, &entry.dict, &entry.key, entry.basis.as_deref()) {
Ok(_) => {
match send_message(cb, Message::new_scalar(
pddb::CbOp::Change.to_usize().unwrap(),
api_key[0] as _,
api_key[1] as _,
api_key[2] as _,
0,
)) {
Err(e) => {
log::warn!("Callback on {}:{} for basis removal failed: {:?}", &entry.dict, &entry.key, e);
},
_ => {
log::debug!("Callback on {}:{} for basis removal success", &entry.dict, &entry.key);
}
}
},
Err(_) => {
// do nothing. It's probably not right that a key doesn't exist that we don't have in our records, but don't crash the system.
log::warn!("Disconnect basis inconsistent state, {}:{} not found", &entry.dict, &entry.key);
}
}
}
}
}