Struct rustrt::mutex::StaticNativeMutex[src]

pub struct StaticNativeMutex {
    // some fields omitted
}

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

Prefer the NativeMutex type where possible, since that does not require manual deallocation.

Methods

impl StaticNativeMutex

unsafe fn new() -> StaticNativeMutex

Creates a new mutex.

Note that a mutex created in this way needs to be explicit freed with a call to destroy or it will leak. Also it is important to avoid locking until mutex has stopped moving

unsafe fn lock<'a>(&'a self) -> LockGuard<'a>

Acquires this lock. This assumes that the current thread does not already hold the lock.

Example

use std::rt::mutex::{StaticNativeMutex, NATIVE_MUTEX_INIT};
static mut LOCK: StaticNativeMutex = NATIVE_MUTEX_INIT;
unsafe {
    let _guard = LOCK.lock();
    // critical section...
} // automatically unlocked in `_guard`'s destructor

Unsafety

This method is unsafe because it will not function correctly if this mutex has been moved since it was last used. The mutex can move an arbitrary number of times before its first usage, but once a mutex has been used once it is no longer allowed to move (or otherwise it invokes undefined behavior).

Additionally, this type does not take into account any form of scheduling model. This will unconditionally block the os thread which is not always desired.

unsafe fn trylock<'a>(&'a self) -> Option<LockGuard<'a>>

Attempts to acquire the lock. The value returned is Some if the attempt succeeded.

Unsafety

This method is unsafe for the same reasons as lock.

unsafe fn lock_noguard(&self)

Acquire the lock without creating a LockGuard.

These needs to be paired with a call to .unlock_noguard. Prefer using .lock.

Unsafety

This method is unsafe for the same reasons as lock. Additionally, this does not guarantee that the mutex will ever be unlocked, and it is undefined to drop an already-locked mutex.

unsafe fn trylock_noguard(&self) -> bool

Attempts to acquire the lock without creating a LockGuard. The value returned is whether the lock was acquired or not.

If true is returned, this needs to be paired with a call to .unlock_noguard. Prefer using .trylock.

Unsafety

This method is unsafe for the same reasons as lock_noguard.

unsafe fn unlock_noguard(&self)

Unlocks the lock. This assumes that the current thread already holds the lock.

Unsafety

This method is unsafe for the same reasons as lock. Additionally, it is not guaranteed that this is unlocking a previously locked mutex. It is undefined to unlock an unlocked mutex.

unsafe fn wait_noguard(&self)

Block on the internal condition variable.

This function assumes that the lock is already held. Prefer using LockGuard.wait since that guarantees that the lock is held.

Unsafety

This method is unsafe for the same reasons as lock. Additionally, this is unsafe because the mutex may not be currently locked.

unsafe fn signal_noguard(&self)

Signals a thread in wait to wake up

Unsafety

This method is unsafe for the same reasons as lock. Additionally, this is unsafe because the mutex may not be currently locked.

unsafe fn destroy(&self)

This function is especially unsafe because there are no guarantees made that no other thread is currently holding the lock or waiting on the condition variable contained inside.