1 | /* |
---|
2 | * Copyright (c) 2018 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * The license and distribution terms for this file may be |
---|
11 | * found in the file LICENSE in this distribution or at |
---|
12 | * http://www.rtems.org/license/LICENSE. |
---|
13 | */ |
---|
14 | |
---|
15 | #ifdef HAVE_CONFIG_H |
---|
16 | #include "config.h" |
---|
17 | #endif |
---|
18 | |
---|
19 | #include <sched.h> |
---|
20 | |
---|
21 | #include <rtems.h> |
---|
22 | #include <rtems/test-info.h> |
---|
23 | #include <rtems/thread.h> |
---|
24 | |
---|
25 | #include <tmacros.h> |
---|
26 | |
---|
27 | const char rtems_test_name[] = "TMONETOONE"; |
---|
28 | |
---|
29 | typedef enum { |
---|
30 | TEST_YIELD, |
---|
31 | TEST_EVENTS, |
---|
32 | TEST_BSEM, |
---|
33 | TEST_CLASSIC_FIFO_BSEM, |
---|
34 | TEST_CLASSIC_PRIO_BSEM |
---|
35 | } test_variant; |
---|
36 | |
---|
37 | typedef struct { |
---|
38 | volatile uint32_t counter; |
---|
39 | test_variant variant; |
---|
40 | rtems_id task; |
---|
41 | rtems_binary_semaphore bsem; |
---|
42 | rtems_id classic_fifo_bsem; |
---|
43 | rtems_id classic_prio_bsem; |
---|
44 | rtems_id other_task; |
---|
45 | rtems_binary_semaphore *other_bsem; |
---|
46 | rtems_id other_classic_fifo_bsem; |
---|
47 | rtems_id other_classic_prio_bsem; |
---|
48 | } task_context RTEMS_ALIGNED(CPU_CACHE_LINE_BYTES); |
---|
49 | |
---|
50 | typedef struct { |
---|
51 | task_context a; |
---|
52 | task_context b; |
---|
53 | } test_context; |
---|
54 | |
---|
55 | static test_context test_instance; |
---|
56 | |
---|
57 | static void test_yield(task_context *tc) |
---|
58 | { |
---|
59 | rtems_event_set events; |
---|
60 | uint32_t counter; |
---|
61 | |
---|
62 | (void)rtems_event_receive( |
---|
63 | RTEMS_EVENT_0, |
---|
64 | RTEMS_WAIT | RTEMS_EVENT_ALL, |
---|
65 | RTEMS_NO_TIMEOUT, |
---|
66 | &events |
---|
67 | ); |
---|
68 | |
---|
69 | counter = 0; |
---|
70 | |
---|
71 | while (true) { |
---|
72 | (void)sched_yield(); |
---|
73 | ++counter; |
---|
74 | tc->counter = counter; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | static void test_events(task_context *tc) |
---|
79 | { |
---|
80 | uint32_t counter; |
---|
81 | rtems_id other; |
---|
82 | |
---|
83 | counter = 0; |
---|
84 | other = tc->other_task; |
---|
85 | |
---|
86 | while (true) { |
---|
87 | rtems_event_set events; |
---|
88 | |
---|
89 | (void)rtems_event_receive( |
---|
90 | RTEMS_EVENT_0, |
---|
91 | RTEMS_WAIT | RTEMS_EVENT_ALL, |
---|
92 | RTEMS_NO_TIMEOUT, |
---|
93 | &events |
---|
94 | ); |
---|
95 | (void)rtems_event_send(other, RTEMS_EVENT_0); |
---|
96 | ++counter; |
---|
97 | tc->counter = counter; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | static void test_bsem(task_context *tc) |
---|
102 | { |
---|
103 | uint32_t counter; |
---|
104 | rtems_binary_semaphore *other; |
---|
105 | |
---|
106 | counter = 0; |
---|
107 | other = tc->other_bsem; |
---|
108 | |
---|
109 | while (true) { |
---|
110 | rtems_binary_semaphore_wait(&tc->bsem); |
---|
111 | rtems_binary_semaphore_post(other); |
---|
112 | ++counter; |
---|
113 | tc->counter = counter; |
---|
114 | } |
---|
115 | } |
---|
116 | |
---|
117 | static void test_classic_fifo_bsem(task_context *tc) |
---|
118 | { |
---|
119 | uint32_t counter; |
---|
120 | rtems_id own; |
---|
121 | rtems_id other; |
---|
122 | |
---|
123 | counter = 0; |
---|
124 | own = tc->classic_fifo_bsem; |
---|
125 | other = tc->other_classic_fifo_bsem; |
---|
126 | |
---|
127 | while (true) { |
---|
128 | (void)rtems_semaphore_obtain(own, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
129 | (void)rtems_semaphore_release(other); |
---|
130 | ++counter; |
---|
131 | tc->counter = counter; |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | static void test_classic_prio_bsem(task_context *tc) |
---|
136 | { |
---|
137 | uint32_t counter; |
---|
138 | rtems_id own; |
---|
139 | rtems_id other; |
---|
140 | |
---|
141 | counter = 0; |
---|
142 | own = tc->classic_prio_bsem; |
---|
143 | other = tc->other_classic_prio_bsem; |
---|
144 | |
---|
145 | while (true) { |
---|
146 | (void)rtems_semaphore_obtain(own, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
147 | (void)rtems_semaphore_release(other); |
---|
148 | ++counter; |
---|
149 | tc->counter = counter; |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | static void worker_task(rtems_task_argument arg) |
---|
154 | { |
---|
155 | task_context *tc; |
---|
156 | |
---|
157 | tc = (task_context *) arg; |
---|
158 | |
---|
159 | switch (tc->variant) { |
---|
160 | case TEST_YIELD: |
---|
161 | test_yield(tc); |
---|
162 | break; |
---|
163 | case TEST_EVENTS: |
---|
164 | test_events(tc); |
---|
165 | break; |
---|
166 | case TEST_BSEM: |
---|
167 | test_bsem(tc); |
---|
168 | break; |
---|
169 | case TEST_CLASSIC_FIFO_BSEM: |
---|
170 | test_classic_fifo_bsem(tc); |
---|
171 | break; |
---|
172 | case TEST_CLASSIC_PRIO_BSEM: |
---|
173 | test_classic_prio_bsem(tc); |
---|
174 | break; |
---|
175 | default: |
---|
176 | rtems_test_assert(0); |
---|
177 | break; |
---|
178 | } |
---|
179 | } |
---|
180 | |
---|
181 | static void create_task(task_context *tc) |
---|
182 | { |
---|
183 | rtems_status_code sc; |
---|
184 | |
---|
185 | rtems_binary_semaphore_init(&tc->bsem, "test"); |
---|
186 | |
---|
187 | sc = rtems_semaphore_create( |
---|
188 | rtems_build_name('T', 'E', 'S', 'T'), |
---|
189 | 0, |
---|
190 | RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_FIFO, |
---|
191 | 0, |
---|
192 | &tc->classic_fifo_bsem |
---|
193 | ); |
---|
194 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
195 | |
---|
196 | sc = rtems_semaphore_create( |
---|
197 | rtems_build_name('T', 'E', 'S', 'T'), |
---|
198 | 0, |
---|
199 | RTEMS_SIMPLE_BINARY_SEMAPHORE | RTEMS_PRIORITY, |
---|
200 | 0, |
---|
201 | &tc->classic_prio_bsem |
---|
202 | ); |
---|
203 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
204 | |
---|
205 | sc = rtems_task_create( |
---|
206 | rtems_build_name('T', 'E', 'S', 'T'), |
---|
207 | 2, |
---|
208 | RTEMS_MINIMUM_STACK_SIZE, |
---|
209 | RTEMS_DEFAULT_MODES, |
---|
210 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
211 | &tc->task |
---|
212 | ); |
---|
213 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
214 | |
---|
215 | sc = rtems_task_start(tc->task, worker_task, (rtems_task_argument) tc); |
---|
216 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
217 | } |
---|
218 | |
---|
219 | static const char * const variant_names[] = { |
---|
220 | "yield", |
---|
221 | "event", |
---|
222 | "self-contained binary semaphore", |
---|
223 | "Classic binary semaphore (FIFO)", |
---|
224 | "Classic binary semaphore (priority)" |
---|
225 | }; |
---|
226 | |
---|
227 | static void prepare(test_context *ctx, test_variant variant) |
---|
228 | { |
---|
229 | rtems_status_code sc; |
---|
230 | |
---|
231 | printf("%s\n", variant_names[variant]); |
---|
232 | |
---|
233 | ctx->a.variant = variant; |
---|
234 | ctx->b.variant = variant; |
---|
235 | |
---|
236 | ctx->a.counter = 0; |
---|
237 | ctx->b.counter = 0; |
---|
238 | |
---|
239 | sc = rtems_task_restart(ctx->a.task, (rtems_task_argument) &ctx->a); |
---|
240 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
241 | |
---|
242 | sc = rtems_task_restart(ctx->b.task, (rtems_task_argument) &ctx->b); |
---|
243 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
244 | |
---|
245 | sc = rtems_task_wake_after(2); |
---|
246 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
247 | } |
---|
248 | |
---|
249 | static void run(test_context *ctx) |
---|
250 | { |
---|
251 | rtems_status_code sc; |
---|
252 | |
---|
253 | sc = rtems_task_wake_after(rtems_clock_get_ticks_per_second()); |
---|
254 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
255 | |
---|
256 | printf("a %" PRIu32 "\nb %" PRIu32 "\n", ctx->a.counter, ctx->b.counter); |
---|
257 | } |
---|
258 | |
---|
259 | static void Init(rtems_task_argument arg) |
---|
260 | { |
---|
261 | test_context *ctx = &test_instance; |
---|
262 | rtems_status_code sc; |
---|
263 | |
---|
264 | TEST_BEGIN(); |
---|
265 | |
---|
266 | create_task(&ctx->a); |
---|
267 | create_task(&ctx->b); |
---|
268 | |
---|
269 | ctx->a.other_task = ctx->b.task; |
---|
270 | ctx->a.other_bsem = &ctx->b.bsem; |
---|
271 | ctx->a.other_classic_fifo_bsem = ctx->b.classic_fifo_bsem; |
---|
272 | ctx->a.other_classic_prio_bsem = ctx->b.classic_prio_bsem; |
---|
273 | |
---|
274 | ctx->b.other_task = ctx->a.task; |
---|
275 | ctx->b.other_bsem = &ctx->a.bsem; |
---|
276 | ctx->b.other_classic_fifo_bsem = ctx->a.classic_fifo_bsem; |
---|
277 | ctx->b.other_classic_prio_bsem = ctx->a.classic_prio_bsem; |
---|
278 | |
---|
279 | prepare(ctx, TEST_YIELD); |
---|
280 | |
---|
281 | sc = rtems_event_send(ctx->a.task, RTEMS_EVENT_0); |
---|
282 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
283 | |
---|
284 | sc = rtems_event_send(ctx->b.task, RTEMS_EVENT_0); |
---|
285 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
286 | |
---|
287 | run(ctx); |
---|
288 | prepare(ctx, TEST_EVENTS); |
---|
289 | |
---|
290 | sc = rtems_event_send(ctx->a.task, RTEMS_EVENT_0); |
---|
291 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
292 | |
---|
293 | run(ctx); |
---|
294 | prepare(ctx, TEST_BSEM); |
---|
295 | |
---|
296 | rtems_binary_semaphore_post(&ctx->a.bsem); |
---|
297 | |
---|
298 | run(ctx); |
---|
299 | prepare(ctx, TEST_CLASSIC_FIFO_BSEM); |
---|
300 | |
---|
301 | sc = rtems_semaphore_release(ctx->a.classic_fifo_bsem); |
---|
302 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
303 | |
---|
304 | run(ctx); |
---|
305 | prepare(ctx, TEST_CLASSIC_PRIO_BSEM); |
---|
306 | |
---|
307 | sc = rtems_semaphore_release(ctx->a.classic_prio_bsem); |
---|
308 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
309 | |
---|
310 | run(ctx); |
---|
311 | |
---|
312 | TEST_END(); |
---|
313 | rtems_test_exit(0); |
---|
314 | } |
---|
315 | |
---|
316 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
317 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
318 | |
---|
319 | #define CONFIGURE_MAXIMUM_TASKS 3 |
---|
320 | |
---|
321 | #define CONFIGURE_MAXIMUM_SEMAPHORES 4 |
---|
322 | |
---|
323 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
324 | |
---|
325 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
326 | |
---|
327 | #define CONFIGURE_INIT |
---|
328 | |
---|
329 | #include <rtems/confdefs.h> |
---|