(index<- )        ./libstd/rt/thread_local_storage.rs

    git branch:    * master           5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
    modified:    Fri May  9 13:02:28 2014
   1  // Copyright 2013 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)]
  12  
  13  #[cfg(test)]
  14  use owned::Box;
  15  #[cfg(unix)]
  16  use libc::c_int;
  17  #[cfg(unix)]
  18  use ptr::null;
  19  #[cfg(windows)]
  20  use libc::types::os::arch::extra::{DWORD, LPVOID, BOOL};
  21  
  22  #[cfg(unix)]
  23  pub type Key = pthread_key_t;
  24  
  25  #[cfg(unix)]
  26  pub unsafe fn create(key: &mut Key) {
  27      assert_eq!(0, pthread_key_create(key, null()));
  28  }
  29  
  30  #[cfg(unix)]
  31  pub unsafe fn set(keyKey, value: *mut u8) {
  32      assert_eq!(0, pthread_setspecific(key, value));
  33  }
  34  
  35  #[cfg(unix)]
  36  pub unsafe fn get(keyKey) -> *mut u8 {
  37      pthread_getspecific(key)
  38  }
  39  
  40  #[cfg(unix)]
  41  pub unsafe fn destroy(keyKey) {
  42      assert_eq!(0, pthread_key_delete(key));
  43  }
  44  
  45  #[cfg(target_os="macos")]
  46  #[allow(non_camel_case_types)] // foreign type
  47  type pthread_key_t = ::libc::c_ulong;
  48  
  49  #[cfg(target_os="linux")]
  50  #[cfg(target_os="freebsd")]
  51  #[cfg(target_os="android")]
  52  #[allow(non_camel_case_types)] // foreign type
  53  type pthread_key_t = ::libc::c_uint;
  54  
  55  #[cfg(unix)]
  56  extern {
  57      fn pthread_key_create(key: *mut pthread_key_t, dtor: *u8) -> c_int;
  58      fn pthread_key_delete(keypthread_key_t) -> c_int;
  59      fn pthread_getspecific(keypthread_key_t) -> *mut u8;
  60      fn pthread_setspecific(keypthread_key_t, value: *mut u8) -> c_int;
  61  }
  62  
  63  #[cfg(windows)]
  64  pub type Key = DWORD;
  65  
  66  #[cfg(windows)]
  67  pub unsafe fn create(key: &mut Key) {
  68      static TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;
  69      *key = TlsAlloc();
  70      assert!(*key != TLS_OUT_OF_INDEXES);
  71  }
  72  
  73  #[cfg(windows)]
  74  pub unsafe fn set(key: Key, value: *mut u8) {
  75      assert!(0 != TlsSetValue(key, value as *mut ::libc::c_void))
  76  }
  77  
  78  #[cfg(windows)]
  79  pub unsafe fn get(key: Key) -> *mut u8 {
  80      TlsGetValue(key) as *mut u8
  81  }
  82  
  83  #[cfg(windows)]
  84  pub unsafe fn destroy(key: Key) {
  85      assert!(TlsFree(key) != 0);
  86  }
  87  
  88  #[cfg(windows)]
  89  extern "system" {
  90      fn TlsAlloc() -> DWORD;
  91      fn TlsFree(dwTlsIndex: DWORD) -> BOOL;
  92      fn TlsGetValue(dwTlsIndex: DWORD) -> LPVOID;
  93      fn TlsSetValue(dwTlsIndex: DWORD, lpTlsvalue: LPVOID) -> BOOL;
  94  }
  95  
  96  #[test]
  97  fn tls_smoke_test() {
  98      use cast::transmute;
  99      unsafe {
 100          let mut key = 0;
 101          let value = box 20;
 102          create(&mut key);
 103          set(key, transmute(value));
 104          let value: Box<int> = transmute(get(key));
 105          assert_eq!(value, box 20);
 106          let value = box 30;
 107          set(key, transmute(value));
 108          let value: Box<int> = transmute(get(key));
 109          assert_eq!(value, box 30);
 110      }
 111  }


libstd/rt/thread_local_storage.rs:30:13-30:13 -fn- definition:
pub unsafe fn set(key: Key, value: *mut u8) {
    assert_eq!(0, pthread_setspecific(key, value));
}
references:- 3
libstd/rt/local_ptr.rs:
301:                     let ptr: Box<T> = cast::transmute(void_ptr);
302:                     tls::set(key, ptr::mut_null());
303:                     Some(ptr)


libstd/rt/thread_local_storage.rs:35:13-35:13 -fn- definition:
pub unsafe fn get(key: Key) -> *mut u8 {
    pthread_getspecific(key)
}
references:- 6
libstd/rt/local_ptr.rs:
354:             Some(key) => {
355:                 let void_ptr = tls::get(key);
356:                 if void_ptr.is_null() {


libstd/rt/thread_local_storage.rs:22:13-22:13 -NK_AS_STR_TODO- definition:
pub type Key = pthread_key_t;
pub unsafe fn create(key: &mut Key) {
    assert_eq!(0, pthread_key_create(key, null()));
references:- 7
26: pub unsafe fn create(key: &mut Key) {
27:     assert_eq!(0, pthread_key_create(key, null()));
--
41: pub unsafe fn destroy(key: Key) {
42:     assert_eq!(0, pthread_key_delete(key));
libstd/rt/local_ptr.rs:
244:     static mut RT_TLS_KEY: tls::Key = -1;
--
366:     #[inline]
367:     fn tls_key() -> tls::Key {
368:         match maybe_tls_key() {
--
376:     #[allow(visible_private_types)]
377:     pub fn maybe_tls_key() -> Option<tls::Key> {
378:         unsafe {


libstd/rt/thread_local_storage.rs:52:47-52:47 -NK_AS_STR_TODO- definition:
type pthread_key_t = ::libc::c_uint;
extern {
    fn pthread_key_create(key: *mut pthread_key_t, dtor: *u8) -> c_int;
references:- 5
23: pub type Key = pthread_key_t;
--
58:     fn pthread_key_delete(key: pthread_key_t) -> c_int;
59:     fn pthread_getspecific(key: pthread_key_t) -> *mut u8;
60:     fn pthread_setspecific(key: pthread_key_t, value: *mut u8) -> c_int;
61: }