From 92405ffe91f99c443a0a958462e529e92a413b76 Mon Sep 17 00:00:00 2001 From: pca006132 Date: Wed, 15 Jul 2020 17:04:16 +0800 Subject: [PATCH] logger: changed from RefCell to Mutex. --- default.nix | 2 +- src/Cargo.lock | 10 +++++----- src/runtime/src/logger.rs | 18 +++++++++--------- src/runtime/src/mgmt.rs | 2 +- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/default.nix b/default.nix index 90a00623..04bd4e19 100644 --- a/default.nix +++ b/default.nix @@ -14,7 +14,7 @@ let version = "0.1.0"; src = ./src; - cargoSha256 = "1q66h2avk0gvw8k07jjazzv1f3n1p4y11vkc15c6s31pcapz3d81"; + cargoSha256 = "0q0xy5f1qzg6iifx6zqcfrs5l33ylhm4h2ii19l4c978l4vd3k5y"; nativeBuildInputs = [ pkgs.gnumake diff --git a/src/Cargo.lock b/src/Cargo.lock index 4e655fb9..cf18eb51 100644 --- a/src/Cargo.lock +++ b/src/Cargo.lock @@ -200,7 +200,7 @@ dependencies = [ [[package]] name = "libasync" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822" +source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0" dependencies = [ "embedded-hal", "libcortex_a9", @@ -212,7 +212,7 @@ dependencies = [ [[package]] name = "libboard_zynq" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822" +source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0" dependencies = [ "bit_field", "embedded-hal", @@ -236,7 +236,7 @@ dependencies = [ [[package]] name = "libcortex_a9" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822" +source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0" dependencies = [ "bit_field", "libregister", @@ -245,7 +245,7 @@ dependencies = [ [[package]] name = "libregister" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822" +source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0" dependencies = [ "bit_field", "vcell", @@ -255,7 +255,7 @@ dependencies = [ [[package]] name = "libsupport_zynq" version = "0.0.0" -source = "git+https://git.m-labs.hk/M-Labs/zc706.git#371e59cef57746e3dd4cae915be7fd3286972822" +source = "git+https://git.m-labs.hk/M-Labs/zc706.git#074438c3c7becd30a08a59490754f3120a0650f0" dependencies = [ "compiler_builtins", "libboard_zynq", diff --git a/src/runtime/src/logger.rs b/src/runtime/src/logger.rs index 2e3caca4..dc0de14a 100644 --- a/src/runtime/src/logger.rs +++ b/src/runtime/src/logger.rs @@ -1,16 +1,17 @@ -use core::cell::{Cell, RefCell, RefMut}; +use core::cell::Cell; use core::fmt::Write; use log::{Log, LevelFilter}; use log_buffer::LogBuffer; +use libcortex_a9::mutex::{Mutex, MutexGuard}; use libboard_zynq::{println, timer::GlobalTimer}; pub struct LogBufferRef<'a> { - buffer: RefMut<'a, LogBuffer<&'static mut [u8]>>, + buffer: MutexGuard<'a, LogBuffer<&'static mut [u8]>>, old_log_level: LevelFilter } impl<'a> LogBufferRef<'a> { - fn new(buffer: RefMut<'a, LogBuffer<&'static mut [u8]>>) -> LogBufferRef<'a> { + fn new(buffer: MutexGuard<'a, LogBuffer<&'static mut [u8]>>) -> LogBufferRef<'a> { let old_log_level = log::max_level(); log::set_max_level(LevelFilter::Off); LogBufferRef { buffer, old_log_level } @@ -36,7 +37,7 @@ impl<'a> Drop for LogBufferRef<'a> { } pub struct BufferLogger { - buffer: RefCell>, + buffer: Mutex>, uart_filter: Cell } @@ -45,7 +46,7 @@ static mut LOGGER: Option = None; impl BufferLogger { pub fn new(buffer: &'static mut [u8]) -> BufferLogger { BufferLogger { - buffer: RefCell::new(LogBuffer::new(buffer)), + buffer: Mutex::new(LogBuffer::new(buffer)), uart_filter: Cell::new(LevelFilter::Info), } } @@ -62,11 +63,10 @@ impl BufferLogger { &mut LOGGER } - pub fn buffer<'a>(&'a self) -> Result, ()> { + pub fn buffer<'a>(&'a self) -> Option> { self.buffer - .try_borrow_mut() + .try_lock() .map(LogBufferRef::new) - .map_err(|_| ()) } pub fn uart_log_level(&self) -> LevelFilter { @@ -94,7 +94,7 @@ impl Log for BufferLogger { let seconds = timestamp / 1_000_000; let micros = timestamp % 1_000_000; - if let Ok(mut buffer) = self.buffer.try_borrow_mut() { + if let Some(mut buffer) = self.buffer.try_lock() { writeln!(buffer, "[{:6}.{:06}s] {:>5}({}): {}", seconds, micros, record.level(), record.target(), record.args()).unwrap(); } diff --git a/src/runtime/src/mgmt.rs b/src/runtime/src/mgmt.rs index 26234388..79f811d6 100644 --- a/src/runtime/src/mgmt.rs +++ b/src/runtime/src/mgmt.rs @@ -17,7 +17,7 @@ where poll_fn(|ctx| { let logger = unsafe { BufferLogger::get_logger().as_mut().unwrap() }; match logger.buffer() { - Ok(buffer) if f(&buffer) => Poll::Ready(buffer), + Some(buffer) if f(&buffer) => Poll::Ready(buffer), _ => { ctx.waker().wake_by_ref(); Poll::Pending