1 | /* |
---|
2 | * Copyright (c) 2015 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 "tmacros.h" |
---|
20 | |
---|
21 | #include <sys/lock.h> |
---|
22 | #include <errno.h> |
---|
23 | #include <limits.h> |
---|
24 | #include <pthread.h> |
---|
25 | #include <string.h> |
---|
26 | #include <time.h> |
---|
27 | |
---|
28 | const char rtems_test_name[] = "SPSYSLOCK 1"; |
---|
29 | |
---|
30 | #define US_PER_TICK 10000 |
---|
31 | |
---|
32 | #define EVENT_MTX_ACQUIRE RTEMS_EVENT_0 |
---|
33 | |
---|
34 | #define EVENT_MTX_RELEASE RTEMS_EVENT_1 |
---|
35 | |
---|
36 | #define EVENT_MTX_PRIO_INV RTEMS_EVENT_2 |
---|
37 | |
---|
38 | #define EVENT_MTX_DEADLOCK RTEMS_EVENT_3 |
---|
39 | |
---|
40 | #define EVENT_REC_MTX_ACQUIRE RTEMS_EVENT_4 |
---|
41 | |
---|
42 | #define EVENT_REC_MTX_RELEASE RTEMS_EVENT_5 |
---|
43 | |
---|
44 | #define EVENT_REC_MTX_PRIO_INV RTEMS_EVENT_6 |
---|
45 | |
---|
46 | #define EVENT_SEM_WAIT RTEMS_EVENT_7 |
---|
47 | |
---|
48 | #define EVENT_FUTEX_WAIT RTEMS_EVENT_8 |
---|
49 | |
---|
50 | #define EVENT_CONDITION_WAIT RTEMS_EVENT_9 |
---|
51 | |
---|
52 | #define EVENT_CONDITION_WAIT_REC RTEMS_EVENT_10 |
---|
53 | |
---|
54 | typedef struct { |
---|
55 | rtems_id high[2]; |
---|
56 | rtems_id mid; |
---|
57 | rtems_id low; |
---|
58 | struct _Mutex_Control mtx; |
---|
59 | struct _Mutex_Control deadlock_mtx; |
---|
60 | struct _Mutex_recursive_Control rec_mtx; |
---|
61 | struct _Condition_Control cond; |
---|
62 | struct _Semaphore_Control sem; |
---|
63 | struct _Futex_Control futex; |
---|
64 | int val; |
---|
65 | int eno[2]; |
---|
66 | int generation[2]; |
---|
67 | int current_generation[2]; |
---|
68 | } test_context; |
---|
69 | |
---|
70 | static test_context test_instance; |
---|
71 | |
---|
72 | static int generation(test_context *ctx, size_t idx) |
---|
73 | { |
---|
74 | return ++ctx->current_generation[idx]; |
---|
75 | } |
---|
76 | |
---|
77 | static void send_event(test_context *ctx, size_t idx, rtems_event_set events) |
---|
78 | { |
---|
79 | rtems_status_code sc; |
---|
80 | |
---|
81 | sc = rtems_event_send(ctx->high[idx], events); |
---|
82 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
83 | } |
---|
84 | |
---|
85 | static void get_abs_timeout(struct timespec *to) |
---|
86 | { |
---|
87 | int rv; |
---|
88 | |
---|
89 | rv = clock_gettime(CLOCK_REALTIME, to); |
---|
90 | rtems_test_assert(rv == 0); |
---|
91 | |
---|
92 | to->tv_nsec += 2 * US_PER_TICK * 1000; |
---|
93 | if (to->tv_nsec >= 1000000000) { |
---|
94 | ++to->tv_sec; |
---|
95 | to->tv_nsec -= 1000000000; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | static void test_initialization(test_context *ctx) |
---|
100 | { |
---|
101 | struct _Mutex_Control mtx = _MUTEX_INITIALIZER; |
---|
102 | struct _Mutex_recursive_Control rec_mtx = _MUTEX_RECURSIVE_INITIALIZER; |
---|
103 | struct _Condition_Control cond = _CONDITION_INITIALIZER; |
---|
104 | struct _Semaphore_Control sem = _SEMAPHORE_INITIALIZER(1); |
---|
105 | struct _Futex_Control futex = _FUTEX_INITIALIZER; |
---|
106 | |
---|
107 | _Mutex_Initialize(&ctx->mtx); |
---|
108 | _Mutex_recursive_Initialize(&ctx->rec_mtx); |
---|
109 | _Condition_Initialize(&ctx->cond); |
---|
110 | _Semaphore_Initialize(&ctx->sem, 1); |
---|
111 | _Futex_Initialize(&ctx->futex); |
---|
112 | |
---|
113 | rtems_test_assert(memcmp(&mtx, &ctx->mtx, sizeof(mtx)) == 0); |
---|
114 | rtems_test_assert(memcmp(&rec_mtx, &ctx->rec_mtx, sizeof(rec_mtx)) == 0); |
---|
115 | rtems_test_assert(memcmp(&cond, &ctx->cond, sizeof(cond)) == 0); |
---|
116 | rtems_test_assert(memcmp(&sem, &ctx->sem, sizeof(sem)) == 0); |
---|
117 | rtems_test_assert(memcmp(&futex, &ctx->futex, sizeof(futex)) == 0); |
---|
118 | |
---|
119 | _Mutex_Destroy(&mtx); |
---|
120 | _Mutex_recursive_Destroy(&rec_mtx); |
---|
121 | _Condition_Destroy(&cond); |
---|
122 | _Semaphore_Destroy(&sem); |
---|
123 | _Futex_Destroy(&futex); |
---|
124 | } |
---|
125 | |
---|
126 | static void test_recursive_acquire_normal(test_context *ctx) |
---|
127 | { |
---|
128 | struct _Mutex_Control *mtx = &ctx->mtx; |
---|
129 | size_t idx = 0; |
---|
130 | int eno; |
---|
131 | |
---|
132 | eno = _Mutex_Try_acquire(mtx); |
---|
133 | rtems_test_assert(eno == 0); |
---|
134 | |
---|
135 | eno = _Mutex_Try_acquire(mtx); |
---|
136 | rtems_test_assert(eno == EBUSY); |
---|
137 | |
---|
138 | _Mutex_Release(mtx); |
---|
139 | |
---|
140 | eno = _Mutex_Try_acquire(mtx); |
---|
141 | rtems_test_assert(eno == 0); |
---|
142 | |
---|
143 | _Mutex_Release(mtx); |
---|
144 | |
---|
145 | _Mutex_Acquire(mtx); |
---|
146 | |
---|
147 | eno = _Mutex_Try_acquire(mtx); |
---|
148 | rtems_test_assert(eno == EBUSY); |
---|
149 | |
---|
150 | _Mutex_Release(mtx); |
---|
151 | |
---|
152 | send_event(ctx, idx, EVENT_MTX_ACQUIRE); |
---|
153 | |
---|
154 | eno = _Mutex_Try_acquire(mtx); |
---|
155 | rtems_test_assert(eno == EBUSY); |
---|
156 | |
---|
157 | send_event(ctx, idx, EVENT_MTX_RELEASE); |
---|
158 | } |
---|
159 | |
---|
160 | static void test_recursive_acquire_recursive(test_context *ctx) |
---|
161 | { |
---|
162 | struct _Mutex_recursive_Control *mtx = &ctx->rec_mtx; |
---|
163 | size_t idx = 0; |
---|
164 | int eno; |
---|
165 | |
---|
166 | eno = _Mutex_recursive_Try_acquire(mtx); |
---|
167 | rtems_test_assert(eno == 0); |
---|
168 | |
---|
169 | _Mutex_recursive_Acquire(mtx); |
---|
170 | |
---|
171 | eno = _Mutex_recursive_Try_acquire(mtx); |
---|
172 | rtems_test_assert(eno == 0); |
---|
173 | |
---|
174 | _Mutex_recursive_Release(mtx); |
---|
175 | _Mutex_recursive_Release(mtx); |
---|
176 | _Mutex_recursive_Release(mtx); |
---|
177 | |
---|
178 | send_event(ctx, idx, EVENT_REC_MTX_ACQUIRE); |
---|
179 | |
---|
180 | eno = _Mutex_recursive_Try_acquire(mtx); |
---|
181 | rtems_test_assert(eno == EBUSY); |
---|
182 | |
---|
183 | send_event(ctx, idx, EVENT_REC_MTX_RELEASE); |
---|
184 | } |
---|
185 | |
---|
186 | static void test_prio_acquire_order(test_context *ctx) |
---|
187 | { |
---|
188 | struct _Mutex_Control *mtx = &ctx->mtx; |
---|
189 | size_t a = 0; |
---|
190 | size_t b = 1; |
---|
191 | int gen_a; |
---|
192 | int gen_b; |
---|
193 | |
---|
194 | _Mutex_Acquire(mtx); |
---|
195 | |
---|
196 | gen_a = ctx->generation[a]; |
---|
197 | gen_b = ctx->generation[b]; |
---|
198 | |
---|
199 | send_event(ctx, b, EVENT_MTX_ACQUIRE); |
---|
200 | send_event(ctx, a, EVENT_MTX_ACQUIRE); |
---|
201 | |
---|
202 | rtems_test_assert(ctx->generation[a] == gen_a); |
---|
203 | rtems_test_assert(ctx->generation[b] == gen_b); |
---|
204 | |
---|
205 | _Mutex_Release(mtx); |
---|
206 | |
---|
207 | rtems_test_assert(ctx->generation[a] == gen_a + 1); |
---|
208 | rtems_test_assert(ctx->generation[b] == gen_b); |
---|
209 | |
---|
210 | send_event(ctx, a, EVENT_MTX_RELEASE); |
---|
211 | |
---|
212 | rtems_test_assert(ctx->generation[a] == gen_a + 1); |
---|
213 | rtems_test_assert(ctx->generation[b] == gen_b + 1); |
---|
214 | |
---|
215 | send_event(ctx, b, EVENT_MTX_RELEASE); |
---|
216 | } |
---|
217 | |
---|
218 | static void test_prio_inv_normal(test_context *ctx) |
---|
219 | { |
---|
220 | struct _Mutex_Control *mtx = &ctx->mtx; |
---|
221 | size_t idx = 0; |
---|
222 | int gen; |
---|
223 | |
---|
224 | _Mutex_Acquire(mtx); |
---|
225 | gen = ctx->generation[idx]; |
---|
226 | send_event(ctx, idx, EVENT_MTX_PRIO_INV); |
---|
227 | rtems_test_assert(ctx->generation[idx] == gen); |
---|
228 | _Mutex_Release(mtx); |
---|
229 | rtems_test_assert(ctx->generation[idx] == gen + 1); |
---|
230 | } |
---|
231 | |
---|
232 | static void test_prio_inv_recursive(test_context *ctx) |
---|
233 | { |
---|
234 | struct _Mutex_recursive_Control *mtx = &ctx->rec_mtx; |
---|
235 | size_t idx = 0; |
---|
236 | int gen; |
---|
237 | |
---|
238 | _Mutex_recursive_Acquire(mtx); |
---|
239 | gen = ctx->generation[idx]; |
---|
240 | send_event(ctx, idx, EVENT_REC_MTX_PRIO_INV); |
---|
241 | rtems_test_assert(ctx->generation[idx] == gen); |
---|
242 | _Mutex_recursive_Release(mtx); |
---|
243 | rtems_test_assert(ctx->generation[idx] == gen + 1); |
---|
244 | } |
---|
245 | |
---|
246 | static void test_mtx_timeout_normal(test_context *ctx) |
---|
247 | { |
---|
248 | struct _Mutex_Control *mtx = &ctx->mtx; |
---|
249 | size_t idx = 0; |
---|
250 | struct timespec to; |
---|
251 | int eno; |
---|
252 | |
---|
253 | eno = _Mutex_Acquire_timed(mtx, NULL); |
---|
254 | rtems_test_assert(eno == 0); |
---|
255 | _Mutex_Release(mtx); |
---|
256 | |
---|
257 | send_event(ctx, idx, EVENT_MTX_ACQUIRE); |
---|
258 | |
---|
259 | memset(&to, 0x00, sizeof(to)); |
---|
260 | eno = _Mutex_Acquire_timed(mtx, &to); |
---|
261 | rtems_test_assert(eno == ETIMEDOUT); |
---|
262 | memset(&to, 0xff, sizeof(to)); |
---|
263 | eno = _Mutex_Acquire_timed(mtx, &to); |
---|
264 | rtems_test_assert(eno == EINVAL); |
---|
265 | get_abs_timeout(&to); |
---|
266 | eno = _Mutex_Acquire_timed(mtx, &to); |
---|
267 | rtems_test_assert(eno == ETIMEDOUT); |
---|
268 | |
---|
269 | send_event(ctx, idx, EVENT_MTX_RELEASE); |
---|
270 | } |
---|
271 | |
---|
272 | static void test_mtx_timeout_recursive(test_context *ctx) |
---|
273 | { |
---|
274 | struct _Mutex_recursive_Control *mtx = &ctx->rec_mtx; |
---|
275 | size_t idx = 0; |
---|
276 | struct timespec to; |
---|
277 | int eno; |
---|
278 | |
---|
279 | eno = _Mutex_recursive_Acquire_timed(mtx, NULL); |
---|
280 | rtems_test_assert(eno == 0); |
---|
281 | eno = _Mutex_recursive_Acquire_timed(mtx, NULL); |
---|
282 | rtems_test_assert(eno == 0); |
---|
283 | _Mutex_recursive_Release(mtx); |
---|
284 | _Mutex_recursive_Release(mtx); |
---|
285 | |
---|
286 | send_event(ctx, idx, EVENT_REC_MTX_ACQUIRE); |
---|
287 | |
---|
288 | memset(&to, 0x00, sizeof(to)); |
---|
289 | eno = _Mutex_recursive_Acquire_timed(mtx, &to); |
---|
290 | rtems_test_assert(eno == ETIMEDOUT); |
---|
291 | memset(&to, 0xff, sizeof(to)); |
---|
292 | eno = _Mutex_recursive_Acquire_timed(mtx, &to); |
---|
293 | rtems_test_assert(eno == EINVAL); |
---|
294 | get_abs_timeout(&to); |
---|
295 | eno = _Mutex_recursive_Acquire_timed(mtx, &to); |
---|
296 | rtems_test_assert(eno == ETIMEDOUT); |
---|
297 | |
---|
298 | send_event(ctx, idx, EVENT_REC_MTX_RELEASE); |
---|
299 | } |
---|
300 | |
---|
301 | static void test_condition(test_context *ctx) |
---|
302 | { |
---|
303 | struct _Condition_Control *cond = &ctx->cond; |
---|
304 | size_t a = 0; |
---|
305 | size_t b = 1; |
---|
306 | int gen_a; |
---|
307 | int gen_b; |
---|
308 | |
---|
309 | gen_a = ctx->generation[a]; |
---|
310 | gen_b = ctx->generation[b]; |
---|
311 | |
---|
312 | _Condition_Signal(cond); |
---|
313 | _Condition_Broadcast(cond); |
---|
314 | |
---|
315 | send_event(ctx, a, EVENT_CONDITION_WAIT); |
---|
316 | send_event(ctx, b, EVENT_CONDITION_WAIT_REC); |
---|
317 | |
---|
318 | rtems_test_assert(ctx->generation[a] == gen_a + 1); |
---|
319 | rtems_test_assert(ctx->generation[b] == gen_b + 1); |
---|
320 | |
---|
321 | _Condition_Signal(cond); |
---|
322 | |
---|
323 | rtems_test_assert(ctx->generation[a] == gen_a + 2); |
---|
324 | rtems_test_assert(ctx->generation[b] == gen_b + 1); |
---|
325 | |
---|
326 | _Condition_Signal(cond); |
---|
327 | |
---|
328 | rtems_test_assert(ctx->generation[a] == gen_a + 2); |
---|
329 | rtems_test_assert(ctx->generation[b] == gen_b + 2); |
---|
330 | |
---|
331 | send_event(ctx, a, EVENT_CONDITION_WAIT); |
---|
332 | send_event(ctx, b, EVENT_CONDITION_WAIT_REC); |
---|
333 | |
---|
334 | rtems_test_assert(ctx->generation[a] == gen_a + 3); |
---|
335 | rtems_test_assert(ctx->generation[b] == gen_b + 3); |
---|
336 | |
---|
337 | _Condition_Broadcast(cond); |
---|
338 | |
---|
339 | rtems_test_assert(ctx->generation[a] == gen_a + 4); |
---|
340 | rtems_test_assert(ctx->generation[b] == gen_b + 4); |
---|
341 | } |
---|
342 | |
---|
343 | static void test_condition_timeout(test_context *ctx) |
---|
344 | { |
---|
345 | struct timespec to; |
---|
346 | int eno; |
---|
347 | |
---|
348 | _Mutex_Acquire(&ctx->mtx); |
---|
349 | memset(&to, 0x00, sizeof(to)); |
---|
350 | eno = _Condition_Wait_timed(&ctx->cond, &ctx->mtx, &to); |
---|
351 | rtems_test_assert(eno == ETIMEDOUT); |
---|
352 | memset(&to, 0xff, sizeof(to)); |
---|
353 | eno = _Condition_Wait_timed(&ctx->cond, &ctx->mtx, &to); |
---|
354 | rtems_test_assert(eno == EINVAL); |
---|
355 | get_abs_timeout(&to); |
---|
356 | eno = _Condition_Wait_timed(&ctx->cond, &ctx->mtx, &to); |
---|
357 | rtems_test_assert(eno == ETIMEDOUT); |
---|
358 | _Mutex_Release(&ctx->mtx); |
---|
359 | |
---|
360 | _Mutex_recursive_Acquire(&ctx->rec_mtx); |
---|
361 | _Mutex_recursive_Acquire(&ctx->rec_mtx); |
---|
362 | memset(&to, 0x00, sizeof(to)); |
---|
363 | eno = _Condition_Wait_recursive_timed(&ctx->cond, &ctx->rec_mtx, &to); |
---|
364 | rtems_test_assert(eno == ETIMEDOUT); |
---|
365 | memset(&to, 0xff, sizeof(to)); |
---|
366 | eno = _Condition_Wait_recursive_timed(&ctx->cond, &ctx->rec_mtx, &to); |
---|
367 | rtems_test_assert(eno == EINVAL); |
---|
368 | get_abs_timeout(&to); |
---|
369 | eno = _Condition_Wait_recursive_timed(&ctx->cond, &ctx->rec_mtx, &to); |
---|
370 | rtems_test_assert(eno == ETIMEDOUT); |
---|
371 | _Mutex_recursive_Release(&ctx->rec_mtx); |
---|
372 | _Mutex_recursive_Release(&ctx->rec_mtx); |
---|
373 | } |
---|
374 | |
---|
375 | static void test_sem(test_context *ctx) |
---|
376 | { |
---|
377 | struct _Semaphore_Control *sem = &ctx->sem; |
---|
378 | size_t idx = 0; |
---|
379 | int gen; |
---|
380 | |
---|
381 | _Semaphore_Wait(sem); |
---|
382 | gen = ctx->generation[idx]; |
---|
383 | send_event(ctx, idx, EVENT_SEM_WAIT); |
---|
384 | rtems_test_assert(ctx->generation[idx] == gen); |
---|
385 | _Semaphore_Post(sem); |
---|
386 | rtems_test_assert(ctx->generation[idx] == gen + 1); |
---|
387 | _Semaphore_Post(sem); |
---|
388 | } |
---|
389 | |
---|
390 | static void test_sem_prio_wait_order(test_context *ctx) |
---|
391 | { |
---|
392 | struct _Semaphore_Control *sem = &ctx->sem; |
---|
393 | size_t a = 0; |
---|
394 | size_t b = 1; |
---|
395 | int gen_a; |
---|
396 | int gen_b; |
---|
397 | |
---|
398 | _Semaphore_Wait(sem); |
---|
399 | |
---|
400 | gen_a = ctx->generation[a]; |
---|
401 | gen_b = ctx->generation[b]; |
---|
402 | |
---|
403 | send_event(ctx, b, EVENT_SEM_WAIT); |
---|
404 | send_event(ctx, a, EVENT_SEM_WAIT); |
---|
405 | |
---|
406 | rtems_test_assert(ctx->generation[a] == gen_a); |
---|
407 | rtems_test_assert(ctx->generation[b] == gen_b); |
---|
408 | |
---|
409 | _Semaphore_Post(sem); |
---|
410 | |
---|
411 | rtems_test_assert(ctx->generation[a] == gen_a + 1); |
---|
412 | rtems_test_assert(ctx->generation[b] == gen_b); |
---|
413 | |
---|
414 | _Semaphore_Post(sem); |
---|
415 | |
---|
416 | rtems_test_assert(ctx->generation[a] == gen_a + 1); |
---|
417 | rtems_test_assert(ctx->generation[b] == gen_b + 1); |
---|
418 | } |
---|
419 | |
---|
420 | static void test_futex(test_context *ctx) |
---|
421 | { |
---|
422 | struct _Futex_Control *futex = &ctx->futex; |
---|
423 | size_t a = 0; |
---|
424 | size_t b = 1; |
---|
425 | int eno; |
---|
426 | int woken; |
---|
427 | |
---|
428 | eno = _Futex_Wait(futex, &ctx->val, 1); |
---|
429 | rtems_test_assert(eno == EWOULDBLOCK); |
---|
430 | |
---|
431 | woken = _Futex_Wake(futex, 0); |
---|
432 | rtems_test_assert(woken == 0); |
---|
433 | |
---|
434 | woken = _Futex_Wake(futex, 1); |
---|
435 | rtems_test_assert(woken == 0); |
---|
436 | |
---|
437 | ctx->val = 1; |
---|
438 | |
---|
439 | ctx->eno[a] = -1; |
---|
440 | send_event(ctx, a, EVENT_FUTEX_WAIT); |
---|
441 | rtems_test_assert(ctx->eno[a] == -1); |
---|
442 | |
---|
443 | woken = _Futex_Wake(futex, INT_MAX); |
---|
444 | rtems_test_assert(woken == 1); |
---|
445 | rtems_test_assert(ctx->eno[a] == 0); |
---|
446 | |
---|
447 | ctx->eno[a] = -1; |
---|
448 | ctx->eno[b] = -1; |
---|
449 | send_event(ctx, a, EVENT_FUTEX_WAIT); |
---|
450 | send_event(ctx, b, EVENT_FUTEX_WAIT); |
---|
451 | rtems_test_assert(ctx->eno[a] == -1); |
---|
452 | rtems_test_assert(ctx->eno[b] == -1); |
---|
453 | |
---|
454 | woken = _Futex_Wake(futex, 1); |
---|
455 | rtems_test_assert(woken == 1); |
---|
456 | rtems_test_assert(ctx->eno[a] == 0); |
---|
457 | rtems_test_assert(ctx->eno[b] == -1); |
---|
458 | |
---|
459 | woken = _Futex_Wake(futex, 1); |
---|
460 | rtems_test_assert(woken == 1); |
---|
461 | rtems_test_assert(ctx->eno[a] == 0); |
---|
462 | rtems_test_assert(ctx->eno[b] == 0); |
---|
463 | |
---|
464 | ctx->eno[a] = -1; |
---|
465 | ctx->eno[b] = -1; |
---|
466 | send_event(ctx, a, EVENT_FUTEX_WAIT); |
---|
467 | send_event(ctx, b, EVENT_FUTEX_WAIT); |
---|
468 | rtems_test_assert(ctx->eno[a] == -1); |
---|
469 | rtems_test_assert(ctx->eno[b] == -1); |
---|
470 | |
---|
471 | woken = _Futex_Wake(futex, 2); |
---|
472 | rtems_test_assert(woken == 2); |
---|
473 | rtems_test_assert(ctx->eno[a] == 0); |
---|
474 | rtems_test_assert(ctx->eno[b] == 0); |
---|
475 | } |
---|
476 | |
---|
477 | static void test_sched(void) |
---|
478 | { |
---|
479 | rtems_test_assert(_Sched_Index() == 0); |
---|
480 | rtems_test_assert(_Sched_Name_to_index("", 0) == -1); |
---|
481 | rtems_test_assert(_Sched_Name_to_index("b", 1) == -1); |
---|
482 | rtems_test_assert(_Sched_Name_to_index("bl", 2) == -1); |
---|
483 | rtems_test_assert(_Sched_Name_to_index("blu", 3) == -1); |
---|
484 | rtems_test_assert(_Sched_Name_to_index("blue", 4) == 0); |
---|
485 | rtems_test_assert(_Sched_Name_to_index("blueX", 5) == 0); |
---|
486 | rtems_test_assert(_Sched_Processor_count(-1) == 0); |
---|
487 | rtems_test_assert(_Sched_Processor_count(0) == 1); |
---|
488 | rtems_test_assert(_Sched_Processor_count(1) == 0); |
---|
489 | } |
---|
490 | |
---|
491 | static void mid_task(rtems_task_argument arg) |
---|
492 | { |
---|
493 | rtems_test_assert(0); |
---|
494 | } |
---|
495 | |
---|
496 | static void deadlock_cleanup(void *arg) |
---|
497 | { |
---|
498 | struct _Mutex_Control *deadlock_mtx = arg; |
---|
499 | |
---|
500 | /* |
---|
501 | * The thread terminate procedure will dequeue us from the wait queue. So, |
---|
502 | * one release is sufficient. |
---|
503 | */ |
---|
504 | |
---|
505 | _Mutex_Release(deadlock_mtx); |
---|
506 | _Mutex_Destroy(deadlock_mtx); |
---|
507 | } |
---|
508 | |
---|
509 | static void high_task(rtems_task_argument idx) |
---|
510 | { |
---|
511 | test_context *ctx = &test_instance; |
---|
512 | rtems_status_code sc; |
---|
513 | |
---|
514 | if (idx == 0) { |
---|
515 | sc = rtems_task_start(ctx->mid, mid_task, 0); |
---|
516 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
517 | |
---|
518 | sc = rtems_task_suspend(ctx->mid); |
---|
519 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
520 | } |
---|
521 | |
---|
522 | while (true) { |
---|
523 | rtems_event_set events; |
---|
524 | |
---|
525 | sc = rtems_event_receive( |
---|
526 | RTEMS_ALL_EVENTS, |
---|
527 | RTEMS_EVENT_ANY | RTEMS_WAIT, |
---|
528 | RTEMS_NO_TIMEOUT, |
---|
529 | &events |
---|
530 | ); |
---|
531 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
532 | |
---|
533 | if ((events & EVENT_MTX_ACQUIRE) != 0) { |
---|
534 | _Mutex_Acquire(&ctx->mtx); |
---|
535 | ctx->generation[idx] = generation(ctx, idx); |
---|
536 | } |
---|
537 | |
---|
538 | if ((events & EVENT_MTX_RELEASE) != 0) { |
---|
539 | _Mutex_Release(&ctx->mtx); |
---|
540 | } |
---|
541 | |
---|
542 | if ((events & EVENT_MTX_PRIO_INV) != 0) { |
---|
543 | sc = rtems_task_resume(ctx->mid); |
---|
544 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
545 | |
---|
546 | _Mutex_Acquire(&ctx->mtx); |
---|
547 | ctx->generation[idx] = generation(ctx, idx); |
---|
548 | _Mutex_Release(&ctx->mtx); |
---|
549 | |
---|
550 | sc = rtems_task_suspend(ctx->mid); |
---|
551 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
552 | } |
---|
553 | |
---|
554 | if ((events & EVENT_MTX_DEADLOCK) != 0) { |
---|
555 | struct _Mutex_Control *deadlock_mtx = &ctx->deadlock_mtx; |
---|
556 | |
---|
557 | pthread_cleanup_push(deadlock_cleanup, deadlock_mtx); |
---|
558 | |
---|
559 | _Mutex_Initialize(deadlock_mtx); |
---|
560 | _Mutex_Acquire(deadlock_mtx); |
---|
561 | _Mutex_Acquire(deadlock_mtx); |
---|
562 | |
---|
563 | pthread_cleanup_pop(0); |
---|
564 | } |
---|
565 | |
---|
566 | if ((events & EVENT_REC_MTX_ACQUIRE) != 0) { |
---|
567 | _Mutex_recursive_Acquire(&ctx->rec_mtx); |
---|
568 | } |
---|
569 | |
---|
570 | if ((events & EVENT_REC_MTX_RELEASE) != 0) { |
---|
571 | _Mutex_recursive_Release(&ctx->rec_mtx); |
---|
572 | } |
---|
573 | |
---|
574 | if ((events & EVENT_REC_MTX_PRIO_INV) != 0) { |
---|
575 | sc = rtems_task_resume(ctx->mid); |
---|
576 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
577 | |
---|
578 | _Mutex_recursive_Acquire(&ctx->rec_mtx); |
---|
579 | ctx->generation[idx] = generation(ctx, idx); |
---|
580 | _Mutex_recursive_Release(&ctx->rec_mtx); |
---|
581 | |
---|
582 | sc = rtems_task_suspend(ctx->mid); |
---|
583 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
584 | } |
---|
585 | |
---|
586 | if ((events & EVENT_SEM_WAIT) != 0) { |
---|
587 | _Semaphore_Wait(&ctx->sem); |
---|
588 | ctx->generation[idx] = generation(ctx, idx); |
---|
589 | } |
---|
590 | |
---|
591 | if ((events & EVENT_FUTEX_WAIT) != 0) { |
---|
592 | ctx->eno[idx] = _Futex_Wait(&ctx->futex, &ctx->val, 1); |
---|
593 | } |
---|
594 | |
---|
595 | if ((events & EVENT_CONDITION_WAIT) != 0) { |
---|
596 | _Mutex_Acquire(&ctx->mtx); |
---|
597 | ctx->generation[idx] = generation(ctx, idx); |
---|
598 | _Condition_Wait(&ctx->cond, &ctx->mtx); |
---|
599 | ctx->generation[idx] = generation(ctx, idx); |
---|
600 | _Mutex_Release(&ctx->mtx); |
---|
601 | } |
---|
602 | |
---|
603 | if ((events & EVENT_CONDITION_WAIT_REC) != 0) { |
---|
604 | _Mutex_recursive_Acquire(&ctx->rec_mtx); |
---|
605 | _Mutex_recursive_Acquire(&ctx->rec_mtx); |
---|
606 | ctx->generation[idx] = generation(ctx, idx); |
---|
607 | _Condition_Wait_recursive(&ctx->cond, &ctx->rec_mtx); |
---|
608 | ctx->generation[idx] = generation(ctx, idx); |
---|
609 | _Mutex_recursive_Release(&ctx->rec_mtx); |
---|
610 | _Mutex_recursive_Release(&ctx->rec_mtx); |
---|
611 | } |
---|
612 | } |
---|
613 | } |
---|
614 | |
---|
615 | static void test(void) |
---|
616 | { |
---|
617 | test_context *ctx = &test_instance; |
---|
618 | rtems_status_code sc; |
---|
619 | |
---|
620 | test_initialization(ctx); |
---|
621 | |
---|
622 | ctx->low = rtems_task_self(); |
---|
623 | |
---|
624 | sc = rtems_task_create( |
---|
625 | rtems_build_name('M', 'I', 'D', ' '), |
---|
626 | 3, |
---|
627 | RTEMS_MINIMUM_STACK_SIZE, |
---|
628 | RTEMS_DEFAULT_MODES, |
---|
629 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
630 | &ctx->mid |
---|
631 | ); |
---|
632 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
633 | |
---|
634 | sc = rtems_task_create( |
---|
635 | rtems_build_name('H', 'I', 'G', '0'), |
---|
636 | 1, |
---|
637 | RTEMS_MINIMUM_STACK_SIZE, |
---|
638 | RTEMS_DEFAULT_MODES, |
---|
639 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
640 | &ctx->high[0] |
---|
641 | ); |
---|
642 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
643 | |
---|
644 | sc = rtems_task_start(ctx->high[0], high_task, 0); |
---|
645 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
646 | |
---|
647 | sc = rtems_task_create( |
---|
648 | rtems_build_name('H', 'I', 'G', '1'), |
---|
649 | 2, |
---|
650 | RTEMS_MINIMUM_STACK_SIZE, |
---|
651 | RTEMS_DEFAULT_MODES, |
---|
652 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
653 | &ctx->high[1] |
---|
654 | ); |
---|
655 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
656 | |
---|
657 | sc = rtems_task_start(ctx->high[1], high_task, 1); |
---|
658 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
659 | |
---|
660 | test_recursive_acquire_normal(ctx); |
---|
661 | test_recursive_acquire_recursive(ctx); |
---|
662 | test_prio_acquire_order(ctx); |
---|
663 | test_prio_inv_normal(ctx); |
---|
664 | test_prio_inv_recursive(ctx); |
---|
665 | test_mtx_timeout_normal(ctx); |
---|
666 | test_mtx_timeout_recursive(ctx); |
---|
667 | test_condition(ctx); |
---|
668 | test_condition_timeout(ctx); |
---|
669 | test_sem(ctx); |
---|
670 | test_sem_prio_wait_order(ctx); |
---|
671 | test_futex(ctx); |
---|
672 | test_sched(); |
---|
673 | |
---|
674 | send_event(ctx, 0, EVENT_MTX_DEADLOCK); |
---|
675 | |
---|
676 | sc = rtems_task_delete(ctx->mid); |
---|
677 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
678 | |
---|
679 | sc = rtems_task_delete(ctx->high[0]); |
---|
680 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
681 | |
---|
682 | sc = rtems_task_delete(ctx->high[1]); |
---|
683 | rtems_test_assert(sc == RTEMS_SUCCESSFUL); |
---|
684 | |
---|
685 | _Mutex_Destroy(&ctx->mtx); |
---|
686 | _Mutex_recursive_Destroy(&ctx->rec_mtx); |
---|
687 | _Condition_Destroy(&ctx->cond); |
---|
688 | _Semaphore_Destroy(&ctx->sem); |
---|
689 | _Futex_Destroy(&ctx->futex); |
---|
690 | } |
---|
691 | |
---|
692 | static void Init(rtems_task_argument arg) |
---|
693 | { |
---|
694 | TEST_BEGIN(); |
---|
695 | |
---|
696 | test(); |
---|
697 | |
---|
698 | TEST_END(); |
---|
699 | rtems_test_exit(0); |
---|
700 | } |
---|
701 | |
---|
702 | #define CONFIGURE_MICROSECONDS_PER_TICK US_PER_TICK |
---|
703 | |
---|
704 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
705 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
706 | |
---|
707 | #define CONFIGURE_MAXIMUM_TASKS 4 |
---|
708 | |
---|
709 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
710 | |
---|
711 | #define CONFIGURE_INIT_TASK_PRIORITY 4 |
---|
712 | #define CONFIGURE_INIT_TASK_INITIAL_MODES RTEMS_DEFAULT_MODES |
---|
713 | |
---|
714 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
715 | |
---|
716 | #define CONFIGURE_SCHEDULER_NAME rtems_build_name('b', 'l', 'u', 'e') |
---|
717 | |
---|
718 | #define CONFIGURE_INIT |
---|
719 | |
---|
720 | #include <rtems/confdefs.h> |
---|