FreeRTOScpp
Loading...
Searching...
No Matches
Lock Class Reference

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

#include <Lock.h>

Collaboration diagram for Lock:

Public Member Functions

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

Private Member Functions

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

Private Attributes

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

Detailed Description

Class to hold a block based lock.

(auto unlocks on in its destructor)

Typical calling sequences:

// Somewhere global
Mutex mutex
Mutex Wrapper.
Definition MutexCPP.h:75

then, in a block

{
Lock lock(mutex); // Mutex is taken here
...
} // and released here
Class to hold a block based lock.
Definition Lock.h:121
bool lock(TickType_t wait=portMAX_DELAY)
Try to take to lock.
Definition Lock.cpp:99

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/3]

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/3]

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

Lock::~Lock ( )
virtual

Destructor.

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

Here is the call graph for this function:

◆ Lock() [3/3]

Lock::Lock ( Lock const & )
privatedelete

We are not copyable.

Member Function Documentation

◆ lock()

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 Lockable::take().

Referenced by Lock().

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

◆ locked()

bool Lock::locked ( ) const
inline

Do we have the lock?

Returns
True if we have the lock.

◆ operator=()

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

We are not assignable.

◆ unlock()

void Lock::unlock ( )

Release a lock.

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

Here is the call graph for this function:

Member Data Documentation

◆ lockable

Lockable& Lock::lockable
private

The Lockage object we are connected to.

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

◆ lockCnt

int Lock::lockCnt
private

The number of locks we hold on lockable.

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


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