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 Original line Diff line number Diff line
@@ -2,8 +2,6 @@
///Tokio's time
///Tokio's time
use nix::sys::time::TimeSpec;
use nix::sys::time::TimeSpec;
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags};
use nix::sys::timerfd::{ClockId, Expiration, TimerFd, TimerFlags, TimerSetTimeFlags};
use nix::unistd::close;
use std::os::unix::io::AsRawFd;
use std::time::Duration;
use std::time::Duration;
use tokio::io::unix::AsyncFd;
use tokio::io::unix::AsyncFd;


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


    /// Reset the alarm to duration, starting from now
    /// Reset the alarm to duration, starting from now
    pub fn reset(&mut self, duration: Duration) {
    pub fn reset(&mut self, duration: Duration) {
        self.fd.get_ref()
        self.fd
            .set(
            .get_ref()
                Expiration::OneShot(TimeSpec::from(duration)),
            .set(Expiration::OneShot(TimeSpec::from(duration)), TimerSetTimeFlags::empty())
                TimerSetTimeFlags::empty(),
            )
            .unwrap();
            .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
/// Similar to tokio's interval, except the first tick does *not* complete immediately
pub fn interval(period: Duration) -> Interval {
pub fn interval(period: Duration) -> Interval {
    let timer = TimerFd::new(get_clock(), TimerFlags::empty()).unwrap();
    let timer = TimerFd::new(get_clock(), TimerFlags::empty()).unwrap();
    timer
    timer.set(Expiration::Interval(TimeSpec::from(period)), TimerSetTimeFlags::empty()).unwrap();
        .set(
            Expiration::Interval(TimeSpec::from(period)),
            TimerSetTimeFlags::empty(),
        )
        .unwrap();


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


/// Future returned by interval()
/// 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 {
fn get_clock() -> ClockId {
    if cfg!(target_os = "android") {
    if cfg!(target_os = "android") {
        ClockId::CLOCK_BOOTTIME_ALARM
        ClockId::CLOCK_BOOTTIME_ALARM