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

Commit 744eaac2 authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Do not close the fd on drop." am: ee13091d

Original change: https://android-review.googlesource.com/c/platform/system/bt/+/1662765

Change-Id: I94233e774bc4174cc1be46ad37e7ab7bd2cbc0b8
parents a6bd81d5 ee13091d
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