mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-24 09:40:52 +00:00
the connection number is re-used for all objects created within a process space, so you can't `Drop` the connection immediately when an object goes out of scope. You have to make sure all instance of the object go out of scope. This patch adds reference counting to all the objects so that the `Drop` function works as intended.
29 lines
No EOL
881 B
Rust
29 lines
No EOL
881 B
Rust
#![cfg_attr(target_os = "none", no_std)]
|
|
|
|
pub mod api;
|
|
use xous::{CID, send_message};
|
|
use num_traits::ToPrimitive;
|
|
|
|
pub struct Codec {
|
|
conn: CID,
|
|
}
|
|
impl Codec {
|
|
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
|
|
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
|
|
})
|
|
}
|
|
}
|
|
|
|
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();}
|
|
}
|
|
}
|
|
} |