Add simple TLS implementation
This commit is contained in:
parent
ed6f4e98d3
commit
e5b6f47f9f
@ -1,21 +1,32 @@
|
|||||||
|
use crate::{
|
||||||
|
ptr,
|
||||||
|
sync::atomic::{AtomicUsize, Ordering}
|
||||||
|
};
|
||||||
|
|
||||||
pub type Key = usize;
|
pub type Key = usize;
|
||||||
|
|
||||||
|
static mut TLS_SLOTS: [*mut u8; 64] = [ptr::null_mut(); 64];
|
||||||
|
static NEXT_TLS: AtomicUsize = AtomicUsize::new(1);
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn create(_dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key {
|
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]
|
#[inline]
|
||||||
pub unsafe fn set(_key: Key, _value: *mut u8) {
|
pub unsafe fn set(key: Key, value: *mut u8) {
|
||||||
panic!("should not be used on this target");
|
unsafe { TLS_SLOTS[key - 1] = value }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn get(_key: Key) -> *mut u8 {
|
pub unsafe fn get(key: Key) -> *mut u8 {
|
||||||
panic!("should not be used on this target");
|
unsafe { TLS_SLOTS[key - 1]}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn destroy(_key: Key) {
|
pub unsafe fn destroy(_key: Key) {
|
||||||
panic!("should not be used on this target");
|
// Ignore destructors for now
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user