Skip to content

Commit

Permalink
Rollup merge of rust-lang#66350 - hermitcore:hermit, r=rkruppe
Browse files Browse the repository at this point in the history
protect creation of destructors by a mutex

- add on HermitCore an additional lock to protect static data
  • Loading branch information
Centril authored Nov 15, 2019
2 parents fd84f03 + 8871731 commit 12065b1
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/libstd/sys/hermit/thread_local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use crate::collections::BTreeMap;
use crate::ptr;
use crate::sync::atomic::{AtomicUsize, Ordering};
use crate::sys_common::mutex::Mutex;

pub type Key = usize;

Expand All @@ -11,6 +12,7 @@ type Dtor = unsafe extern fn(*mut u8);
static NEXT_KEY: AtomicUsize = AtomicUsize::new(0);

static mut KEYS: *mut BTreeMap<Key, Option<Dtor>> = ptr::null_mut();
static KEYS_LOCK: Mutex = Mutex::new();

#[thread_local]
static mut LOCALS: *mut BTreeMap<Key, *mut u8> = ptr::null_mut();
Expand All @@ -32,6 +34,7 @@ unsafe fn locals() -> &'static mut BTreeMap<Key, *mut u8> {
#[inline]
pub unsafe fn create(dtor: Option<Dtor>) -> Key {
let key = NEXT_KEY.fetch_add(1, Ordering::SeqCst);
let _guard = KEYS_LOCK.lock();
keys().insert(key, dtor);
key
}
Expand Down

0 comments on commit 12065b1

Please sign in to comment.