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

Commit fddfbdd9 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: sequence immediate message loop posts

this ensures order of operations is preserved

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost DirectHciTest
Change-Id: I6368fd2e07e95b505ac67c3e67343ba2a488b842
parent 6c1449d4
Loading
Loading
Loading
Loading
+27 −12
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ use std::convert::TryInto;
use std::sync::Arc;
use std::time::Duration;
use tokio::runtime::Runtime;
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};

#[cxx::bridge(namespace = bluetooth::shim::rust)]
mod ffi {
@@ -32,12 +33,21 @@ unsafe impl Send for ffi::OnceClosure {}

pub struct MessageLoopThread {
    rt: Arc<Runtime>,
    tx: UnboundedSender<cxx::UniquePtr<ffi::OnceClosure>>,
}

pub fn main_message_loop_thread_create() -> Box<MessageLoopThread> {
    assert!(init_flags::gd_rust_is_enabled());

    Box::new(MessageLoopThread { rt: crate::stack::RUNTIME.clone() })
    let rt = crate::stack::RUNTIME.clone();
    let (tx, mut rx) = unbounded_channel::<cxx::UniquePtr<ffi::OnceClosure>>();
    rt.spawn(async move {
        while let Some(c) = rx.recv().await {
            c.Run();
        }
    });

    Box::new(MessageLoopThread { rt, tx })
}

pub fn main_message_loop_thread_start(thread: &mut MessageLoopThread) -> i32 {
@@ -52,7 +62,11 @@ pub fn main_message_loop_thread_do_delayed(
    delay_ms: i64,
) {
    assert!(init_flags::gd_rust_is_enabled());

    if delay_ms == 0 {
        if thread.tx.send(closure).is_err() {
            log::error!("could not post task - shutting down?");
        }
    } else {
        thread.rt.spawn(async move {
            // NOTE: tokio's sleep can't wake up the system...
            // but hey, neither could the message loop from libchrome.
@@ -64,3 +78,4 @@ pub fn main_message_loop_thread_do_delayed(
            closure.Run();
        });
    }
}