Compare commits

..

1 Commits

Author SHA1 Message Date
Simon Renblad 1ab13f5799 runtime: run idle kernel on start-up 2023-11-02 12:21:18 +08:00
2 changed files with 58 additions and 51 deletions

View File

@ -638,6 +638,27 @@ async fn handle_connection(
} }
} }
async fn flash_kernel_worker(
control: &Rc<RefCell<kernel::Control>>,
up_destinations: &Rc<RefCell<[bool; drtio_routing::DEST_COUNT]>>,
aux_mutex: &Rc<Mutex<bool>>,
drtio_routing_table: &Rc<RefCell<drtio_routing::RoutingTable>>,
timer: GlobalTimer,
cfg: &Rc<Config>,
config_key: &str,
) {
if let Ok(buffer) = cfg.read(config_key) {
let routing_table = drtio_routing_table.borrow();
info!("Loading {}", config_key);
let _ = load_kernel(&buffer, &control, None)
.await.map_err(|_| warn!("Error loading {}", config_key));
info!("Running idle kernel");
let _ = handle_run_kernel(None, &control, &up_destinations, &aux_mutex, &routing_table, timer)
.await.map_err(|_| warn!("Error running {}", config_key));
info!("Terminating {}", config_key);
}
}
pub fn main(timer: GlobalTimer, cfg: Config) { pub fn main(timer: GlobalTimer, cfg: Config) {
let net_addresses = net_settings::get_addresses(&cfg); let net_addresses = net_settings::get_addresses(&cfg);
info!("network addresses: {}", net_addresses); info!("network addresses: {}", net_addresses);
@ -699,54 +720,42 @@ pub fn main(timer: GlobalTimer, cfg: Config) {
moninj::start(timer, &aux_mutex, &drtio_routing_table); moninj::start(timer, &aux_mutex, &drtio_routing_table);
let control: Rc<RefCell<kernel::Control>> = Rc::new(RefCell::new(kernel::Control::start())); let control: Rc<RefCell<kernel::Control>> = Rc::new(RefCell::new(kernel::Control::start()));
let idle_kernel = Rc::new(cfg.read("idle_kernel").ok()); let cfg: Rc<Config> = Rc::new(cfg);
if let Ok(buffer) = cfg.read("startup_kernel") { task::block_on(flash_kernel_worker(
info!("Loading startup kernel..."); &control,
if let Ok(()) = task::block_on(load_kernel(&buffer, &control, None)) { &up_destinations,
info!("Starting startup kernel..."); &aux_mutex,
let routing_table = drtio_routing_table.borrow(); &drtio_routing_table,
let _ = task::block_on(handle_run_kernel( timer,
None, &cfg,
&control, "startup_kernel")
&up_destinations, );
&aux_mutex,
&routing_table, mgmt::start(cfg.clone());
timer,
));
info!("Startup kernel finished!");
} else {
error!("Error loading startup kernel!");
}
}
mgmt::start(cfg);
task::spawn(async move { task::spawn(async move {
let connection = Rc::new(Semaphore::new(0, 1)); let connection = Rc::new(Semaphore::new(0, 1));
let terminate = Rc::new(Semaphore::new(0, 1)); let terminate = Rc::new(Semaphore::new(0, 1));
task::spawn({ task::spawn({
let cfg = cfg.clone();
let control = control.clone(); let control = control.clone();
let idle_kernel = idle_kernel.clone();
let connection = connection.clone(); let connection = connection.clone();
let terminate = terminate.clone(); let terminate = terminate.clone();
let up_destinations = up_destinations.clone(); let up_destinations = up_destinations.clone();
let aux_mutex = aux_mutex.clone(); let aux_mutex = aux_mutex.clone();
let routing_table = drtio_routing_table.clone(); let drtio_routing_table = drtio_routing_table.clone();
async move { async move {
let routing_table = routing_table.borrow();
select_biased! { select_biased! {
_ = (async { _ = flash_kernel_worker(
if let Some(buffer) = &*idle_kernel { &control,
info!("Loading idle kernel"); &up_destinations,
let _ = load_kernel(&buffer, &control, None) &aux_mutex,
.await.map_err(|_| warn!("error loading idle kernel")); &drtio_routing_table,
info!("Running idle kernel"); timer,
let _ = handle_run_kernel(None, &control, &up_destinations, &aux_mutex, &routing_table, timer) &cfg,
.await.map_err(|_| warn!("error running idle kernel")); "idle_kernel"
info!("Idle kernel terminated"); ).fuse() => (),
}
}).fuse() => (),
_ = terminate.async_wait().fuse() => () _ = terminate.async_wait().fuse() => ()
} }
connection.signal(); connection.signal();
@ -761,33 +770,32 @@ pub fn main(timer: GlobalTimer, cfg: Config) {
terminate.signal(); terminate.signal();
connection.async_wait().await; connection.async_wait().await;
} }
let cfg = cfg.clone();
let control = control.clone(); let control = control.clone();
let idle_kernel = idle_kernel.clone();
let connection = connection.clone(); let connection = connection.clone();
let terminate = terminate.clone(); let terminate = terminate.clone();
let up_destinations = up_destinations.clone(); let up_destinations = up_destinations.clone();
let aux_mutex = aux_mutex.clone(); let aux_mutex = aux_mutex.clone();
let routing_table = drtio_routing_table.clone(); let drtio_routing_table = drtio_routing_table.clone();
// we make sure the value of terminate is 0 before we start // we make sure the value of terminate is 0 before we start
let _ = terminate.try_wait(); let _ = terminate.try_wait();
task::spawn(async move { task::spawn(async move {
let routing_table = routing_table.borrow(); let routing_table = drtio_routing_table.borrow();
select_biased! { select_biased! {
_ = (async { _ = (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 { flash_kernel_worker(
info!("Loading idle kernel"); &control,
let _ = load_kernel(&buffer, &control, None) &up_destinations,
.await.map_err(|_| warn!("error loading idle kernel")); &aux_mutex,
info!("Running idle kernel"); &drtio_routing_table,
let _ = handle_run_kernel(None, &control, &up_destinations, &aux_mutex, &routing_table, timer) timer,
.await.map_err(|_| warn!("error running idle kernel")); &cfg,
info!("Idle kernel terminated"); "idle_kernel").await;
}
}).fuse() => (), }).fuse() => (),
_ = terminate.async_wait().fuse() => () _ = terminate.async_wait().fuse() => ()
} }
@ -842,7 +850,7 @@ pub fn soft_panic_main(timer: GlobalTimer, cfg: Config) -> ! {
Sockets::init(32); Sockets::init(32);
mgmt::start(cfg); mgmt::start(Rc::new(cfg));
// 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

View File

@ -229,10 +229,9 @@ async fn handle_connection(stream: &mut TcpStream, pull_id: Rc<RefCell<u32>>, cf
} }
} }
pub fn start(cfg: Config) { pub fn start(cfg: Rc<Config>) {
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();