fix up the REFCOUNT on connections

Was doing disjoint loads and stores on an Atomic, really, should
be using the fetch_add, fetch_sub variants to be proper.

Didn't seem to impact anything but maybe there's a future version
of me that high fives me for doing this.
This commit is contained in:
bunnie 2022-01-31 23:13:10 +08:00
parent 884943c3ad
commit 827888ccbb
22 changed files with 46 additions and 68 deletions

5
API.md
View file

@ -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();}
}
}

View file

@ -15,7 +15,7 @@ pub struct Codec {
}
impl Codec {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();}
}
}

View file

@ -49,7 +49,7 @@ pub struct Com {
}
impl Com {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();}
}
}

View file

@ -15,7 +15,7 @@ pub struct Dns {
}
impl Dns {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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),

View file

@ -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<Self, xous::Error> {
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

View file

@ -76,7 +76,7 @@ pub struct Gam {
}
impl Gam {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();}
}
}

View file

@ -24,7 +24,7 @@ pub struct Gfx {
}
impl Gfx {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();
}

View file

@ -242,7 +242,7 @@ pub struct ImeFrontEnd {
}
impl ImeFrontEnd {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();
}

View file

@ -12,7 +12,7 @@ pub struct Jtag {
}
impl Jtag {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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),

View file

@ -14,7 +14,7 @@ pub struct Keyboard {
}
impl Keyboard {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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();}
}
}

View file

@ -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();}
}
}

View file

@ -12,7 +12,7 @@ pub struct Rtc {
static mut RTC_CB: Option<fn(DateTime)> = 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();}
}
}

View file

@ -16,7 +16,7 @@ pub struct Modals {
}
impl Modals {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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),

View file

@ -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();}
}
}

View file

@ -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();}
}
}

View file

@ -29,7 +29,7 @@ pub struct RootKeys {
}
impl RootKeys {
pub fn new(xns: &xous_names::XousNames, key_index: Option<AesRootkeyType>) -> Result<Self, xous::Error> {
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();}
}
}

View file

@ -9,7 +9,7 @@ pub struct Codec {
}
impl Codec {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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),

View file

@ -32,7 +32,7 @@ impl Spinor {
#[cfg(not(test))]
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
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),

View file

@ -19,7 +19,7 @@ pub struct Susres {
impl Susres {
#[cfg(any(target_os = "none", target_os = "xous"))]
pub fn new(order: Option<SuspendOrder>, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result<Self, xous::Error> {
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<SuspendOrder>, xns: &xous_names::XousNames, cb_discriminant: u32, cid: CID) -> Result<Self, xous::Error> {
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();}
}
}

View file

@ -11,7 +11,7 @@ pub struct Ticktimer {
}
impl Ticktimer {
pub fn new() -> Result<Self, Error> {
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();
}

View file

@ -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();
}

View file

@ -16,7 +16,7 @@ pub struct XousNames {
}
impl XousNames {
pub fn new() -> Result<Self, xous::Error> {
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();
}