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
example.cpp File Reference

Critical section demo: a data race without it and a race-free update with it. More...

#include <stk.h>
#include "example.h"
Include dependency graph for critical_section/example.cpp:

Go to the source code of this file.

Classes

class  Thread< _AccessMode >

Enumerations

enum  {
  THREADS_MAX = 8 ,
  ITERATIONS_MAX = 1000 / THREADS_MAX ,
  COUNTER_EXPECT = ITERATIONS_MAX * THREADS_MAX
}

Functions

static void UnsafeIncrement ()
static void IncrementWithoutCS (void *)
static void IncrementWithCS (void *)
void RunExample ()

Variables

int32_t g_Counter = 0

Detailed Description

Critical section demo: a data race without it and a race-free update with it.

Definition in file critical_section/example.cpp.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
THREADS_MAX 
ITERATIONS_MAX 
COUNTER_EXPECT 

Definition at line 33 of file critical_section/example.cpp.

Function Documentation

◆ IncrementWithCS()

void IncrementWithCS ( void * )
static
Examples
critical_section/example.cpp.

Definition at line 69 of file critical_section/example.cpp.

70{
71 for (int32_t i = 0; i < ITERATIONS_MAX; ++i)
72 {
74 stk::hw::CriticalSection::ScopedLock __cs2; // STK supports nesting of critical sections
75
76 // you can also use sync::ScopedCriticalSection instead of hw::CriticalSection
77
79 }
80}
RAII instance that enters the critical section on construction and exits it on destruction.
Definition stk_arch.h:495
static void UnsafeIncrement()

References ITERATIONS_MAX, and UnsafeIncrement().

Referenced by RunExample().

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

◆ IncrementWithoutCS()

void IncrementWithoutCS ( void * )
static
Examples
critical_section/example.cpp.

Definition at line 60 of file critical_section/example.cpp.

61{
62 for (int32_t i = 0; i < ITERATIONS_MAX; ++i)
63 {
65 }
66}

References ITERATIONS_MAX, and UnsafeIncrement().

Referenced by RunExample().

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

◆ RunExample()

void RunExample ( void )

Definition at line 93 of file critical_section/example.cpp.

94{
95 using namespace stk;
96
97 Led::InitAll(false);
98
99 // allocate scheduling kernel for THREADS_MAX threads (tasks) with Round-robin scheduling strategy
101
102 // note: using ACCESS_PRIVILEGED as some MCUs may not allow writing to GPIO from a user thread, such as i.MX RT1050 (Arm Cortex-M7)
104
105 // init scheduling kernel
106 kernel.Initialize();
107
108 // RED and GREEN leds will blink one by one, in case of RED led g_Counter variable got incorrect
109 // count due to race, in case of GREEN led a critical section was used which protected
110 // variable from the race
111
112 static bool toogle = false;
113 while (true)
114 {
115 // init counter (or re-init in the next round)
116 g_Counter = 0;
117
118 // register threads (tasks)
119 for (int32_t i = 0; i < THREADS_MAX; ++i)
120 {
121 // we shall see Green/Red LEDs switching on one by one in a loop
122 task[i].m_func = (toogle ? &IncrementWithCS : &IncrementWithoutCS);
123
124 // add task to the kernel
125 kernel.AddTask(&task[i]);
126 }
127
128 // start scheduler (it will start threads added by AddTask), execution in main()
129 // will be blocked on this line until all tasks exit (complete)
130 kernel.Start();
131
132 // signal result: red - thread function was executed without critical section, thus
133 // g_Counter will not match COUNTER_EXPECT, green - thread function's execution
134 // was protected by the critical section and g_Counter matched COUNTER_EXPECT,
135 // as this loop is endless green and red LEDs will clink one by one
138
139 // switch to the next round
140 toogle = !toogle;
141 }
142}
Namespace of STK package.
Concrete implementation of IKernel.
Definition stk.h:102
void Start() override
Start the scheduler. This call does not return until all tasks have exited (KERNEL_DYNAMIC mode) or i...
Definition stk.h:1999
EAddResult AddTask(ITask *user_task) override
Register task for a soft real-time (SRT) scheduling.
Definition stk.h:1465
void Initialize(uint32_t resolution_us=PERIODICITY_DEFAULT) override
Initialize kernel.
Definition stk.h:1425
void(* m_func)(void *user_data)
static constexpr Id GREEN
Definition led.h:34
static constexpr Id RED
Definition led.h:36
static void InitAll(bool init_state)
Definition led.h:40
static void Set(Id led, bool state)
int32_t g_Counter
static void IncrementWithoutCS(void *)
static void IncrementWithCS(void *)

References stk::Kernel< TMode, TSize, TStrategy, TPlatform >::AddTask(), COUNTER_EXPECT, g_Counter, bsp::Led::GREEN, IncrementWithCS(), IncrementWithoutCS(), bsp::Led::InitAll(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Initialize(), Thread< _AccessMode >::m_func, bsp::Led::RED, bsp::Led::Set(), stk::Kernel< TMode, TSize, TStrategy, TPlatform >::Start(), and THREADS_MAX.

Here is the call graph for this function:

◆ UnsafeIncrement()

void UnsafeIncrement ( )
static
Examples
critical_section/example.cpp.

Definition at line 43 of file critical_section/example.cpp.

44{
45 // read current value
46 int32_t temp = g_Counter;
47
48 // we are making thread-race with this delay inevitable, we can't invoke stk::Sleep() or stk::Delay()
49 // here as it will deadlock program execution because critical section temporarily frozen
50 // our scheduler, thus use critical section with care and ONLY if REALLY necessary
51 for (volatile uint32_t delay = 0; delay < 100000; delay++)
52 {}
53
54 // it will overwrite the result which was most likely set by another thread
55 // therefore counter will not be incremented ITERATIONS_MAX * THREADS_MAX times
56 g_Counter = temp + 1;
57}

References g_Counter.

Referenced by IncrementWithCS(), and IncrementWithoutCS().

Here is the caller graph for this function:

Variable Documentation

◆ g_Counter

int32_t g_Counter = 0
Examples
critical_section/example.cpp.

Definition at line 41 of file critical_section/example.cpp.

Referenced by RunExample(), and UnsafeIncrement().