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

A simple RAII type that owns a lock. More...

#include <locks.hh>

Public Member Functions

 LockGuard (Lock &lock)
 Constructor, acquires the lock with unbounded wait.
 LockGuard (Lock &lock, TimeoutArgument timeout)
 Constructor, attempts to acquire the lock with a timeout.
 LockGuard (LockGuard &&guard)
 Move constructor, transfers ownership of the lock.
void lock ()
 Explicitly lock the wrapped lock.
void unlock ()
 Explicitly lock the wrapped lock.
void release ()
 Unwrap the lock without unlocking it.
bool try_lock (TimeoutArgument timeout)
 If the wrapped lock type supports locking with a timeout, try to lock it with the specified timeout.
 ~LockGuard ()
 Destructor, releases the lock.
 operator bool ()
 Conversion to bool.
int yield (TimeoutArgument t, uint32_t ticks=1)
 Drop and reacquire the lock around a yield.

Detailed Description

template<typename Lock>
class LockGuard< Lock >

A simple RAII type that owns a lock.

Definition at line 297 of file locks.hh.

Constructor & Destructor Documentation

◆ LockGuard() [1/3]

template<typename Lock>
LockGuard< Lock >::LockGuard ( Lock & lock)
inlineexplicitnodiscard

Constructor, acquires the lock with unbounded wait.

The lock to be wrapped must not be held by the calling thread on entry. Further, for this constructor to be safe to invoke, the Lock type must have an unfailable lock member; for example, this precludes the use of FlagLock-s that may be placed in "destruction mode".

In general, prefer the LockGuard constructor that takes a Timeout.

Definition at line 317 of file locks.hh.

References lock().

Referenced by LockGuard().

◆ LockGuard() [2/3]

template<typename Lock>
LockGuard< Lock >::LockGuard ( Lock & lock,
TimeoutArgument timeout )
inlineexplicitnodiscard

Constructor, attempts to acquire the lock with a timeout.

The lock to be wrapped must not be held by the calling thread on entry.

Definition at line 328 of file locks.hh.

References lock(), and try_lock().

◆ LockGuard() [3/3]

template<typename Lock>
LockGuard< Lock >::LockGuard ( LockGuard< Lock > && guard)
inlineexplicitnodiscard

Move constructor, transfers ownership of the lock.

Definition at line 336 of file locks.hh.

References LockGuard().

◆ ~LockGuard()

template<typename Lock>
LockGuard< Lock >::~LockGuard ( )
inline

Destructor, releases the lock.

Definition at line 397 of file locks.hh.

Member Function Documentation

◆ lock()

template<typename Lock>
void LockGuard< Lock >::lock ( )
inline

Explicitly lock the wrapped lock.

The wrapped lock must not be held by the calling thread on entry. For this method to be safe to call, the Lock type must have an unfailable lock member; for example, this precludes the use of FlagLock-s that may be placed in "destruction mode". In general, prefer try_lock.

Definition at line 353 of file locks.hh.

Referenced by LockGuard(), and LockGuard().

◆ operator bool()

template<typename Lock>
LockGuard< Lock >::operator bool ( )
inline

Conversion to bool.

Returns true if this guard owns the lock, false otherwise. This allows lock guards to be used with a timeout in conditional blocks, such as:

if (LockGuard g{lock, timeout})
{
// Run this code if we acquired the lock, releasing the lock at the
end.
}
else
{
// Run this code if we did not acquire the lock.
}
LockGuard(Lock &lock)
Constructor, acquires the lock with unbounded wait.
Definition locks.hh:317
void lock()
Explicitly lock the wrapped lock.
Definition locks.hh:353

Definition at line 422 of file locks.hh.

◆ release()

template<typename Lock>
void LockGuard< Lock >::release ( )
inline

Unwrap the lock without unlocking it.

This is useful to call before destroying a lock.

Definition at line 375 of file locks.hh.

◆ try_lock()

template<typename Lock>
bool LockGuard< Lock >::try_lock ( TimeoutArgument timeout)
inline

If the wrapped lock type supports locking with a timeout, try to lock it with the specified timeout.

The wrapped lock must not be held by the calling thread on entry. Returns true if the lock has been acquired, false otherwise.

Definition at line 388 of file locks.hh.

Referenced by LockGuard(), and yield().

◆ unlock()

template<typename Lock>
void LockGuard< Lock >::unlock ( )
inline

Explicitly lock the wrapped lock.

Must be called with the lock locked by this wrapper.

Definition at line 364 of file locks.hh.

Referenced by yield().

◆ yield()

template<typename Lock>
int LockGuard< Lock >::yield ( TimeoutArgument t,
uint32_t ticks = 1 )
inline

Drop and reacquire the lock around a yield.

Definition at line 430 of file locks.hh.

References thread_sleep(), try_lock(), and unlock().


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