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.
18 use libc::types::os::arch::extra::{DWORD, LPVOID, BOOL};
21 pub type Key = pthread_key_t;
24 pub unsafe fn create(key: &mut Key) {
25 assert_eq!(0, pthread_key_create(key, null()));
29 pub unsafe fn set(key: Key, value: *mut u8) {
30 assert_eq!(0, pthread_setspecific(key, value));
34 pub unsafe fn get(key: Key) -> *mut u8 {
35 pthread_getspecific(key)
39 pub unsafe fn destroy(key: Key) {
40 assert_eq!(0, pthread_key_delete(key));
43 #[cfg(target_os="macos")]
44 #[allow(non_camel_case_types)] // foreign type
45 type pthread_key_t = ::libc::c_ulong;
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;
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;
65 pub unsafe fn create(key: &mut Key) {
66 static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
68 assert!(*key != TLS_OUT_OF_INDEXES);
72 pub unsafe fn set(key: Key, value: *mut u8) {
73 assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void))
77 pub unsafe fn get(key: Key) -> *mut u8 {
78 TlsGetValue(key) as *mut u8
82 pub unsafe fn destroy(key: Key) {
83 assert!(TlsFree(key) != 0);
88 fn TlsAlloc() -> DWORD;
89 fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
90 fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
91 fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
101 set(key, transmute(value));
102 let value: ~int = transmute(get(key));
103 assert_eq!(value, ~20);
105 set(key, transmute(value));
106 let value: ~int = transmute(get(key));
107 assert_eq!(value, ~30);