Compare commits

...

7 Commits

2 changed files with 76 additions and 67 deletions

View File

@ -4,9 +4,7 @@ use core::{cell::RefCell, fmt, slice, str};
use core_io::Error as IoError; use core_io::Error as IoError;
use cslice::CSlice; use cslice::CSlice;
use dyld::elf; use dyld::elf;
use futures::{future::FutureExt, select_biased}; use futures::{future::FutureExt, select_biased}; #[cfg(has_drtio)] use io::Cursor;
#[cfg(has_drtio)]
use io::Cursor;
#[cfg(has_drtio)] #[cfg(has_drtio)]
use ksupport::rpc; use ksupport::rpc;
use ksupport::{kernel, resolve_channel_name}; use ksupport::{kernel, resolve_channel_name};
@ -803,70 +801,63 @@ pub fn main(timer: GlobalTimer, cfg: Config) {
error!("Error loading startup kernel!"); error!("Error loading startup kernel!");
} }
} }
let cfg = Rc::new(cfg);
let restart_idle = Rc::new(Semaphore::new(0, 1));
mgmt::start(cfg.clone(), restart_idle.clone());
mgmt::start(cfg); let terminate = Rc::new(Semaphore::new(0, 1));
let connection = Rc::new(Semaphore::new(1, 1));
let finish = Rc::new(Semaphore::new(1, 1));
let start = Rc::new(Semaphore::new(1, 1)); // run idle kernel once without needing restart
// turn the above into a control obj
//
let stream_refcell = Rc::new(RefCell::new(None));
task::spawn(async move { task::spawn(async move {
let connection = Rc::new(Semaphore::new(0, 1));
let terminate = Rc::new(Semaphore::new(0, 1));
{
let control = control.clone();
let idle_kernel = idle_kernel.clone();
let connection = connection.clone();
let terminate = terminate.clone();
let up_destinations = up_destinations.clone();
let aux_mutex = aux_mutex.clone();
let routing_table = drtio_routing_table.clone();
task::spawn(async move {
let routing_table = routing_table.borrow();
select_biased! {
_ = (async {
if let Some(buffer) = &*idle_kernel {
load_and_run_idle_kernel(&buffer, &control, &up_destinations, &aux_mutex, &routing_table, timer).await;
}
}).fuse() => (),
_ = terminate.async_wait().fuse() => ()
}
connection.signal();
});
}
loop { loop {
let mut stream = TcpStream::accept(1381, 0x10_000, 0x10_000).await.unwrap(); clone_mult!(semaphores, control, up_destinations, aux_mutex, routing_table);
start.async_wait().await;
if connection.try_wait().is_none() { let stream_opt = stream_refcell.borrow_mut();
// there is an existing connection let _ = connection.try_wait();
terminate.signal(); let _ = finish.try_wait();
connection.async_wait().await; select_biased! {
} _ = (async {
if let Some(&mut stream) = stream_opt {
let control = control.clone();
let idle_kernel = idle_kernel.clone();
let connection = connection.clone();
let terminate = terminate.clone();
let up_destinations = up_destinations.clone();
let aux_mutex = aux_mutex.clone();
let routing_table = drtio_routing_table.clone();
// we make sure the value of terminate is 0 before we start
let _ = terminate.try_wait();
task::spawn(async move {
let routing_table = routing_table.borrow();
select_biased! {
_ = (async {
let _ = handle_connection(&mut stream, control.clone(), &up_destinations, &aux_mutex, &routing_table, timer) let _ = handle_connection(&mut stream, control.clone(), &up_destinations, &aux_mutex, &routing_table, timer)
.await .await
.map_err(|e| warn!("connection terminated: {}", e)); .map_err(|e| warn!("connection terminated: {}", e));
if let Some(buffer) = &*idle_kernel { }
load_and_run_idle_kernel(&buffer, &control, &up_destinations, &aux_mutex, &routing_table, timer).await; connection.signal();
} if let Some(buffer) = cfg.read("idle_kernel").ok() {
}).fuse() => (), load_and_run_idle_kernel(&buffer, &control, &up_destinations, &aux_mutex, &routing_table, timer).await;
_ = terminate.async_wait().fuse() => () }
} }).fuse() => (), // TODO: on clean exit with existing idle -> run again?
connection.signal(); _ = terminate.async_wait().fuse() => (),
let _ = stream.flush().await; }
let _ = stream.abort().await; if let Some(&stream) = stream_opt {
}); stream.flush().await();
stream.abort().await();
}
finish.signal();
}
});
task::spawn(async move {
loop {
clone_mult!(semaphores, stream_refcell);
let stream_opt = select_biased! {
temp_s = (async {
Some(Rc::new(TcpStream::accept(1381, 0x10_000, 0x10_000).await.unwrap()))
}).fuse() => temp_s,
_ = (async {
restart_idle.async_wait().await;
connection.async_wait().await;
}).fuse() => None
};
terminate.signal();
finish.async_wait().await;
stream_refcell.replace(stream_opt);
start.signal();
} }
}); });
@ -914,7 +905,8 @@ pub fn soft_panic_main(timer: GlobalTimer, cfg: Config) -> ! {
Sockets::init(32); Sockets::init(32);
mgmt::start(cfg); let dummy_restart = Rc::new(Semaphore::new(0, 1));
mgmt::start(Rc::new(cfg), dummy_restart);
// getting eth settings disables the LED as it resets GPIO // getting eth settings disables the LED as it resets GPIO
// need to re-enable it here // need to re-enable it here
@ -926,3 +918,15 @@ pub fn soft_panic_main(timer: GlobalTimer, cfg: Config) -> ! {
Sockets::run(&mut iface, || Instant::from_millis(timer.get_time().0 as i32)); Sockets::run(&mut iface, || Instant::from_millis(timer.get_time().0 as i32));
} }
macro_rules! clone_mult {
($id:ident) => {
let $id = $id.clone();
};
// Decompose multiple `eval`s recursively
($id:ident, $($ids:ident),+) => {
clone_mult!($id)
clone_mult!($($ids),+)
};
}

View File

@ -6,6 +6,7 @@ use libasync::{smoltcp::TcpStream, task};
use libboard_artiq::logger::{BufferLogger, LogBufferRef}; use libboard_artiq::logger::{BufferLogger, LogBufferRef};
use libboard_zynq::{slcr, smoltcp}; use libboard_zynq::{slcr, smoltcp};
use libconfig::Config; use libconfig::Config;
use libcortex_a9::semaphore::Semaphore;
use log::{self, debug, error, info, warn, LevelFilter}; use log::{self, debug, error, info, warn, LevelFilter};
use num_derive::FromPrimitive; use num_derive::FromPrimitive;
use num_traits::FromPrimitive; use num_traits::FromPrimitive;
@ -111,7 +112,7 @@ async fn read_key(stream: &mut TcpStream) -> Result<String> {
Ok(String::from_utf8(buffer).unwrap()) Ok(String::from_utf8(buffer).unwrap())
} }
async fn handle_connection(stream: &mut TcpStream, pull_id: Rc<RefCell<u32>>, cfg: Rc<Config>) -> Result<()> { async fn handle_connection(stream: &mut TcpStream, pull_id: Rc<RefCell<u32>>, cfg: Rc<Config>, restart_idle: Rc<Semaphore>) -> Result<()> {
if !expect(&stream, b"ARTIQ management\n").await? { if !expect(&stream, b"ARTIQ management\n").await? {
return Err(Error::UnexpectedPattern); return Err(Error::UnexpectedPattern);
} }
@ -200,6 +201,10 @@ async fn handle_connection(stream: &mut TcpStream, pull_id: Rc<RefCell<u32>>, cf
let value = cfg.write(&key, buffer); let value = cfg.write(&key, buffer);
if value.is_ok() { if value.is_ok() {
debug!("write success"); debug!("write success");
if key == "idle_kernel" {
restart_idle.1.async_wait().await?;
restart_idle.0.signal();
}
write_i8(stream, Reply::Success as i8).await?; write_i8(stream, Reply::Success as i8).await?;
} else { } else {
// this is an error because we do not expect write to fail // this is an error because we do not expect write to fail
@ -229,17 +234,17 @@ async fn handle_connection(stream: &mut TcpStream, pull_id: Rc<RefCell<u32>>, cf
} }
} }
pub fn start(cfg: Config) { pub fn start(cfg: Rc<Config>, restart_idle: Rc<Semaphore>) {
task::spawn(async move { task::spawn(async move {
let pull_id = Rc::new(RefCell::new(0u32)); let pull_id = Rc::new(RefCell::new(0u32));
let cfg = Rc::new(cfg);
loop { loop {
let mut stream = TcpStream::accept(1380, 2048, 2048).await.unwrap(); let mut stream = TcpStream::accept(1380, 2048, 2048).await.unwrap();
let pull_id = pull_id.clone(); let pull_id = pull_id.clone();
let restart_idle = restart_idle.clone();
let cfg = cfg.clone(); let cfg = cfg.clone();
task::spawn(async move { task::spawn(async move {
info!("received connection"); info!("received connection");
let _ = handle_connection(&mut stream, pull_id, cfg) let _ = handle_connection(&mut stream, pull_id, cfg, restart_idle)
.await .await
.map_err(|e| warn!("connection terminated: {:?}", e)); .map_err(|e| warn!("connection terminated: {:?}", e));
let _ = stream.flush().await; let _ = stream.flush().await;