Struct std::thread_local::OsStaticKeyUnstable [-]  [+] [src]

pub struct OsStaticKey {
    pub inner: StaticKeyInner,
    pub dtor: Option<unsafe  extern "C" fn(*mut u8)>,
}

A type for TLS keys that are statically allocated.

This type is entirely unsafe to use as it does not protect against use-after-deallocation or use-during-deallocation.

The actual OS-TLS key is lazily allocated when this is used for the first time. The key is also deallocated when the Rust runtime exits or destroy is called, whichever comes first.

Example

fn main() { use tls::os::{StaticKey, INIT}; static KEY: StaticKey = INIT; unsafe { assert!(KEY.get().is_null()); KEY.set(1 as *mut u8); } }
use tls::os::{StaticKey, INIT};

static KEY: StaticKey = INIT;

unsafe {
    assert!(KEY.get().is_null());
    KEY.set(1 as *mut u8);
}

Fields

inner

Inner static TLS key (internals), created with by INIT_INNER in this module.

dtor

Destructor for the TLS value.

See Key::new for information about when the destructor runs and how it runs.

Methods

impl StaticKey

unsafe fn get(&self) -> *mut u8

Gets the value associated with this TLS key

This will lazily allocate a TLS key from the OS if one has not already been allocated.

unsafe fn set(&self, val: *mut u8)

Sets this TLS key to a new value.

This will lazily allocate a TLS key from the OS if one has not already been allocated.

unsafe fn destroy(&self)

Deallocates this OS TLS key.

This function is unsafe as there is no guarantee that the key is not currently in use by other threads or will not ever be used again.

Note that this does not run the user-provided destructor if one was specified at definition time. Doing so must be done manually.