From ae07c05db4e3d961700ea7d2216c6f1d10970726 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Tue, 1 Sep 2020 09:44:34 +0800 Subject: [PATCH] runtime: optimize for speed and fix deadlock The previous method of taking the channel could cause deadlock, we now use semaphore to signal if the channel is available instead of busy polling the mutex. --- src/Cargo.lock | 10 +++++----- src/Cargo.toml | 2 +- src/runtime/src/kernel/control.rs | 3 ++- src/runtime/src/kernel/core1.rs | 2 ++ src/runtime/src/kernel/mod.rs | 3 ++- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/Cargo.lock b/src/Cargo.lock index 3217e60..d42c75c 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -187,7 +187,7 @@ dependencies = [ [[package]] name = "libasync" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#a73df780d0d0e757738f27feac162904f5bf93f9" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#157439bc88cbb18bb40009428acf1fdee800e32e" dependencies = [ "embedded-hal", "libcortex_a9", @@ -199,7 +199,7 @@ dependencies = [ [[package]] name = "libboard_zynq" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#a73df780d0d0e757738f27feac162904f5bf93f9" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#157439bc88cbb18bb40009428acf1fdee800e32e" dependencies = [ "bit_field", "embedded-hal", @@ -233,7 +233,7 @@ dependencies = [ [[package]] name = "libcortex_a9" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#a73df780d0d0e757738f27feac162904f5bf93f9" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#157439bc88cbb18bb40009428acf1fdee800e32e" dependencies = [ "bit_field", "libregister", @@ -249,7 +249,7 @@ checksum = "c7d73b3f436185384286bd8098d17ec07c9a7d2388a6599f824d8502b529702a" [[package]] name = "libregister" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#a73df780d0d0e757738f27feac162904f5bf93f9" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#157439bc88cbb18bb40009428acf1fdee800e32e" dependencies = [ "bit_field", "vcell", @@ -259,7 +259,7 @@ dependencies = [ [[package]] name = "libsupport_zynq" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#a73df780d0d0e757738f27feac162904f5bf93f9" +source = "git+https://git.m-labs.hk/M-Labs/zynq-rs.git#157439bc88cbb18bb40009428acf1fdee800e32e" dependencies = [ "compiler_builtins", "libboard_zynq", diff --git a/src/Cargo.toml b/src/Cargo.toml index e68c9bd..4bc1f76 100644 --- a/src/Cargo.toml +++ b/src/Cargo.toml @@ -24,7 +24,7 @@ overflow-checks = false panic = "abort" debug = true codegen-units = 1 -opt-level = 's' +opt-level = 2 lto = true [patch.crates-io] diff --git a/src/runtime/src/kernel/control.rs b/src/runtime/src/kernel/control.rs index 91fefa3..3ffe131 100644 --- a/src/runtime/src/kernel/control.rs +++ b/src/runtime/src/kernel/control.rs @@ -1,7 +1,7 @@ use libcortex_a9::sync_channel::{Sender, Receiver}; use libsupport_zynq::boot::Core1; -use super::{CHANNEL_0TO1, CHANNEL_1TO0, INIT_LOCK, Message}; +use super::{CHANNEL_0TO1, CHANNEL_1TO0, CHANNEL_SEM, INIT_LOCK, Message}; use crate::irq::restart_core1; use core::mem::{forget, replace}; @@ -12,6 +12,7 @@ pub struct Control { } fn get_channels() -> (Sender<'static, Message>, Receiver<'static, Message>) { + CHANNEL_SEM.wait(); let mut core0_tx = None; while core0_tx.is_none() { core0_tx = CHANNEL_0TO1.lock().take(); diff --git a/src/runtime/src/kernel/core1.rs b/src/runtime/src/kernel/core1.rs index ec2e27c..94e0e24 100644 --- a/src/runtime/src/kernel/core1.rs +++ b/src/runtime/src/kernel/core1.rs @@ -20,6 +20,7 @@ use super::{ rpc::rpc_send_async, INIT_LOCK, CHANNEL_0TO1, CHANNEL_1TO0, + CHANNEL_SEM, KERNEL_CHANNEL_0TO1, KERNEL_CHANNEL_1TO0, KERNEL_IMAGE, Message, @@ -159,6 +160,7 @@ pub fn main_core1() { } *CHANNEL_0TO1.lock() = Some(core0_tx); *CHANNEL_1TO0.lock() = Some(core0_rx); + CHANNEL_SEM.signal(); // set on load, cleared on start let mut loaded_kernel = None; diff --git a/src/runtime/src/kernel/mod.rs b/src/runtime/src/kernel/mod.rs index e06a6e1..c3bb1fb 100644 --- a/src/runtime/src/kernel/mod.rs +++ b/src/runtime/src/kernel/mod.rs @@ -1,7 +1,7 @@ use core::ptr; use alloc::{vec::Vec, string::String}; -use libcortex_a9::{mutex::Mutex, sync_channel}; +use libcortex_a9::{mutex::Mutex, sync_channel, semaphore::Semaphore}; use crate::eh_artiq; mod control; @@ -48,6 +48,7 @@ pub enum Message { static CHANNEL_0TO1: Mutex>> = Mutex::new(None); static CHANNEL_1TO0: Mutex>> = Mutex::new(None); +static CHANNEL_SEM: Semaphore = Semaphore::new(0, 1); static mut KERNEL_CHANNEL_0TO1: Option> = None; static mut KERNEL_CHANNEL_1TO0: Option> = None;