From 803667ba28ad177b976fc7dbd046cacf76d4ee04 Mon Sep 17 00:00:00 2001 From: Sean Cross Date: Tue, 31 May 2022 14:51:21 +0800 Subject: [PATCH 1/5] xous: add a small comment to nameserver Signed-off-by: Sean Cross --- xous-rs/src/services/nameserver.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/xous-rs/src/services/nameserver.rs b/xous-rs/src/services/nameserver.rs index ec7b3dc12..e37a6e649 100644 --- a/xous-rs/src/services/nameserver.rs +++ b/xous-rs/src/services/nameserver.rs @@ -53,6 +53,7 @@ impl ConnectRequest { } } +/// Request a connection to the nameserver-managed `name`. pub fn connect(name: &str) -> Option { let mut request = ConnectRequest::new(name)?; let ns_cid = nameserver(); @@ -96,6 +97,7 @@ pub fn connect(name: &str) -> Option { } } +/// Return the connection ID of the nameserver pub(crate) fn nameserver() -> crate::CID { static NAMESERVER_CID: AtomicU32 = AtomicU32::new(0); From a1fc015541b99b78ae30a202ddb1e68ef5c919bd Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 31 May 2022 23:54:16 +0800 Subject: [PATCH 2/5] use interior mutability to handle callbacks this eases a mutability requirement for the PDDB object --- services/pddb/src/lib.rs | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 65d6f47ee..b53fbd16a 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -18,6 +18,7 @@ use std::thread::JoinHandle; use core::sync::atomic::{AtomicU32, Ordering}; pub(crate) static 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)] pub enum CbOp { @@ -73,7 +74,8 @@ 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: Option<(SID, JoinHandle::<()>)>, + cb: RefCell>, + cb_handle: RefCell>>, /// 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. @@ -95,13 +97,14 @@ impl Pddb { let keys: Arc >>> = Arc::new(Mutex::new(HashMap::new())); Pddb { conn, - cb: None, + cb: RefCell::new(None), + cb_handle: RefCell::new(None), keys, trng: trng::Trng::new(&xns).unwrap(), } } - fn ensure_async_responder(&mut self) { - if self.cb.is_none() { + fn ensure_async_responder(&self) { + if self.cb.borrow().is_none() { let sid = xous::create_server().unwrap(); let handle = thread::spawn({ let keys = Arc::clone(&self.keys); @@ -128,7 +131,8 @@ impl Pddb { 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, @@ -296,7 +300,7 @@ impl Pddb { /// 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 /// 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, key_changed_cb: Option) -> Result { if key_name.len() > (KEY_NAME_LEN - 1) { return Err(Error::new(ErrorKind::InvalidInput, "key name too long")); @@ -316,11 +320,15 @@ impl Pddb { if key_changed_cb.is_some() { 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()) } else { None }; + self.cb.replace(maybe_cb); + let request = PddbKeyRequest { basis_specified: basis_name.is_some(), basis: xous_ipc::String::::from_str(&bname), @@ -375,7 +383,7 @@ impl Pddb { } /// 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) { return Err(Error::new(ErrorKind::InvalidInput, "key name too long")); } @@ -391,11 +399,14 @@ impl Pddb { xous_ipc::String::::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()) } else { None }; + self.cb.replace(maybe_cb); + let request = PddbKeyRequest { basis_specified: basis_name.is_some(), basis: xous_ipc::String::::from_str(&bname), @@ -421,7 +432,7 @@ impl Pddb { } } /// 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) { return Err(Error::new(ErrorKind::InvalidInput, "dictionary name too long")); } @@ -434,11 +445,14 @@ impl Pddb { xous_ipc::String::::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()) } else { None }; + self.cb.replace(maybe_cb); + let request = PddbKeyRequest { basis_specified: basis_name.is_some(), basis: xous_ipc::String::::from_str(&bname), @@ -643,7 +657,8 @@ impl Pddb { impl Drop for Pddb { 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(); send_message(cid, Message::new_blocking_scalar(CbOp::Quit.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); unsafe{xous::disconnect(cid).ok();} From 3b1629744c222bb2aa8a56e69de1f1e6adecb725 Mon Sep 17 00:00:00 2001 From: bunnie Date: Tue, 31 May 2022 23:55:12 +0800 Subject: [PATCH 3/5] fix warnings that result from the PDDB mutability change --- services/shellchat/src/cmds/wlan.rs | 2 +- services/status/src/time.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/shellchat/src/cmds/wlan.rs b/services/shellchat/src/cmds/wlan.rs index 8c704f58c..8deeed431 100644 --- a/services/shellchat/src/cmds/wlan.rs +++ b/services/shellchat/src/cmds/wlan.rs @@ -84,7 +84,7 @@ impl<'a> ShellCmdApi<'a> for Wlan { }; } "save" => { - let mut pddb = pddb::Pddb::new(); + let pddb = pddb::Pddb::new(); if let Some(ssid) = &self.current_ssid { if let Some(pass) = &self.current_pass { match pddb.get( diff --git a/services/status/src/time.rs b/services/status/src/time.rs index dca00aa7d..4a9fe6789 100644 --- a/services/status/src/time.rs +++ b/services/status/src/time.rs @@ -241,7 +241,7 @@ pub fn start_time_server() { } // once the PDDB is mounted, read in the time zone offsets and then restart the // loop handler using the offsets. - let mut offset_handle = Pddb::new(); + let offset_handle = Pddb::new(); let mut offset_key = offset_handle.get( TIME_SERVER_DICT, TIME_SERVER_UTC_OFFSET, @@ -249,7 +249,7 @@ pub fn start_time_server() { Some(8), None:: ).expect("couldn't open UTC offset key"); - let mut tz_handle = Pddb::new(); + let tz_handle = Pddb::new(); let mut tz_key = tz_handle.get( TIME_SERVER_DICT, TIME_SERVER_TZ_OFFSET, @@ -496,7 +496,7 @@ pub fn start_time_ux(sid: xous::SID) { modals.show_notification(t!("stats.please_mount", xous::LANG), None).expect("couldn't show notification"); continue; } - let mut tz_set_handle = pddb::Pddb::new(); + let tz_set_handle = pddb::Pddb::new(); let mut tz_set = false; let mut tz_offset_ms = 0i64; let maybe_tz_set_key = tz_set_handle.get( From 2eee81b218559936cb5e1f39abbff89b4319f705 Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 Jun 2022 00:06:55 +0800 Subject: [PATCH 4/5] fix another errant mut that wasn't needed in pddb --- services/net/src/connection_manager.rs | 2 +- services/pddb/src/lib.rs | 4 ++-- services/shellchat/src/cmds/wlan.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/services/net/src/connection_manager.rs b/services/net/src/connection_manager.rs index 20586e0a7..9037e9043 100644 --- a/services/net/src/connection_manager.rs +++ b/services/net/src/connection_manager.rs @@ -62,7 +62,7 @@ pub(crate) fn connection_manager(sid: xous::SID, activity_interval: Arc) -> Result> { + pub fn list_keys(&self, dict_name: &str, basis_name: Option<&str>) -> Result> { if dict_name.len() > (DICT_NAME_LEN - 1) { return Err(Error::new(ErrorKind::InvalidInput, "dictionary name too long")); } @@ -557,7 +557,7 @@ impl Pddb { } - pub fn list_dict(&mut self, basis_name: Option<&str>) -> Result> { + pub fn list_dict(&self, basis_name: Option<&str>) -> Result> { let bname = if let Some(bname) = basis_name { if bname.len() > BASIS_NAME_LEN - 1 { return Err(Error::new(ErrorKind::InvalidInput, "basis name too long")); diff --git a/services/shellchat/src/cmds/wlan.rs b/services/shellchat/src/cmds/wlan.rs index 8deeed431..2b590a59b 100644 --- a/services/shellchat/src/cmds/wlan.rs +++ b/services/shellchat/src/cmds/wlan.rs @@ -121,7 +121,7 @@ impl<'a> ShellCmdApi<'a> for Wlan { } } "known" => { - let mut pddb = pddb::Pddb::new(); + let pddb = pddb::Pddb::new(); match pddb.list_keys(net::AP_DICT_NAME, None) { Ok(list) => { write!(ret, "Saved network configs:\n").unwrap(); From 5337d34d45f65de540c7493fbda21812c345138c Mon Sep 17 00:00:00 2001 From: bunnie Date: Wed, 1 Jun 2022 00:32:49 +0800 Subject: [PATCH 5/5] fix warnings in hosted mode --- services/shellchat/src/cmds/pddb_cmd.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/shellchat/src/cmds/pddb_cmd.rs b/services/shellchat/src/cmds/pddb_cmd.rs index 0726905b8..762d14f6b 100644 --- a/services/shellchat/src/cmds/pddb_cmd.rs +++ b/services/shellchat/src/cmds/pddb_cmd.rs @@ -219,7 +219,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { "test" => { let bname = tokens.next(); // zero-length key test - let mut test_handle = pddb::Pddb::new(); + let test_handle = pddb::Pddb::new(); // build a key, but don't write to it. let _ = test_handle.get( "test", @@ -254,7 +254,7 @@ impl<'a> ShellCmdApi<'a> for PddbCmd { log::info!("finished dict delete with zero-length key"); // seek test - a bunch of terrible, handcrafted test cases to exercise Start, Current, End cases of seeking. - let mut test_handle = pddb::Pddb::new(); + let test_handle = pddb::Pddb::new(); // build a key, but don't write to it. let mut seekwrite = test_handle.get( "test",