SuperTinyKernel™ RTOS 1.06.0
Lightweight, high-performance, deterministic, bare-metal C++ RTOS for resource-constrained embedded systems. MIT Open Source License.
Loading...
Searching...
No Matches
stk::sync::ScopedCriticalSection Class Reference

RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for other stk::sync classes, consider using hw::CriticalSection::ScopedLock implementation instead. More...

#include <stk_sync_cs.h>

Inheritance diagram for stk::sync::ScopedCriticalSection:
Collaboration diagram for stk::sync::ScopedCriticalSection:

Public Member Functions

 ScopedCriticalSection ()
 Enters critical section.
 ~ScopedCriticalSection ()
 Exits critical section.

Private Member Functions

 STK_NONCOPYABLE_CLASS (ScopedCriticalSection)
void Lock ()
 Lock the mutex.
void Unlock ()
 Unlock the mutex.

Friends

class Event
class EventFlags
class Mutex
class RWMutex
class Semaphore
template<typename T, size_t N>
class Pipe

Detailed Description

RAII-style low-level synchronization primitive for atomic code execution. Used as building brick for other stk::sync classes, consider using hw::CriticalSection::ScopedLock implementation instead.

Disables interrupts on caller's CPU core and guards from access by another CPU core in case of multi-core system. Enters a critical section upon construction and exits automatically when the object goes out of scope.

// Example: Protecting a global shared variable
uint32_t g_SharedCounter = 0;
void IncrementCounter() {
{ // code execution scope starts here
stk::sync::CriticalSection::ScopedLock __cs; // critical section starts here
g_SharedCounter++; // atomic update
} // critical section ends here (RAII)
}
Note
Global Impact: Use with extreme care. This primitive has a global effect on the system by preventing preemption. Long-running code inside a critical section will increase interrupt latency and may cause other tasks to miss their deadlines.
Unlike higher-level synchronization primitives, this is always available and does not depend on the KERNEL_SYNC configuration.
ISR-safe, the only safe primitive along with hw::CriticalSection for guarding code which can be accessed by ISR or another CPU core.
See also
IMutex, hw::CriticalSection

Definition at line 53 of file stk_sync_cs.h.

Constructor & Destructor Documentation

◆ ScopedCriticalSection()

stk::sync::ScopedCriticalSection::ScopedCriticalSection ( )
inlineexplicit

Enters critical section.

Definition at line 65 of file stk_sync_cs.h.

65{ Lock(); }
void Lock()
Lock the mutex.
Definition stk_sync_cs.h:75

References Lock().

Referenced by STK_NONCOPYABLE_CLASS().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ~ScopedCriticalSection()

stk::sync::ScopedCriticalSection::~ScopedCriticalSection ( )
inline

Exits critical section.

Note
MISRA deviation: [STK-DEV-005] Rule 10-3-2.

Definition at line 70 of file stk_sync_cs.h.

70{ Unlock(); }
void Unlock()
Unlock the mutex.
Definition stk_sync_cs.h:76

References Unlock().

Here is the call graph for this function:

Member Function Documentation

◆ Lock()

void stk::sync::ScopedCriticalSection::Lock ( )
inlineprivatevirtual

Lock the mutex.

Implements stk::IMutex.

Definition at line 75 of file stk_sync_cs.h.

static void Enter()
Enter a critical section.

References stk::hw::CriticalSection::Enter().

Referenced by ScopedCriticalSection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ STK_NONCOPYABLE_CLASS()

stk::sync::ScopedCriticalSection::STK_NONCOPYABLE_CLASS ( ScopedCriticalSection )
private

References ScopedCriticalSection().

Here is the call graph for this function:

◆ Unlock()

void stk::sync::ScopedCriticalSection::Unlock ( )
inlineprivatevirtual

Unlock the mutex.

Implements stk::IMutex.

Definition at line 76 of file stk_sync_cs.h.

static void Exit()
Exit a critical section.

References stk::hw::CriticalSection::Exit().

Referenced by ~ScopedCriticalSection().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Event

friend class Event
friend

Definition at line 55 of file stk_sync_cs.h.

References Event.

Referenced by Event.

◆ EventFlags

friend class EventFlags
friend

Definition at line 56 of file stk_sync_cs.h.

References EventFlags.

Referenced by EventFlags.

◆ Mutex

friend class Mutex
friend

Definition at line 57 of file stk_sync_cs.h.

References Mutex.

Referenced by Mutex.

◆ Pipe

template<typename T, size_t N>
friend class Pipe
friend

Definition at line 60 of file stk_sync_cs.h.

References Pipe.

Referenced by Pipe.

◆ RWMutex

friend class RWMutex
friend

Definition at line 58 of file stk_sync_cs.h.

References RWMutex.

Referenced by RWMutex.

◆ Semaphore

friend class Semaphore
friend

Definition at line 59 of file stk_sync_cs.h.

References Semaphore.

Referenced by Semaphore.


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