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.
|
#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) |
|
|
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) |
|
|
volatile bool | lock |
|
volatile uint8_t | funcBufferCnt |
|
std::function< void(void)> | funcBuffer [THREAD_LOCK_FUNC_BUFF_SIZE] = {nullptr} |
|
This file contains lock class for handling safe cross-thread operations.
Credits
- Author
- Matej Fitoš
- Date
- Nov 9, 2021
- See also
- ThreadLock
◆ 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:
- New way:
- 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
-
- 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:
- New way:
- 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
-
lockObj | ThreadLock instance. |
ret | Bool 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
-
◆ 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
-
lockObj | ThreadLock instance. |
ret | Bool value, which is true when code in block was done or false, when lockObj is locked. |
◆ isLocked()
volatile bool isLocked |
( |
| ) |
|
|
inline |
Check if lock is locked.
- Returns
- Returns true when lock is locked.
#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