Skip to content

Commit eacdddb

Browse files
committed
Introduce STK_TLS macro to enable TLS (off by default).
Introduce STK_TLS_PREFER_REGISTER macro to use R9 register for ARM Cortex-M arch. Add TLS example for emulator arch.
1 parent 14156cd commit eacdddb

22 files changed

Lines changed: 776 additions & 77 deletions

File tree

.github/workflows/cmake-test-generic-stm32.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ jobs:
1515
matrix:
1616
include:
1717
- name: "Configure CMake (Board: STM32F407DISC1, CPU: Arm Cortex-M4)"
18-
cmake_flags: -DVENDOR_STM32=ON -DTARGET_CORTEX_M4=ON -DTARGET_CPU_FAMILY=STM32F407xx
18+
cmake_flags: -DVENDOR_STM32=ON -DTARGET_CORTEX_M4=ON -DTARGET_CPU_FAMILY=STM32F407xx -DENABLE_TLS=ON
1919
cpu: cortex-m4
2020
board: STM32F4-Discovery
2121

.github/workflows/cmake-test-generic.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- uses: actions/checkout@v6
1818

1919
- name: Configure CMake
20-
run: cmake -G "Unix Makefiles" -B ${{github.workspace}}/build -DBUILD_LIB=ON -DBUILD_TESTS=ON -DTEST_GENERIC=ON
20+
run: cmake -G "Unix Makefiles" -B ${{github.workspace}}/build -DBUILD_LIB=ON -DBUILD_TESTS=ON -DTEST_GENERIC=ON -DENABLE_TLS=ON
2121

2222
- name: Build
2323
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} --parallel 4

CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,17 @@ if (BUILD_DOC)
8585
endif()
8686

8787
# Declare tickles mode if enabled (see stk_config.h.in)
88-
if (MODE_TICKLESS)
88+
if (ENABLE_TICKLESS)
8989
message(STATUS "* tickless mode")
9090
set(STK_TICKLESS_IDLE 1)
9191
endif()
9292

93+
# Declare tickles mode if enabled (see stk_config.h.in)
94+
if (ENABLE_TLS)
95+
message(STATUS "* enable TLS")
96+
set(STK_TLS 1)
97+
endif()
98+
9399
# Create stk_config.h (note: must be defined after inclusion of the projects to catch value of _STK_DEVICE_INC)
94100
if (WIN32)
95101
set(_STK_ARCH_X86_WIN32 TRUE)

README.md

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -571,55 +571,54 @@ Compatible with:
571571
Below example toggles RGB LEDs on a development board. Each LED is controlled by its own thread, switching at 1s intervals:
572572

573573
```cpp
574-
#include <stk_config.h>
575574
#include <stk.h>
576-
#include "example.h"
575+
#include <sync/stk_sync_eventflags.h>
577576

578-
static volatile uint8_t g_TaskSwitch = 0;
577+
using namespace bsp;
578+
579+
enum { TASK_STACK_SIZE = 256 };
580+
581+
static const uint32_t FLAGS_ALL[] = {
582+
(1U << LED_RED),
583+
(1U << LED_ORANGE),
584+
(1U << LED_GREEN),
585+
(1U << LED_BLUE)
586+
};
587+
static stk::sync::EventFlags g_TaskFlags(FLAGS_ALL[LED_RED]);
588+
static stk::Ticks g_Timeline = 0;
579589

580590
template <stk::EAccessMode _AccessMode>
581-
class MyTask : public stk::Task<256, _AccessMode>
591+
class MyTask : public stk::Task<TASK_STACK_SIZE, _AccessMode>
582592
{
583-
uint8_t m_taskId;
593+
uint8_t m_task_id;
594+
uint32_t m_my_flag;
595+
uint32_t m_next_flag;
584596

585597
public:
586-
MyTask(uint8_t taskId) : m_taskId(taskId)
598+
MyTask(uint8_t task_id) : m_task_id(task_id), m_my_flag(FLAGS_ALL[task_id]),
599+
m_next_flag(FLAGS_ALL[(task_id + 1) % LED_MAX])
587600
{}
588601

589602
private:
590-
void Run()
603+
void Run() override
591604
{
592-
uint8_t task_id = m_taskId;
605+
const stk::Timeout period = stk::GetTicksFromMs(1000);
606+
g_Timeline = stk::GetTicks();
593607

594608
while (true)
595609
{
596-
if (g_TaskSwitch != task_id)
597-
{
598-
stk::Sleep(10);
610+
uint32_t result = g_TaskFlags.Wait(m_my_flag, stk::sync::EventFlags::OPT_WAIT_ANY);
611+
if (stk::sync::EventFlags::IsError(result))
599612
continue;
600-
}
601613

602-
switch (task_id)
603614
{
604-
case 0:
605-
LED_SET_STATE(LED_RED, true);
606-
LED_SET_STATE(LED_GREEN, false);
607-
LED_SET_STATE(LED_BLUE, false);
608-
break;
609-
case 1:
610-
LED_SET_STATE(LED_RED, false);
611-
LED_SET_STATE(LED_GREEN, true);
612-
LED_SET_STATE(LED_BLUE, false);
613-
break;
614-
case 2:
615-
LED_SET_STATE(LED_RED, false);
616-
LED_SET_STATE(LED_GREEN, false);
617-
LED_SET_STATE(LED_BLUE, true);
618-
break;
615+
stk::hw::CriticalSection::ScopedLock __guard;
616+
Led::SwitchOnExclusive(static_cast<LedId>(m_task_id));
619617
}
620618

621-
stk::Sleep(1000);
622-
g_TaskSwitch = (task_id + 1) % 3;
619+
stk::SleepUntil(g_Timeline += period);
620+
621+
g_TaskFlags.Set(m_next_flag);
623622
}
624623
}
625624
};
@@ -628,23 +627,27 @@ void RunExample()
628627
{
629628
using namespace stk;
630629

631-
LED_INIT(LED_RED, false);
632-
LED_INIT(LED_GREEN, false);
633-
LED_INIT(LED_BLUE, false);
630+
Led::InitAll(false);
631+
632+
const uint8_t KernelMode = KERNEL_STATIC | KERNEL_SYNC | (STK_TICKLESS_IDLE ? KERNEL_TICKLESS : 0);
633+
634+
static Kernel<KernelMode, 4, SwitchStrategyRR, PlatformDefault> kernel;
634635

635-
static Kernel<KERNEL_STATIC, 3, SwitchStrategyRoundRobin, PlatformDefault> kernel;
636-
static MyTask<ACCESS_PRIVILEGED> task1(0), task2(1), task3(2);
636+
static MyTask<ACCESS_PRIVILEGED> task1(LED_RED);
637+
static MyTask<ACCESS_PRIVILEGED> task2(LED_ORANGE);
638+
static MyTask<ACCESS_PRIVILEGED> task3(LED_GREEN);
639+
static MyTask<ACCESS_PRIVILEGED> task4(LED_BLUE);
637640

638-
kernel.Initialize(PERIODICITY_DEFAULT);
641+
kernel.Initialize();
639642

640643
kernel.AddTask(&task1);
641644
kernel.AddTask(&task2);
642645
kernel.AddTask(&task3);
646+
kernel.AddTask(&task4);
643647

644648
kernel.Start();
645649

646-
assert(false);
647-
while (true);
650+
STK_ASSERT(false);
648651
}
649652
```
650653

build/example/project/eclipse/risc-v/tls/src/stk_config.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@
1212

1313
#include "risc-v/encoding.h"
1414

15-
// Risc-V
15+
// RISC-V arch.
1616
#define _STK_ARCH_RISC_V
1717

18+
// Use TLS.
19+
#define STK_TLS (1)
20+
1821
// Minimal stack size depending on the configured architecture (STK default is 32).
1922
#if (__riscv_32e != 1)
2023
#if (__riscv_flen == 0)
@@ -24,6 +27,7 @@
2427
#endif
2528
#endif
2629

30+
// Override ISR handlers if your BSP are using other names.
2731
#ifdef _STK_ARCH_RISC_V
2832
// Redefine if SysTick handler name is different from SysTick_Handler
2933
//#define STK_SYSTICK_HANDLER SysTick_Handler

build/example/project/eclipse/stm/tls-stm32f407g-disc1/.project

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,16 @@
125125
<type>2</type>
126126
<locationURI>virtual:/virtual</locationURI>
127127
</link>
128+
<link>
129+
<name>deps/stk/include/sync</name>
130+
<type>2</type>
131+
<locationURI>virtual:/virtual</locationURI>
132+
</link>
133+
<link>
134+
<name>deps/stk/include/time</name>
135+
<type>2</type>
136+
<locationURI>virtual:/virtual</locationURI>
137+
</link>
128138
<link>
129139
<name>deps/stk/src/arch</name>
130140
<type>2</type>
@@ -220,11 +230,111 @@
220230
<type>2</type>
221231
<locationURI>virtual:/virtual</locationURI>
222232
</link>
233+
<link>
234+
<name>deps/stk/include/strategy/stk_strategy_edf.h</name>
235+
<type>1</type>
236+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/strategy/stk_strategy_edf.h</locationURI>
237+
</link>
238+
<link>
239+
<name>deps/stk/include/strategy/stk_strategy_fpriority.h</name>
240+
<type>1</type>
241+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/strategy/stk_strategy_fpriority.h</locationURI>
242+
</link>
243+
<link>
244+
<name>deps/stk/include/strategy/stk_strategy_monotonic.h</name>
245+
<type>1</type>
246+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/strategy/stk_strategy_monotonic.h</locationURI>
247+
</link>
223248
<link>
224249
<name>deps/stk/include/strategy/stk_strategy_rrobin.h</name>
225250
<type>1</type>
226251
<locationURI>PARENT-6-PROJECT_LOC/stk/include/strategy/stk_strategy_rrobin.h</locationURI>
227252
</link>
253+
<link>
254+
<name>deps/stk/include/strategy/stk_strategy_swrrobin.h</name>
255+
<type>1</type>
256+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/strategy/stk_strategy_swrrobin.h</locationURI>
257+
</link>
258+
<link>
259+
<name>deps/stk/include/sync/README.md</name>
260+
<type>1</type>
261+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/README.md</locationURI>
262+
</link>
263+
<link>
264+
<name>deps/stk/include/sync/stk_sync.h</name>
265+
<type>1</type>
266+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync.h</locationURI>
267+
</link>
268+
<link>
269+
<name>deps/stk/include/sync/stk_sync_cs.h</name>
270+
<type>1</type>
271+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_cs.h</locationURI>
272+
</link>
273+
<link>
274+
<name>deps/stk/include/sync/stk_sync_cv.h</name>
275+
<type>1</type>
276+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_cv.h</locationURI>
277+
</link>
278+
<link>
279+
<name>deps/stk/include/sync/stk_sync_event.h</name>
280+
<type>1</type>
281+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_event.h</locationURI>
282+
</link>
283+
<link>
284+
<name>deps/stk/include/sync/stk_sync_eventflags.h</name>
285+
<type>1</type>
286+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_eventflags.h</locationURI>
287+
</link>
288+
<link>
289+
<name>deps/stk/include/sync/stk_sync_msgqueue.h</name>
290+
<type>1</type>
291+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_msgqueue.h</locationURI>
292+
</link>
293+
<link>
294+
<name>deps/stk/include/sync/stk_sync_mutex.h</name>
295+
<type>1</type>
296+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_mutex.h</locationURI>
297+
</link>
298+
<link>
299+
<name>deps/stk/include/sync/stk_sync_pipe.h</name>
300+
<type>1</type>
301+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_pipe.h</locationURI>
302+
</link>
303+
<link>
304+
<name>deps/stk/include/sync/stk_sync_rwmutex.h</name>
305+
<type>1</type>
306+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_rwmutex.h</locationURI>
307+
</link>
308+
<link>
309+
<name>deps/stk/include/sync/stk_sync_semaphore.h</name>
310+
<type>1</type>
311+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_semaphore.h</locationURI>
312+
</link>
313+
<link>
314+
<name>deps/stk/include/sync/stk_sync_spinlock.h</name>
315+
<type>1</type>
316+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/sync/stk_sync_spinlock.h</locationURI>
317+
</link>
318+
<link>
319+
<name>deps/stk/include/time/README.md</name>
320+
<type>1</type>
321+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/time/README.md</locationURI>
322+
</link>
323+
<link>
324+
<name>deps/stk/include/time/stk_time.h</name>
325+
<type>1</type>
326+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/time/stk_time.h</locationURI>
327+
</link>
328+
<link>
329+
<name>deps/stk/include/time/stk_time_timer.h</name>
330+
<type>1</type>
331+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/time/stk_time_timer.h</locationURI>
332+
</link>
333+
<link>
334+
<name>deps/stk/include/time/stk_time_util.h</name>
335+
<type>1</type>
336+
<locationURI>PARENT-6-PROJECT_LOC/stk/include/time/stk_time_util.h</locationURI>
337+
</link>
228338
<link>
229339
<name>deps/stk/src/arch/arm</name>
230340
<type>2</type>

build/example/project/eclipse/stm/tls-stm32f407g-disc1/src/stk_config.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111
#define STK_CONFIG_H_
1212

1313
#include "cmsis_device.h"
14-
#include "core_cm4.h"
1514

16-
// Undefine if MCU is Arm Cortex-M4
15+
// MCU is Arm Cortex-M.
1716
#define _STK_ARCH_ARM_CORTEX_M
1817

19-
// Low-power scenario, use (0) for a high-performance processing when consumed power does not matter
18+
// Low-power scenario, use (0) for a high-performance processing when consumed power does not matter.
2019
#define STK_TICKLESS_IDLE (1)
2120

21+
// Use TLS.
22+
#define STK_TLS (1)
23+
24+
// Use fast inline TLS based on CPU register (-ffixed-r9 compiler flag is used to preserve R9 for TLS).
25+
#define STK_TLS_PREFER_REGISTER (1)
26+
27+
// Override ISR handlers if your BSP are using other names.
2228
#ifdef _STK_ARCH_ARM_CORTEX_M
2329
// Redefine if SysTick handler name is different from SysTick_Handler
2430
//#define STK_SYSTICK_HANDLER SysTick_Handler

0 commit comments

Comments
 (0)