CHERIoT RTOS
A compartmentalised RTOS for CHERIoT hardware
Loading...
Searching...
No Matches
FlagLockGeneric< IsPriorityInherited > Class Template Reference

A simple flag log, wrapping an atomic word used with the futex calls. More...

#include <locks.hh>

Public Member Functions

bool try_lock (TimeoutArgument timeout)
 Attempt to acquire the lock, blocking until a timeout specified by the timeout parameter has expired.
bool try_lock ()
 Try to acquire the lock, do not block.
void lock ()
 Acquire the lock, potentially blocking forever.
void unlock ()
 Release the lock.
void upgrade_for_destruction ()
 Set the lock in destruction mode.
uint16_t get_owner_thread_id ()
 Return the thread ID of the owner of the lock.

Detailed Description

template<bool IsPriorityInherited>
class FlagLockGeneric< IsPriorityInherited >

A simple flag log, wrapping an atomic word used with the futex calls.

Threads blocked on this will be woken in priority order. If IsPriorityInherited is set, priority is inherited by waiters to avoid priority inversion issues.

The lock word that this wraps is directly accessibly by any malicious compartment that has a reference to this thread. If this is a security concern then you may have other problems: a malicious compartment with access to a mutex's interface (irrespective of the underlying implementation) can cause deadlock by spuriously acquiring a lock or cause data corruption via races by spuriously releasing it. Anything that requires mutual exclusion in the presence of mutual distrust should consider an using a lock manager compartment with an API that returns a single-use capability to unlock on any lock call.

Flag locks are not recursive; if needed, use a RecursiveMutex instead.

Definition at line 46 of file locks.hh.

Member Function Documentation

◆ get_owner_thread_id()

template<bool IsPriorityInherited>
uint16_t FlagLockGeneric< IsPriorityInherited >::get_owner_thread_id ( )
inline

Return the thread ID of the owner of the lock.

This is only available for priority inherited locks, as this is the only case where we store the thread ID of the owner. See the documentation of flaglock_priority_inheriting_get_owner_thread_id for more information.

Definition at line 144 of file locks.hh.

◆ lock()

template<bool IsPriorityInherited>
void FlagLockGeneric< IsPriorityInherited >::lock ( )
inline

Acquire the lock, potentially blocking forever.

This function returns void despite that this is a bad idea. Because the C++ standard library has void-returning ::lock() methods, templated callers would not check a return value, making it unsafe to return in the event of failed lock acquisition. Failures instead manifest as tripping a Debug::Invariant, causing this function not to return (and yet not block forever). Thus, it is not safe with locks that may be put into "destrucion mode" (see flaglock_upgrade_for_destruction). Attempting to recursively lock a held priority inheriting flag lock (acquired by either lock or try_lock) will trip the same Invariant. (Recall that flag locks explicitly do not support recursive acquisition; the failure mode is, however, not one of blocking forever.) In general, defensive code should prefer try_lock.

Definition at line 110 of file locks.hh.

◆ try_lock() [1/2]

template<bool IsPriorityInherited>
bool FlagLockGeneric< IsPriorityInherited >::try_lock ( )
inline

Try to acquire the lock, do not block.

Returns true if and only if the lock transitioned from being unheld to held by the current thread.

Definition at line 87 of file locks.hh.

Referenced by FlagLockGeneric< false >::try_lock().

◆ try_lock() [2/2]

template<bool IsPriorityInherited>
bool FlagLockGeneric< IsPriorityInherited >::try_lock ( TimeoutArgument timeout)
inline

Attempt to acquire the lock, blocking until a timeout specified by the timeout parameter has expired.

Returns true if and only if the lock transitioned from being unheld to held by the current thread.

Definition at line 76 of file locks.hh.

◆ unlock()

template<bool IsPriorityInherited>
void FlagLockGeneric< IsPriorityInherited >::unlock ( )
inline

Release the lock.

Note: This does not check that the lock is owned by the calling thread.

Definition at line 122 of file locks.hh.

◆ upgrade_for_destruction()

template<bool IsPriorityInherited>
void FlagLockGeneric< IsPriorityInherited >::upgrade_for_destruction ( )
inline

Set the lock in destruction mode.

See the documentation of flaglock_upgrade_for_destruction for more information.

Definition at line 131 of file locks.hh.


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