thread_local_node

Macro thread_local_node 

Source
macro_rules! thread_local_node {
    () => { ... };
    ($vis:vis static $node:ident; $($rest:tt)*) => { ... };
    ($vis:vis static $node:ident) => { ... };
}
Expand description

Declares a new raw::LocalMutexNode key, which is a handle to the thread local node of the currently running thread.

The macro wraps any number of static declarations and make them thread local. Each provided name is associated with a single thread local key. The keys are wrapped and managed by the LocalMutexNode type, which are the actual handles meant to be used with the lock_with_local_then API family from raw::Mutex. Handles are provided by reference to functions.

See: try_lock_with_local_then, lock_with_local_then, try_lock_with_local_then_unchecked or lock_with_local_then_unchecked.

The thread local node definition generated by this macro avoids lazy initialization and does not need to be dropped, which enables a more efficient underlying implementation. See std::thread_local! macro.

§Sintax

  • Allows multiple static definitions, must be separated with semicolons.
  • Visibility is optional (private by default).
  • Requires static keyword and a UPPER_SNAKE_CASE name.

§Example

use mcslock::raw::spins::Mutex;

// Multiple difenitions.
mcslock::thread_local_node! {
    pub static NODE;
    static OTHER_NODE1;
}

// Single definition.
mcslock::thread_local_node!(pub static OTHER_NODE2);

let mutex = Mutex::new(0);
// Keys are provided to APIs by reference.
mutex.lock_with_local_then(&NODE, |data| *data = 10);
assert_eq!(mutex.lock_with_local_then(&NODE, |data| *data), 10);