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.
19 use libc::types::os::arch::extra::{DWORD, LPVOID, BOOL};
22 pub type Key = pthread_key_t;
25 pub unsafe fn create(key: &mut Key) {
26 assert_eq!(0, pthread_key_create(key, null()));
30 pub unsafe fn set(key: Key, value: *mut c_void) {
31 assert_eq!(0, pthread_setspecific(key, value));
35 pub unsafe fn get(key: Key) -> *mut c_void {
36 pthread_getspecific(key)
40 pub unsafe fn destroy(key: Key) {
41 assert_eq!(0, pthread_key_delete(key));
44 #[cfg(target_os="macos")]
45 #[allow(non_camel_case_types)] // foreign type
46 type pthread_key_t = ::libc::c_ulong;
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;
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;
66 pub unsafe fn create(key: &mut Key) {
67 static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
69 assert!(*key != TLS_OUT_OF_INDEXES);
73 pub unsafe fn set(key: Key, value: *mut c_void) {
74 assert!(0 != TlsSetValue(key, value))
78 pub unsafe fn get(key: Key) -> *mut c_void {
83 pub unsafe fn destroy(key: Key) {
84 assert!(TlsFree(key) != 0);
89 fn TlsAlloc() -> DWORD;
90 fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
91 fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
92 fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
102 set(key, transmute(value));
103 let value: ~int = transmute(get(key));
104 assert_eq!(value, ~20);
106 set(key, transmute(value));
107 let value: ~int = transmute(get(key));
108 assert_eq!(value, ~30);