]> git.proxmox.com Git - rustc.git/blob - src/libstd/sys/unix/thread_local.rs
3afe84b25804c3c5d11836da16d80052984b7ec0
[rustc.git] / src / libstd / sys / unix / thread_local.rs
1 // Copyright 2014-2015 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.
4 //
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.
10
11 #![allow(dead_code)] // sys isn't exported yet
12
13 use prelude::v1::*;
14 use libc::c_int;
15
16 pub type Key = pthread_key_t;
17
18 #[inline]
19 pub unsafe fn create(dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
20 let mut key = 0;
21 assert_eq!(pthread_key_create(&mut key, dtor), 0);
22 return key;
23 }
24
25 #[inline]
26 pub unsafe fn set(key: Key, value: *mut u8) {
27 let r = pthread_setspecific(key, value);
28 debug_assert_eq!(r, 0);
29 }
30
31 #[inline]
32 pub unsafe fn get(key: Key) -> *mut u8 {
33 pthread_getspecific(key)
34 }
35
36 #[inline]
37 pub unsafe fn destroy(key: Key) {
38 let r = pthread_key_delete(key);
39 debug_assert_eq!(r, 0);
40 }
41
42 #[cfg(any(target_os = "macos",
43 target_os = "ios"))]
44 type pthread_key_t = ::libc::c_ulong;
45
46 #[cfg(any(target_os = "freebsd",
47 target_os = "dragonfly",
48 target_os = "bitrig",
49 target_os = "openbsd"))]
50 type pthread_key_t = ::libc::c_int;
51
52 #[cfg(not(any(target_os = "macos",
53 target_os = "ios",
54 target_os = "freebsd",
55 target_os = "dragonfly",
56 target_os = "bitrig",
57 target_os = "openbsd")))]
58 type pthread_key_t = ::libc::c_uint;
59
60 extern {
61 fn pthread_key_create(key: *mut pthread_key_t,
62 dtor: Option<unsafe extern fn(*mut u8)>) -> c_int;
63 fn pthread_key_delete(key: pthread_key_t) -> c_int;
64 fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
65 fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
66 }