Skip to main content

embedded_wg_display/runtime/
platform.rs

1/// most basic func needed to run wasmtime in no_std: https://github.com/bytecodealliance/wasmtime/blob/main/examples/min-platform/embedding/wasmtime-platform.h
2/// memory for both cores is separated to avoid potential race conditions.
3use core::ptr;
4use core::sync::atomic::{AtomicPtr, Ordering};
5use esp_hal::system::Cpu;
6
7static WASMTIME_TLS: [AtomicPtr<u8>; 2] = [
8    AtomicPtr::new(ptr::null_mut()),
9    AtomicPtr::new(ptr::null_mut()),
10];
11
12#[inline(always)]
13fn current_core_index() -> usize {
14    match Cpu::current() {
15        Cpu::ProCpu => 0,
16        _ => 1,
17    }
18}
19
20#[unsafe(no_mangle)]
21pub unsafe extern "C" fn wasmtime_tls_get() -> *mut u8 {
22    WASMTIME_TLS[current_core_index()].load(Ordering::SeqCst)
23}
24
25#[unsafe(no_mangle)]
26pub unsafe extern "C" fn wasmtime_tls_set(ptr: *mut u8) {
27    WASMTIME_TLS[current_core_index()].store(ptr, Ordering::SeqCst);
28}