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.
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.
17 use libc::types::os::arch::extra::{DWORD, LPVOID, BOOL};
20 pub type Key = pthread_key_t;
23 pub unsafe fn create(key: &mut Key) {
24 assert_eq!(0, pthread_key_create(key, null()));
28 pub unsafe fn set(key: Key, value: *mut c_void) {
29 assert_eq!(0, pthread_setspecific(key, value));
33 pub unsafe fn get(key: Key) -> *mut c_void {
34 pthread_getspecific(key)
37 #[cfg(target_os="macos")]
38 #[allow(non_camel_case_types)] // foreign type
39 type pthread_key_t = ::libc::c_ulong;
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;
50 fn pthread_key_create(key: *mut pthread_key_t, dtor: *u8) -> c_int;
52 fn pthread_setspecific(key: pthread_key_t, value: *mut c_void) -> c_int;
54 fn pthread_getspecific(key: pthread_key_t) -> *mut c_void;
61 pub unsafe fn create(key: &mut Key) {
62 static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
64 assert!(*key != TLS_OUT_OF_INDEXES);
68 pub unsafe fn set(key: Key, value: *mut c_void) {
69 assert!(0 != TlsSetValue(key, value))
73 pub unsafe fn get(key: Key) -> *mut c_void {
80 fn TlsAlloc() -> DWORD;
81 fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
82 fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
92 set(key, transmute(value));
93 let value: ~int = transmute(get(key));
94 assert_eq!(value, ~20);
96 set(key, transmute(value));
97 let value: ~int = transmute(get(key));
98 assert_eq!(value, ~30);