From e7ff759f1ca78a5b53c1ea95c24a6317227dc8b0 Mon Sep 17 00:00:00 2001 From: Dario Nieuwenhuis Date: Thu, 13 Apr 2023 23:50:41 +0200 Subject: [PATCH] time: remove dependency on embassy-sync. --- embassy-time/Cargo.toml | 1 - embassy-time/src/driver_std.rs | 20 +++++++++----------- embassy-time/src/queue_generic.rs | 12 +++++------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/embassy-time/Cargo.toml b/embassy-time/Cargo.toml index 3a36dfde0..a8a7e9337 100644 --- a/embassy-time/Cargo.toml +++ b/embassy-time/Cargo.toml @@ -156,7 +156,6 @@ embedded-hal-1 = { package = "embedded-hal", version = "=1.0.0-alpha.10", option embedded-hal-async = { version = "=0.2.0-alpha.1", optional = true} futures-util = { version = "0.3.17", default-features = false } -embassy-sync = { version = "0.2.0", path = "../embassy-sync" } atomic-polyfill = "1.0.1" critical-section = "1.1" cfg-if = "1.0.0" diff --git a/embassy-time/src/driver_std.rs b/embassy-time/src/driver_std.rs index da46a599d..9f8c57b5c 100644 --- a/embassy-time/src/driver_std.rs +++ b/embassy-time/src/driver_std.rs @@ -5,8 +5,7 @@ use std::time::{Duration as StdDuration, Instant as StdInstant}; use std::{mem, ptr, thread}; use atomic_polyfill::{AtomicU8, Ordering}; -use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -use embassy_sync::blocking_mutex::Mutex as EmbassyMutex; +use critical_section::Mutex as CsMutex; use crate::driver::{AlarmHandle, Driver}; @@ -40,7 +39,7 @@ struct TimeDriver { // The STD Driver implementation requires the alarms' mutex to be reentrant, which the STD Mutex isn't // Fortunately, mutexes based on the `critical-section` crate are reentrant, because the critical sections // themselves are reentrant - alarms: UninitCell>>, + alarms: UninitCell>>, zero_instant: UninitCell, signaler: UninitCell, } @@ -58,8 +57,7 @@ crate::time_driver_impl!(static DRIVER: TimeDriver = TimeDriver { impl TimeDriver { fn init(&self) { self.once.call_once(|| unsafe { - self.alarms - .write(EmbassyMutex::new(RefCell::new([ALARM_NEW; ALARM_COUNT]))); + self.alarms.write(CsMutex::new(RefCell::new([ALARM_NEW; ALARM_COUNT]))); self.zero_instant.write(StdInstant::now()); self.signaler.write(Signaler::new()); @@ -72,7 +70,8 @@ impl TimeDriver { loop { let now = DRIVER.now(); - let next_alarm = unsafe { DRIVER.alarms.as_ref() }.lock(|alarms| { + let next_alarm = critical_section::with(|cs| { + let alarms = unsafe { DRIVER.alarms.as_ref() }.borrow(cs); loop { let pending = alarms .borrow_mut() @@ -139,8 +138,8 @@ impl Driver for TimeDriver { fn set_alarm_callback(&self, alarm: AlarmHandle, callback: fn(*mut ()), ctx: *mut ()) { self.init(); - unsafe { self.alarms.as_ref() }.lock(|alarms| { - let mut alarms = alarms.borrow_mut(); + critical_section::with(|cs| { + let mut alarms = unsafe { self.alarms.as_ref() }.borrow_ref_mut(cs); let alarm = &mut alarms[alarm.id() as usize]; alarm.callback = callback as *const (); alarm.ctx = ctx; @@ -149,9 +148,8 @@ impl Driver for TimeDriver { fn set_alarm(&self, alarm: AlarmHandle, timestamp: u64) -> bool { self.init(); - unsafe { self.alarms.as_ref() }.lock(|alarms| { - let mut alarms = alarms.borrow_mut(); - + critical_section::with(|cs| { + let mut alarms = unsafe { self.alarms.as_ref() }.borrow_ref_mut(cs); let alarm = &mut alarms[alarm.id() as usize]; alarm.timestamp = timestamp; unsafe { self.signaler.as_ref() }.signal(); diff --git a/embassy-time/src/queue_generic.rs b/embassy-time/src/queue_generic.rs index 20ae7e6cc..0f67d9dbb 100644 --- a/embassy-time/src/queue_generic.rs +++ b/embassy-time/src/queue_generic.rs @@ -2,8 +2,7 @@ use core::cell::RefCell; use core::cmp::{min, Ordering}; use core::task::Waker; -use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex; -use embassy_sync::blocking_mutex::Mutex; +use critical_section::Mutex; use heapless::Vec; use crate::driver::{allocate_alarm, set_alarm, set_alarm_callback, AlarmHandle}; @@ -129,7 +128,7 @@ impl InnerQueue { } struct Queue { - inner: Mutex>>, + inner: Mutex>>, } impl Queue { @@ -140,8 +139,8 @@ impl Queue { } fn schedule_wake(&'static self, at: Instant, waker: &Waker) { - self.inner.lock(|inner| { - let mut inner = inner.borrow_mut(); + critical_section::with(|cs| { + let mut inner = self.inner.borrow_ref_mut(cs); if inner.is_none() {} @@ -159,8 +158,7 @@ impl Queue { } fn handle_alarm(&self) { - self.inner - .lock(|inner| inner.borrow_mut().as_mut().unwrap().handle_alarm()); + critical_section::with(|cs| self.inner.borrow_ref_mut(cs).as_mut().unwrap().handle_alarm()) } fn handle_alarm_callback(ctx: *mut ()) {