JFLib

plat/android_thread.rs#

Source: lib/plat/android_thread.rs

android_thread -- threads (Bionic's pthreads), semaphores (futex), the UI thread's wake-up (an
eventfd), sleeps and the system's numbers on Android (docs/ANDROID.md AN1; the same functions as
linux_thread.rs and apple_thread.rs)

Threads only through pthread_create: Bionic gives every thread its TLS block, its signal stack and
a guard page, and a thread of a raw clone would crash in the first Bionic function it calls.  The
rest is the Linux way, with rt::kernel's numbers (decision 2): the semaphores are linux_thread.rs's
futex counters, the wake-up is its eventfd.  The host's looper on the UI thread watches the eventfd
(android/bridge.c, ALooper_addFd) and asks the Choreographer for a frame; that frame's tick turns
the wake-up into E_WAKE (drain_wake).  A program without a UI (android/exe_main.c) waits on it in
wait_events, as on Linux.  Signals stay Bionic's and debuggerd's: no handlers of our own (a panic
that leaves a thread ends the program with exit code 3, rt::abort_on_unwind).
  • struct Thread(usize) a thread started by thread_spawn; join it to wait for its end

  • fn thread_spawn(stack: usize, f: Start) -> Option<Thread> run f on a new thread with stack bytes of stack (0 = 1 MB; rounded up to 16 KiB) [plat_thread_create]

impl Thread

  • fn join(self) wait until the thread has ended [plat_thread_join]

  • fn thread_yield() give the rest of the time slice away [plat_thread_yield]

  • fn sleep_ms(ms: u32) sleep ms milliseconds (ppoll without descriptors, as on Linux); not on the UI thread, which must answer within five seconds (an ANR)

  • struct Sem(Box<SemInner>) a counting semaphore

impl Sem

  • fn new(initial: u32) -> Option<Sem>

  • fn post(&self, n: u32) add n units, wake sleepers (0: nothing)

  • fn wait(&self, ms: i32) -> bool take one unit: true taken, false timeout (ms -1 = forever, 0 = only try)

  • extern "C" fn jfgui_wake_fd() -> i32 (android/bridge.c) the descriptor the UI thread's looper watches (ALooper_addFd): readable after a wake(); -1 without one

  • fn wake() any thread: E_WAKE on the UI thread (coalesced); under the host its looper asks for a frame at once [plat_wake]

  • fn wait_events(deadline_us: u64, on_event: &mut dyn FnMut(&Event)) -> u32 headless wait (a program without a UI; scripts): until a wake-up (E_WAKE to on_event, returns 1) or the deadline in time_us() (0 = only poll, u64::MAX = forever; returns 0)

  • fn cpu_count() -> u32 CPUs online (sysconf), >= 1. big.LITTLE phones count every kind of core [plat_cpu_count]

  • fn mem_total() -> u64 physical memory in bytes (MemTotal of /proc/meminfo), 0 unknown [plat_mem_total]

Source: docs/API.md in the JFLib repository.