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

Read/Write Lock control. More...

#include <ReadWrite.h>

Inheritance diagram for ReadWriteLock:
Collaboration diagram for ReadWriteLock:

Public Member Functions

 ReadWriteLock ()
 
 ~ReadWriteLock ()
 
bool give () override
 
bool give () override
 
bool readLock (TickType_t wait=portMAX_DELAY)
 ReadLock.
 
bool readUnlock ()
 Remove our lock.
 
bool releaseReserved ()
 If we have a reserved lock, down grade to just a read lock.
 
bool requestReserved ()
 Task with a Read Lock request upgrade to a reserved lock.
 
bool reservedLock (TickType_t wait=portMAX_DELAY)
 Get an upgradable Read Lock.
 
Readerrlock ()
 Get Read Lockable.
 
bool take (TickType_t wait) override
 
bool take (TickType_t wait) override
 
Writerwlock ()
 Get Write Lockable.
 
bool writeLock (TickType_t wait=portMAX_DELAY)
 Take the write lock, requires readCount to be 0, or 1 if we reserved the upgrade.
 
bool writeUnlock ()
 Release the write lock.
 

Protected Attributes

EventGroup event
 
int readCount = 0
 Count of Read Locks.
 
TaskHandle_t reserved = nullptr
 Reserved Lock indicator.
 
int writeReq = -1
 Write Request Pending Priority.
 

Detailed Description

Read/Write Lock control.

States:

  • Free: readCount == 0
  • Read: readCount > 0, reserved == 0
  • Reserved: readCount > 0, reserved != 0
  • Write: readCount == -1, reserved == 0
  • Upgraded: readCount == -1, reserved != 0
dot_inline_dotgraph_1.png
Warning
This is a fairly new module, and may not be fully tested

Constructor & Destructor Documentation

◆ ReadWriteLock()

ReadWriteLock::ReadWriteLock ( )

◆ ~ReadWriteLock()

ReadWriteLock::~ReadWriteLock ( )

Member Function Documentation

◆ give() [1/2]

bool Reader::give ( )
overridevirtualinherited

Implements Lockable.

◆ give() [2/2]

bool Writer::give ( )
overridevirtualinherited

Implements Lockable.

◆ readLock()

bool ReadWriteLock::readLock ( TickType_t wait = portMAX_DELAY)

ReadLock.

Get a read lock.

Parameters
waitThe maximum number of ticks to wait to get the lock
Returns
true if the read lock has been granted

Algorithm:

  • Loop
    • If readCount >=0 and our priority > writeReq
      • Increment readCount
      • return true
    • if Time has expired:
      • return false
    • else wait a tick for read_bit event.

References read_bit.

◆ readUnlock()

bool ReadWriteLock::readUnlock ( )

Remove our lock.

If we reserved the upgrade, release that.

References read_bit, and write_bit.

◆ releaseReserved()

bool ReadWriteLock::releaseReserved ( )

If we have a reserved lock, down grade to just a read lock.

References read_bit.

◆ requestReserved()

bool ReadWriteLock::requestReserved ( )

Task with a Read Lock request upgrade to a reserved lock.

If no reservation current, will be granted, if reservation present, will be rejected.

Does not check if this task has a read lock, but that is assumed.

◆ reservedLock()

bool ReadWriteLock::reservedLock ( TickType_t wait = portMAX_DELAY)

Get an upgradable Read Lock.

like readLock, but add reserved == nullptr to the conditions. On success will set reserved to our task handle.

Only one task can reserve this, as if two are in this state they will deadlock when they both try to upgrade

References read_bit.

◆ rlock()

Reader & ReadWriteLock::rlock ( )
inline

Get Read Lockable.

Returns
a lockable object for using with Read Locks.

◆ take() [1/2]

bool Reader::take ( TickType_t wait)
overridevirtualinherited

Implements Lockable.

◆ take() [2/2]

bool Writer::take ( TickType_t wait)
overridevirtualinherited

Implements Lockable.

◆ wlock()

Writer & ReadWriteLock::wlock ( )
inline

Get Write Lockable.

Returns
a lockable object for using with Write Locks

◆ writeLock()

bool ReadWriteLock::writeLock ( TickType_t wait = portMAX_DELAY)

Take the write lock, requires readCount to be 0, or 1 if we reserved the upgrade.

References read_bit, and write_bit.

◆ writeUnlock()

bool ReadWriteLock::writeUnlock ( )

Release the write lock.

If we upgraded go back to a reserved lock which will need to be unlocked.

References read_bit, and write_bit.

Member Data Documentation

◆ event

EventGroup ReadWriteLock::event
protected

◆ readCount

int ReadWriteLock::readCount = 0
protected

Count of Read Locks.

If 0, then lock is free if >0, lock is in read mode and is the count of the number of read locks granted if <0, loci is in write mode.

◆ reserved

TaskHandle_t ReadWriteLock::reserved = nullptr
protected

Reserved Lock indicator.

If nullptr, then no reservedLock are currently in existence, so a reservedLock can be granted

Else, TaskHandle of the task that has reserved the right to upgrade to a write lock.

◆ writeReq

int ReadWriteLock::writeReq = -1
protected

Write Request Pending Priority.

If in Read mode, and a task with priority below writeReq


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