Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit ee13091d authored by Treehugger Robot's avatar Treehugger Robot Committed by Gerrit Code Review
Browse files

Merge "Do not close the fd on drop."

parents cce79b1b 93943d05
Loading
Loading
Loading
Loading
+7 −32
Original line number Diff line number Diff line
@@ -2,8 +2,6 @@
///Tokio's time
use nix::sys::time::TimeSpec;
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags};
use nix::unistd::close;
use std::os::unix::io::AsRawFd;
use std::time::Duration;
use tokio::io::unix::AsyncFd;

@@ -16,18 +14,14 @@ impl Alarm {
    /// Construct a new alarm
    pub fn new() -> Self {
        let timer = TimerFd::new(get_clock(), TimerFlags::empty()).unwrap();
        Self {
            fd: AsyncFd::new(timer).unwrap(),
        }
        Self { fd: AsyncFd::new(timer).unwrap() }
    }

    /// Reset the alarm to duration, starting from now
    pub fn reset(&mut self, duration: Duration) {
        self.fd.get_ref()
            .set(
                Expiration::OneShot(TimeSpec::from(duration)),
                TimerSetTimeFlags::empty(),
            )
        self.fd
            .get_ref()
            .set(Expiration::OneShot(TimeSpec::from(duration)), TimerSetTimeFlags::empty())
            .unwrap();
    }

@@ -50,25 +44,12 @@ impl Default for Alarm {
    }
}

impl Drop for Alarm {
    fn drop(&mut self) {
        close(self.fd.as_raw_fd()).unwrap();
    }
}

/// Similar to tokio's interval, except the first tick does *not* complete immediately
pub fn interval(period: Duration) -> Interval {
    let timer = TimerFd::new(get_clock(), TimerFlags::empty()).unwrap();
    timer
        .set(
            Expiration::Interval(TimeSpec::from(period)),
            TimerSetTimeFlags::empty(),
        )
        .unwrap();
    timer.set(Expiration::Interval(TimeSpec::from(period)), TimerSetTimeFlags::empty()).unwrap();

    Interval {
        fd: AsyncFd::new(timer).unwrap(),
    }
    Interval { fd: AsyncFd::new(timer).unwrap() }
}

/// Future returned by interval()
@@ -85,12 +66,6 @@ impl Interval {
    }
}

impl Drop for Interval {
    fn drop(&mut self) {
        close(self.fd.as_raw_fd()).unwrap();
    }
}

fn get_clock() -> ClockId {
    if cfg!(target_os = "android") {
        ClockId::CLOCK_BOOTTIME_ALARM