From eb20ca0ac23d772d4c233eaa682d5402d4f34780 Mon Sep 17 00:00:00 2001 From: overweight Date: Mon, 22 Nov 2021 14:30:17 +0800 Subject: [PATCH 1/2] remove Cargo.lock and add into .gitignore --- Cargo.lock | 74 ------------------------------------------------------ 1 file changed, 74 deletions(-) delete mode 100644 Cargo.lock diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index 911e8f1e..00000000 --- a/Cargo.lock +++ /dev/null @@ -1,74 +0,0 @@ -# This file is automatically @generated by Cargo. -# It is not intended for manual editing. -version = 3 - -[[package]] -name = "autocfg" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" - -[[package]] -name = "bitflags" -version = "1.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" - -[[package]] -name = "cc" -version = "1.0.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "79c2681d6594606957bbb8631c4b90a7fcaaa72cdb714743a437b156d6a7eedd" - -[[package]] -name = "cfg-if" -version = "1.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" - -[[package]] -name = "init" -version = "0.1.0" -dependencies = [ - "nix", -] - -[[package]] -name = "libc" -version = "0.2.103" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8f7255a17a627354f321ef0055d63b898c6fb27eff628af4d1b66b7331edf6" - -[[package]] -name = "memoffset" -version = "0.6.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9" -dependencies = [ - "autocfg", -] - -[[package]] -name = "nix" -version = "0.23.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f305c2c2e4c39a82f7bf0bf65fb557f9070ce06781d4f2454295cc34b1c43188" -dependencies = [ - "bitflags", - "cc", - "cfg-if", - "libc", - "memoffset", -] - -[[package]] -name = "process1" -version = "0.1.0" - -[[package]] -name = "systemd" -version = "0.1.0" - -[[package]] -name = "tests" -version = "0.1.0" -- Gitee From 61fea1e5b933cd1149f478bf550bac713b43980d Mon Sep 17 00:00:00 2001 From: overweight Date: Wed, 24 Nov 2021 21:16:34 +0800 Subject: [PATCH 2/2] add platform-independent poll for event-drive programming framwork --- Cargo.toml | 1 + src/event/Cargo.toml | 2 +- src/event/src/lib.rs | 150 +------------------------------------ src/event/src/sys/epoll.rs | 81 ++++++++++++++++++++ src/event/src/sys/mod.rs | 43 +++++++++++ 5 files changed, 127 insertions(+), 150 deletions(-) create mode 100644 src/event/src/sys/epoll.rs create mode 100644 src/event/src/sys/mod.rs diff --git a/Cargo.toml b/Cargo.toml index b5f089a9..ebc13507 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ + "src/event", "src/init", "src/process1", "src/systemd", diff --git a/src/event/Cargo.toml b/src/event/Cargo.toml index 7fc3bf75..21a84da1 100644 --- a/src/event/Cargo.toml +++ b/src/event/Cargo.toml @@ -7,4 +7,4 @@ edition = "2018" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -utils = { path = "../utils" } +libc = { version = "0.2.*" } diff --git a/src/event/src/lib.rs b/src/event/src/lib.rs index a77740f6..e79c79d9 100644 --- a/src/event/src/lib.rs +++ b/src/event/src/lib.rs @@ -1,149 +1 @@ -#[cfg(test)] -mod tests { - #[test] - fn it_works() { - assert_eq!(2 + 2, 4); - } -} - -#[warn(dead_code)] -#[derive(Debug)] -#[warn(unused_variables)] -struct Todo { - unimplemented: i32, -} - -type Type = Todo; -type RetType = Todo; - - -#[derive(Debug)] -enum EVENT_TYPE { - EVENT_OFF = 0, - EVENT_ON = 1, - EVENT_ONESHOT = -1, -} - -#[derive(Debug)] -enum EVENT_STATE { - EVENT_INITIAL, - EVENT_ARMED, - EVENT_PENDING, - EVENT_RUNNING, - EVENT_EXITING, - EVENT_FINISHED, - EVENT_PREPARING, -} - -#[derive(Debug)] -enum EVENT_PRIORITY { - EVENT_PRIORITY_IMPORTANT = -100, - EVENT_PRIORITY_NORMAL = 0, - EVENT_PRIORITY_IDLE = 100, -} - -struct Event { - n_ref: u32, - epoll_fd: i32, - wd_fd: i32, - -} - -impl Event { - fn new(arg: Type) -> Self { - unimplemented!(); - } - - fn default(arg: Type) -> RetType { - unimplemented!(); - } - - fn for_loop(arg: Type) -> RetType { - unimplemented!(); - } - - fn dispatch(arg: Type) -> RetType { - unimplemented!(); - } - - fn run(arg: Type) -> RetType { - unimplemented!(); - } - - fn wait(arg: Type) -> RetType { - unimplemented!(); - } - - fn exit(arg: Type) -> RetType { - unimplemented!(); - } - - fn refer(arg: Type) -> RetType { - unimplemented!(); - } - - fn unrefer(arg: Type) -> RetType { - unimplemented!(); - } - - fn now(arg: Type) -> RetType { - unimplemented!(); - } - - ///set get functions - fn get_fd(arg: Type) -> RetType { - unimplemented!(); - } - - fn get_state(arg: Type) -> RetType { - unimplemented!(); - } - - fn exit_code(arg: Type) -> RetType { - unimplemented!(); - } - - fn set_watchdog(arg: Type) -> RetType { - unimplemented!(); - } - - fn get_watchdog(arg: Type) -> RetType { - unimplemented!(); - } - - fn get_iteration(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_io(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_time(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_signal(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_inotify(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_child(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_defer(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_post(arg: Type) -> RetType { - unimplemented!(); - } - - fn add_exit(arg: Type) -> RetType { - unimplemented!(); - } -} \ No newline at end of file +mod sys; diff --git a/src/event/src/sys/epoll.rs b/src/event/src/sys/epoll.rs new file mode 100644 index 00000000..ef6caaf1 --- /dev/null +++ b/src/event/src/sys/epoll.rs @@ -0,0 +1,81 @@ +use std::{io, os::unix::io::RawFd, ptr, sync::atomic::{AtomicUsize, Ordering::Relaxed}}; +use libc::{epoll_event, EPOLL_CLOEXEC, EPOLL_CTL_ADD, EPOLL_CTL_DEL, EPOLL_CTL_MOD}; + +#[allow(unused_macros)] +macro_rules! syscall { + ($fn: ident ( $($arg: expr),* $(,)* ) ) => {{ + let res = unsafe { libc::$fn($($arg, )*) }; + if res == -1 { + Err(std::io::Error::last_os_error()) + } else { + Ok(res) + } + }}; +} + +#[derive(Debug)] +pub struct Epoll { + epoll_fd: RawFd, + n_sources: AtomicUsize, +} + +impl Epoll { + pub fn new() -> io::Result { + syscall!(epoll_create1(EPOLL_CLOEXEC)).map( | ep | + Epoll { epoll_fd: ep, + n_sources: AtomicUsize::new(0), + }) + } + + pub fn poll(&mut self, timeout: Option) -> io::Result> { + let mut events = Vec::::with_capacity(self.n_sources.load(Relaxed)); + let _n_ready = syscall!(epoll_wait( + self.epoll_fd, + events.as_mut_ptr(), + events.capacity() as i32, + timeout.map(|to| to.as_millis() as libc::c_int).unwrap_or(-1), + )); + Ok(events) + } + + pub fn register(&mut self, fd: RawFd, event: &mut epoll_event) -> io::Result<()> { + self.n_sources.fetch_add(1, Relaxed); + syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_ADD, fd, event)).map(|_| ()) + } + + pub fn reregister(&mut self, fd: RawFd, event: &mut epoll_event) -> io::Result<()> { + syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_MOD, fd, event)).map(|_| ()) + } + + pub fn unregister(&mut self, fd: RawFd) -> io::Result<()> { + self.n_sources.fetch_sub(1, Relaxed); + syscall!(epoll_ctl(self.epoll_fd, EPOLL_CTL_DEL, fd, ptr::null_mut())).map(|_| ()) + } +} + +impl Drop for Epoll { + fn drop(&mut self) { + let _ = syscall!(close(self.epoll_fd)); + } +} + +#[cfg(test)] +mod test { + use libc::EPOLLIN; + use super::Epoll; + + #[cfg(unix)] + #[test] + fn epoll_new() { + let _ = Epoll::new(); + } + + #[cfg(unix)] + #[test] + fn epoll_add() { + let mut poll = Epoll::new().unwrap(); + let mut events = libc::epoll_event {events: EPOLLIN as u32, u64: 0,}; + poll.register(poll.epoll_fd, &mut events); + poll.unregister(poll.epoll_fd); + } +} \ No newline at end of file diff --git a/src/event/src/sys/mod.rs b/src/event/src/sys/mod.rs new file mode 100644 index 00000000..393d11a9 --- /dev/null +++ b/src/event/src/sys/mod.rs @@ -0,0 +1,43 @@ +mod epoll; + +use std::{io, os::unix::io::RawFd, time}; +use epoll::Epoll as Epoll; +use libc::epoll_event; + +#[derive(Debug)] +pub struct Poll { + poller: Epoll, +} + +impl Poll { + pub(crate) fn new() -> io::Result { + Ok( Poll { poller: Epoll::new()?, }) + } + + pub(crate) fn poll( + &mut self, + timeout: Option, + ) -> io::Result> { + self.poller.poll(timeout) + } + + pub(crate) unsafe fn register( + &mut self, + fd: RawFd, + event: &mut epoll_event, + ) -> io::Result<()> { + self.poller.register(fd, event) + } + + pub(crate) unsafe fn reregister( + &mut self, + fd: RawFd, + event: &mut epoll_event, + ) -> io::Result<()> { + self.poller.reregister(fd, event) + } + + pub(crate) fn unregister(&mut self, fd: RawFd) -> io::Result<()> { + self.poller.unregister(fd) + } +} -- Gitee