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.c File Reference

Blinky built on the STK pthread API (stk_c_pthread.h). More...

#include <pthread.h>
#include <stk_c.h>
#include "example.h"
Include dependency graph for blinky_c-pthread/example.c:

Go to the source code of this file.

Macros

#define PT_CHECK(call)

Enumerations

enum  {
  MAIN_STACK_SIZE = 256 ,
  THREAD_STACK_SIZE = 256
}

Functions

 STK_DEFINE_STACK_POOL (g_MainStack, 1, MAIN_STACK_SIZE)
 STK_DEFINE_STACK_POOL (g_ThreadStack, LED_MAX, THREAD_STACK_SIZE)
static void * LedThread (void *arg)
static void MainTask (void *arg)
void RunExample ()

Variables

static pthread_mutex_t g_Lock = PTHREAD_MUTEX_INITIALIZER
static pthread_cond_t g_Turn = PTHREAD_COND_INITIALIZER
static uint32_t g_Baton = LED_RED
static stk_tick_t g_Timeline = 0
static pthread_t g_Threads [LED_MAX]

Detailed Description

Blinky built on the STK pthread API (stk_c_pthread.h).

Definition in file blinky_c-pthread/example.c.

Macro Definition Documentation

◆ PT_CHECK

#define PT_CHECK ( call)
Value:
do { int rc_ = (call); STK_C_ASSERT(rc_ == 0); (void)rc_; } while (0)
#define STK_C_ASSERT(e)
Assertion macro used inside STK C bindings.
Definition stk_c.h:104
Examples
blinky_c-pthread/example.c.

Definition at line 52 of file blinky_c-pthread/example.c.

Referenced by LedThread(), and MainTask().

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
MAIN_STACK_SIZE 
THREAD_STACK_SIZE 

Definition at line 35 of file blinky_c-pthread/example.c.

35{ MAIN_STACK_SIZE = 256, THREAD_STACK_SIZE = 256 }; // in stk_word_t units

Function Documentation

◆ LedThread()

void * LedThread ( void * arg)
static
Examples
blinky_c-pthread/example.c.

Definition at line 54 of file blinky_c-pthread/example.c.

55{
56 const uint32_t id = (uint32_t)(uintptr_t)arg;
57 const uint32_t next = (id + 1U) % LED_MAX;
58 const stk_tick_t period = stk_ticks_from_ms(250);
59
60 while (true)
61 {
62 // block until it is this thread's turn
64 while (g_Baton != id)
65 {
67 }
69
70 // change LED state
71 {
75 }
76
77 // sleep 1s drift-free: only the baton holder touches g_Timeline, so no locking needed
78 g_Timeline += period;
80
81 // hand off to the next thread
83 g_Baton = next;
86 }
87
88 return NULL; // never reached
89}
void Led_SwitchOnExclusive(LedId led)
Definition c_wrapper.cpp:32
LedId
Definition led.h:16
@ LED_MAX
Definition led.h:22
stk_tick_t stk_ticks_from_ms(stk_time_t msec)
Get ticks from milliseconds using current kernel tick resolution.
Definition stk_c.cpp:792
void stk_critical_section_enter()
Enter global critical section - disable context switches on current core.
Definition stk_c.cpp:834
int64_t stk_tick_t
Ticks value.
Definition stk_c.h:165
bool stk_sleep_until(stk_tick_t ts)
Put current task to sleep (non-HRT kernels only).
Definition stk_c.cpp:800
void stk_critical_section_exit()
Leave global critical section - re-enable context switches.
Definition stk_c.cpp:841
int pthread_mutex_lock(pthread_mutex_t *mutex)
Lock a mutex, blocking until it becomes available.
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Atomically unlock mutex and wait for a signal; re-locks mutex before returning.
int pthread_mutex_unlock(pthread_mutex_t *mutex)
Unlock a mutex held by the calling thread.
int pthread_cond_broadcast(pthread_cond_t *cond)
Wake all threads waiting on cond (no effect if none is waiting).
static stk::Ticks g_Timeline
static pthread_mutex_t g_Lock
#define PT_CHECK(call)
static uint32_t g_Baton
static pthread_cond_t g_Turn

References g_Baton, g_Lock, g_Timeline, g_Turn, LED_MAX, Led_SwitchOnExclusive(), PT_CHECK, pthread_cond_broadcast(), pthread_cond_wait(), pthread_mutex_lock(), pthread_mutex_unlock(), stk_critical_section_enter(), stk_critical_section_exit(), stk_sleep_until(), and stk_ticks_from_ms().

Referenced by MainTask().

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

◆ MainTask()

void MainTask ( void * arg)
static
Examples
blinky_c-pthread/example.c.

Definition at line 92 of file blinky_c-pthread/example.c.

93{
94 (void)arg;
95
96 // initialize explicitly (instead of lazily on first use) to avoid any first-use race
99
100 // start of the timeline
102
103 for (uint32_t i = 0; i < LED_MAX; i++)
104 {
105 pthread_attr_t attr;
107
108 // caller-owned static stack: avoids the default 1024-word pool and malloc()
110 sizeof(g_ThreadStack[i])));
111
112 // privileged thread: some MCUs (e.g. Cortex-M7/M33) do not allow GPIO access from user mode
114
115 PT_CHECK(pthread_create(&g_Threads[i], &attr, LedThread, (void *)(uintptr_t)i));
117 }
118
119 // block forever
120 for (uint32_t i = 0; i < LED_MAX; i++)
121 {
122 pthread_join(g_Threads[i], NULL);
123 }
124}
#define STK_GET_STACK_FROM_POOL(name, task_id)
Retrieves the base stack pointer for a specific task ID from a stack pool.
Definition stk_c.h:292
stk_tick_t stk_ticks(void)
Returns number of ticks elapsed since kernel start.
Definition stk_c.cpp:789
int pthread_attr_init(pthread_attr_t *attr)
Initialize a thread attributes object with default values (default stack size, no external stack,...
int pthread_join(pthread_t thread, void **retval)
Block until the given joinable thread finishes, then reclaim its resources.
#define PTHREAD_CREATE_PRIVILEGED_NP
int pthread_attr_destroy(pthread_attr_t *attr)
Destroy a thread attributes object (no-op; no owned resources).
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Initialize a mutex.
int pthread_attr_setprivileged_np(pthread_attr_t *attr, int mode)
Non-portable: select the STK access mode of threads created with attr.
int pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
Supply an external, caller-owned stack buffer for the thread.
int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Initialize a condition variable.
int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Create and start a new thread.
Thread creation attributes.
static void * LedThread(void *arg)
static pthread_t g_Threads[LED_MAX]

References g_Lock, g_Threads, g_Timeline, g_Turn, LED_MAX, LedThread(), PT_CHECK, pthread_attr_destroy(), pthread_attr_init(), pthread_attr_setprivileged_np(), pthread_attr_setstack(), pthread_cond_init(), pthread_create(), PTHREAD_CREATE_PRIVILEGED_NP, pthread_join(), pthread_mutex_init(), STK_GET_STACK_FROM_POOL, and stk_ticks().

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 126 of file blinky_c-pthread/example.c.

127{
128 Led_InitAll(false);
129
130 // allocate scheduling kernel (KERNEL_DYNAMIC + KERNEL_SYNC required for pthreads)
132 STK_C_ASSERT(k != NULL);
133
134 // init kernel with default periodicity - 1ms tick
136
137 // tell the pthread shim which kernel pthread_create() adds threads to
139
140 stk_task_t *main_task = stk_task_create_user(MainTask, NULL,
141 STK_GET_STACK_FROM_POOL(g_MainStack, 0),
143 STK_C_ASSERT(main_task != NULL);
144
145 stk_kernel_add_task(k, main_task);
146
147 // start scheduler, execution in main() will be blocked on this line
149
150 // shall not reach here after stk_kernel_start() was called
151 STK_C_ASSERT(false);
152}
void Led_InitAll(bool state)
Definition c_wrapper.cpp:27
stk_task_t * stk_task_create_user(stk_task_entry_t entry, void *arg, stk_word_t *stack, uint32_t stack_size)
Create user-mode task.
Definition stk_c.cpp:662
struct stk_kernel_t stk_kernel_t
Opaque handle to a kernel instance.
Definition stk_c.h:188
void stk_kernel_start(stk_kernel_t *k)
Start the scheduler - never returns.
Definition stk_c.cpp:478
stk_add_result_t stk_kernel_add_task(stk_kernel_t *k, stk_task_t *tsk)
Add task to non-HRT kernel (static or dynamic).
Definition stk_c.cpp:506
#define STK_PERIODICITY_DEFAULT
Default tick period (1 ms).
Definition stk_c.h:196
void stk_kernel_init(stk_kernel_t *k, uint32_t tick_period_us)
Initialize kernel with given tick period.
Definition stk_c.cpp:471
stk_kernel_t * stk_kernel_create(uint8_t core_nr)
Create kernel.
Definition stk_c.cpp:403
void stk_pthread_bind_kernel(void *kernel)
Bind the STK kernel instance that pthread_create() will add new threads to.
static void MainTask(void *arg)

References Led_InitAll(), MAIN_STACK_SIZE, MainTask(), STK_C_ASSERT, STK_GET_STACK_FROM_POOL, stk_kernel_add_task(), stk_kernel_create(), stk_kernel_init(), stk_kernel_start(), STK_PERIODICITY_DEFAULT, stk_pthread_bind_kernel(), and stk_task_create_user().

Here is the call graph for this function:

◆ STK_DEFINE_STACK_POOL() [1/2]

STK_DEFINE_STACK_POOL ( g_MainStack ,
1 ,
MAIN_STACK_SIZE  )

References MAIN_STACK_SIZE.

◆ STK_DEFINE_STACK_POOL() [2/2]

STK_DEFINE_STACK_POOL ( g_ThreadStack ,
LED_MAX ,
THREAD_STACK_SIZE  )

References LED_MAX, and THREAD_STACK_SIZE.

Variable Documentation

◆ g_Baton

uint32_t g_Baton = LED_RED
static
Examples
blinky_c-pthread/example.c.

Definition at line 44 of file blinky_c-pthread/example.c.

Referenced by LedThread().

◆ g_Lock

Examples
blinky_c-pthread/example.c.

Definition at line 42 of file blinky_c-pthread/example.c.

Referenced by LedThread(), and MainTask().

◆ g_Threads

pthread_t g_Threads[LED_MAX]
static
Examples
blinky_c-pthread/example.c.

Definition at line 49 of file blinky_c-pthread/example.c.

Referenced by MainTask().

◆ g_Timeline

stk_tick_t g_Timeline = 0
static

Definition at line 47 of file blinky_c-pthread/example.c.

◆ g_Turn

Examples
blinky_c-pthread/example.c.

Definition at line 43 of file blinky_c-pthread/example.c.

Referenced by LedThread(), and MainTask().