Skip to main content
Pigweed's logo Pigweed
  1. Home
  2. Reference
  3. C/C++
  4. pw_sync
  5. pw::sync::Mutex Class Reference
Loading...
Searching...
No Matches
pw::sync::Mutex Class Reference

Overview

The Mutex is a synchronization primitive that can be used to protect shared data from being simultaneously accessed by multiple threads. It offers exclusive, non-recursive ownership semantics where priority inheritance is used to solve the classic priority-inversion problem. This is thread safe, but NOT IRQ safe.

Warning
In order to support global statically constructed Mutexes, the user and/or backend MUST ensure that any initialization required in your environment is done prior to the creation and/or initialization of the native synchronization primitives (e.g. kernel initialization).
Inheritance diagram for pw::sync::Mutex:
pw::sync::TimedMutex

Public Types

using native_handle_type = backend::NativeMutexHandle
 

Public Member Functions

 Mutex (const Mutex &)=delete
 
 Mutex (Mutex &&)=delete
 
Mutexoperator= (const Mutex &)=delete
 
Mutexoperator= (Mutex &&)=delete
 
void lock ()
 
bool try_lock ()
 
void unlock ()
 
native_handle_type native_handle ()
 

Protected Member Functions

backend::NativeMutex & native_type ()
 
const backend::NativeMutex & native_type () const
 

Member Function Documentation

◆ lock()

void pw::sync::Mutex::lock ( )

Locks the mutex, blocking indefinitely. Failures are fatal.

Precondition
The lock isn't already held by this thread. Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ native_type()

backend::NativeMutex & pw::sync::Mutex::native_type ( )
inlineprotected

Expose the NativeMutex directly to derived classes (TimedMutex) in case implementations use different types for backend::NativeMutex and native_handle().

◆ try_lock()

bool pw::sync::Mutex::try_lock ( )

Attempts to lock the mutex in a non-blocking manner. Returns true if the mutex was successfully acquired.

Precondition
The lock isn't already held by this thread. Recursive locking is undefined behavior.

thread safeyes isr safeno nmi safeno

◆ unlock()

void pw::sync::Mutex::unlock ( )

Unlocks the mutex. Failures are fatal.

Precondition
The mutex is held by this thread.

thread safeyes isr safeno nmi safeno


The documentation for this class was generated from the following file: