Mutual-exclusion critical section for protecting shared resources between tasks.
More...
#include <nbrtos.h>
Inherits OS_TASK_DLY_OBJ.
|
| | OS_CRIT () |
| | Construct and initialize the OS_CRIT object.
|
| |
| uint8_t | Init () |
| | Reset the OS_CRIT object to its initial (unclaimed) state.
|
| |
| uint8_t | LockAndEnter (uint32_t timeoutTicks=WAIT_FOREVER) |
| | Disable task switching (lock) and then claim the critical section.
|
| |
| uint8_t | Enter (TickTimeout &t) |
| | Claim the critical section, using a TickTimeout to limit waiting.
|
| |
| uint8_t | Enter (uint32_t timeoutTicks=WAIT_FOREVER) |
| | Claim the critical section with a timeout in system ticks.
|
| |
| uint8_t | EnterNoWait () |
| | Attempt to claim the critical section without blocking.
|
| |
| uint8_t | Leave () |
| | Release one level of ownership of the critical section.
|
| |
| uint8_t | LeaveAndUnlock () |
| | Release the critical section and re-enable task switching.
|
| |
| bool | UsedFromISR () |
| | Check whether this critical section is marked for ISR usage.
|
| |
| void | SetUseFromISR (bool enableFromISR) |
| | Enable or disable the UsedFromISR flag for this critical section.
|
| |
| bool | OwnedByCurTask () |
| | Check whether the calling task currently owns this critical section.
|
| |
| uint32_t | CurDepth () |
| | Get the current recursive entry depth of this critical section.
|
| |
|
|
class | fifo_buffer_storage |
| |
| void | ForceReboot (bool fromIRQ=false) |
| | Friend declaration allowing ForceReboot() to access critical section internals.
|
| |
Mutual-exclusion critical section for protecting shared resources between tasks.
An OS_CRIT ensures that only one task at a time can execute a protected code region. Tasks that attempt to enter a critical section already owned by another task will block until the owning task calls Leave(). Critical sections support recursive (nested) entry by the same task – each Enter() must be paired with a Leave().
For RAII-style usage, see OSCriticalSectionObj, OSLockAndCritObj, or OSSpinCrit.
- See also
- OSCriticalSectionObj, OSLockAndCritObj, OSSpinCrit
Expand for Example Usage
Examples
- Protect Shared Data
int gSharedCounter;
void IncrementCounter()
{
gSharedCounter++;
}
Mutual-exclusion critical section for protecting shared resources between tasks.
Definition nbrtos.h:1391
uint8_t Enter(TickTimeout &t)
Claim the critical section, using a TickTimeout to limit waiting.
uint8_t Leave()
Release one level of ownership of the critical section.
- RAII Usage with OSCriticalSectionObj
void SafeAccess()
{
gSharedCounter++;
}
RAII wrapper for OS_CRIT that automatically enters and leaves a critical section.
Definition nbrtos.h:3022
◆ OS_CRIT()
◆ CurDepth()
| uint32_t OS_CRIT::CurDepth |
( |
| ) |
|
|
inline |
Get the current recursive entry depth of this critical section.
A depth of 0 means the section is not currently claimed.
- Returns
- The current nesting depth count.
◆ Enter() [1/2]
Claim the critical section, using a TickTimeout to limit waiting.
The section supports recursive entry: if the calling task already owns it, this call succeeds immediately and increments the depth count. Each successful Enter() must be paired with a corresponding Leave().
- Parameters
-
- Return values
-
| OS_NO_ERR | The section was claimed (or already owned by this task). |
| OS_TIMEOUT | The section could not be claimed before the timeout. |
- See also
- EnterNoWait(), Leave(), NBRTOS Error Codes
◆ Enter() [2/2]
| uint8_t OS_CRIT::Enter |
( |
uint32_t | timeoutTicks = WAIT_FOREVER | ) |
|
|
inline |
Claim the critical section with a timeout in system ticks.
Each successful Enter() must be paired with a corresponding Leave().
- Parameters
-
| timeoutTicks | Maximum system ticks to wait. A value of 0 (default) waits forever. |
- Return values
-
| OS_NO_ERR | The section was claimed (or already owned by this task). |
| OS_TIMEOUT | The section could not be claimed before the timeout. |
- See also
- EnterNoWait(), Leave(), NBRTOS Error Codes
◆ EnterNoWait()
| uint8_t OS_CRIT::EnterNoWait |
( |
| ) |
|
Attempt to claim the critical section without blocking.
Returns immediately whether or not the section was claimed. Each successful call must be paired with a corresponding Leave().
- Return values
-
| OS_NO_ERR | The section was claimed (or already owned by this task). |
| OS_TIMEOUT | The section is owned by another task. |
- See also
- Enter(), Leave(), NBRTOS Error Codes
◆ Init()
| uint8_t OS_CRIT::Init |
( |
| ) |
|
◆ Leave()
| uint8_t OS_CRIT::Leave |
( |
| ) |
|
Release one level of ownership of the critical section.
Must be called once for each successful Enter() or EnterNoWait(). When the depth count reaches zero, the section is released and the highest-priority waiting task (if any) is made ready.
- Return values
-
| OS_NO_ERR | The depth count was decremented (or the section was fully released). |
| OS_CRIT_ERR | The calling task does not own this critical section. |
- See also
- Enter(), EnterNoWait(), NBRTOS Error Codes
◆ LeaveAndUnlock()
| uint8_t OS_CRIT::LeaveAndUnlock |
( |
| ) |
|
Release the critical section and re-enable task switching.
Must be called once for each successful LockAndEnter() call to release both the critical section and the scheduler lock.
- Return values
-
- See also
- LockAndEnter(), NBRTOS Error Codes
◆ LockAndEnter()
| uint8_t OS_CRIT::LockAndEnter |
( |
uint32_t | timeoutTicks = WAIT_FOREVER | ) |
|
Disable task switching (lock) and then claim the critical section.
Combines OSLock() with Enter() in a single call. If the section cannot be claimed before the timeout, the lock is automatically released. On success, you must call LeaveAndUnlock() once for each LockAndEnter() call.
- Parameters
-
| timeoutTicks | Maximum ticks to wait. A value of 0 (default) waits forever. |
- Return values
-
| OS_NO_ERR | The section was claimed (or was already owned by this task). |
| OS_TIMEOUT | The section could not be claimed before the timeout. |
- See also
- LeaveAndUnlock(), Enter(), NBRTOS Error Codes
◆ SetUseFromISR()
| void OS_CRIT::SetUseFromISR |
( |
bool | enableFromISR | ) |
|
|
inline |
Enable or disable the UsedFromISR flag for this critical section.
When set, the critical section uses interrupt-safe operations suitable for use from interrupt service routines.
- Parameters
-
| enableFromISR | Set to true to enable ISR usage, false to disable. |
- See also
- UsedFromISR()
◆ UsedFromISR()
| bool OS_CRIT::UsedFromISR |
( |
| ) |
|
|
inline |
Check whether this critical section is marked for ISR usage.
- Return values
-
| true | The UsedFromISR flag is set. |
| false | The flag is not set. |
- See also
- SetUseFromISR()
◆ ForceReboot
| void ForceReboot |
( |
bool | fromIRQ = false | ) |
|
|
friend |
Friend declaration allowing ForceReboot() to access critical section internals.
This function triggers a complete system restart by directly commanding the hardware reset mechanisms. The reset is immediate and unconditional - all running processes are terminated, memory contents are cleared, and the system restarts from the boot sequence. This is equivalent to a power cycle but is performed through software control. The function never returns to the calling code as the system undergoes a complete restart.
- Parameters
-
| fromIRQ | Optional parameter indicating the reset context true: reset is being triggered from within an interrupt service routine or exception handler, which may require special handling procedures false: reset is being triggered from normal application code (default) |
- Note
- This function never returns - execution does not continue after this call. All unsaved data, network connections, and application state will be lost. File systems and persistent storage should be properly synchronized before calling. The fromIRQ parameter ensures proper reset handling when called from interrupt contexts. After reset, the system will restart from the beginning of the boot sequence. Use this function only when a complete system restart is absolutely necessary.
- Warning
- This function immediately terminates all system operations without cleanup. Ensure critical data is saved and network connections are properly closed before use.
- See also
- attribute((noreturn)) indicates this function never returns to the caller
The documentation for this struct was generated from the following file: