mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
add reference counting on connections between servers
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.
This commit is contained in:
parent
58edcc56f2
commit
30aa794f30
12 changed files with 82 additions and 20 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -13,6 +13,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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -45,6 +45,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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,6 +18,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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -220,6 +220,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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -19,6 +19,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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -41,6 +41,7 @@ pub struct Llio {
|
|||
}
|
||||
impl Llio {
|
||||
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_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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ pub struct Rtc {
|
|||
static mut RTC_CB: Option<fn(DateTime)> = None;
|
||||
impl Rtc {
|
||||
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_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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -9,6 +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);
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Self, xous::Error> {
|
||||
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<Self, xous::Error> {
|
||||
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.
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ pub struct Ticktimer {
|
|||
}
|
||||
impl Ticktimer {
|
||||
pub fn new() -> Result<Self, Error> {
|
||||
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();}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue