]> git.lizzy.rs Git - rust.git/blob - src/libstd/rt/thread_local_storage.rs
5041b559ecbff8bb67b710f73012358920b19c08
[rust.git] / src / libstd / rt / thread_local_storage.rs
1 // Copyright 2013 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 libc::c_void;
12 #[cfg(unix)]
13 use libc::c_int;
14 #[cfg(unix)]
15 use ptr::null;
16 #[cfg(windows)]
17 use libc::types::os::arch::extra::{DWORD, LPVOID, BOOL};
18
19 #[cfg(unix)]
20 pub type Key = pthread_key_t;
21
22 #[cfg(unix)]
23 pub unsafe fn create(key: &mut Key) {
24     assert_eq!(0, pthread_key_create(key, null()));
25 }
26
27 #[cfg(unix)]
28 pub unsafe fn set(key: Key, value: *mut c_void) {
29     assert_eq!(0, pthread_setspecific(key, value));
30 }
31
32 #[cfg(unix)]
33 pub unsafe fn get(key: Key) -> *mut c_void {
34     pthread_getspecific(key)
35 }
36
37 #[cfg(target_os="macos")]
38 #[allow(non_camel_case_types)] // foreign type
39 type pthread_key_t = ::libc::c_ulong;
40
41 #[cfg(target_os="linux")]
42 #[cfg(target_os="freebsd")]
43 #[cfg(target_os="android")]
44 #[allow(non_camel_case_types)] // foreign type
45 type pthread_key_t = ::libc::c_uint;
46
47 #[cfg(unix)]
48 extern {
49     #[fast_ffi]
50     fn pthread_key_create(key: *mut pthread_key_t, dtor: *u8) -> c_int;
51     #[fast_ffi]
52     fn pthread_setspecific(key: pthread_key_t, value: *mut c_void) -> c_int;
53     #[fast_ffi]
54     fn pthread_getspecific(key: pthread_key_t) -> *mut c_void;
55 }
56
57 #[cfg(windows)]
58 pub type Key = DWORD;
59
60 #[cfg(windows)]
61 pub unsafe fn create(key: &mut Key) {
62     static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
63     *key = TlsAlloc();
64     assert!(*key != TLS_OUT_OF_INDEXES);
65 }
66
67 #[cfg(windows)]
68 pub unsafe fn set(key: Key, value: *mut c_void) {
69     assert!(0 != TlsSetValue(key, value))
70 }
71
72 #[cfg(windows)]
73 pub unsafe fn get(key: Key) -> *mut c_void {
74     TlsGetValue(key)
75 }
76
77 #[cfg(windows)]
78 #[abi = "stdcall"]
79 extern "stdcall" {
80        fn TlsAlloc() -> DWORD;
81        fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
82        fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
83 }
84
85 #[test]
86 fn tls_smoke_test() {
87     use cast::transmute;
88     unsafe {
89         let mut key = 0;
90         let value = ~20;
91         create(&mut key);
92         set(key, transmute(value));
93         let value: ~int = transmute(get(key));
94         assert_eq!(value, ~20);
95         let value = ~30;
96         set(key, transmute(value));
97         let value: ~int = transmute(get(key));
98         assert_eq!(value, ~30);
99     }
100 }