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