FreeRTOScpp
FreeRTOScpp::Lock Class Reference

Class to hold a block based lock. More...

#include <Lock.h>

Collaboration diagram for FreeRTOScpp::Lock:

Public Member Functions

 Lock (Lockable &mylockable, bool mylocked=true, TickType_t wait=portMAX_DELAY)
 Constructor. More...
 
 Lock (Lockable &mylockable, TickType_t wait)
 Constructor with assumed locking by specifying lock time. More...
 
 Lock (Lockable &mylockable, Time_ms wait)
 Constructor. More...
 
virtual ~Lock ()
 Destructor. More...
 
bool lock (TickType_t wait=portMAX_DELAY)
 Try to take to lock. More...
 
bool lock (Time_ms ms)
 
bool locked () const
 Do we have the lock? More...
 
void unlock ()
 Release a lock. More...
 

Private Member Functions

 Lock (Lock const &)=delete
 We are not copyable. More...
 
void operator= (Lock const &)=delete
 We are not assignable. More...
 

Private Attributes

Lockablelockable
 The Lockage object we are connected to. More...
 
int lockCnt
 The number of locks we hold on lockable. More...
 

Detailed Description

Class to hold a block based lock.

(auto unlocks on in its destructor)

Typical calling sequences:

// Somewhere global
Mutex mutex

then, in a block

{
Lock lock(mutex); // Mutex is taken here
...
} // and released here

or

{
Lock lokc(mutex, false); // don't take it yet
...
if (lock.lock(5)) { // but take it here, with a maximum timeout
...
lock.unlock(); // and possible release it
}
} // will be released here if taken and not released

or

{
Lock lock(mute, 5); // Try to take the semaphore with a timeout
if (lock.locked()) {
// Semaphore was locked, so we could use it
} else {
// Error handling because we couldn't take the semaphore.
}
} // Mutex will be released here if not otherwise release

Constructor & Destructor Documentation

◆ Lock() [1/4]

Lock::Lock ( Lockable mylockable,
bool  mylocked = true,
TickType_t  wait = portMAX_DELAY 
)

Constructor.

This is the most generic constructor, and the one that will be used if no parameters beyond the lockable object are given. The alternate constructor only take the time to wait, and assumes the second parameter is true

If wait is specified (or some code might abort the wait) then the code should check with a call to locked() to confirm the lock has been taken.

Parameters
mylockableThe Lockable object we will be using
mylockedShould we start by taking the lock
waitHow long to wait to take the lock

References lock().

Here is the call graph for this function:

◆ Lock() [2/4]

FreeRTOScpp::Lock::Lock ( Lockable mylockable,
TickType_t  wait 
)
inline

Constructor with assumed locking by specifying lock time.

Parameters
mylockableThe Lockabe object to use
waitThe time it Ticks to wait to get the lock.

◆ Lock() [3/4]

Lock::Lock ( Lockable myLockable,
Time_ms  wait_ms 
)

Constructor.

Version to specifiy the time to wait to get the lock as a Chrono in milliseconds

Code will need to check that the lock was gotten with a call to locked()

Parameters
mylockableThe Lockable object we will be using
wait_msHow long to wait to take the lock in millisecons

References lock().

Here is the call graph for this function:

◆ ~Lock()

Lock::~Lock ( )
virtual

Destructor.

References FreeRTOScpp::Lockable::give(), lockable, and lockCnt.

Here is the call graph for this function:

◆ Lock() [4/4]

FreeRTOScpp::Lock::Lock ( Lock const &  )
privatedelete

We are not copyable.

Member Function Documentation

◆ lock() [1/2]

bool Lock::lock ( TickType_t  wait = portMAX_DELAY)

Try to take to lock.

Note, lock allows for nested calls, which need to be unlocked as many times as taken, even if the lockable isn't recursive.

Parameters
waitHow long to wait in Ticks
Returns
true if lock has been taken

References lockable, lockCnt, and FreeRTOScpp::Lockable::take().

Referenced by Lock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lock() [2/2]

bool FreeRTOScpp::Lock::lock ( Time_ms  ms)
inline

References lock(), and FreeRTOScpp::ms2ticks().

Referenced by lock().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ locked()

bool FreeRTOScpp::Lock::locked ( ) const
inline

Do we have the lock?

Returns
True if we have the lock.

◆ operator=()

void FreeRTOScpp::Lock::operator= ( Lock const &  )
privatedelete

We are not assignable.

◆ unlock()

void Lock::unlock ( )

Release a lock.

References FreeRTOScpp::Lockable::give(), lockable, and lockCnt.

Here is the call graph for this function:

Member Data Documentation

◆ lockable

Lockable& FreeRTOScpp::Lock::lockable
private

The Lockage object we are connected to.

Referenced by lock(), unlock(), and ~Lock().

◆ lockCnt

int FreeRTOScpp::Lock::lockCnt
private

The number of locks we hold on lockable.

Referenced by lock(), unlock(), and ~Lock().


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