1
0
Fork 0

runtime.comms: run idle kernel pre experiment

Split idle kernel and experiment kernel into separate tasks.
Added binary semaphore to control program flow.
This commit is contained in:
Simon Renblad 2023-10-25 10:12:28 +08:00
parent a08a42c954
commit a97e33b304
1 changed files with 62 additions and 40 deletions

View File

@ -722,49 +722,71 @@ pub fn main(timer: GlobalTimer, cfg: Config) {
mgmt::start(cfg); mgmt::start(cfg);
task::spawn(async move { task::spawn(async move {
let connection = Rc::new(Semaphore::new(1, 1)); let enter_idle = Rc::new(Semaphore::new(1, 1));
let terminate = Rc::new(Semaphore::new(0, 1)); let term_idle = Rc::new(Semaphore::new(0, 1));
let enter_conn = Rc::new(Semaphore::new(0, 1));
let term_conn = Rc::new(Semaphore::new(0, 1));
loop { loop {
let mut stream = TcpStream::accept(1381, 0x10_000, 0x10_000).await.unwrap(); task::spawn({
let control = control.clone();
if connection.try_wait().is_none() { let idle_kernel = idle_kernel.clone();
// there is an existing connection let up_destinations = up_destinations.clone();
terminate.signal(); let aux_mutex = aux_mutex.clone();
connection.async_wait().await; let drtio_routing_table = drtio_routing_table.clone();
} let enter_idle = enter_idle.clone();
let term_idle = term_idle.clone();
let control = control.clone(); let enter_conn = enter_conn.clone();
let idle_kernel = idle_kernel.clone(); async move {
let connection = connection.clone(); enter_idle.async_wait().await;
let terminate = terminate.clone(); if term_idle.try_wait().is_none() {
let up_destinations = up_destinations.clone(); let routing_table = drtio_routing_table.borrow();
let aux_mutex = aux_mutex.clone(); select_biased! {
let routing_table = drtio_routing_table.clone(); _ = (async {
if let Some(buffer) = &*idle_kernel {
// we make sure the value of terminate is 0 before we start info!("Loading idle kernel");
let _ = terminate.try_wait(); let _ = load_kernel(&buffer, &control, None)
task::spawn(async move { .await.map_err(|_| warn!("error loading idle kernel"));
let routing_table = routing_table.borrow(); info!("Running idle kernel");
select_biased! { let _ = handle_run_kernel(None, &control, &up_destinations, &aux_mutex, &routing_table, timer)
_ = (async { .await.map_err(|_| warn!("error running idle kernel"));
let _ = handle_connection(&mut stream, control.clone(), &up_destinations, &aux_mutex, &routing_table, timer) info!("Idle kernel terminated");
.await }
.map_err(|e| warn!("connection terminated: {}", e)); }).fuse() => (),
if let Some(buffer) = &*idle_kernel { _ = term_idle.async_wait().fuse() => ()
info!("Loading idle kernel");
let _ = load_kernel(&buffer, &control, None)
.await.map_err(|_| warn!("error loading idle kernel"));
info!("Running idle kernel");
let _ = handle_run_kernel(None, &control, &up_destinations, &aux_mutex, &routing_table, timer)
.await.map_err(|_| warn!("error running idle kernel"));
info!("Idle kernel terminated");
} }
}).fuse() => (), }
_ = terminate.async_wait().fuse() => () enter_conn.signal()
}
});
let mut stream = TcpStream::accept(1381, 0x10_000, 0x10_000).await.unwrap();
term_idle.signal();
term_conn.signal();
enter_conn.async_wait().await;
let _ = term_conn.try_wait();
let _ = term_idle.try_wait();
task::spawn({
let control = control.clone();
let up_destinations = up_destinations.clone();
let aux_mutex = aux_mutex.clone();
let drtio_routing_table = drtio_routing_table.clone();
let term_conn = term_conn.clone();
let enter_idle = enter_idle.clone();
async move {
let routing_table = drtio_routing_table.borrow();
select_biased! {
_ = (async {
let _ = handle_connection(&mut stream, control.clone(), &up_destinations, &aux_mutex, &routing_table, timer)
.await
.map_err(|e| warn!("connection terminated: {}", e));
}).fuse() => (),
_ = term_conn.async_wait().fuse() => ()
}
enter_idle.signal();
let _ = stream.flush().await;
let _ = stream.abort().await;
} }
connection.signal();
let _ = stream.flush().await;
let _ = stream.abort().await;
}); });
} }
}); });