diff --git a/services/pddb/src/api.rs b/services/pddb/src/api.rs index c3df8a0ae..43cac7163 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -194,7 +194,7 @@ pub struct PddbKeyRequest { pub create_dict: bool, pub create_key: bool, pub alloc_hint: Option, // 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, } diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 603c2dca2..1f194758a 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -20,7 +20,7 @@ pub(crate) static REFCOUNT: AtomicU32 = AtomicU32::new(0); pub(crate) static POLLER_REFCOUNT: AtomicU32 = AtomicU32::new(0); #[derive(num_derive::FromPrimitive, num_derive::ToPrimitive, Debug)] -enum CbOp { +pub enum CbOp { Change, Quit } @@ -73,8 +73,7 @@ impl Drop for PddbMountPoller { pub struct Pddb { conn: CID, /// a SID that we can directly share with the PDDB server for the purpose of handling key change callbacks - cb_sid: SID, - cb_handle: Option>, + cb: Option<(SID, JoinHandle::<()>)>, /// 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, /// 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); 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 sid = xous::create_server().unwrap(); let keys: Arc >>> = 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 { conn, - cb_sid: sid, - cb_handle: Some(handle), + cb: None, keys, 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, /// 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. @@ -310,6 +313,14 @@ impl Pddb { xous_ipc::String::::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 { basis_specified: basis_name.is_some(), basis: xous_ipc::String::::from_str(&bname), @@ -319,7 +330,7 @@ impl Pddb { create_key, token: None, 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}, }; let mut buf = Buffer::into_buf(request) @@ -380,6 +391,11 @@ impl Pddb { xous_ipc::String::::new() }; + let cb_sid = if let Some((sid, _handle)) = &self.cb { + Some(sid.to_array()) + } else { + None + }; let request = PddbKeyRequest { basis_specified: basis_name.is_some(), basis: xous_ipc::String::::from_str(&bname), @@ -389,7 +405,7 @@ impl Pddb { create_key: false, token: None, result: PddbRequestCode::Uninit, - cb_sid: self.cb_sid.to_array(), + cb_sid, alloc_hint: None, }; let mut buf = Buffer::into_buf(request) @@ -418,6 +434,11 @@ impl Pddb { xous_ipc::String::::new() }; + let cb_sid = if let Some((sid, _handle)) = &self.cb { + Some(sid.to_array()) + } else { + None + }; let request = PddbKeyRequest { basis_specified: basis_name.is_some(), basis: xous_ipc::String::::from_str(&bname), @@ -427,7 +448,7 @@ impl Pddb { create_key: false, token: None, result: PddbRequestCode::Uninit, - cb_sid: self.cb_sid.to_array(), + cb_sid, alloc_hint: None, }; let mut buf = Buffer::into_buf(request) @@ -607,10 +628,10 @@ impl Pddb { impl Drop for Pddb { fn drop(&mut self) { - let cid = xous::connect(self.cb_sid).unwrap(); - send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); - unsafe{xous::disconnect(cid).ok();} - if let Some(handle) = self.cb_handle.take() { + if let Some((cb_sid, handle)) = self.cb.take() { + let cid = xous::connect(cb_sid).unwrap(); + send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); + unsafe{xous::disconnect(cid).ok();} handle.join().expect("couldn't terminate callback helper thread"); } diff --git a/services/pddb/src/main.rs b/services/pddb/src/main.rs index c9f7b6a2d..06d612071 100644 --- a/services/pddb/src/main.rs +++ b/services/pddb/src/main.rs @@ -410,7 +410,7 @@ struct TokenRecord { pub key: String, pub basis: Option, pub alloc_hint: Option, - pub conn: xous::CID, // callback connection + pub conn: Option, // callback connection, if one was specified } #[xous::xous_main] @@ -683,6 +683,7 @@ fn xmain() -> ! { Some(Opcode::CloseBasis) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut mgmt = buffer.to_original::().unwrap(); + notify_of_disconnect(&mut pddb_os, &token_dict, &mut basis_cache); match mgmt.code { PddbRequestCode::Close => { 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) => { let mut buffer = unsafe { Buffer::from_memory_message_mut(msg.body.memory_message_mut().unwrap()) }; let mut mgmt = buffer.to_original::().unwrap(); + notify_of_disconnect(&mut pddb_os, &token_dict, &mut basis_cache); match mgmt.code { PddbRequestCode::Delete => { 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. 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 { dict: String::from(dict), key: String::from(key), @@ -789,15 +795,23 @@ fn xmain() -> ! { if let Some(rec) = token_dict.remove(&token) { // now check if we can safely disconnect and recycle our connection number. // This is important because we can only have 32 outgoing connections... - let mut has_cid = false; - for r in token_dict.values() { - if r.conn == rec.conn { - has_cid = true; - break; + if let Some(conn_to_remove) = rec.conn { + let mut still_needs_cid = false; + for r in token_dict.values() { + // check through the remaining dictionary values to see if they have a connection that is the same as our number + if let Some(existing_conn) = r.conn { + if existing_conn == conn_to_remove { + still_needs_cid = true; + break; + } + } } - } - if !has_cid { - unsafe{xous::disconnect(rec.conn).expect("couldn't disconnect from callback server")}; + // if nobody else had my connection number, disconnect it. + if !still_needs_cid { + 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"); @@ -1392,3 +1406,33 @@ pub(crate) fn hw_testcase(pddb_os: &mut PddbOs) { pddb_os.dbg_dump(Some("manual".to_string()), None); } +fn notify_of_disconnect(pddb_os: &mut PddbOs, token_dict: &HashMap::, 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); + } + } + } + } +} \ No newline at end of file