fix an issue where we don't decrement the connection counter on drop

This commit is contained in:
bunnie 2022-01-05 20:01:54 +08:00
parent bc98064a59
commit 489a26dd11
29 changed files with 118 additions and 140 deletions

View file

@ -1,3 +1,4 @@
{
"rust-analyzer.checkOnSave.allTargets": false,
"rust-analyzer.cargo.target":"riscv32imac-unknown-xous-elf"
}

1
API.md
View file

@ -240,6 +240,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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -129,6 +129,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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -557,8 +557,8 @@ impl Drop for Com {
Message::new_scalar(api::Callback::Drop.to_usize().unwrap(), 0, 0, 0, 0)).unwrap();
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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -55,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -149,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -344,6 +344,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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -259,6 +259,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 {
unsafe {
xous::disconnect(self.conn).unwrap();

View file

@ -272,6 +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 {
unsafe {
xous::disconnect(self.cid).unwrap();

View file

@ -146,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -65,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -590,6 +590,7 @@ impl Drop for Llio {
if let Some(sid) = self.gpio_sid.take() {
drop_conn(sid);
}
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed);
if REFCOUNT.load(Ordering::Relaxed) == 0 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -216,6 +216,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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -34,6 +34,7 @@ impl Drop for NetConn {
// 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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -48,6 +48,7 @@ use core::sync::atomic::{AtomicU32, Ordering};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
impl Drop for Oqc {
fn drop(&mut self) {
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) - 1, Ordering::Relaxed);
if REFCOUNT.load(Ordering::Relaxed) == 0 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -24,6 +24,11 @@
"zh": "密码错误。 再试一次?",
"en-audio": "Incorrect password. Try again?"
},
"pddb.badpass_infallible": {
"en": "Incorrect password.\n\nPlease try again.",
"zh": "密码错误。",
"en-audio": "Incorrect password. Please try again."
},
"pddb.requestformat": {
"en": "The PDDB storage needs\nformatting. This takes\nabout 15 minutes and can't\nbe interrupted.\n\nProceed?\n",
"zh": "存储需要格式化。这需要8分钟。继续?",

View file

@ -64,28 +64,22 @@ pub(crate) const PDDB_MODAL_NAME: &'static str = "pddb modal";
pub(crate) enum Opcode {
KeyRequest,
/// read implemented using only scalars -- fast, but limited size
ReadKeyScalar,
/// read implemented using a memory buffer -- slower, but just shy of a page in size
ReadKeyMem,
WriteKeyScalar1,
WriteKeyScalar2,
WriteKeyScalar3,
WriteKeyScalar4,
WriteKeyMem,
ReadKey,
WriteKey,
WriteKeyFlush,
/// quit the server
Quit,
}
pub type ApiToken = [u32; 3];
/// A structure for requesting a token to access a particular key/value pair
#[derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize)]
pub(crate) struct PddbKeyRequest {
pub(crate) dict: xous_ipc::String::</*DICT_NAME_LEN*/ 111>, // pending https://github.com/rust-lang/rust/issues/90195
pub(crate) key: xous_ipc::String::</*KEY_NAME_LEN*/ 95>, // pending https://github.com/rust-lang/rust/issues/90195
pub(crate) token: Option<[u32; 3]>,
pub(crate) token: Option<ApiToken>,
}
/// Return codes for Read/Write API calls to the main server
@ -107,13 +101,15 @@ pub(crate) enum PddbRetcode {
#[repr(C, packed)]
pub(crate) struct PddbBuf {
/// api token for the given buffer
pub(crate) token: [u32; 3],
pub(crate) token: ApiToken,
/// point in the key stream. 64-bit for future-compatibility; but, can't be larger than 32 bits on a 32-bit target.
pub(crate) position: u64,
/// length of the data field
pub(crate) len: u16,
/// a field reserved for the return code
pub(crate) retcode: PddbRetcode,
pub(crate) reserved: u8,
pub(crate) data: [u8; 4080],
pub(crate) data: [u8; 4072],
}
impl PddbBuf {
pub(crate) fn from_slice_mut(slice: &mut [u8]) -> &mut PddbBuf {

View file

@ -505,20 +505,11 @@ impl PddbOs {
}
}
fn syskey_ensure(&mut self) {
if self.system_basis_key.is_none() {
let scd = self.static_crypto_data_get();
let mut system_key: [u8; AES_KEYSIZE] = [0; AES_KEYSIZE];
for (&src, dst) in scd.system_key.iter().zip(system_key.iter_mut()) {
*dst = src;
}
for block in system_key.chunks_mut(aes::BLOCK_SIZE) {
self.rootkeys.decrypt_block(block.try_into().unwrap());
}
self.system_basis_key = Some(system_key);
}
if self.cipher_ecb.is_none() {
let cipher = Aes256::new(GenericArray::from_slice(&self.system_basis_key.unwrap()));
self.cipher_ecb = Some(cipher);
while !self.try_login() {
self.clear_password(); // clear the bad password entry
let xns = xous_names::XousNames::new().unwrap();
let modals = modals::Modals::new(&xns).expect("can't connect to Modals server");
modals.show_notification(t!("pddb.badpass_infallible", xous::LANG)).expect("notification failed");
}
}

View file

@ -14,7 +14,9 @@ pub struct PddbKey<'a> {
conn: CID,
dict: String,
key: String,
token: [u32; 3],
token: ApiToken,
/// position in the key's data "stream"
pos: u64,
buf: Buffer<'a>,
}
impl<'a> PddbKey<'a> {
@ -80,6 +82,7 @@ impl<'a> PddbKey<'a> {
dict,
key,
token,
pos: 0,
buf: Buffer::new(core::mem::size_of::<PddbBuf>()),
})
} else {
@ -100,35 +103,6 @@ impl<'a> Read for PddbKey<'a> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize> {
if buf.len() == 0 {
Ok(0)
} else if buf.len() <= 8 {
let response = send_message(
self.conn,
Message::new_blocking_scalar(
Opcode::ReadKeyScalar.to_usize().unwrap(),
self.token[0] as usize,
self.token[1] as usize,
self.token[2] as usize,
buf.len() as usize,
)
).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
// scalars can return up to 64 bits of data. Split into an array and copy into the buf as demanded.
if let xous::Result::Scalar2(a, b) = response {
let mut maxbuf: [u8; 8] = [0; 8];
for (&src, dst) in (a as u32).to_le_bytes().iter().zip(maxbuf[..4].iter_mut()) {
*dst = src;
}
if buf.len() > 4 {
for (&src, dst) in (b as u32).to_le_bytes().iter().zip(maxbuf[4..].iter_mut()) {
*dst = src;
}
}
for (&src, dst) in maxbuf.iter().zip(buf.iter_mut()) {
*dst = src;
}
Ok(buf.len())
} else {
Err(Error::new(ErrorKind::Other, "Xous internal error"))
}
} else {
// create pbuf from a pre-reserved chunk of memory, to save on allocator thrashing
// note that it does mean that un-erased data from previous reads and writes are passed back
@ -148,10 +122,11 @@ impl<'a> Read for PddbKey<'a> {
};
pbuf.len = readlen;
pbuf.retcode = PddbRetcode::Uninit;
pbuf.position = self.pos;
readlen
};
// this takes the buffer and remaps it to the server, and on return the data is mapped back
self.buf.lend_mut(self.conn, Opcode::ReadKeyMem.to_u32().unwrap())
self.buf.lend_mut(self.conn, Opcode::ReadKey.to_u32().unwrap())
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
{
// at this point, pbuf has been mutated by the server with a return code and the return data.
@ -162,6 +137,7 @@ impl<'a> Read for PddbKey<'a> {
*dst = src;
}
assert!(pbuf.len <= readlen, "More data returned than we requested");
self.pos += buf.len() as u64;
Ok(pbuf.len as usize)
}
PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")),
@ -177,39 +153,6 @@ impl<'a> Write for PddbKey<'a> {
fn write(&mut self, buf: &[u8]) -> Result<usize> {
if buf.len() == 0 {
Ok(0)
} else if buf.len() <= 4 {
let opcode = match buf.len() {
1 => Opcode::WriteKeyScalar1.to_usize().unwrap(),
2 => Opcode::WriteKeyScalar2.to_usize().unwrap(),
3 => Opcode::WriteKeyScalar3.to_usize().unwrap(),
4 => Opcode::WriteKeyScalar4.to_usize().unwrap(),
_ => panic!("This shouldn't happen")
};
// guarantee we have enough bytes to do the u32 conversion, even if we have less data in the incoming buf
let mut u32buf: [u8; 4] = [0; 4];
for (&src, dst) in buf.iter().zip(u32buf.iter_mut()) {
*dst = src;
}
let response = send_message(
self.conn,
Message::new_blocking_scalar(
opcode,
self.token[0] as usize,
self.token[1] as usize,
self.token[2] as usize,
u32::from_le_bytes(u32buf) as usize,
)
).or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
if let xous::Result::Scalar2(rcode, len) = response {
match FromPrimitive::from_u8(rcode as u8) {
Some(PddbRetcode::Ok) => Ok(len),
Some(PddbRetcode::BasisLost) => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")),
Some(PddbRetcode::AccessDenied) => Err(Error::new(ErrorKind::PermissionDenied, "Access denied")),
_ => Err(Error::new(ErrorKind::Other, "Unhandled error code in PddbKey Read")),
}
} else {
Err(Error::new(ErrorKind::Other, "Xous internal error"))
}
} else {
let writelen = {
let pbuf = PddbBuf::from_slice_mut(self.buf.as_mut());
@ -227,10 +170,11 @@ impl<'a> Write for PddbKey<'a> {
for (&src, dst) in buf.iter().zip(pbuf.data.iter_mut()) {
*dst = src;
}
pbuf.position = self.pos;
writelen
};
// this takes the buffer and remaps it to the server, and on return the data is mapped back
self.buf.lend_mut(self.conn, Opcode::WriteKeyMem.to_u32().unwrap())
self.buf.lend_mut(self.conn, Opcode::WriteKey.to_u32().unwrap())
.or(Err(Error::new(ErrorKind::Other, "Xous internal error")))?;
{
// at this point, pbuf has been mutated by the server with a return code and the return data.
@ -238,6 +182,7 @@ impl<'a> Write for PddbKey<'a> {
match pbuf.retcode {
PddbRetcode::Ok => {
assert!(pbuf.len <= writelen, "More data written than we requested");
self.pos += writelen as u64;
Ok(pbuf.len as usize)
}
PddbRetcode::BasisLost => Err(Error::new(ErrorKind::BrokenPipe, "Basis lost")),
@ -273,6 +218,7 @@ impl<'a> Drop for PddbKey<'a> {
// 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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -7,54 +7,46 @@ pub use frontend::*;
use num_traits::*;
use std::io::{Result, Error, ErrorKind};
use xous::CID;
use std::collections::HashMap;
////// probably things in this file should eventually be split ount into their own
////// frontend module file.
// this is an intenal structure for managing the overall PDDB
#[allow(dead_code)]
pub(crate) struct PddbManager {
}
#[allow(dead_code)]
impl PddbManager {
// return a list of open bases
fn list_basis() {}
}
// this is an internal struct for managing a basis
#[allow(dead_code)]
pub(crate) struct PddbBasis {
}
#[allow(dead_code)]
impl PddbBasis {
// opening and closing basis will side-effect PddbDicts and PddbKeys
fn open() {} // will result in a password box being triggered to open a basis
fn close() {}
}
use core::sync::atomic::{AtomicU32, Ordering};
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
// this structure can be shared on the user side?
#[allow(dead_code)]
pub struct PddbDict<'a> {
pub struct Pddb<'a> {
conn: CID,
contents: HashMap<PddbKey<'a>, &'a [u8]>,
callback: Box<dyn FnMut() + 'a>,
}
impl<'a> PddbDict<'a> {
// opens a dictionary only if it exists
pub fn open(_dict_name: &str) -> Option<PddbDict> { None }
// creates a dictionary only if it does not already exist
pub fn create(_dict_name: &str) -> Option<PddbDict> { None }
impl<'a> Pddb<'a> {
/// return a list of all open bases
pub fn list_basis() -> Vec::<String> {
Vec::new()
}
/// returns the latest basis that is opened -- this is where all new values are being sent by default
pub fn get_current_basis() -> String {
String::new()
}
// returns a key only if it exists
// opens a dictionary only if it exists
pub fn open(_dict_name: &str) -> Option<Self> { None }
// creates a dictionary only if it does not already exist
pub fn create(dict_name: &str) -> Option<Self> {
REFCOUNT.store(REFCOUNT.load(Ordering::Relaxed) + 1, Ordering::Relaxed);
None
}
/// returns a key only if it exists
pub fn get(&mut self, _key_name: &str, key_changed_cb: impl FnMut() + 'a) -> Result<Option<PddbKey>> {
self.callback = Box::new(key_changed_cb);
Ok(None)
}
/// closes the key, de-allocating the OS memory to track it.
pub fn close(&mut self, _key: PddbKey) -> Result<()> {
Ok(())
}
// updates an existing key's value. mainly used by write().
pub fn update(&mut self, _key: PddbKey) -> Result<Option<PddbKey>> { Ok(None) }
// creates a key or overwrites it
@ -65,3 +57,16 @@ impl<'a> PddbDict<'a> {
pub fn delete(&mut self) {}
}
impl<'a> Drop for Pddb<'a> {
fn drop(&mut self) {
// 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 {
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),
// de-allocate those items here. They don't need a reference count because they are object-specific
}
}

View file

@ -381,8 +381,9 @@ use xous::{msg_blocking_scalar_unpack, msg_scalar_unpack, send_message, Message}
use xous_ipc::Buffer;
use core::cell::RefCell;
use std::rc::Rc;
use std::thread;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use locales::t;
@ -392,6 +393,25 @@ pub(crate) struct BasisRequestPassword {
plaintext_pw: Option<xous_ipc::String::<{crate::api::PASSWORD_LEN}>>,
}
#[derive(Eq)]
struct TokenRecord {
token: ApiToken,
dict: String,
key: String,
basis: Option<String>,
}
impl Hash for TokenRecord {
fn hash<H: Hasher>(&self, state: &mut H) {
self.token.hash(state);
}
}
impl PartialEq for TokenRecord {
fn eq(&self, other: &Self) -> bool {
self.token == other.token
}
}
#[xous::xous_main]
fn xmain() -> ! {
log_server::init_wait().unwrap();
@ -415,6 +435,8 @@ fn xmain() -> ! {
let mut pddb_os = PddbOs::new(Rc::clone(&entropy));
// storage for the basis cache
let mut basis_cache = BasisCache::new();
// storage for the token lookup: given an ApiToken, return a dict/key/basis set. Basis can be None or specified.
let mut token_dict = HashSet::<TokenRecord>::new();
// run the CI tests if the option has been selected
#[cfg(all(
@ -540,19 +562,10 @@ fn xmain() -> ! {
Some(Opcode::KeyRequest) => {
// placeholder
}
Some(Opcode::ReadKeyScalar) => msg_blocking_scalar_unpack!(msg, _tok0, _tok1, _tok2, _len, {
// placeholder
}),
Some(Opcode::ReadKeyMem) => {
Some(Opcode::ReadKey) => {
// placeholder
}
Some(Opcode::WriteKeyScalar1)
| Some(Opcode::WriteKeyScalar2)
| Some(Opcode::WriteKeyScalar3)
| Some(Opcode::WriteKeyScalar4) => msg_blocking_scalar_unpack!(msg, _tok0, _tok1, _tok2, _data, {
// placeholder
}),
Some(Opcode::WriteKeyMem) => {
Some(Opcode::WriteKey) => {
// placeholder
}
Some(Opcode::WriteKeyFlush) => {

View file

@ -178,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -115,6 +115,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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -24,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -396,6 +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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -128,6 +128,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 {
unsafe{xous::disconnect(self.conn).unwrap();}
}

View file

@ -66,6 +66,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 {
unsafe {
xous::disconnect(self.conn).unwrap();

View file

@ -124,6 +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 {
unsafe {
xous::disconnect(self.conn).unwrap();

View file

@ -175,6 +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 {
unsafe {
xous::disconnect(self.conn).unwrap();