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.
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.
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};
18 pub type Key = pthread_key_t;
21 pub unsafe fn create(key: &mut Key) {
22 assert!(pthread_key_create(key, null()) == 0);
26 pub unsafe fn set(key: Key, value: *mut u8) {
27 assert!(pthread_setspecific(key, value) == 0);
31 pub unsafe fn get(key: Key) -> *mut u8 {
32 pthread_getspecific(key)
36 pub unsafe fn destroy(key: Key) {
37 assert!(pthread_key_delete(key) == 0);
40 #[cfg(target_os="macos")]
41 #[allow(non_camel_case_types)] // foreign type
42 type pthread_key_t = ::libc::c_ulong;
44 #[cfg(target_os="linux")]
45 #[cfg(target_os="freebsd")]
46 #[cfg(target_os="android")]
47 #[allow(non_camel_case_types)] // foreign type
48 type pthread_key_t = ::libc::c_uint;
52 fn pthread_key_create(key: *mut pthread_key_t, dtor: *u8) -> c_int;
53 fn pthread_key_delete(key: pthread_key_t) -> c_int;
54 fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
55 fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
62 pub unsafe fn create(key: &mut Key) {
63 static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
65 assert!(*key != TLS_OUT_OF_INDEXES);
69 pub unsafe fn set(key: Key, value: *mut u8) {
70 assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void))
74 pub unsafe fn get(key: Key) -> *mut u8 {
75 TlsGetValue(key) as *mut u8
79 pub unsafe fn destroy(key: Key) {
80 assert!(TlsFree(key) != 0);
84 #[allow(non_snake_case_functions)]
86 fn TlsAlloc() -> DWORD;
87 fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
88 fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
89 fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
99 use std::mem::transmute;
104 set(key, transmute(value));
105 let value: Box<int> = transmute(get(key));
106 assert_eq!(value, box 20);
108 set(key, transmute(value));
109 let value: Box<int> = transmute(get(key));
110 assert_eq!(value, box 30);