Module rustrt::mutex[src]

A native mutex and condition variable type.

This module contains bindings to the platform's native mutex/condition variable primitives. It provides two types: StaticNativeMutex, which can be statically initialized via the NATIVE_MUTEX_INIT value, and a simple wrapper NativeMutex that has a destructor to clean up after itself. These objects serve as both mutexes and condition variables simultaneously.

The static lock is lazily initialized, but it can only be unsafely destroyed. A statically initialized lock doesn't necessarily have a time at which it can get deallocated. For this reason, there is no Drop implementation of the static mutex, but rather the destroy() method must be invoked manually if destruction of the mutex is desired.

The non-static NativeMutex type does have a destructor, but cannot be statically initialized.

It is not recommended to use this type for idiomatic rust use. These types are appropriate where no other options are available, but other rust concurrency primitives should be used before them: the sync crate defines StaticMutex and Mutex types.

Example

use std::rt::mutex::{NativeMutex, StaticNativeMutex, NATIVE_MUTEX_INIT};

// Use a statically initialized mutex
static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;

unsafe {
    let _guard = LOCK.lock();
} // automatically unlocked here

// Use a normally initialized mutex
unsafe {
    let mut lock = NativeMutex::new();

    {
        let _guard = lock.lock();
    } // unlocked here

    // sometimes the RAII guard isn't appropriate
    lock.lock_noguard();
    lock.unlock_noguard();
} // `lock` is deallocated here

Structs

LockGuard

Automatically unlocks the mutex that it was created from on destruction.

NativeMutex

A native mutex with a destructor for clean-up.

StaticNativeMutex

A native mutex suitable for storing in statics (that is, it has the destroy method rather than a destructor).

Statics

pub static NATIVE_MUTEX_INIT: StaticNativeMutex = [definition]