|
1 | | -/* main.c - Hello World demo */ |
| 1 | +/* main.c - Synchronization demo */ |
2 | 2 |
|
3 | 3 | /* |
4 | 4 | * Copyright (c) 2012-2014 Wind River Systems, Inc. |
|
10 | 10 | #include <zephyr/sys/printk.h> |
11 | 11 |
|
12 | 12 | /* |
13 | | - * The hello world demo has two threads that utilize semaphores and sleeping |
| 13 | + * The synchronization demo has two threads that utilize semaphores and sleeping |
14 | 14 | * to take turns printing a greeting message at a controlled rate. The demo |
15 | 15 | * shows both the static and dynamic approaches for spawning a thread; a real |
16 | 16 | * world application would likely use the static approach for both threads. |
|
33 | 33 | * @param my_sem thread's own semaphore |
34 | 34 | * @param other_sem other thread's semaphore |
35 | 35 | */ |
36 | | -void helloLoop(const char *my_name, |
37 | | - 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 | 38 | { |
39 | 39 | const char *tname; |
40 | 40 | uint8_t cpu; |
@@ -68,66 +68,61 @@ void helloLoop(const char *my_name, |
68 | 68 | } |
69 | 69 |
|
70 | 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" */ |
71 | 73 |
|
72 | | -K_SEM_DEFINE(threadA_sem, 1, 1); /* starts off "available" */ |
73 | | -K_SEM_DEFINE(threadB_sem, 0, 1); /* starts off "not available" */ |
74 | | - |
75 | | - |
76 | | -/* threadB is a dynamic thread that is spawned by threadA */ |
77 | | - |
78 | | -void threadB(void *dummy1, void *dummy2, void *dummy3) |
| 74 | +/* thread_a is a dynamic thread that is spawned in main */ |
| 75 | +void thread_a_entry_point(void *dummy1, void *dummy2, void *dummy3) |
79 | 76 | { |
80 | 77 | ARG_UNUSED(dummy1); |
81 | 78 | ARG_UNUSED(dummy2); |
82 | 79 | ARG_UNUSED(dummy3); |
83 | 80 |
|
84 | | - /* invoke routine to ping-pong hello messages with threadA */ |
85 | | - helloLoop(__func__, &threadB_sem, &threadA_sem); |
| 81 | + /* invoke routine to ping-pong hello messages with thread_b */ |
| 82 | + hello_loop(__func__, &thread_a_sem, &thread_b_sem); |
86 | 83 | } |
| 84 | +K_THREAD_STACK_DEFINE(thread_a_stack_area, STACKSIZE); |
| 85 | +static struct k_thread thread_a_data; |
87 | 86 |
|
88 | | -K_THREAD_STACK_DEFINE(threadA_stack_area, STACKSIZE); |
89 | | -static struct k_thread threadA_data; |
90 | | - |
91 | | -K_THREAD_STACK_DEFINE(threadB_stack_area, STACKSIZE); |
92 | | -static struct k_thread threadB_data; |
93 | | - |
94 | | -/* threadA is a static thread that is spawned automatically */ |
95 | | - |
96 | | -void threadA(void *dummy1, void *dummy2, void *dummy3) |
| 87 | +/* thread_b is a static thread spawned immediately */ |
| 88 | +void thread_b_entry_point(void *dummy1, void *dummy2, void *dummy3) |
97 | 89 | { |
98 | 90 | ARG_UNUSED(dummy1); |
99 | 91 | ARG_UNUSED(dummy2); |
100 | 92 | ARG_UNUSED(dummy3); |
101 | 93 |
|
102 | | - /* invoke routine to ping-pong hello messages with threadB */ |
103 | | - helloLoop(__func__, &threadA_sem, &threadB_sem); |
| 94 | + /* invoke routine to ping-pong hello messages with thread_a */ |
| 95 | + hello_loop(__func__, &thread_b_sem, &thread_a_sem); |
104 | 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; |
105 | 101 |
|
106 | 102 | int main(void) |
107 | 103 | { |
108 | | - k_thread_create(&threadA_data, threadA_stack_area, |
109 | | - K_THREAD_STACK_SIZEOF(threadA_stack_area), |
110 | | - threadA, NULL, NULL, NULL, |
| 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, |
111 | 107 | PRIORITY, 0, K_FOREVER); |
112 | | - k_thread_name_set(&threadA_data, "thread_a"); |
113 | | -#if PIN_THREADS |
114 | | - if (arch_num_cpus() > 1) { |
115 | | - k_thread_cpu_pin(&threadA_data, 0); |
116 | | - } |
117 | | -#endif |
| 108 | + k_thread_name_set(&thread_a_data, "thread_a"); |
118 | 109 |
|
119 | | - k_thread_create(&threadB_data, threadB_stack_area, |
120 | | - K_THREAD_STACK_SIZEOF(threadB_stack_area), |
121 | | - threadB, NULL, NULL, NULL, |
122 | | - PRIORITY, 0, K_FOREVER); |
123 | | - k_thread_name_set(&threadB_data, "thread_b"); |
124 | 110 | #if PIN_THREADS |
125 | 111 | if (arch_num_cpus() > 1) { |
126 | | - k_thread_cpu_pin(&threadB_data, 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); |
127 | 123 | } |
128 | 124 | #endif |
129 | 125 |
|
130 | | - k_thread_start(&threadA_data); |
131 | | - k_thread_start(&threadB_data); |
| 126 | + k_thread_start(&thread_a_data); |
132 | 127 | return 0; |
133 | 128 | } |
0 commit comments