sync.lock


class mutex §source

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() §source
    

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

  • void unlock() §source
    

    Release the lock.

template <auto MaxThreads>
class rwlock §source

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

template <auto M, auto I, auto Blocking = true>
class semaphore §source

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) §source
    

    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() §source
    

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

  • void wait_multiple(semaphore::sem_ctr_t val) §source
    

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

  • inline void post() §source
    

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

  • inline semaphore::sem_ctr_t count() §source
    

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

  • void post_multiple(semaphore::sem_ctr_t amount) §source
    

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

Invariants

  • (I <= M)