]> git.lizzy.rs Git - rust.git/blob - src/librustrt/thread_local_storage.rs
auto merge of #15999 : Kimundi/rust/fix_folder, r=nikomatsakis
[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="dragonfly")]
47 #[cfg(target_os="android")]
48 #[cfg(target_os = "ios")]
49 #[allow(non_camel_case_types)] // foreign type
50 type pthread_key_t = ::libc::c_uint;
51
52 #[cfg(unix)]
53 extern {
54     fn pthread_key_create(key: *mut pthread_key_t, dtor: *const u8) -> c_int;
55     fn pthread_key_delete(key: pthread_key_t) -> c_int;
56     fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
57     fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
58 }
59
60 #[cfg(windows)]
61 pub type Key = DWORD;
62
63 #[cfg(windows)]
64 pub unsafe fn create(key: &mut Key) {
65     static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
66     *key = TlsAlloc();
67     assert!(*key != TLS_OUT_OF_INDEXES);
68 }
69
70 #[cfg(windows)]
71 pub unsafe fn set(key: Key, value: *mut u8) {
72     assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void))
73 }
74
75 #[cfg(windows)]
76 pub unsafe fn get(key: Key) -> *mut u8 {
77     TlsGetValue(key) as *mut u8
78 }
79
80 #[cfg(windows)]
81 pub unsafe fn destroy(key: Key) {
82     assert!(TlsFree(key) != 0);
83 }
84
85 #[cfg(windows)]
86 #[allow(non_snake_case_functions)]
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 #[cfg(test)]
95 mod test {
96     use std::prelude::*;
97     use super::*;
98
99     #[test]
100     fn tls_smoke_test() {
101         use std::mem::transmute;
102         unsafe {
103             let mut key = 0;
104             let value = box 20i;
105             create(&mut key);
106             set(key, transmute(value));
107             let value: Box<int> = transmute(get(key));
108             assert_eq!(value, box 20i);
109             let value = box 30i;
110             set(key, transmute(value));
111             let value: Box<int> = transmute(get(key));
112             assert_eq!(value, box 30i);
113         }
114     }
115 }