Skip to content

Commit 5e01348

Browse files
committed
Make Handle trait public, add EventLoop.schedule_handle method
1 parent f3c99f1 commit 5e01348

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/event_loop.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct TCPListenerHandleData {
4343
server: TCPServerRef,
4444
}
4545

46-
pub(crate) struct EventLoopRunState {
46+
pub struct EventLoopRunState {
4747
buf: Box<[u8]>,
4848
events: event::Events,
4949
pub read_buf: Box<[u8]>,
@@ -631,6 +631,41 @@ impl EventLoop {
631631

632632
Ok(())
633633
}
634+
635+
#[allow(clippy::missing_errors_doc)]
636+
pub fn schedule_handle(&self, handle: impl Handle + Send + 'static, delay: Option<Duration>) -> Result<()> {
637+
match delay {
638+
Some(delay) => {
639+
let when = (Instant::now().duration_since(self.epoch) + delay).as_micros();
640+
let timer = Timer {
641+
handle: Box::new(handle),
642+
when,
643+
};
644+
{
645+
let mut guard = self
646+
.handles_sched
647+
.lock()
648+
.map_err(|_| anyhow::anyhow!("lock acquisition failed"))?;
649+
guard.push(timer);
650+
}
651+
}
652+
None => {
653+
{
654+
let mut guard = self
655+
.handles_ready
656+
.lock()
657+
.map_err(|_| anyhow::anyhow!("lock acquisition failed"))?;
658+
guard.push_back(Box::new(handle));
659+
}
660+
self.counter_ready.fetch_add(1, atomic::Ordering::Release);
661+
}
662+
}
663+
if self.idle.load(atomic::Ordering::Acquire) {
664+
self.wake();
665+
}
666+
667+
Ok(())
668+
}
634669
}
635670

636671
#[pymethods]

src/handles.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
#[cfg(not(PyPy))]
1111
use crate::py::run_in_ctx1;
1212

13-
pub(crate) trait Handle {
13+
pub trait Handle {
1414
fn run(&self, py: Python, event_loop: &EventLoop, state: &mut EventLoopRunState);
1515
fn cancelled(&self) -> bool {
1616
false

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use pyo3::prelude::*;
22
use std::sync::OnceLock;
33

44
pub mod event_loop;
5-
mod handles;
5+
pub mod handles;
66
mod io;
77
mod log;
88
mod py;

0 commit comments

Comments
 (0)