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(any(target_os="linux",
46 target_os="dragonfly",
49 #[allow(non_camel_case_types)] // foreign type
50 type pthread_key_t = ::libc::c_uint;
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;
64 pub unsafe fn create(key: &mut Key) {
65 static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
67 assert!(*key != TLS_OUT_OF_INDEXES);
71 pub unsafe fn set(key: Key, value: *mut u8) {
72 assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void))
76 pub unsafe fn get(key: Key) -> *mut u8 {
77 TlsGetValue(key) as *mut u8
81 pub unsafe fn destroy(key: Key) {
82 assert!(TlsFree(key) != 0);
86 #[allow(non_snake_case)]
88 fn TlsAlloc() -> DWORD;
89 fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
90 fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
91 fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
100 fn tls_smoke_test() {
101 use std::mem::transmute;
106 set(key, transmute(value));
107 let value: Box<int> = transmute(get(key));
108 assert_eq!(value, box 20i);
110 set(key, transmute(value));
111 let value: Box<int> = transmute(get(key));
112 assert_eq!(value, box 30i);