#![allow(unsafe_code)]
use std::collections::HashMap;
use std::sync::{Mutex, OnceLock};
#[derive(Clone, Copy, Debug, Default)]
pub struct TlsSlot {
pub host_tpidr: u64,
pub guest_tpidr: u64,
pub active: bool,
}
#[derive(Clone, Copy, Debug)]
pub struct AltStackSlot {
pub base: *mut u8,
pub len: usize,
pub top: u64,
}
unsafe impl Send for AltStackSlot {}
#[derive(Clone, Copy, Debug, Default)]
pub struct WorkerSlot {
pub exit_pc: u64,
pub exit_sp: u64,
pub has_exit: bool,
pub guest_pthread: u64,
pub guest_tid: u64,
}
#[derive(Default)]
struct ThreadSlot {
tls: TlsSlot,
alt: Option<AltStackSlot>,
worker: WorkerSlot,
}
fn slots() -> &'static Mutex<HashMap<u64, ThreadSlot>> {
static SLOTS: OnceLock<Mutex<HashMap<u64, ThreadSlot>>> = OnceLock::new();
SLOTS.get_or_init(|| Mutex::new(HashMap::new()))
}
#[must_use]
pub fn os_tid() -> u64 {
#[cfg(target_os = "linux")]
{
let raw = unsafe { libc::syscall(libc::SYS_gettid) };
u64::try_from(raw).unwrap_or(0)
}
#[cfg(not(target_os = "linux"))]
{
let p = unsafe { libc::pthread_self() };
#[allow(clippy::as_conversions)]
{
p as u64
}
}
}
fn with_slot_init<R>(f: impl FnOnce(&mut ThreadSlot) -> R) -> R {
let tid = os_tid();
let mut guard = slots()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let slot = guard.entry(tid).or_default();
f(slot)
}
fn with_slot_existing_mut<R>(f: impl FnOnce(&mut ThreadSlot) -> R) -> Option<R> {
let tid = os_tid();
let mut guard = slots()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let slot = guard.get_mut(&tid)?;
Some(f(slot))
}
fn with_slot_existing<R>(f: impl FnOnce(&ThreadSlot) -> R) -> Option<R> {
let tid = os_tid();
let guard = slots()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
guard.get(&tid).map(f)
}
pub fn with_tls_init<R>(f: impl FnOnce(&mut TlsSlot) -> R) -> R {
with_slot_init(|s| f(&mut s.tls))
}
pub fn with_tls_existing_mut<R>(f: impl FnOnce(&mut TlsSlot) -> R) -> Option<R> {
with_slot_existing_mut(|s| f(&mut s.tls))
}
#[must_use]
pub fn tls_get() -> TlsSlot {
with_slot_existing(|s| s.tls).unwrap_or_default()
}
pub fn with_alt_mut<R>(f: impl FnOnce(&mut Option<AltStackSlot>) -> R) -> R {
let tid = os_tid();
let mut guard = slots()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(slot) = guard.get_mut(&tid) {
return f(&mut slot.alt);
}
let slot = guard.entry(tid).or_default();
f(&mut slot.alt)
}
pub fn with_worker_mut<R>(f: impl FnOnce(&mut WorkerSlot) -> R) -> R {
let tid = os_tid();
let mut guard = slots()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if let Some(slot) = guard.get_mut(&tid) {
return f(&mut slot.worker);
}
let slot = guard.entry(tid).or_default();
f(&mut slot.worker)
}
#[must_use]
pub fn worker_get() -> WorkerSlot {
with_slot_existing(|s| s.worker).unwrap_or_default()
}
pub fn clear_current() {
let tid = os_tid();
let mut guard = slots()
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let _removed = guard.remove(&tid);
}