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