1 | /* |
---|
2 | * Test for rtems_semaphore_flush |
---|
3 | * |
---|
4 | * COPYRIGHT (c) 1989-2012. |
---|
5 | * On-Line Applications Research Corporation (OAR). |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #ifdef HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <bsp.h> |
---|
17 | #include <tmacros.h> |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | #include <stdlib.h> |
---|
21 | |
---|
22 | /* forward declarations to avoid warnings */ |
---|
23 | rtems_task Init(rtems_task_argument argument); |
---|
24 | void starttask(int arg); |
---|
25 | rtems_task subtask(rtems_task_argument arg); |
---|
26 | void doTest(void); |
---|
27 | |
---|
28 | #define NTASK 4 |
---|
29 | |
---|
30 | #if defined(USE_COUNTING_SEMAPHORE) |
---|
31 | #define TEST_NAME "27a" |
---|
32 | #define TEST_SEMAPHORE_TYPE "counting" |
---|
33 | #define TEST_SEMAPHORE_ATTRIBUTES RTEMS_DEFAULT_ATTRIBUTES |
---|
34 | #else |
---|
35 | #define TEST_NAME "27" |
---|
36 | #define TEST_SEMAPHORE_TYPE "binary" |
---|
37 | #define TEST_SEMAPHORE_ATTRIBUTES (RTEMS_LOCAL| \ |
---|
38 | RTEMS_BINARY_SEMAPHORE|RTEMS_NO_INHERIT_PRIORITY| \ |
---|
39 | RTEMS_NO_PRIORITY_CEILING|RTEMS_FIFO) |
---|
40 | #endif |
---|
41 | |
---|
42 | rtems_id semaphore; |
---|
43 | volatile int flags[NTASK]; |
---|
44 | |
---|
45 | rtems_task subtask( |
---|
46 | rtems_task_argument arg |
---|
47 | ) |
---|
48 | { |
---|
49 | rtems_status_code sc; |
---|
50 | |
---|
51 | for (;;) { |
---|
52 | flags[arg]++; |
---|
53 | sc = rtems_semaphore_obtain(semaphore, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
54 | if (sc == RTEMS_SUCCESSFUL) |
---|
55 | puts("Obtained semaphore -- and should not have done so!"); |
---|
56 | else if (sc != RTEMS_UNSATISFIED) |
---|
57 | printf("Can't get semaphore: %s\n", rtems_status_text(sc)); |
---|
58 | } |
---|
59 | } |
---|
60 | |
---|
61 | void starttask( |
---|
62 | int arg |
---|
63 | ) |
---|
64 | { |
---|
65 | rtems_id tid; |
---|
66 | rtems_status_code sc; |
---|
67 | rtems_task_priority priority; |
---|
68 | |
---|
69 | rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &priority); |
---|
70 | sc = rtems_task_create(rtems_build_name('S', 'R', 'V', arg + 'A'), |
---|
71 | priority, |
---|
72 | RTEMS_MINIMUM_STACK_SIZE, |
---|
73 | RTEMS_PREEMPT|RTEMS_NO_TIMESLICE|RTEMS_NO_ASR|RTEMS_INTERRUPT_LEVEL(0), |
---|
74 | RTEMS_NO_FLOATING_POINT|RTEMS_LOCAL, |
---|
75 | &tid); |
---|
76 | directive_failed( sc, "task create" ); |
---|
77 | |
---|
78 | sc = rtems_task_start(tid, subtask, (rtems_task_argument) arg); |
---|
79 | directive_failed( sc, "task start" ); |
---|
80 | } |
---|
81 | |
---|
82 | void doTest(void) |
---|
83 | { |
---|
84 | rtems_status_code sc; |
---|
85 | int pass, i; |
---|
86 | |
---|
87 | sc = rtems_semaphore_create( |
---|
88 | rtems_build_name('S', 'E', 'M', 'F'), |
---|
89 | 0, |
---|
90 | TEST_SEMAPHORE_ATTRIBUTES, |
---|
91 | 0, |
---|
92 | &semaphore); |
---|
93 | directive_failed( sc, "semaphore create" ); |
---|
94 | |
---|
95 | for (i = 0 ; i < NTASK ; i++) |
---|
96 | flags[i] = 0; |
---|
97 | |
---|
98 | for (i = 0 ; i < NTASK ; i++) |
---|
99 | starttask(i); |
---|
100 | |
---|
101 | for (pass = 1 ; pass < 10 ; pass++) { |
---|
102 | rtems_task_wake_after(1); |
---|
103 | for (i = 0 ; i < NTASK ; i++) { |
---|
104 | if (flags[i] != pass) |
---|
105 | printf("flags[%d] = %d -- expected %d\n", i, flags[i], pass); |
---|
106 | } |
---|
107 | sc = rtems_semaphore_flush(semaphore); |
---|
108 | directive_failed( sc, "semaphore flush" ); |
---|
109 | } |
---|
110 | |
---|
111 | printf("Flushed all waiting tasks\n" ); |
---|
112 | } |
---|
113 | |
---|
114 | rtems_task Init( |
---|
115 | rtems_task_argument ignored |
---|
116 | ) |
---|
117 | { |
---|
118 | puts( "\n\n*** TEST " TEST_NAME " ***" ); |
---|
119 | puts( "Testing " TEST_SEMAPHORE_TYPE " semaphore flush" ); |
---|
120 | doTest(); |
---|
121 | puts( "*** END OF TEST " TEST_NAME " ***" ); |
---|
122 | |
---|
123 | rtems_test_exit(0); |
---|
124 | } |
---|
125 | |
---|
126 | /**************** START OF CONFIGURATION INFORMATION ****************/ |
---|
127 | |
---|
128 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
129 | |
---|
130 | #define CONFIGURE_APPLICATION_NEEDS_CONSOLE_DRIVER |
---|
131 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
132 | |
---|
133 | #define CONFIGURE_MAXIMUM_TASKS 6 |
---|
134 | #define CONFIGURE_MAXIMUM_SEMAPHORES 1 |
---|
135 | |
---|
136 | #define CONFIGURE_INIT |
---|
137 | |
---|
138 | #include <rtems/confdefs.h> |
---|
139 | |
---|
140 | /**************** END OF CONFIGURATION INFORMATION ****************/ |
---|
141 | |
---|