Skip to content

Commit

Permalink
remove useless nix
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Oct 13, 2023
1 parent fc05641 commit 4c7a789
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 25 deletions.
7 changes: 1 addition & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ std = ["error-code/std"]
# Enables C API wrapper for platform code.
c_wrapper = ["cc"]
# Enables usage of tokio 1.0
tokio1 = ["tokio_1", "nix", "std"]
tokio1 = ["tokio_1", "std"]
# Enables Stream implementation
stream = ["futures-core"]

Expand All @@ -39,11 +39,6 @@ default-features = true
libc = { version = "0.2.60", default-features = false }
tokio_1 = { package = "tokio", version = "1.15", default-features = false, optional = true, features = ["net"] }

#kqueue needs nix
#Avoid bumping nix for now because these idiots break perfectly usable API
[target.'cfg(any(target_os = "bitrig", target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))'.dependencies]
nix = { version = "0.26", optional = true }

[target.'cfg(any(target_arch = "wasm32"))'.dependencies]
wasm-bindgen = "0.2"

Expand Down
78 changes: 59 additions & 19 deletions src/timer/async_tokio1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,50 +96,90 @@ impl TimerFd for RawTimer {
#[cfg(any(target_os = "bitrig", target_os = "dragonfly", target_os = "freebsd", target_os = "ios", target_os = "macos", target_os = "netbsd", target_os = "openbsd"))]
impl TimerFd for RawTimer {
fn new() -> Self {
let fd = nix::sys::event::kqueue().unwrap_or(-1);
let fd = unsafe {
libc::kqueue()
};

//If you hit this, then most likely you run into OS imposed limit on file descriptor number
os_assert!(fd != -1);
Self(fd)
}

fn set(&mut self, time: time::Duration) {
use nix::sys::event::*;
let timeout = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let mut empty = [];
let mut event = libc::kevent {
ident: 1,
filter: libc::EVFILT_TIMER,
flags: libc::EV_ADD | libc::EV_ENABLE | libc::EV_ONESHOT,
fflags: libc::NOTE_NSECONDS,
data: 0,
udata: core::ptr::null_mut(),
};

let flags = EventFlag::EV_ADD | EventFlag::EV_ENABLE | EventFlag::EV_ONESHOT;
let mut time = time.as_nanos();
let mut unit = FilterFlag::NOTE_NSECONDS;

if time > isize::max_value() as u128 {
unit = FilterFlag::NOTE_USECONDS;
event.fflags = libc::NOTE_USECONDS;
time /= 1_000;
}
if time > isize::max_value() as u128 {
unit = FilterFlag::empty(); // default is milliseconds
event.fflags = 0; //default value is ms
time /= 1_000;
}
if time > isize::max_value() as u128 {
unit = FilterFlag::NOTE_SECONDS;
event.fflags = libc::NOTE_SECONDS;
time /= 1_000;
}

let time = time as isize;
kevent(self.0, &[KEvent::new(1, EventFilter::EVFILT_TIMER, flags, unit, time, 0)], &mut [], 0).expect("To arm timer");
event.data = time as _;
let set = unsafe {
libc::kevent(self.0, &event, 1, empty.as_mut_ptr(), 0, &timeout)
};
os_assert!(set != -1);
}

fn unset(&mut self) {
use nix::sys::event::*;

let flags = EventFlag::EV_DELETE;
kevent(self.0, &[KEvent::new(1, EventFilter::EVFILT_TIMER, flags, FilterFlag::empty(), 0, 0)], &mut [], 0).expect("To disarm timer");
let timeout = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let mut empty = [];
let event = libc::kevent {
ident: 1,
filter: libc::EVFILT_TIMER,
flags: libc::EV_DELETE,
fflags: 0,
data: 0,
udata: core::ptr::null_mut(),
};
let unset = unsafe {
libc::kevent(self.0, &event, 1, empty.as_mut_ptr(), 0, &timeout)
};
os_assert!(unset != -1);
}

fn read(&mut self) -> usize {
use nix::sys::event::*;

let mut ev = [KEvent::new(0, EventFilter::EVFILT_TIMER, EventFlag::empty(), FilterFlag::empty(), 0, 0)];

kevent(self.0, &[], &mut ev[..], 0).expect("To execute kevent")
let timeout = libc::timespec {
tv_sec: 0,
tv_nsec: 0,
};
let empty = [];
let mut event = libc::kevent {
ident: 0,
filter: libc::EVFILT_TIMER,
flags: 0,
fflags: 0,
data: 0,
udata: core::ptr::null_mut(),
};
let read = unsafe {
libc::kevent(self.0, empty.as_ptr(), 0, &mut event, 1, &timeout)
};
os_assert!(read != -1);
read as _
}
}

Expand Down

0 comments on commit 4c7a789

Please sign in to comment.