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

Commit 0aec55d7 authored by Abhishek Pandit-Subedi's avatar Abhishek Pandit-Subedi Committed by Abhishek Pandit-Subedi
Browse files

gd: Alarm should take const ref, not mut

Alarm's reset and cancel methods don't require mut references. Making
them const ref allows us to create and use `Arc<Alarm>`, which is safe
because AsyncFd implements Send + Sync.

Bug: 193261630
Test: atest --host bluetooth_test_common
Tag: #gd-refactor
Change-Id: Ia057815593abb1fed504c5c9d9dce7ce8b961151
parent 543402eb
Loading
Loading
Loading
Loading
+6 −6
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ impl Alarm {
    }

    /// Reset the alarm to duration, starting from now
    pub fn reset(&mut self, duration: Duration) {
    pub fn reset(&self, duration: Duration) {
        self.fd
            .get_ref()
            .set(Expiration::OneShot(TimeSpec::from(duration)), TimerSetTimeFlags::empty())
@@ -26,12 +26,12 @@ impl Alarm {
    }

    /// Stop the alarm if it is currently started
    pub fn cancel(&mut self) {
    pub fn cancel(&self) {
        self.reset(Duration::from_millis(0));
    }

    /// Completes when the alarm has expired
    pub async fn expired(&mut self) {
    pub async fn expired(&self) {
        let mut read_ready = self.fd.readable().await.unwrap();
        read_ready.clear_ready();
        drop(read_ready);
@@ -94,7 +94,7 @@ mod tests {
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let timer = Instant::now();
            let mut alarm = Alarm::new();
            let alarm = Alarm::new();
            alarm.reset(Duration::from_millis(10));
            alarm.expired().await;

@@ -106,7 +106,7 @@ mod tests {
    fn alarm_cancel_after_expired() {
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let mut alarm = Alarm::new();
            let alarm = Alarm::new();
            alarm.reset(Duration::from_millis(10));
            tokio::time::sleep(Duration::from_millis(30)).await;
            alarm.cancel();
@@ -131,7 +131,7 @@ mod tests {
        let runtime = tokio::runtime::Runtime::new().unwrap();
        runtime.block_on(async {
            let timer = Instant::now();
            let mut alarm = Alarm::new();
            let alarm = Alarm::new();
            alarm.reset(Duration::from_millis(10));
            alarm.expired().await;
            let ready_in_10_ms = async {
+1 −1
Original line number Diff line number Diff line
@@ -173,7 +173,7 @@ async fn dispatch(
    mut cmd_rx: Receiver<QueuedCommand>,
) {
    let mut pending: Option<QueuedCommand> = None;
    let mut hci_timeout = Alarm::new();
    let hci_timeout = Alarm::new();
    loop {
        select! {
            Some(evt) = consume(&evt_rx) => {