2015-01-29 08:19:28 +01:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-11-14 14:20:57 -08:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
#![allow(dead_code)] // not used on all platforms
|
2015-05-27 11:18:36 +03:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
use mem;
|
|
|
|
|
use libc;
|
2014-11-14 14:20:57 -08:00
|
|
|
|
2015-11-02 16:23:22 -08:00
|
|
|
pub type Key = libc::pthread_key_t;
|
2014-11-14 14:20:57 -08:00
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn create(dtor: Option<unsafe extern fn(*mut u8)>) -> Key {
|
|
|
|
|
let mut key = 0;
|
2015-11-02 16:23:22 -08:00
|
|
|
assert_eq!(libc::pthread_key_create(&mut key, mem::transmute(dtor)), 0);
|
2015-09-08 00:36:29 +02:00
|
|
|
key
|
2014-11-14 14:20:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn set(key: Key, value: *mut u8) {
|
2015-11-02 16:23:22 -08:00
|
|
|
let r = libc::pthread_setspecific(key, value as *mut _);
|
2014-11-14 14:20:57 -08:00
|
|
|
debug_assert_eq!(r, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn get(key: Key) -> *mut u8 {
|
2015-11-02 16:23:22 -08:00
|
|
|
libc::pthread_getspecific(key) as *mut u8
|
2014-11-14 14:20:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn destroy(key: Key) {
|
2015-11-02 16:23:22 -08:00
|
|
|
let r = libc::pthread_key_delete(key);
|
2014-11-14 14:20:57 -08:00
|
|
|
debug_assert_eq!(r, 0);
|
|
|
|
|
}
|