]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/sgx/thread_local.rs
2126e0a853eb01618922a2605b1a2e5c16c18a57
[rust.git] / src / libstd / sys / sgx / thread_local.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use boxed::Box;
12 use ptr;
13
14 pub type Key = usize;
15
16 struct Allocated {
17     value: *mut u8,
18     dtor: Option<unsafe extern fn(*mut u8)>,
19 }
20
21 #[inline]
22 pub unsafe fn create(dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
23     Box::into_raw(Box::new(Allocated {
24         value: ptr::null_mut(),
25         dtor,
26     })) as usize
27 }
28
29 #[inline]
30 pub unsafe fn set(key: Key, value: *mut u8) {
31     (*(key as *mut Allocated)).value = value;
32 }
33
34 #[inline]
35 pub unsafe fn get(key: Key) -> *mut u8 {
36     (*(key as *mut Allocated)).value
37 }
38
39 #[inline]
40 pub unsafe fn destroy(key: Key) {
41     let key = Box::from_raw(key as *mut Allocated);
42     if let Some(f) = key.dtor {
43         f(key.value);
44     }
45 }
46
47 #[inline]
48 pub fn requires_synchronized_create() -> bool {
49     false
50 }