|
| 1 | +use crate::sync::{RwLock, Mutex}; |
| 2 | +use crate::os::custom::thread::{ThreadId, IMPL}; |
| 3 | + |
| 4 | +pub type Key = usize; |
| 5 | + |
| 6 | +const NULL: *mut u8 = core::ptr::null_mut(); |
| 7 | + |
| 8 | +type Destructor = Option<unsafe extern "C" fn(*mut u8)>; |
| 9 | + |
| 10 | +fn thread_id() -> Option<ThreadId> { |
| 11 | + let reader = IMPL.read_no_poison_check(); |
| 12 | + reader.as_ref()?.current_thread_id() |
| 13 | +} |
| 14 | + |
| 15 | +// safety: each ThreadStorage is virtually owned by one thread |
| 16 | +unsafe impl Sync for ThreadStorage {} |
| 17 | +unsafe impl Send for ThreadStorage {} |
| 18 | + |
| 19 | +struct ThreadStorage(Mutex<Vec<*mut u8>>); |
| 20 | + |
| 21 | +impl ThreadStorage { |
| 22 | + pub const fn new() -> Self { |
| 23 | + Self(Mutex::new(Vec::new())) |
| 24 | + } |
| 25 | + |
| 26 | + pub fn get(&self, key: Key) -> *mut u8 { |
| 27 | + let locked = self.0.lock_no_poison_check(); |
| 28 | + match locked.get(key - 1) { |
| 29 | + Some(pointer_ref) => *pointer_ref, |
| 30 | + None => NULL, |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + pub fn set(&self, key: Key, value: *mut u8) { |
| 35 | + let mut locked = self.0.lock_no_poison_check(); |
| 36 | + locked.resize(key, NULL); |
| 37 | + locked[key - 1] = value; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +struct Slots { |
| 42 | + destructors: RwLock<Vec<Destructor>>, |
| 43 | + initial_thread: ThreadStorage, |
| 44 | + other_threads: RwLock<Vec<(ThreadId, ThreadStorage)>>, |
| 45 | +} |
| 46 | + |
| 47 | +impl Slots { |
| 48 | + pub const fn new() -> Self { |
| 49 | + Self { |
| 50 | + destructors: RwLock::new(Vec::new()), |
| 51 | + initial_thread: ThreadStorage::new(), |
| 52 | + other_threads: RwLock::new(Vec::new()), |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + pub fn push(&self, destructor: Destructor) -> Key { |
| 57 | + let mut destructors = self.destructors.write_no_poison_check(); |
| 58 | + destructors.push(destructor); |
| 59 | + destructors.len() |
| 60 | + } |
| 61 | + |
| 62 | + pub fn get_dtor(&self, key: Key) -> Destructor { |
| 63 | + let destructors = self.destructors.read_no_poison_check(); |
| 64 | + destructors[key - 1] |
| 65 | + } |
| 66 | + |
| 67 | + pub fn with_thread_storage<T, F: FnOnce(&ThreadStorage) -> T>(&self, callback: F) -> T { |
| 68 | + if let Some(id) = thread_id() { |
| 69 | + loop { |
| 70 | + let finder = |(tid, _): &(ThreadId, ThreadStorage)| tid.cmp(&id); |
| 71 | + |
| 72 | + { |
| 73 | + let other_threads = self.other_threads.read_no_poison_check(); |
| 74 | + let position = other_threads.binary_search_by(finder); |
| 75 | + |
| 76 | + if let Ok(i) = position { |
| 77 | + return callback(&other_threads[i].1); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // cannot re-use `position` (race condition) |
| 82 | + let mut other_threads = self.other_threads.write_no_poison_check(); |
| 83 | + let i = other_threads.binary_search_by(finder).unwrap_err(); |
| 84 | + other_threads.insert(i, (id, ThreadStorage::new())); |
| 85 | + } |
| 86 | + } else { |
| 87 | + callback(&self.initial_thread) |
| 88 | + } |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +static SLOTS: Slots = Slots::new(); |
| 93 | + |
| 94 | +#[inline] |
| 95 | +pub unsafe fn create(dtor: Option<unsafe extern "C" fn(*mut u8)>) -> Key { |
| 96 | + SLOTS.push(dtor) |
| 97 | +} |
| 98 | + |
| 99 | +#[inline] |
| 100 | +pub unsafe fn set(key: Key, value: *mut u8) { |
| 101 | + SLOTS.with_thread_storage(|storage| storage.set(key, value)) |
| 102 | +} |
| 103 | + |
| 104 | +#[inline] |
| 105 | +pub unsafe fn get(key: Key) -> *mut u8 { |
| 106 | + SLOTS.with_thread_storage(|storage| storage.get(key)) |
| 107 | +} |
| 108 | + |
| 109 | +#[inline] |
| 110 | +pub unsafe fn destroy(key: Key) { |
| 111 | + let value = get(key); |
| 112 | + if !value.is_null() { |
| 113 | + if let Some(destructor) = SLOTS.get_dtor(key) { |
| 114 | + destructor(value) |
| 115 | + } |
| 116 | + } |
| 117 | +} |
0 commit comments