1 /* main.c - Synchronization demo */
2
3 /*
4 * Copyright (c) 2012-2014 Wind River Systems, Inc.
5 *
6 * SPDX-License-Identifier: Apache-2.0
7 */
8
9 #include <zephyr/kernel.h>
10 #include <zephyr/sys/printk.h>
11
12 /*
13 * The synchronization demo has two threads that utilize semaphores and sleeping
14 * to take turns printing a greeting message at a controlled rate. The demo
15 * shows both the static and dynamic approaches for spawning a thread; a real
16 * world application would likely use the static approach for both threads.
17 */
18
19 #define PIN_THREADS (IS_ENABLED(CONFIG_SMP) && IS_ENABLED(CONFIG_SCHED_CPU_MASK))
20
21 /* size of stack area used by each thread */
22 #define STACKSIZE 1024
23
24 /* scheduling priority used by each thread */
25 #define PRIORITY 7
26
27 /* delay between greetings (in ms) */
28 #define SLEEPTIME 500
29
30
31 /*
32 * @param my_name thread identification string
33 * @param my_sem thread's own semaphore
34 * @param other_sem other thread's semaphore
35 */
hello_loop(const char * my_name,struct k_sem * my_sem,struct k_sem * other_sem)36 void hello_loop(const char *my_name,
37 struct k_sem *my_sem, struct k_sem *other_sem)
38 {
39 const char *tname;
40 uint8_t cpu;
41 struct k_thread *current_thread;
42
43 while (1) {
44 /* take my semaphore */
45 k_sem_take(my_sem, K_FOREVER);
46
47 current_thread = k_current_get();
48 tname = k_thread_name_get(current_thread);
49 #if CONFIG_SMP
50 cpu = arch_curr_cpu()->id;
51 #else
52 cpu = 0;
53 #endif
54 /* say "hello" */
55 if (tname == NULL) {
56 printk("%s: Hello World from cpu %d on %s!\n",
57 my_name, cpu, CONFIG_BOARD);
58 } else {
59 printk("%s: Hello World from cpu %d on %s!\n",
60 tname, cpu, CONFIG_BOARD);
61 }
62
63 /* wait a while, then let other thread have a turn */
64 k_busy_wait(100000);
65 k_msleep(SLEEPTIME);
66 k_sem_give(other_sem);
67 }
68 }
69
70 /* define semaphores */
71 K_SEM_DEFINE(thread_a_sem, 1, 1); /* starts off "available" */
72 K_SEM_DEFINE(thread_b_sem, 0, 1); /* starts off "not available" */
73
74 /* thread_a is a dynamic thread that is spawned in main */
thread_a_entry_point(void * dummy1,void * dummy2,void * dummy3)75 void thread_a_entry_point(void *dummy1, void *dummy2, void *dummy3)
76 {
77 ARG_UNUSED(dummy1);
78 ARG_UNUSED(dummy2);
79 ARG_UNUSED(dummy3);
80
81 /* invoke routine to ping-pong hello messages with thread_b */
82 hello_loop(__func__, &thread_a_sem, &thread_b_sem);
83 }
84 K_THREAD_STACK_DEFINE(thread_a_stack_area, STACKSIZE);
85 static struct k_thread thread_a_data;
86
87 /* thread_b is a static thread spawned immediately */
thread_b_entry_point(void * dummy1,void * dummy2,void * dummy3)88 void thread_b_entry_point(void *dummy1, void *dummy2, void *dummy3)
89 {
90 ARG_UNUSED(dummy1);
91 ARG_UNUSED(dummy2);
92 ARG_UNUSED(dummy3);
93
94 /* invoke routine to ping-pong hello messages with thread_a */
95 hello_loop(__func__, &thread_b_sem, &thread_a_sem);
96 }
97 K_THREAD_DEFINE(thread_b, STACKSIZE,
98 thread_b_entry_point, NULL, NULL, NULL,
99 PRIORITY, 0, 0);
100 extern const k_tid_t thread_b;
101
main(void)102 int main(void)
103 {
104 k_thread_create(&thread_a_data, thread_a_stack_area,
105 K_THREAD_STACK_SIZEOF(thread_a_stack_area),
106 thread_a_entry_point, NULL, NULL, NULL,
107 PRIORITY, 0, K_FOREVER);
108 k_thread_name_set(&thread_a_data, "thread_a");
109
110 #if PIN_THREADS
111 if (arch_num_cpus() > 1) {
112 k_thread_cpu_pin(&thread_a_data, 0);
113
114 /*
115 * Thread b is a static thread that is spawned immediately. This means that the
116 * following `k_thread_cpu_pin` call can fail with `-EINVAL` if the thread is
117 * actively running. Let's suspend the thread and resume it after the affinity mask
118 * is set.
119 */
120 k_thread_suspend(thread_b);
121 k_thread_cpu_pin(thread_b, 1);
122 k_thread_resume(thread_b);
123 }
124 #endif
125
126 k_thread_start(&thread_a_data);
127 return 0;
128 }
129