native_executor/
polyfill.rs1use std::{
4 sync::OnceLock,
5 thread::{self, ThreadId},
6};
7
8#[cfg(any(feature = "polyfill", target_os = "android"))]
9pub mod executor;
10
11#[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
38#[must_use]
41pub fn is_main_thread_registered() -> bool {
42 MAIN_THREAD_ID.get().is_some()
43}
44
45pub(crate) fn is_main_thread() -> bool {
46 MAIN_THREAD_ID
47 .get()
48 .is_some_and(|id| *id == thread::current().id())
49}