diff --git a/API.md b/API.md index 119b30125..f76f28295 100644 --- a/API.md +++ b/API.md @@ -234,14 +234,13 @@ 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); +//REFCOUNT.fetch_add(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. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/codec/src/lib.rs b/services/codec/src/lib.rs index 7f66d91f1..75850ef5a 100644 --- a/services/codec/src/lib.rs +++ b/services/codec/src/lib.rs @@ -15,7 +15,7 @@ pub struct Codec { } impl Codec { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_CODEC).expect("Can't connect to Codec server"); Ok(Codec { conn, @@ -131,8 +131,7 @@ 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. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/com/src/lib.rs b/services/com/src/lib.rs index 46785bab5..543809c93 100644 --- a/services/com/src/lib.rs +++ b/services/com/src/lib.rs @@ -49,7 +49,7 @@ pub struct Com { } impl Com { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_COM).expect("Can't connect to COM server"); Ok(Com { conn, @@ -635,8 +635,7 @@ impl Drop for Com { unsafe{xous::disconnect(cid).unwrap();} } // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/dns/src/lib.rs b/services/dns/src/lib.rs index f9187ab8f..2d24f6257 100644 --- a/services/dns/src/lib.rs +++ b/services/dns/src/lib.rs @@ -15,7 +15,7 @@ pub struct Dns { } impl Dns { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_DNS).expect("Can't connect to Dns server"); Ok(Dns { conn @@ -55,8 +55,7 @@ impl Drop for Dns { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), diff --git a/services/engine-25519/src/lib.rs b/services/engine-25519/src/lib.rs index fab252d5f..ff8f9c3c0 100644 --- a/services/engine-25519/src/lib.rs +++ b/services/engine-25519/src/lib.rs @@ -26,7 +26,7 @@ pub struct Engine25519 { impl Engine25519 { // this is used to set up a system with async callbacks pub fn new_async(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_ENGINE25519).expect("Can't connect to Engine25519 server"); let sid = xous::create_server().unwrap(); @@ -41,7 +41,7 @@ impl Engine25519 { } // typically, we just want synchronous callbacks. This integrates more nicely into single-threaded code. pub fn new() -> Self { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let xns = xous_names::XousNames::new().unwrap(); let conn = xns.request_connection_blocking(api::SERVER_NAME_ENGINE25519).expect("Can't connect to Engine25519 server"); @@ -149,8 +149,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Engine25519 { fn drop(&mut self) { // disconnect from the main server, only if there are no more instances active - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } // disconnect and destroy the callback server that is specific to this instance diff --git a/services/gam/src/lib.rs b/services/gam/src/lib.rs index fd7b88e2d..10aa2ab85 100644 --- a/services/gam/src/lib.rs +++ b/services/gam/src/lib.rs @@ -76,7 +76,7 @@ pub struct Gam { } impl Gam { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_GAM).expect("Can't connect to GAM"); Ok(Gam { conn, @@ -385,8 +385,7 @@ impl Drop for Gam { unsafe{xous::disconnect(cid).unwrap();} xous::destroy_server(sid).unwrap(); } - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/graphics-server/src/lib.rs b/services/graphics-server/src/lib.rs index 3628adedb..f7e6f3188 100644 --- a/services/graphics-server/src/lib.rs +++ b/services/graphics-server/src/lib.rs @@ -24,7 +24,7 @@ pub struct Gfx { } impl Gfx { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns .request_connection_blocking(api::SERVER_NAME_GFX) .expect("Can't connect to GFX"); @@ -266,8 +266,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Gfx { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe { xous::disconnect(self.conn).unwrap(); } diff --git a/services/ime-plugin-api/src/lib.rs b/services/ime-plugin-api/src/lib.rs index 6f4221ea4..965ce1a05 100644 --- a/services/ime-plugin-api/src/lib.rs +++ b/services/ime-plugin-api/src/lib.rs @@ -242,7 +242,7 @@ pub struct ImeFrontEnd { } impl ImeFrontEnd { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns .request_connection_blocking(SERVER_NAME_IME_FRONT) .expect("Can't connect to IMEF"); @@ -272,8 +272,7 @@ impl Drop for ImeFrontEnd { } xous::destroy_server(sid).unwrap(); } - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe { xous::disconnect(self.cid).unwrap(); } diff --git a/services/jtag/src/lib.rs b/services/jtag/src/lib.rs index f9a5a9a9a..232835181 100644 --- a/services/jtag/src/lib.rs +++ b/services/jtag/src/lib.rs @@ -12,7 +12,7 @@ pub struct Jtag { } impl Jtag { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_JTAG).expect("Can't connect to JTAG server"); Ok(Jtag { conn @@ -146,8 +146,7 @@ impl Drop for Jtag { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), diff --git a/services/keyboard/src/lib.rs b/services/keyboard/src/lib.rs index 0e33148fd..3b69d8306 100644 --- a/services/keyboard/src/lib.rs +++ b/services/keyboard/src/lib.rs @@ -14,7 +14,7 @@ pub struct Keyboard { } impl Keyboard { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_KBD).expect("Can't connect to KBD"); Ok(Keyboard { conn, @@ -65,8 +65,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Keyboard { fn drop(&mut self) { // now de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/llio/src/llio_lib.rs b/services/llio/src/llio_lib.rs index 811d41dd2..1171e2682 100644 --- a/services/llio/src/llio_lib.rs +++ b/services/llio/src/llio_lib.rs @@ -16,7 +16,7 @@ pub struct Llio { } impl Llio { pub fn new(xns: &xous_names::XousNames) -> Self { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(SERVER_NAME_LLIO).expect("Can't connect to LLIO"); Llio { conn, @@ -405,8 +405,7 @@ impl Drop for Llio { if let Some(sid) = self.rtc_sid.take() { drop_conn(sid); } - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/llio/src/rtc_lib.rs b/services/llio/src/rtc_lib.rs index f08afd8bc..1f5f0c647 100644 --- a/services/llio/src/rtc_lib.rs +++ b/services/llio/src/rtc_lib.rs @@ -12,7 +12,7 @@ pub struct Rtc { static mut RTC_CB: Option = None; impl Rtc { pub fn new(xns: &xous_names::XousNames) -> Self { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(crate::api::SERVER_NAME_RTC).expect("Can't connect to RTC"); Rtc { conn, @@ -101,8 +101,7 @@ 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 - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/modals/src/lib.rs b/services/modals/src/lib.rs index d2ddb8dfc..325fc7e92 100644 --- a/services/modals/src/lib.rs +++ b/services/modals/src/lib.rs @@ -16,7 +16,7 @@ pub struct Modals { } impl Modals { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_MODALS).expect("Can't connect to Modals server"); let trng = trng::Trng::new(&xns).unwrap(); let mut token = [0u32; 4]; @@ -250,8 +250,7 @@ impl Drop for Modals { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), diff --git a/services/pddb/src/frontend/pddbkey.rs b/services/pddb/src/frontend/pddbkey.rs index 56a6f1c62..bc37c801f 100644 --- a/services/pddb/src/frontend/pddbkey.rs +++ b/services/pddb/src/frontend/pddbkey.rs @@ -152,8 +152,7 @@ impl<'a> Drop for PddbKey<'a> { send_message(self.conn, Message::new_blocking_scalar(Opcode::KeyDrop.to_usize().unwrap(), self.token[0] as usize, self.token[1] as usize, self.token[2] as usize, 0)).expect("couldn't send KeyDrop message"); - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/pddb/src/lib.rs b/services/pddb/src/lib.rs index 4e7e42cbb..78b4e0646 100644 --- a/services/pddb/src/lib.rs +++ b/services/pddb/src/lib.rs @@ -50,7 +50,7 @@ pub struct Pddb { } impl Pddb { pub fn new() -> Self { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + 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(); @@ -294,7 +294,7 @@ impl Pddb { if let Some(cb) = key_changed_cb { self.keys.lock().unwrap().insert(token, Box::new(cb)); } - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let pk = PddbKey { // i think these fields are redundant, let's save the storage and remove them for now... //dict: String::from(dict_name), @@ -526,8 +526,7 @@ impl Drop for Pddb { handle.join().expect("couldn't terminate callback helper thread"); } - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/root-keys/src/lib.rs b/services/root-keys/src/lib.rs index f720fd1cf..507e8672f 100644 --- a/services/root-keys/src/lib.rs +++ b/services/root-keys/src/lib.rs @@ -29,7 +29,7 @@ pub struct RootKeys { } impl RootKeys { pub fn new(xns: &xous_names::XousNames, key_index: Option) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_KEYS).expect("Can't connect to Keys server"); let index = if let Some(ki) = key_index { ki @@ -178,8 +178,7 @@ impl Drop for RootKeys { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/skeleton/src/lib.rs b/services/skeleton/src/lib.rs index 15b2ce66c..49f1532ce 100644 --- a/services/skeleton/src/lib.rs +++ b/services/skeleton/src/lib.rs @@ -9,7 +9,7 @@ pub struct Codec { } impl Codec { pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_CODEC).expect("Can't connect to Codec server"); Ok(Codec { conn @@ -24,8 +24,7 @@ impl Drop for Codec { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), diff --git a/services/spinor/src/lib.rs b/services/spinor/src/lib.rs index dabe983e6..7c0a46c8e 100644 --- a/services/spinor/src/lib.rs +++ b/services/spinor/src/lib.rs @@ -32,7 +32,7 @@ impl Spinor { #[cfg(not(test))] pub fn new(xns: &xous_names::XousNames) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xns.request_connection_blocking(api::SERVER_NAME_SPINOR).expect("Can't connect to Spinor server"); let trng = trng::Trng::new(&xns).expect("Can't connect to TRNG servere"); @@ -396,8 +396,7 @@ impl Drop for Spinor { // the connection to the server side must be reference counted, so that multiple instances of this object within // a single process do not end up de-allocating the CID on other threads before they go out of scope. // Note to future me: you want this. Don't get rid of it because you think, "nah, nobody will ever make more than one copy of this object". - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } // if there was object-specific state (such as a one-time use server for async callbacks, specific to the object instance), diff --git a/services/susres/src/lib.rs b/services/susres/src/lib.rs index 6bf732b37..5fa89b85c 100644 --- a/services/susres/src/lib.rs +++ b/services/susres/src/lib.rs @@ -19,7 +19,7 @@ pub struct Susres { impl Susres { #[cfg(any(target_os = "none", target_os = "xous"))] pub fn new(order: Option, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(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"); @@ -49,7 +49,7 @@ impl Susres { // during boot when all the hosted mode servers try to connect. This isn't an issue on real hardware. #[cfg(not(any(target_os = "none", target_os = "xous")))] pub fn new(_ordering: Option, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); Ok(Susres { conn: 0, suspend_cb_sid: None, @@ -147,8 +147,7 @@ impl Drop for Susres { if let Some(sid) = self.suspend_cb_sid.take() { drop_conn(sid); } - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe{xous::disconnect(self.conn).unwrap();} } } diff --git a/services/ticktimer-server/src/lib.rs b/services/ticktimer-server/src/lib.rs index a303bb10a..7ae3a106d 100644 --- a/services/ticktimer-server/src/lib.rs +++ b/services/ticktimer-server/src/lib.rs @@ -11,7 +11,7 @@ pub struct Ticktimer { } impl Ticktimer { pub fn new() -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xous::connect(xous::SID::from_bytes(b"ticktimer-server").unwrap())?; Ok(Ticktimer { conn }) } @@ -76,8 +76,7 @@ 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. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe { xous::disconnect(self.conn).unwrap(); } diff --git a/services/trng/src/lib.rs b/services/trng/src/lib.rs index 61d58f76c..ebbc83cc3 100644 --- a/services/trng/src/lib.rs +++ b/services/trng/src/lib.rs @@ -15,7 +15,7 @@ impl Trng { let conn = xns .request_connection_blocking(api::SERVER_NAME_TRNG) .expect("Can't connect to TRNG server"); - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); Ok(Trng { conn, error_sid: None, @@ -124,8 +124,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for Trng { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe { xous::disconnect(self.conn).unwrap(); } diff --git a/services/xous-names/src/lib.rs b/services/xous-names/src/lib.rs index 92af0dfbc..9d4af65cf 100644 --- a/services/xous-names/src/lib.rs +++ b/services/xous-names/src/lib.rs @@ -16,7 +16,7 @@ pub struct XousNames { } impl XousNames { pub fn new() -> Result { - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed); + REFCOUNT.fetch_add(1, Ordering::Relaxed); let conn = xous::connect(xous::SID::from_bytes(b"xous-name-server").unwrap()) .expect("Couldn't connect to XousNames"); Ok(XousNames { conn }) @@ -175,8 +175,7 @@ static REFCOUNT: AtomicU32 = AtomicU32::new(0); impl Drop for XousNames { fn drop(&mut self) { // de-allocate myself. It's unsafe because we are responsible to make sure nobody else is using the connection. - REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed); - if REFCOUNT.load(Ordering::Relaxed) == 0 { + if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 { unsafe { xous::disconnect(self.conn).unwrap(); }