SuperTinyKernel™ RTOS 1.08.x
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 Referencefinal

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.
STK_VIRT_DTOR ~ScopedCriticalSection ()
 Exits critical section.

Protected Member Functions

 STK_NONCOPYABLE_CLASS (ScopedCriticalSection)
void Lock () override
 Lock the mutex.
bool TryLock () override
 Try locking the mutex in a single non-blocking attempt.
void Unlock () override
 Unlock the mutex.

Protected Attributes

hw::CriticalSection::Session m_ses
 current session of hw::CriticalSection

Friends

class Event
template<typename T>
class EventFlagsT
class Mutex
class FastMutex
class RWMutex
template<typename T>
class SemaphoreT
class Pipe
template<typename T, size_t N>
class PipeT

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 71 of file stk_sync_cs.h.

72 {
73 Lock();
74 }
static constexpr Session DEFAULT_SESSION
Default session value passed to Enter()/Exit() when the caller does not need to force a specific hand...
Definition stk_arch.h:479
hw::CriticalSection::Session m_ses
current session of hw::CriticalSection
void Lock() override
Lock the mutex.
Definition stk_sync_cs.h:87

References Lock(), and m_ses.

Referenced by STK_NONCOPYABLE_CLASS().

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

◆ ~ScopedCriticalSection()

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

Exits critical section.

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

Definition at line 79 of file stk_sync_cs.h.

80 {
81 Unlock();
82 }
void Unlock() override
Unlock the mutex.
Definition stk_sync_cs.h:98

References STK_VIRT_DTOR, and Unlock().

Here is the call graph for this function:

Member Function Documentation

◆ Lock()

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

Lock the mutex.

Implements stk::IMutex.

Definition at line 87 of file stk_sync_cs.h.

88 {
90 }
static Session Enter(const Session ses=DEFAULT_SESSION)
Enter a critical section.

References stk::hw::CriticalSection::Enter(), and m_ses.

Referenced by ScopedCriticalSection(), and TryLock().

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 )
protected

References ScopedCriticalSection().

Here is the call graph for this function:

◆ TryLock()

bool stk::sync::ScopedCriticalSection::TryLock ( )
inlineoverrideprotectedvirtual

Try locking the mutex in a single non-blocking attempt.

Returns
true if the lock was acquired; false if it was already held by another thread/core.

Implements stk::IMutex.

Definition at line 92 of file stk_sync_cs.h.

93 {
94 Lock();
95 return true;
96 }

References Lock().

Here is the call graph for this function:

◆ Unlock()

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

Unlock the mutex.

Implements stk::IMutex.

Definition at line 98 of file stk_sync_cs.h.

99 {
101 }
static void Exit(const Session ses=DEFAULT_SESSION)
Exit a critical section.

References stk::hw::CriticalSection::Exit(), and m_ses.

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 59 of file stk_sync_cs.h.

References Event.

Referenced by Event.

◆ EventFlagsT

template<typename T>
friend class EventFlagsT
friend

Definition at line 60 of file stk_sync_cs.h.

References EventFlagsT.

Referenced by EventFlagsT.

◆ FastMutex

friend class FastMutex
friend

Definition at line 62 of file stk_sync_cs.h.

References FastMutex.

Referenced by FastMutex.

◆ Mutex

friend class Mutex
friend

Definition at line 61 of file stk_sync_cs.h.

References Mutex.

Referenced by Mutex.

◆ Pipe

friend class Pipe
friend

Definition at line 65 of file stk_sync_cs.h.

References Pipe.

Referenced by Pipe.

◆ PipeT

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

Definition at line 66 of file stk_sync_cs.h.

References PipeT.

Referenced by PipeT.

◆ RWMutex

friend class RWMutex
friend

Definition at line 63 of file stk_sync_cs.h.

References RWMutex.

Referenced by RWMutex.

◆ SemaphoreT

template<typename T>
friend class SemaphoreT
friend

Definition at line 64 of file stk_sync_cs.h.

References SemaphoreT.

Referenced by SemaphoreT.

Member Data Documentation

◆ m_ses

hw::CriticalSection::Session stk::sync::ScopedCriticalSection::m_ses
protected

current session of hw::CriticalSection

Definition at line 103 of file stk_sync_cs.h.

Referenced by Lock(), ScopedCriticalSection(), and Unlock().


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