remove disconnect idiom from HAL API

the implementation is flawed - because multiple APIs hit
a single server, the reference count can reach zeoro for
a given interface, and trigger a disconnect, while another
API is still using it.

the long-term fix for this, if we wanted auto-disconnect,
would be to wrap the connection query into a helper that
uses an Arc to ensure the count is maintained across all
possible interfaces.

However, for the HAL, it's likely that pretty much every
server is going to want this connection, and want it to
stay around so - we'll just remove the auto-disconnect
idiom for now and figure something out if we end up
hitting a connection resource limit on a service that
truly only needs the HAL connection early on in its
lifecycle and never needs it again.
This commit is contained in:
bunnie 2026-02-02 01:17:24 +08:00
parent 867d9cf2a8
commit 8bc480994c
2 changed files with 0 additions and 34 deletions

View file

@ -1,8 +1,3 @@
#[cfg(feature = "std")]
use core::sync::atomic::{AtomicU32, Ordering};
#[cfg(feature = "std")]
static REFCOUNT: AtomicU32 = AtomicU32::new(0);
#[cfg(feature = "std")]
use num_traits::ToPrimitive;
@ -133,7 +128,6 @@ pub struct IoxHal {
#[cfg(feature = "std")]
impl IoxHal {
pub fn new() -> Self {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let xns = xous_names::XousNames::new().unwrap();
let conn =
xns.request_connection(SERVER_NAME_BAO1X_HAL).expect("Couldn't connect to bao1x HAL server");
@ -329,19 +323,6 @@ impl IoGpio for IoxHal {
}
}
#[cfg(feature = "std")]
impl Drop for IoxHal {
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.fetch_sub(1, Ordering::Relaxed) == 1 {
unsafe {
xous::disconnect(self.conn).ok();
}
}
}
}
#[cfg(feature = "std")]
impl IoIrq for IoxHal {
fn set_irq_pin(&self, port: IoxPort, pin: u8, active: IoxValue, server: &str, opcode: usize) {

View file

@ -121,7 +121,6 @@ pub struct Keyboard {
}
impl Keyboard {
pub fn new(xns: &xous_names::XousNames) -> Result<Self, xous::Error> {
REFCOUNT.fetch_add(1, Ordering::Relaxed);
let conn = xns.request_connection_blocking(crate::SERVER_NAME_KBD).expect("Can't connect to KBD");
Ok(Keyboard { conn })
}
@ -205,17 +204,3 @@ impl Keyboard {
/// Reveal the connection ID for use with unsafe FFI calls
pub fn conn(&self) -> xous::CID { self.conn }
}
use core::sync::atomic::{AtomicU32, Ordering};
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.
if REFCOUNT.fetch_sub(1, Ordering::Relaxed) == 1 {
unsafe {
xous::disconnect(self.conn).unwrap();
}
}
}
}