sync.lock


class mutex §

Mutex: mutually exclusive lock. Call lock to acquire the mutex and unlock to release it. You would typically use this class in instances where you have code that implements a read-modify-write pattern on some variable(s) which takes multiple cycles to complete. In cases where this operation could be completed within a single clock cycle, you may consider instead putting the code in a function which internally uses the atomic keyword to encapsulate the read-modify-write.

Methods

  • void lock() §
    

    Atomically acquire the lock if it is not already acquired, or block waiting on the lock to be released.

  • void unlock() §
    

    Release the lock.

template <auto MaxThreads>
class rwlock §

Reader-Writer lock is a synchronization primitive that allows multiple “threads” to acquire a read-lock, but only one thread may have (any) lock if that thread has the write lock.

Parameters

  • auto MaxThreads
    

    The maximum number of threads that might be manipulating the lock. This determines the maximum number of read-locks that can be outstanding. Typically 512 is a reasonable value to use here.

Methods

  • void lock(bool is_reader) §
    
  • inline void read_lock() §
    
  • inline void write_lock() §
    
  • inline void unlock(bool is_reader) §
    
  • void read_unlock() §
    
  • void write_unlock() §
    
template <auto M, auto I, auto Blocking = true>
class semaphore §

Semaphore

Parameters

  • auto M
    

    Maximum semaphore value.

  • auto I
    

    Initial value for the semaphore count. Must be less than or equal to M.

  • auto Blocking = true
    

    When true, this parameter causes the semaphore to block when wait is called. Setting to false should only be used when the caller can ensure that the semaphore count is greater than 0 when wait is called otherwise a assert is raised.

Aliases

Methods

  • inline bool test_and_decrement(semaphore::sem_ctr_t val) §
    

    Decrement the counter if the current count is greater or equal than val. This function is not inherently threadsafe and must be called from inside an atomic.

  • void wait() §
    

    Decrement the semaphore count by 1 if it is nonzero otherwise block.

  • void wait_multiple(semaphore::sem_ctr_t val) §
    

    Decrement the semaphore count by val if it is nonzero otherwise block.

  • inline void post() §
    

    Increment the semaphore count by 1, potentially waking a thread.

  • inline semaphore::sem_ctr_t count() §
    

    Return current value of semaphore. Provided primarily for debug/diagnostic purposes.

  • void post_multiple(semaphore::sem_ctr_t amount) §
    

    Increment the semaphore count by the specified amount, potentially waking one or more threads.

Invariants

  • (I <= M)