MeshProtocolSimulator  1.0.0
ThreadLock Class Reference

Class, that can be used for locking specific processes from being interrupted. Those interrupts functions are done after unlocking process. This can by used also for hardware periphery control, but all write access to this control has to be inside of locked process. More...

#include <ThreadLock.h>

Detailed Description

Class, that can be used for locking specific processes from being interrupted. Those interrupts functions are done after unlocking process. This can by used also for hardware periphery control, but all write access to this control has to be inside of locked process.

Example:

ThreadLock lock; //Lock has to be global instance
extern SPI_HandleTypeDef hspi1;
HardwareSPI spi1(hspi1);
volatile uint8_t a = 0;
//This method is called in main
void mainThread(){
// Some stuff
lock.DoLocked([](){
//Locked process begin
spi1.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
spi1.transfer(0xFF);
spi1.endTransaction();
//Locked process end
});
// Another stuff
}
//This function is done in some interrupt and can interrupts mainThread() function.
void interruptHandler(){
lock.DoLocked([](){
//Locked process begin
spi1.beginTransaction(SPISettings(spi_speed, MSBFIRST, SPI_MODE0));
spi1.transfer(0xFF);
spi1.endTransaction();
//Locked process end
});
}

Also macro can be used instead of DoLocked method like this:

ThreadLock lock;
DO(lock){
//Locked process here
}LOCKED;
&endcode
@see TLock
@see TLockS
@see ThreadLock.h
/
class ThreadLock{
public:
lock(false),
funcBufferCnt(0)
{
}

Note: To be 100% sure, variable a will not be changed in locked process. All write access to a (except from main thread) has to be done in locked process.

Parameters
funcFunction, that has to be done in locked mode.
Warning
func parameter cannot point currently executed function, because it will create endless recursive loop and stack overflow.
Returns
Returns false, when buffer of pending functions and current func will be discarded.

The documentation for this class was generated from the following file:
TLWhenUnlocked
#define TLWhenUnlocked(lockObj)
Code block specified after this macro is done only when lockObj is unlocked. lockObj is locked before...
Definition: ThreadLock.h:104
ThreadLock
Class, that can be used for locking specific processes from being interrupted. Those interrupts funct...
TLock
#define TLock(lockObj)
This macro makes locking prettier, same as C#. Code block after this macro is just lambda with refere...
Definition: ThreadLock.h:41
TLWhenUnlockedS
#define TLWhenUnlockedS(lockObj, ret)
Code block specified after this macro is done only when lockObj is unlocked. lockObj is locked before...
Definition: ThreadLock.h:85
TLockS
#define TLockS(lockObj, ret)
This macro makes locking prettier, same as C#. Code block after this macro is just lambda with refere...
Definition: ThreadLock.h:65