]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/thread_local.rs
std: Add a new top-level thread_local module
[rust.git] / src / libstd / sys / unix / thread_local.rs
1 // Copyright 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.
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 use prelude::*;
12 use libc::c_int;
13
14 pub type Key = pthread_key_t;
15
16 #[inline]
17 pub unsafe fn create(dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
18     let mut key = 0;
19     assert_eq!(pthread_key_create(&mut key, dtor), 0);
20     return key;
21 }
22
23 #[inline]
24 pub unsafe fn set(key: Key, value: *mut u8) {
25     let r = pthread_setspecific(key, value);
26     debug_assert_eq!(r, 0);
27 }
28
29 #[inline]
30 pub unsafe fn get(key: Key) -> *mut u8 {
31     pthread_getspecific(key)
32 }
33
34 #[inline]
35 pub unsafe fn destroy(key: Key) {
36     let r = pthread_key_delete(key);
37     debug_assert_eq!(r, 0);
38 }
39
40 #[cfg(target_os = "macos")]
41 type pthread_key_t = ::libc::c_ulong;
42
43 #[cfg(not(target_os = "macos"))]
44 type pthread_key_t = ::libc::c_uint;
45
46 extern {
47     fn pthread_key_create(key: *mut pthread_key_t,
48                           dtor: Option<unsafe extern fn(*mut u8)>) -> c_int;
49     fn pthread_key_delete(key: pthread_key_t) -> c_int;
50     fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
51     fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
52 }