diff --git a/RELEASE-v0.8.md b/RELEASE-v0.8.md index 32d1b659d..88f04faba 100644 --- a/RELEASE-v0.8.md +++ b/RELEASE-v0.8.md @@ -295,6 +295,19 @@ impl Drop for MyServer { } } +// reference counting implementation for servers that can be cloned or have multiple instances in a thread +//REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); +impl Drop for Codec { + fn drop(&mut self) { + // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } + } +} + ``` ### main.rs diff --git a/services/codec/src/lib.rs b/services/codec/src/lib.rs index e3b42f6f4..b7a94bbb2 100644 --- a/services/codec/src/lib.rs +++ b/services/codec/src/lib.rs @@ -13,6 +13,7 @@ pub struct Codec { } impl Codec { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_CODEC).expect("Can't connect to Codec server"); Ok(Codec { conn, @@ -76,11 +77,14 @@ impl Codec { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Codec { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 83c40359f..311fa86d8 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -45,6 +45,7 @@ pub struct Com { } impl Com { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_COM).expect("Can't connect to COM server"); Ok(Com { conn, @@ -239,6 +240,8 @@ impl Com { // note to future self: add other event listener registrations (such as network events) here } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Com { fn drop(&mut self) { // if we have callbacks, destroy the battstats callback server @@ -254,6 +257,8 @@ impl Drop for Com { } // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index 985196572..ee7a050a6 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -18,6 +18,7 @@ pub struct Gam { } impl Gam { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_GAM).expect("Can't connect to GAM"); Ok(Gam { conn, @@ -161,10 +162,13 @@ impl Gam { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Gam { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index e4e063039..c19cb0f53 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -18,6 +18,7 @@ pub struct Gfx { } impl Gfx { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_GFX).expect("Can't connect to GFX"); Ok(Gfx { conn, @@ -142,9 +143,13 @@ impl Gfx { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Gfx { fn drop(&mut self) { - // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} + // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } -} \ No newline at end of file +} diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index 3b28b1f89..396e0453e 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -220,6 +220,7 @@ pub struct ImeFrontEnd { } impl ImeFrontEnd { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(SERVER_NAME_IME_FRONT).expect("Can't connect to IMEF"); Ok(ImeFrontEnd { cid: conn, @@ -227,6 +228,8 @@ impl ImeFrontEnd { }) } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for ImeFrontEnd { fn drop(&mut self) { if let Some(sid) = self.callback_sid.take() { @@ -240,8 +243,9 @@ impl Drop for ImeFrontEnd { unsafe{xous::disconnect(cid).unwrap();} xous::destroy_server(sid).unwrap(); } - - unsafe{xous::disconnect(self.cid).unwrap();} + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.cid).unwrap();} + } } } diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index dc152317c..824fcf334 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -19,6 +19,7 @@ pub struct Keyboard { } impl Keyboard { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_KBD).expect("Can't connect to KBD"); Ok(Keyboard { conn, @@ -82,6 +83,8 @@ impl Keyboard { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Keyboard { fn drop(&mut self) { // if we have callbacks, destroy the callback server @@ -108,7 +111,9 @@ impl Drop for Keyboard { } // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } diff --git a/services/llio/src/lib.rs b/services/llio/src/lib.rs index ef39b0c56..06abc2904 100644 --- a/services/llio/src/lib.rs +++ b/services/llio/src/lib.rs @@ -41,6 +41,7 @@ pub struct Llio { } impl Llio { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_LLIO).expect("Can't connect to LLIO"); let i2c_conn = xns.request_connection_blocking(api::SERVER_NAME_I2C).expect("Can't connect to I2C"); Ok(Llio { @@ -475,6 +476,8 @@ fn drop_conn(sid: xous::SID) { Message::new_scalar(EventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); unsafe{xous::disconnect(cid).unwrap();} } +use core::sync::atomic::AtomicU32; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Llio { fn drop(&mut self) { if let Some(sid) = self.i2c_sid.take() { @@ -492,7 +495,9 @@ impl Drop for Llio { if let Some(sid) = self.gpio_sid.take() { drop_conn(sid); } - unsafe{xous::disconnect(self.conn).unwrap();} + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } diff --git a/services/rtc/src/lib.rs b/services/rtc/src/lib.rs index 856d00d93..5ae6ff6c8 100644 --- a/services/rtc/src/lib.rs +++ b/services/rtc/src/lib.rs @@ -17,6 +17,7 @@ pub struct Rtc { static mut RTC_CB: Option = None; impl Rtc { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_RTC).expect("Can't connect to RTC"); Ok(Rtc { conn, @@ -95,6 +96,8 @@ impl Rtc { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Rtc { fn drop(&mut self) { // if we have callbacks, destroy the callback server @@ -109,7 +112,9 @@ impl Drop for Rtc { // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. // all implementations will need this - unsafe{xous::disconnect(self.conn).unwrap();} + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } diff --git a/services/skeleton/src/lib.rs b/services/skeleton/src/lib.rs index a9b91544f..e6e31810c 100644 --- a/services/skeleton/src/lib.rs +++ b/services/skeleton/src/lib.rs @@ -9,6 +9,7 @@ pub struct Codec { } impl Codec { pub fn new(xns: &xous_names::XousNames) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_CODEC).expect("Can't connect to Codec server"); Ok(Codec { conn @@ -16,10 +17,13 @@ impl Codec { } } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Codec { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } \ No newline at end of file diff --git a/services/susres/src/lib.rs b/services/susres/src/lib.rs index f43bc27e2..3a89adfc2 100644 --- a/services/susres/src/lib.rs +++ b/services/susres/src/lib.rs @@ -16,6 +16,7 @@ pub struct Susres { impl Susres { #[cfg(target_os = "none")] pub fn new(xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_SUSRES).expect("Can't connect to SUSRES"); let execution_gate_conn = xns.request_connection_blocking(api::SERVER_NAME_EXEC_GATE).expect("Can't connect to the execution gate"); @@ -43,6 +44,7 @@ impl Susres { // during boot when all the hosted mode servers try to connect. This isn't an issue on real hardware. #[cfg(not(target_os = "none"))] pub fn new(xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); Ok(Susres { conn: 0, suspend_cb_sid: None, @@ -100,13 +102,16 @@ fn drop_conn(sid: xous::SID) { Message::new_scalar(SuspendEventCallback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap(); unsafe{xous::disconnect(cid).unwrap();} } +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Susres { fn drop(&mut self) { if let Some(sid) = self.suspend_cb_sid.take() { drop_conn(sid); } - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } /// handles callback messages that indicate a USB interrupt has happened, in the library user's process space. diff --git a/services/ticktimer-server/src/lib.rs b/services/ticktimer-server/src/lib.rs index 38c0d4a65..696f8b72a 100644 --- a/services/ticktimer-server/src/lib.rs +++ b/services/ticktimer-server/src/lib.rs @@ -11,6 +11,7 @@ pub struct Ticktimer { } impl Ticktimer { pub fn new() -> Result { + REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); let conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap())?; Ok(Ticktimer { conn, @@ -38,11 +39,13 @@ impl Ticktimer { ).map(|_| ()) } } - +use core::sync::atomic::{AtomicU32, Ordering}; +static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Ticktimer { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - unsafe{xous::disconnect(self.conn).unwrap();} - + if REFCOUNT.load(Ordering::Relaxed) == 0 { + unsafe{xous::disconnect(self.conn).unwrap();} + } } } \ No newline at end of file