diff options
| author | Fenrir <[email protected]> | 2017-02-18 01:09:18 -0700 |
|---|---|---|
| committer | Fenrir <[email protected]> | 2017-02-18 01:12:14 -0700 |
| commit | 0436b3a945fb833647bbeb81fdc37dee69dc1e94 (patch) | |
| tree | 6e8571294b55969a32dfbe8c5077a89e2901eb9b /ctr-std/src/sys/unix/thread_local.rs | |
| parent | Merge pull request #16 from FenrirWolf/collections (diff) | |
| download | archived-ctru-rs-0436b3a945fb833647bbeb81fdc37dee69dc1e94.tar.xz archived-ctru-rs-0436b3a945fb833647bbeb81fdc37dee69dc1e94.zip | |
Add thread local storage support
Diffstat (limited to 'ctr-std/src/sys/unix/thread_local.rs')
| -rw-r--r-- | ctr-std/src/sys/unix/thread_local.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/ctr-std/src/sys/unix/thread_local.rs b/ctr-std/src/sys/unix/thread_local.rs new file mode 100644 index 0000000..abdd9ac --- /dev/null +++ b/ctr-std/src/sys/unix/thread_local.rs @@ -0,0 +1,66 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![allow(dead_code)] // not used on all platforms + +use collections::BTreeMap; +use ptr; +use sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; + +pub type Key = usize; + +type Dtor = unsafe extern fn(*mut u8); + +static NEXT_KEY: AtomicUsize = ATOMIC_USIZE_INIT; + +static mut KEYS: *mut BTreeMap<Key, Option<Dtor>> = ptr::null_mut(); + +#[thread_local] +static mut LOCALS: *mut BTreeMap<Key, *mut u8> = ptr::null_mut(); + +unsafe fn keys() -> &'static mut BTreeMap<Key, Option<Dtor>> { + if KEYS == ptr::null_mut() { + KEYS = Box::into_raw(Box::new(BTreeMap::new())); + } + &mut *KEYS +} + +unsafe fn locals() -> &'static mut BTreeMap<Key, *mut u8> { + if LOCALS == ptr::null_mut() { + LOCALS = Box::into_raw(Box::new(BTreeMap::new())); + } + &mut *LOCALS +} + +#[inline] +pub unsafe fn create(dtor: Option<Dtor>) -> Key { + let key = NEXT_KEY.fetch_add(1, Ordering::SeqCst); + keys().insert(key, dtor); + key +} + +#[inline] +pub unsafe fn get(key: Key) -> *mut u8 { + if let Some(&entry) = locals().get(&key) { + entry + } else { + ptr::null_mut() + } +} + +#[inline] +pub unsafe fn set(key: Key, value: *mut u8) { + locals().insert(key, value); +} + +#[inline] +pub unsafe fn destroy(key: Key) { + keys().remove(&key); +} |