Add simple TLS implementation

This commit is contained in:
pjht 2024-06-04 15:36:29 -05:00
parent ed6f4e98d3
commit e5b6f47f9f
Signed by: pjht
GPG Key ID: 7B5F6AFBEC7EE78E

View File

@ -1,21 +1,32 @@
use crate::{
ptr,
sync::atomic::{AtomicUsize, Ordering}
};
pub type Key = usize;
static mut TLS_SLOTS: [*mut u8; 64] = [ptr::null_mut(); 64];
static NEXT_TLS: AtomicUsize = AtomicUsize::new(1);
#[inline]
pub unsafe fn create(_dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key {
panic!("should not be used on this target");
if NEXT_TLS.load(Ordering::Relaxed) == 65 {
panic!("Out of TLS slots");
}
NEXT_TLS.fetch_add(1, Ordering::Relaxed)
}
#[inline]
pub unsafe fn set(_key: Key, _value: *mut u8) {
panic!("should not be used on this target");
pub unsafe fn set(key: Key, value: *mut u8) {
unsafe { TLS_SLOTS[key - 1] = value }
}
#[inline]
pub unsafe fn get(_key: Key) -> *mut u8 {
panic!("should not be used on this target");
pub unsafe fn get(key: Key) -> *mut u8 {
unsafe { TLS_SLOTS[key - 1]}
}
#[inline]
pub unsafe fn destroy(_key: Key) {
panic!("should not be used on this target");
// Ignore destructors for now
}