native_executor/
polyfill.rs

1//! Polyfill executor implementation using async-executor.
2
3use std::{
4    sync::OnceLock,
5    thread::{self, ThreadId},
6};
7
8#[cfg(any(feature = "polyfill", target_os = "android"))]
9pub mod executor;
10
11// polyfill timer would be used on non-apple platforms, including android
12#[cfg(any(feature = "polyfill", target_os = "android"))]
13pub mod timer;
14
15static MAIN_THREAD_ID: OnceLock<ThreadId> = OnceLock::new();
16
17pub(crate) fn register_main_thread() {
18    let id = thread::current().id();
19    if MAIN_THREAD_ID.set(id).is_err() {
20        let existing = MAIN_THREAD_ID
21            .get()
22            .copied()
23            .expect("main thread id not initialized");
24        assert_eq!(
25            existing, id,
26            "polyfill main executor already registered on a different thread"
27        );
28    }
29}
30
31pub(crate) fn assert_main_thread(op: &str) {
32    assert!(
33        is_main_thread(),
34        "{op} must be called from the polyfill main thread"
35    );
36}
37
38pub(crate) fn is_main_thread() -> bool {
39    MAIN_THREAD_ID
40        .get()
41        .is_some_and(|id| *id == thread::current().id())
42}