protect creation of destructors by a mutex

add on HermizCore an additional lock to protect static data
This commit is contained in:
Stefan Lankes 2019-11-13 00:21:05 +01:00
parent 3a1b3b30c6
commit 969b741446

View File

@ -3,6 +3,7 @@
use crate::collections::BTreeMap; use crate::collections::BTreeMap;
use crate::ptr; use crate::ptr;
use crate::sync::atomic::{AtomicUsize, Ordering}; use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sys_common::mutex::Mutex;
pub type Key = usize; pub type Key = usize;
@ -11,6 +12,7 @@
static NEXT_KEY: AtomicUsize = AtomicUsize::new(0); static NEXT_KEY: AtomicUsize = AtomicUsize::new(0);
static mut KEYS: *mut BTreeMap<Key, Option<Dtor>> = ptr::null_mut(); static mut KEYS: *mut BTreeMap<Key, Option<Dtor>> = ptr::null_mut();
static KEYS_LOCK: Mutex = Mutex::new();
#[thread_local] #[thread_local]
static mut LOCALS: *mut BTreeMap<Key, *mut u8> = ptr::null_mut(); static mut LOCALS: *mut BTreeMap<Key, *mut u8> = ptr::null_mut();
@ -32,6 +34,7 @@ unsafe fn locals() -> &'static mut BTreeMap<Key, *mut u8> {
#[inline] #[inline]
pub unsafe fn create(dtor: Option<Dtor>) -> Key { pub unsafe fn create(dtor: Option<Dtor>) -> Key {
let key = NEXT_KEY.fetch_add(1, Ordering::SeqCst); let key = NEXT_KEY.fetch_add(1, Ordering::SeqCst);
let _guard = KEYS_LOCK.lock();
keys().insert(key, dtor); keys().insert(key, dtor);
key key
} }