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

Commit 93943d05 authored by Joel Galenson's avatar Joel Galenson
Browse files

Do not close the fd on drop.

The nix crate now closes the fd itself, so we cannot close it ourselves.

Test: libbt_common_inline_tests
Change-Id: I270c84ff198416ad472be86b7d9fb506e3a74232
parent d7adb076
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