use super::{Event, Timer};
use crate::Result;
use core::time::Duration;
use hicollections::List;
use hipool::{Boxed};
pub const POLLIN: u32 = 0x01; pub const POLLOUT: u32 = 0x04; pub const POLLET: u32 = 0x8000_0000; pub const POLLONESHOT: u32 = 0x4000_0000;
pub trait Poll {
fn add_event(&self, fd: i32, events: u32, e: u64) -> Result<()>;
fn mod_event(&self, fd: i32, events: u32, e: u64) -> Result<()>;
fn del_event(&self, fd: i32, e: u64) -> Result<()>;
fn wait<F>(&mut self, timeout: i32, f: F) -> Result<u32>
where
F: FnMut(u32, u64);
}
pub type BoxedScheduler<'a, A> = Boxed<'a, Scheduler, A>;
pub struct SchedulerVTable {
pub run: fn(this: *const ()),
pub stop: fn(this: *const ()),
pub stopped: fn(this: *const ()) -> bool,
pub now: fn(this: *const ()) -> Duration,
pub add_fd_event: unsafe fn(this: *const (), e: &Event, events: u32, fd: i32) -> Result<()>,
pub mod_fd_event: unsafe fn(this: *const (), e: &Event, events: u32, fd: i32) -> Result<()>,
pub del_fd_event: unsafe fn(this: *const (), e: &Event, fd: i32) -> Result<()>,
pub add_monitor: unsafe fn(this: *const (), e: &Event),
pub add_event: unsafe fn(this: *const (), e: &Event, events: u32, priority: i32),
pub add_event_list: unsafe fn(this: *const (), events: &mut List<Event>, priority: i32),
pub del_event: unsafe fn(this: *const (), e: &Event),
pub set_timer: unsafe fn(this: *const (), t: &Timer, msecs: u32),
pub del_timer: unsafe fn(this: *const (), t: &Timer),
pub set_private_data: fn(this: *const (), data: *const ()),
pub private_data: fn(this: *const ()) -> *const (),
pub release: fn(this: *const ()),
}
pub struct Scheduler {
vtbl: &'static SchedulerVTable,
}
impl Drop for Scheduler {
fn drop(&mut self) {
(self.vtbl.release)(self.as_ptr())
}
}
impl Scheduler {
fn as_ptr(&self) -> *const () {
self as *const _ as *const ()
}
}
impl Scheduler {
pub fn new(vtbl: &'static SchedulerVTable) -> Self {
Self { vtbl }
}
pub fn run(&mut self) {
(self.vtbl.run)(self.as_ptr())
}
pub fn stop(&self) {
(self.vtbl.stop)(self.as_ptr())
}
pub fn stopped(&self) -> bool {
(self.vtbl.stopped)(self.as_ptr())
}
pub fn now(&self) -> Duration {
(self.vtbl.now)(self.as_ptr())
}
pub unsafe fn add_fd_event(&self, e: &Event, events: u32, fd: i32) -> Result<()> {
(self.vtbl.add_fd_event)(self.as_ptr(), e, events, fd)
}
pub unsafe fn mod_fd_event(&self, e: &Event, events: u32, fd: i32) -> Result<()> {
(self.vtbl.mod_fd_event)(self.as_ptr(), e, events, fd)
}
pub unsafe fn del_fd_event(&self, e: &Event, fd: i32) -> Result<()> {
(self.vtbl.del_fd_event)(self.as_ptr(), e, fd)
}
pub unsafe fn add_monitor(&mut self, e: &Event) {
(self.vtbl.add_monitor)(self.as_ptr(), e)
}
pub unsafe fn add_event(&mut self, e: &Event, events: u32, priority: i32) {
(self.vtbl.add_event)(self.as_ptr(), e, events, priority)
}
pub unsafe fn add_event_list(&mut self, events: &mut List<Event>, priority: i32) {
(self.vtbl.add_event_list)(self.as_ptr(), events, priority)
}
pub unsafe fn del_event(&mut self, e: &Event) {
(self.vtbl.del_event)(self.as_ptr(), e)
}
pub unsafe fn set_timer(&mut self, t: &Timer, msecs: u32) {
(self.vtbl.set_timer)(self.as_ptr(), t, msecs)
}
pub unsafe fn del_timer(&mut self, t: &Timer) {
(self.vtbl.del_timer)(self.as_ptr(), t)
}
pub fn set_private_data(&mut self, data: *const ()) {
(self.vtbl.set_private_data)(self.as_ptr(), data)
}
pub fn private_data(&self) -> *const () {
(self.vtbl.private_data)(self.as_ptr())
}
}