docs
Loading...
Searching...
No Matches
Lock

#include <Ultralight/Lock.h>

Overview

Lightweight mutex optimized for short locking periods.

Uncontended lock and unlock are a single inline atomic operation. Contended waiters spin briefly, then block in the OS until the lock is released (no CPU is consumed while waiting).

Can be used in place of std::mutex since it implements the STL's Lockable interface.

Note
This lock is not recursive: locking it again from the thread that already holds it will deadlock. Debug builds assert when this is attempted.

Public Member Functions

constexpr Lock ()=default
UL_ALWAYS_INLINE void lock () noexcept
UL_ALWAYS_INLINE bool try_lock () noexcept
UL_ALWAYS_INLINE void unlock () noexcept

Protected Member Functions

 Lock (const Lock &)=delete
Lock & operator= (const Lock &)=delete
void contended_lock () noexcept
void contended_unlock () noexcept

Static Protected Member Functions

static uint32_t current_thread_id () noexcept

Protected Attributes

std::atomic< uint32_t > state_ = { kUnlocked }
std::atomic< uint32_t > owner_ = { 0 }

Static Protected Attributes

static constexpr uint32_t kUnlocked = 0
static constexpr uint32_t kLocked = 1
static constexpr uint32_t kLockedContended = 2

Constructor & Destructor Documentation

◆ Lock() [1/2]

Lock ( )
constexprdefault

◆ Lock() [2/2]

Lock ( const Lock & )
protecteddelete

Member Function Documentation

◆ contended_lock()

void contended_lock ( )
protectednoexcept

◆ contended_unlock()

void contended_unlock ( )
protectednoexcept

◆ current_thread_id()

uint32_t current_thread_id ( )
staticprotectednoexcept

◆ lock()

UL_ALWAYS_INLINE void lock ( )
inlinenoexcept

◆ operator=()

Lock & operator= ( const Lock & )
protecteddelete

◆ try_lock()

UL_ALWAYS_INLINE bool try_lock ( )
inlinenoexcept

◆ unlock()

UL_ALWAYS_INLINE void unlock ( )
inlinenoexcept

Member Data Documentation

◆ kLocked

uint32_t kLocked = 1
staticconstexprprotected

◆ kLockedContended

uint32_t kLockedContended = 2
staticconstexprprotected

◆ kUnlocked

uint32_t kUnlocked = 0
staticconstexprprotected

◆ owner_

std::atomic<uint32_t> owner_ = { 0 }
protected

◆ state_

std::atomic<uint32_t> state_ = { kUnlocked }
protected

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