time: remove dependency on embassy-sync.

This commit is contained in:
Dario Nieuwenhuis 2023-04-13 23:50:41 +02:00
parent 62ecd97350
commit e7ff759f1c
3 changed files with 14 additions and 19 deletions

View File

@ -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"

View File

@ -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<EmbassyMutex<CriticalSectionRawMutex, RefCell<[AlarmState; ALARM_COUNT]>>>,
alarms: UninitCell<CsMutex<RefCell<[AlarmState; ALARM_COUNT]>>>,
zero_instant: UninitCell<StdInstant>,
signaler: UninitCell<Signaler>,
}
@ -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();

View File

@ -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<CriticalSectionRawMutex, RefCell<Option<InnerQueue>>>,
inner: Mutex<RefCell<Option<InnerQueue>>>,
}
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 ()) {