]> git.lizzy.rs Git - rust.git/blob - src/librustrt/thread_local_storage.rs
Add a few more derivings to AST types
[rust.git] / src / librustrt / thread_local_storage.rs
1 // Copyright 2013-2014 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 #![allow(dead_code)]
12
13 #[cfg(unix)] use libc::c_int;
14 #[cfg(unix)] use core::ptr::null;
15 #[cfg(windows)] use libc::types::os::arch::extra::{DWORD, LPVOID, BOOL};
16
17 #[cfg(unix)]
18 pub type Key = pthread_key_t;
19
20 #[cfg(unix)]
21 pub unsafe fn create(key: &mut Key) {
22     assert!(pthread_key_create(key, null()) == 0);
23 }
24
25 #[cfg(unix)]
26 pub unsafe fn set(key: Key, value: *mut u8) {
27     assert!(pthread_setspecific(key, value) == 0);
28 }
29
30 #[cfg(unix)]
31 pub unsafe fn get(key: Key) -> *mut u8 {
32     pthread_getspecific(key)
33 }
34
35 #[cfg(unix)]
36 pub unsafe fn destroy(key: Key) {
37     assert!(pthread_key_delete(key) == 0);
38 }
39
40 #[cfg(target_os = "macos")]
41 #[allow(non_camel_case_types)] // foreign type
42 type pthread_key_t = ::libc::c_ulong;
43
44 #[cfg(target_os="linux")]
45 #[cfg(target_os="freebsd")]
46 #[cfg(target_os="android")]
47 #[cfg(target_os = "ios")]
48 #[allow(non_camel_case_types)] // foreign type
49 type pthread_key_t = ::libc::c_uint;
50
51 #[cfg(unix)]
52 extern {
53     fn pthread_key_create(key: *mut pthread_key_t, dtor: *const u8) -> c_int;
54     fn pthread_key_delete(key: pthread_key_t) -> c_int;
55     fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
56     fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
57 }
58
59 #[cfg(windows)]
60 pub type Key = DWORD;
61
62 #[cfg(windows)]
63 pub unsafe fn create(key: &mut Key) {
64     static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
65     *key = TlsAlloc();
66     assert!(*key != TLS_OUT_OF_INDEXES);
67 }
68
69 #[cfg(windows)]
70 pub unsafe fn set(key: Key, value: *mut u8) {
71     assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void))
72 }
73
74 #[cfg(windows)]
75 pub unsafe fn get(key: Key) -> *mut u8 {
76     TlsGetValue(key) as *mut u8
77 }
78
79 #[cfg(windows)]
80 pub unsafe fn destroy(key: Key) {
81     assert!(TlsFree(key) != 0);
82 }
83
84 #[cfg(windows)]
85 #[allow(non_snake_case_functions)]
86 extern "system" {
87     fn TlsAlloc() -> DWORD;
88     fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
89     fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
90     fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
91 }
92
93 #[cfg(test)]
94 mod test {
95     use std::prelude::*;
96     use super::*;
97
98     #[test]
99     fn tls_smoke_test() {
100         use std::mem::transmute;
101         unsafe {
102             let mut key = 0;
103             let value = box 20i;
104             create(&mut key);
105             set(key, transmute(value));
106             let value: Box<int> = transmute(get(key));
107             assert_eq!(value, box 20i);
108             let value = box 30i;
109             set(key, transmute(value));
110             let value: Box<int> = transmute(get(key));
111             assert_eq!(value, box 30i);
112         }
113     }
114 }