MeshProtocolSimulator  1.0.0
Classes | Macros | Functions | Variables
ThreadLock.h File Reference

This file contains lock class for handling safe cross-thread operations. More...

#include <wirish.h>
#include <functional>

Go to the source code of this file.

Classes

class  __magic_lock_raii
 

Macros

#define TLock(lockObj)   for(std::function<void(void)> func = nullptr; func == nullptr; (lockObj).DoLocked(func)) func = [&]()
 This macro makes locking prettier, same as C#. Code block after this macro is just lambda with reference capturing. More...
 
#define TLockS(lockObj, ret)   for(std::function<void(void)> func = nullptr; func == nullptr; (ret) = (lockObj).DoLocked(func)) func = [&]()
 This macro makes locking prettier, same as C#. Code block after this macro is just lambda with reference capturing. More...
 
#define TLWhenUnlockedS(lockObj, ret)   for(__magic_lock_raii rrrrr___(&(lockObj), (ret)); !rrrrr___.lck; rrrrr___.lck = true)
 Code block specified after this macro is done only when lockObj is unlocked. lockObj is locked before calling code block and unlocked after code block. This macro is usable, if you want to assign some local variable in it. That's why it should be used only to lock main thread. Locking in interrupts is recommended with TLockS macro. More...
 
#define TLWhenUnlocked(lockObj)   for(__magic_lock_raii rrrrr___(&(lockObj)); !rrrrr___.lck; rrrrr___.lck = true)
 Code block specified after this macro is done only when lockObj is unlocked. lockObj is locked before calling code block and unlocked after code block. This macro is usable, if you want to assign some local variable in it. That's why it should be used only to lock main thread. Locking in interrupts is recommended with TLock macro. More...
 
#define THREAD_LOCK_FUNC_BUFF_SIZE   (5)
 

Functions

bool DoLocked (std::function< void(void)> func)
 
volatile bool isLocked ()
 Check if lock is locked. More...
 
bool Lock ()
 
bool Unlock ()
 
bool LockPriv ()
 
uint32_t UnlockPriv (uint32_t prim)
 

Variables

volatile bool lock
 
volatile uint8_t funcBufferCnt
 
std::function< void(void)> funcBuffer [THREAD_LOCK_FUNC_BUFF_SIZE] = {nullptr}
 

Detailed Description

This file contains lock class for handling safe cross-thread operations.

Credits

Author
Matej Fitoš
Date
Nov 9, 2021
See also
ThreadLock

Macro Definition Documentation

◆ TLock

#define TLock (   lockObj)    for(std::function<void(void)> func = nullptr; func == nullptr; (lockObj).DoLocked(func)) func = [&]()

This macro makes locking prettier, same as C#. Code block after this macro is just lambda with reference capturing.

@macro TLock

  • Old way:
    lock.DoLocked([&](){
    //Locked process
    });
  • New way:
    TLock(lock){
    //Locked process
    };
    Warning
    It is not recommended to assign local variables in code block, because it may be done in another thread after jumping out of this macro.
    Parameters
    lockObjThreadLock instance.
    Note
    Note that after code block ; has to be written, it's because code block is just lambda.

◆ TLockS

#define TLockS (   lockObj,
  ret 
)    for(std::function<void(void)> func = nullptr; func == nullptr; (ret) = (lockObj).DoLocked(func)) func = [&]()

This macro makes locking prettier, same as C#. Code block after this macro is just lambda with reference capturing.

@macro TLockS

  • Old way:
    lock.DoLocked([&](){
    //Locked process
    });
  • New way:
    TLock(lock){
    //Locked process
    };
    Warning
    It is not recommended to assign local variables in code block, because it may be done in another thread after jumping out of this macro.
    Parameters
    lockObjThreadLock instance.
    retBool value, which is assigned to DoLocked() method return value.
    Note
    Note that after code block ; has to be written, it's because code block is just lambda.

◆ TLWhenUnlocked

#define TLWhenUnlocked (   lockObj)    for(__magic_lock_raii rrrrr___(&(lockObj)); !rrrrr___.lck; rrrrr___.lck = true)

Code block specified after this macro is done only when lockObj is unlocked. lockObj is locked before calling code block and unlocked after code block. This macro is usable, if you want to assign some local variable in it. That's why it should be used only to lock main thread. Locking in interrupts is recommended with TLock macro.

@macro TLWhenUnlocked

Example

bool res = false;
DoWhenUnlocked(lock, res){
Serial.println("Locked process.");
}
Note
return, continue, break can be used inside of code block, because RAII is used.
Parameters
lockObjThreadLock instance.

◆ TLWhenUnlockedS

#define TLWhenUnlockedS (   lockObj,
  ret 
)    for(__magic_lock_raii rrrrr___(&(lockObj), (ret)); !rrrrr___.lck; rrrrr___.lck = true)

Code block specified after this macro is done only when lockObj is unlocked. lockObj is locked before calling code block and unlocked after code block. This macro is usable, if you want to assign some local variable in it. That's why it should be used only to lock main thread. Locking in interrupts is recommended with TLockS macro.

@macro TLWhenUnlockedS

Example

bool res = false;
DoWhenUnlocked(lock, res){
Serial.println("Locked process.");
}
Note
return, continue, break can be used inside of code block, because RAII is used.
Parameters
lockObjThreadLock instance.
retBool value, which is true when code in block was done or false, when lockObj is locked.

Function Documentation

◆ isLocked()

volatile bool isLocked ( )
inline

Check if lock is locked.

Returns
Returns true when lock is locked.
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