]> git.lizzy.rs Git - rust.git/blob - src/libstd/sys/unix/thread_local.rs
Merge pull request #20674 from jbcrail/fix-misspelled-comments
[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::v1::*;
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(any(target_os = "macos",
41           target_os = "ios"))]
42 type pthread_key_t = ::libc::c_ulong;
43
44 #[cfg(any(target_os = "freebsd",
45           target_os = "dragonfly"))]
46 type pthread_key_t = ::libc::c_int;
47
48 #[cfg(not(any(target_os = "macos",
49               target_os = "ios",
50               target_os = "freebsd",
51               target_os = "dragonfly")))]
52 type pthread_key_t = ::libc::c_uint;
53
54 extern {
55     fn pthread_key_create(key: *mut pthread_key_t,
56                           dtor: Option<unsafe extern fn(*mut u8)>) -> c_int;
57     fn pthread_key_delete(key: pthread_key_t) -> c_int;
58     fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
59     fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
60 }