samples: synchronization: add static thread

Replace one of the dynamic threads with a static thread to show the
all the possible ways of creating threads.

Signed-off-by: Vilem Gottwald <xvigo.dev@gmail.com>
This commit is contained in:
Vilem Gottwald 2023-10-03 14:01:30 +02:00 committed by Carles Cufí
parent 5a4e0af54a
commit 1c7eb9ee72

View file

@ -12,8 +12,8 @@
/* /*
* The synchronization demo has two threads that utilize semaphores and sleeping * The synchronization demo has two threads that utilize semaphores and sleeping
* to take turns printing a greeting message at a controlled rate. The demo * to take turns printing a greeting message at a controlled rate. The demo
* shows only the dynamic approach for spawning a thread. Alternatively, * shows both the static and dynamic approaches for spawning a thread; a real
* a thread can be declared at compile time by calling K_THREAD_DEFINE. * world application would likely use the static approach for both threads.
*/ */
#define PIN_THREADS (IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_SCHED_CPU_MASK)) #define PIN_THREADS (IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_SCHED_CPU_MASK))
@ -33,7 +33,7 @@
* @param my_sem thread's own semaphore * @param my_sem thread's own semaphore
* @param other_sem other thread's semaphore * @param other_sem other thread's semaphore
*/ */
void helloLoop(const char *my_name, void hello_loop(const char *my_name,
struct k_sem *my_sem, struct k_sem *other_sem) struct k_sem *my_sem, struct k_sem *other_sem)
{ {
const char *tname; const char *tname;
@ -68,62 +68,52 @@ void helloLoop(const char *my_name,
} }
/* define semaphores */ /* define semaphores */
K_SEM_DEFINE(threadA_sem, 1, 1); /* starts off "available" */ K_SEM_DEFINE(thread_a_sem, 1, 1); /* starts off "available" */
K_SEM_DEFINE(threadB_sem, 0, 1); /* starts off "not available" */ K_SEM_DEFINE(thread_b_sem, 0, 1); /* starts off "not available" */
/* threadA is a dynamic thread that is spawned in main */ /* thread_a is a dynamic thread that is spawned in main */
void threadA(void *dummy1, void *dummy2, void *dummy3) void thread_a_entry_point(void *dummy1, void *dummy2, void *dummy3)
{ {
ARG_UNUSED(dummy1); ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2); ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3); ARG_UNUSED(dummy3);
/* invoke routine to ping-pong hello messages with threadB */ /* invoke routine to ping-pong hello messages with thread_b */
helloLoop(__func__, &threadA_sem, &threadB_sem); hello_loop(__func__, &thread_a_sem, &thread_b_sem);
} }
K_THREAD_STACK_DEFINE(thread_a_stack_area, STACKSIZE);
static struct k_thread thread_a_data;
K_THREAD_STACK_DEFINE(threadB_stack_area, STACKSIZE); /* thread_b is a static thread spawned immediately */
static struct k_thread threadB_data; void thread_b_entry_point(void *dummy1, void *dummy2, void *dummy3)
/* threadB is a dynamic thread that is spawned in main */
void threadB(void *dummy1, void *dummy2, void *dummy3)
{ {
ARG_UNUSED(dummy1); ARG_UNUSED(dummy1);
ARG_UNUSED(dummy2); ARG_UNUSED(dummy2);
ARG_UNUSED(dummy3); ARG_UNUSED(dummy3);
/* invoke routine to ping-pong hello messages with threadA */ /* invoke routine to ping-pong hello messages with thread_a */
helloLoop(__func__, &threadB_sem, &threadA_sem); hello_loop(__func__, &thread_b_sem, &thread_a_sem);
} }
K_THREAD_DEFINE(thread_b, STACKSIZE,
K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE); thread_b_entry_point, NULL, NULL, NULL,
static struct k_thread threadA_data; PRIORITY, 0, 0);
extern const k_tid_t thread_b;
int main(void) int main(void)
{ {
k_thread_create(&threadA_data, threadA_stack_area, k_thread_create(&thread_a_data, thread_a_stack_area,
K_THREAD_STACK_SIZEOF(threadA_stack_area), K_THREAD_STACK_SIZEOF(thread_a_stack_area),
threadA, NULL, NULL, NULL, thread_a_entry_point, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER); PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadA_data, "thread_a"); k_thread_name_set(&thread_a_data, "thread_a");
#if PIN_THREADS #if PIN_THREADS
if (arch_num_cpus() > 1) { if (arch_num_cpus() > 1) {
k_thread_cpu_pin(&threadA_data, 0); k_thread_cpu_pin(&thread_a_data, 0);
k_thread_cpu_pin(thread_b, 1);
} }
#endif #endif
k_thread_create(&threadB_data, threadB_stack_area, k_thread_start(&thread_a_data);
K_THREAD_STACK_SIZEOF(threadB_stack_area),
threadB, NULL, NULL, NULL,
PRIORITY, 0, K_FOREVER);
k_thread_name_set(&threadB_data, "thread_b");
#if PIN_THREADS
if (arch_num_cpus() > 1) {
k_thread_cpu_pin(&threadB_data, 1);
}
#endif
k_thread_start(&threadA_data);
k_thread_start(&threadB_data);
return 0; return 0;
} }