Skip to content

Commit

Permalink
WIP: timer_*_nowake implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Dec 11, 2024
1 parent 0fb6761 commit 8af264d
Showing 1 changed file with 37 additions and 4 deletions.
41 changes: 37 additions & 4 deletions src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,14 @@ where
where
F: FnMut() + Send + 'static,
{
self.internal_timer(callback)
self.internal_timer(callback, false)
}

pub fn timer_nowake<F>(&self, callback: F) -> Result<EspTimer<'static>, EspError>
where
F: FnMut() + Send + 'static,
{
self.internal_timer(callback, true)
}

pub fn timer_async(&self) -> Result<EspAsyncTimer, EspError> {
Expand All @@ -233,6 +240,25 @@ where
})
}

pub fn timer_async_nowake(&self) -> Result<EspAsyncTimer, EspError> {
let notification = Arc::new(Notification::new());

let timer = {
let notification = Arc::downgrade(&notification);

self.timer_nowake(move || {
if let Some(notification) = notification.upgrade() {
notification.notify(NonZeroU32::new(1).unwrap());
}
})?
};

Ok(EspAsyncTimer {
timer,
notification,
})
}

/// # Safety
///
/// This method - in contrast to method `timer` - allows the user to pass
Expand Down Expand Up @@ -260,10 +286,17 @@ where
where
F: FnMut() + Send + 'a,
{
self.internal_timer(callback)
self.internal_timer(callback, false)
}

pub unsafe fn timer_nonstatic_nowake<'a, F>(&self, callback: F) -> Result<EspTimer<'a>, EspError>
where
F: FnMut() + Send + 'a,
{
self.internal_timer(callback, true)
}

fn internal_timer<'a, F>(&self, callback: F) -> Result<EspTimer<'a>, EspError>
fn internal_timer<'a, F>(&self, callback: F, skip_unhandled_events: bool) -> Result<EspTimer<'a>, EspError>
where
F: FnMut() + Send + 'a,
{
Expand Down Expand Up @@ -291,7 +324,7 @@ where
name: b"rust\0" as *const _ as *const _, // TODO
arg: unsafe_callback.as_ptr(),
dispatch_method,
skip_unhandled_events: self.skip_unhandled_events,
skip_unhandled_events,
},
&mut handle as *mut _,
)
Expand Down

0 comments on commit 8af264d

Please sign in to comment.