From f897c41d2b0962c2983dfea2b52b21e693554fdb Mon Sep 17 00:00:00 2001 From: mwojcik Date: Mon, 27 Sep 2021 15:06:32 +0200 Subject: [PATCH] master: fix lifetime of drtio variables --- src/runtime/src/comms.rs | 12 ++++----- src/runtime/src/moninj.rs | 23 ++++++++-------- src/runtime/src/rtio_mgt.rs | 53 ++++++++++++++++++++----------------- 3 files changed, 46 insertions(+), 42 deletions(-) diff --git a/src/runtime/src/comms.rs b/src/runtime/src/comms.rs index 39e36c5..63f0b63 100644 --- a/src/runtime/src/comms.rs +++ b/src/runtime/src/comms.rs @@ -390,20 +390,20 @@ pub fn main(timer: GlobalTimer, cfg: Config) { Sockets::init(32); // before, mutex was on io, but now that io isn't used...? - let aux_mutex: Mutex = Mutex::new(false); + let aux_mutex: Rc> = Rc::new(Mutex::new(false)); #[cfg(has_drtio)] - let drtio_routing_table = RefCell::new( - drtio_routing::config_routing_table(pl::csr::DRTIO.len(), &cfg)); + let drtio_routing_table = Rc::new(RefCell::new( + drtio_routing::config_routing_table(pl::csr::DRTIO.len(), &cfg))); #[cfg(not(has_drtio))] - let drtio_routing_table = RefCell::new(drtio_routing::RoutingTable::default_empty()); - let up_destinations = RefCell::new([false; drtio_routing::DEST_COUNT]); + let drtio_routing_table = Rc::new(RefCell::new(drtio_routing::RoutingTable::default_empty())); + let up_destinations = Rc::new(RefCell::new([false; drtio_routing::DEST_COUNT])); #[cfg(has_drtio_routing)] drtio_routing::interconnect_disable_all(); rtio_mgt::startup(&aux_mutex, &drtio_routing_table, &up_destinations, timer); analyzer::start(); - moninj::start(timer, &aux_mutex, &drtio_routing_table); + moninj::start(timer, aux_mutex, drtio_routing_table); let control: Rc> = Rc::new(RefCell::new(kernel::Control::start())); let idle_kernel = Rc::new(cfg.read("idle").ok()); diff --git a/src/runtime/src/moninj.rs b/src/runtime/src/moninj.rs index f6786f3..ab09c70 100644 --- a/src/runtime/src/moninj.rs +++ b/src/runtime/src/moninj.rs @@ -1,6 +1,6 @@ use core::{fmt, cell::RefCell}; -use alloc::collections::BTreeMap; -use log::{debug, info, warn}; +use alloc::{collections::BTreeMap, rc::Rc}; +use log::{debug, info, warn, error}; use void::Void; use libboard_artiq::drtio_routing; @@ -57,14 +57,11 @@ enum DeviceMessage { #[cfg(has_drtio)] mod remote_moninj { - use libboard_zynq::timer::GlobalTimer; - use libasync::task; + use super::*; use libboard_artiq::drtioaux; use crate::rtio_mgt::drtio; - use libcortex_a9::mutex::Mutex; - use log::error; - pub fn read_probe(aux_mutex: &Mutex, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, probe: i8) -> i32 { + pub fn read_probe(aux_mutex: &Rc>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, probe: i8) -> i32 { let reply = task::block_on(drtio::aux_transact(aux_mutex, linkno, &drtioaux::Packet::MonitorRequest { destination: destination, channel: channel as _, @@ -78,7 +75,7 @@ mod remote_moninj { 0 } - pub fn inject(aux_mutex: &Mutex, _timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8, value: i8) { + pub fn inject(aux_mutex: &Rc>, _timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8, value: i8) { let _lock = aux_mutex.lock(); drtioaux::send(linkno, &drtioaux::Packet::InjectionRequest { destination: destination, @@ -88,7 +85,7 @@ mod remote_moninj { }).unwrap(); } - pub fn read_injection_status(aux_mutex: &Mutex, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8) -> i8 { + pub fn read_injection_status(aux_mutex: &Rc>, timer: GlobalTimer, linkno: u8, destination: u8, channel: i32, overrd: i8) -> i8 { let reply = task::block_on(drtio::aux_transact(aux_mutex, linkno, &drtioaux::Packet::InjectionStatusRequest { @@ -159,7 +156,7 @@ macro_rules! dispatch { } async fn handle_connection(stream: &TcpStream, timer: GlobalTimer, - _aux_mutex: &Mutex, _routing_table: &RefCell) -> Result<()> { + _aux_mutex: &Rc>, _routing_table: &Rc>) -> Result<()> { if !expect(&stream, b"ARTIQ moninj\n").await? { return Err(Error::UnexpectedPattern); } @@ -256,13 +253,15 @@ async fn handle_connection(stream: &TcpStream, timer: GlobalTimer, } } -pub fn start(timer: GlobalTimer, aux_mutex: &'static Mutex, routing_table: &'static RefCell) { +pub fn start(timer: GlobalTimer, aux_mutex: Rc>, routing_table: Rc>) { task::spawn(async move { loop { + let aux_mutex = aux_mutex.clone(); + let routing_table = routing_table.clone(); let stream = TcpStream::accept(1383, 2048, 2048).await.unwrap(); task::spawn(async move { info!("received connection"); - let result = handle_connection(&stream, timer, aux_mutex, routing_table).await; + let result = handle_connection(&stream, timer, &aux_mutex, &routing_table).await; match result { Err(Error::NetworkError(smoltcp::Error::Finished)) => info!("peer closed connection"), Err(error) => warn!("connection terminated: {}", error), diff --git a/src/runtime/src/rtio_mgt.rs b/src/runtime/src/rtio_mgt.rs index 3b38903..30fe288 100644 --- a/src/runtime/src/rtio_mgt.rs +++ b/src/runtime/src/rtio_mgt.rs @@ -1,4 +1,5 @@ use core::cell::RefCell; +use alloc::rc::Rc; use libboard_zynq::{timer::GlobalTimer, time::Milliseconds}; use libboard_artiq::{pl::csr, drtio_routing}; use libcortex_a9::mutex::Mutex; @@ -12,16 +13,16 @@ pub mod drtio { use embedded_hal::blocking::delay::DelayMs; use libasync::{task, delay}; - pub fn startup(aux_mutex: &'static Mutex, - routing_table: &'static RefCell, - up_destinations: &'static RefCell<[bool; drtio_routing::DEST_COUNT]>, + pub fn startup(aux_mutex: &Rc>, + routing_table: &Rc>, + up_destinations: &Rc>, timer: GlobalTimer) { let aux_mutex = aux_mutex.clone(); let routing_table = routing_table.clone(); let up_destinations = up_destinations.clone(); task::spawn(async move { let routing_table = routing_table.borrow(); - link_thread(&aux_mutex, &routing_table, &up_destinations, timer); + link_thread(&aux_mutex, &routing_table, &up_destinations, timer).await; }); } @@ -66,7 +67,7 @@ pub mod drtio { } } - async fn ping_remote(aux_mutex: &Mutex, linkno: u8, timer: GlobalTimer) -> u32 { + async fn ping_remote(aux_mutex: &Rc>, linkno: u8, timer: GlobalTimer) -> u32 { let mut count = 0; loop { if !link_rx_up(linkno).await { @@ -89,7 +90,7 @@ pub mod drtio { } } - async fn sync_tsc(aux_mutex: &Mutex, linkno: u8, timer: GlobalTimer) -> Result<(), &'static str> { + async fn sync_tsc(aux_mutex: &Rc>, linkno: u8, timer: GlobalTimer) -> Result<(), &'static str> { let _lock = aux_mutex.lock(); unsafe { @@ -106,7 +107,7 @@ pub mod drtio { } } - async fn load_routing_table(aux_mutex: &Mutex, linkno: u8, routing_table: &drtio_routing::RoutingTable, + async fn load_routing_table(aux_mutex: &Rc>, linkno: u8, routing_table: &drtio_routing::RoutingTable, timer: GlobalTimer) -> Result<(), &'static str> { for i in 0..drtio_routing::DEST_COUNT { let reply = aux_transact(aux_mutex, linkno, &drtioaux::Packet::RoutingSetPath { @@ -120,7 +121,7 @@ pub mod drtio { Ok(()) } - async fn set_rank(aux_mutex: &Mutex, linkno: u8, rank: u8, timer: GlobalTimer) -> Result<(), &'static str> { + async fn set_rank(aux_mutex: &Rc>, linkno: u8, rank: u8, timer: GlobalTimer) -> Result<(), &'static str> { let reply = aux_transact(aux_mutex, linkno, &drtioaux::Packet::RoutingSetRank { rank: rank }, timer).await?; @@ -143,7 +144,7 @@ pub mod drtio { } } - async fn process_unsolicited_aux(aux_mutex: &Mutex, linkno: u8) { + async fn process_unsolicited_aux(aux_mutex: &Rc>, linkno: u8) { let _lock = aux_mutex.lock(); match drtioaux::recv(linkno) { Ok(Some(packet)) => warn!("[LINK#{}] unsolicited aux packet: {:?}", linkno, packet), @@ -174,7 +175,7 @@ pub mod drtio { } async fn destination_set_up(routing_table: &drtio_routing::RoutingTable, - up_destinations: &RefCell<[bool; drtio_routing::DEST_COUNT]>, + up_destinations: &Rc>, destination: u8, up: bool) { let mut up_destinations = up_destinations.borrow_mut(); up_destinations[destination as usize] = up; @@ -187,14 +188,14 @@ pub mod drtio { } } - async fn destination_up(up_destinations: &RefCell<[bool; drtio_routing::DEST_COUNT]>, destination: u8) -> bool { + async fn destination_up(up_destinations: &Rc>, destination: u8) -> bool { let up_destinations = up_destinations.borrow(); up_destinations[destination as usize] } - async fn destination_survey(aux_mutex: &Mutex, routing_table: &drtio_routing::RoutingTable, + async fn destination_survey(aux_mutex: &Rc>, routing_table: &drtio_routing::RoutingTable, up_links: &[bool], - up_destinations: &RefCell<[bool; drtio_routing::DEST_COUNT]>, + up_destinations: &Rc>, timer: GlobalTimer) { for destination in 0..drtio_routing::DEST_COUNT { let hop = routing_table.0[destination][0]; @@ -248,9 +249,9 @@ pub mod drtio { } } - pub async fn link_thread(aux_mutex: &Mutex, + pub async fn link_thread(aux_mutex: &Rc>, routing_table: &drtio_routing::RoutingTable, - up_destinations: &RefCell<[bool; drtio_routing::DEST_COUNT]>, + up_destinations: &Rc>, timer: GlobalTimer) { let mut up_links = [false; csr::DRTIO.len()]; loop { @@ -295,7 +296,8 @@ pub mod drtio { } } - pub fn reset(aux_mutex: &Mutex, mut timer: GlobalTimer) { + #[allow(dead_code)] + pub fn reset(aux_mutex: Rc>, mut timer: GlobalTimer) { for linkno in 0..csr::DRTIO.len() { unsafe { (csr::DRTIO[linkno].reset_write)(1); @@ -311,7 +313,7 @@ pub mod drtio { for linkno in 0..csr::DRTIO.len() { let linkno = linkno as u8; if task::block_on(link_rx_up(linkno)) { - let reply = task::block_on(aux_transact(aux_mutex, linkno, + let reply = task::block_on(aux_transact(&aux_mutex, linkno, &drtioaux::Packet::ResetRequest, timer)); match reply { Ok(drtioaux::Packet::ResetAck) => (), @@ -327,14 +329,16 @@ pub mod drtio { pub mod drtio { use super::*; - pub fn startup(_aux_mutex: &'static Mutex, _routing_table: &'static RefCell, - _up_destinations: &'static RefCell<[bool; drtio_routing::DEST_COUNT]>, _timer: GlobalTimer) {} - pub fn reset(_aux_mutex: &Mutex, mut _timer: GlobalTimer) {} + pub fn startup(_aux_mutex: &Rc>, _routing_table: &Rc>, + _up_destinations: &Rc>, _timer: GlobalTimer) {} + + #[allow(dead_code)] + pub fn reset(_aux_mutex: Rc>, mut _timer: GlobalTimer) {} } -pub fn startup(aux_mutex: &'static Mutex, - routing_table: &'static RefCell, - up_destinations: &'static RefCell<[bool; drtio_routing::DEST_COUNT]>, +pub fn startup(aux_mutex: &Rc>, + routing_table: &Rc>, + up_destinations: &Rc>, timer: GlobalTimer) { drtio::startup(aux_mutex, routing_table, up_destinations, timer); unsafe { @@ -342,7 +346,8 @@ pub fn startup(aux_mutex: &'static Mutex, } } -pub fn reset(aux_mutex: &Mutex, timer: GlobalTimer) { +#[allow(dead_code)] +pub fn reset(aux_mutex: Rc>, timer: GlobalTimer) { unsafe { csr::rtio_core::reset_write(1); }