|
0
|
1 /*========================================================================== |
|
|
2 // |
|
|
3 // kflag1.cxx |
|
|
4 // |
|
|
5 // Kernel C API Flag test 1 |
|
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
2
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
0
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author: dsm |
|
|
33 // Contributors: hmt |
|
|
34 // Date: 1998-10-19 |
|
|
35 // Description: Tests basic flag functionality. |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 */ |
|
|
38 |
|
2
|
39 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
|
40 |
|
0
|
41 #include <cyg/kernel/kapi.h> |
|
|
42 |
|
|
43 #include <cyg/infra/testcase.h> |
|
|
44 |
|
|
45 #ifdef CYGFUN_KERNEL_API_C |
|
|
46 |
|
|
47 #include "testaux.h" |
|
|
48 |
|
|
49 |
|
|
50 #define NTHREADS 3 |
|
2
|
51 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL |
|
0
|
52 |
|
|
53 static cyg_handle_t thread[NTHREADS]; |
|
|
54 |
|
|
55 static cyg_thread thread_obj[NTHREADS]; |
|
|
56 static char stack[NTHREADS][STACKSIZE]; |
|
|
57 |
|
|
58 static cyg_flag_t f0, f1; |
|
|
59 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
60 static cyg_flag_t f2; |
|
|
61 #endif |
|
|
62 |
|
|
63 static volatile cyg_ucount8 q = 0; |
|
|
64 |
|
|
65 static void entry0( cyg_addrword_t data ) |
|
|
66 { |
|
|
67 CYG_TEST_INFO("Testing cyg_flag_setbits() and cyg_flag_maskbits()"); |
|
|
68 CYG_TEST_CHECK(0==cyg_flag_peek( &f0 ), "flag not initialized properly"); |
|
|
69 cyg_flag_setbits( &f0, 0x1); |
|
|
70 CYG_TEST_CHECK(1==cyg_flag_peek( &f0 ), "setbits"); |
|
|
71 cyg_flag_setbits( &f0, 0x3); |
|
|
72 CYG_TEST_CHECK(3==cyg_flag_peek( &f0 ), "setbits"); |
|
|
73 cyg_flag_maskbits( &f0, ~0x5); |
|
|
74 CYG_TEST_CHECK(2==cyg_flag_peek( &f0 ), "maskbits"); |
|
|
75 cyg_flag_setbits( &f0, ~0 ); |
|
|
76 CYG_TEST_CHECK(~0u==cyg_flag_peek( &f0 ), "setbits all set"); |
|
|
77 cyg_flag_maskbits( &f0, 0 ); |
|
|
78 CYG_TEST_CHECK(0==cyg_flag_peek( &f0 ), "maskbits all clear"); |
|
|
79 CYG_TEST_CHECK(0==q++, "bad synchronization"); |
|
|
80 |
|
|
81 CYG_TEST_INFO("Testing cyg_flag_wait()"); |
|
|
82 cyg_flag_setbits( &f1, 0x4); |
|
|
83 CYG_TEST_CHECK(0x4==cyg_flag_peek( &f1 ), "setbits"); |
|
|
84 CYG_TEST_CHECK(1==q++, "bad synchronization"); |
|
|
85 cyg_flag_setbits( &f1, 0x18); // wake t1 |
|
|
86 cyg_flag_wait( &f1, 0x11, CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR); |
|
|
87 CYG_TEST_CHECK(0==cyg_flag_peek( &f1 ), "flag value wrong"); |
|
|
88 CYG_TEST_CHECK(3==q++, "bad synchronization"); |
|
|
89 cyg_flag_setbits( &f0, 0x2); // wake t1 |
|
|
90 cyg_flag_wait( &f1, 0x10, CYG_FLAG_WAITMODE_AND ); |
|
|
91 cyg_flag_setbits( &f0, 0x1); // wake t1 |
|
|
92 |
|
|
93 cyg_flag_wait( &f1, 0x11, CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR); |
|
|
94 |
|
|
95 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
96 cyg_flag_wait( &f2, 0x2, CYG_FLAG_WAITMODE_OR); |
|
|
97 CYG_TEST_CHECK(20==q,"bad synchronization"); |
|
|
98 cyg_flag_timed_wait( &f2, 0x10, CYG_FLAG_WAITMODE_AND, |
|
|
99 cyg_current_time()+20); |
|
|
100 CYG_TEST_CHECK(21==q++,"bad synchronization"); |
|
|
101 #endif |
|
|
102 cyg_flag_wait( &f0, 1, CYG_FLAG_WAITMODE_OR); |
|
|
103 |
|
|
104 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
105 } |
|
|
106 |
|
|
107 static void entry1( cyg_addrword_t data ) |
|
|
108 { |
|
|
109 cyg_flag_wait( &f1, 0xc, CYG_FLAG_WAITMODE_AND); |
|
|
110 CYG_TEST_CHECK(2==q++, "bad synchronization"); |
|
|
111 CYG_TEST_CHECK(0x1c==cyg_flag_peek( &f1 ), "flag value wrong"); |
|
|
112 cyg_flag_setbits( &f1, 0x1); // wake t0 |
|
|
113 cyg_flag_wait( &f0, 0x3, CYG_FLAG_WAITMODE_OR); |
|
|
114 CYG_TEST_CHECK(4==q++, "bad synchronization"); |
|
|
115 CYG_TEST_CHECK(2==cyg_flag_peek( &f0 ), "flag value wrong"); |
|
|
116 |
|
|
117 cyg_flag_setbits( &f1, 0xf0); // wake t0,t2 |
|
|
118 cyg_flag_wait( &f0, 0x5, CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR); |
|
|
119 CYG_TEST_CHECK(0==cyg_flag_peek( &f0 ), "flag value wrong"); |
|
|
120 CYG_TEST_CHECK(0xf0==cyg_flag_peek( &f1 ), "flag value wrong"); |
|
|
121 CYG_TEST_CHECK(5==q++, "bad synchronization"); |
|
|
122 cyg_flag_maskbits( &f1, 0 ); |
|
|
123 CYG_TEST_CHECK(0==cyg_flag_peek( &f1 ), "flag value wrong"); |
|
|
124 |
|
|
125 CYG_TEST_INFO("Testing cyg_flag_poll()"); |
|
|
126 cyg_flag_setbits( &f0, 0x55); |
|
|
127 CYG_TEST_CHECK(0x55==cyg_flag_peek( &f0 ), "flag value wrong"); |
|
|
128 CYG_TEST_CHECK(0x55==cyg_flag_poll( &f0, 0x3, CYG_FLAG_WAITMODE_OR),"bad poll() return"); |
|
|
129 CYG_TEST_CHECK(0==cyg_flag_poll( &f0, 0xf, CYG_FLAG_WAITMODE_AND),"poll()"); |
|
|
130 CYG_TEST_CHECK(0==cyg_flag_poll( &f0, 0xa, CYG_FLAG_WAITMODE_OR),"poll()"); |
|
|
131 CYG_TEST_CHECK(0x55==cyg_flag_peek( &f0 ), "flag value wrong"); |
|
|
132 CYG_TEST_CHECK(0x55==cyg_flag_poll( &f0, 0xf, CYG_FLAG_WAITMODE_OR | CYG_FLAG_WAITMODE_CLR),"poll"); |
|
|
133 CYG_TEST_CHECK(0x0==cyg_flag_peek( &f0 ), "flag value wrong"); |
|
|
134 cyg_flag_setbits( &f0, 0x50); |
|
|
135 CYG_TEST_CHECK(0x50==cyg_flag_poll( &f0, 0x10, CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR),"poll"); |
|
|
136 CYG_TEST_CHECK(0x0==cyg_flag_peek( &f0 ), "flag value wrong"); |
|
|
137 |
|
|
138 CYG_TEST_INFO("Testing cyg_flag_waiting()"); |
|
|
139 cyg_flag_maskbits( &f0, 0 ); |
|
|
140 CYG_TEST_CHECK(!cyg_flag_waiting( &f0 ), "waiting()"); |
|
|
141 |
|
|
142 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
143 cyg_thread_delay( 10 ); // allow other threads to reach wait on f1 |
|
|
144 CYG_TEST_CHECK(cyg_flag_waiting( &f1 ), "waiting() not true"); |
|
|
145 cyg_flag_setbits( &f1, ~0 ); // wake one of t0,t2 |
|
|
146 CYG_TEST_CHECK(cyg_flag_waiting( &f1 ),"waiting() not true"); |
|
|
147 #else |
|
|
148 cyg_flag_setbits( &f1, 0x11); // wake one of t0,t2 |
|
|
149 #endif |
|
|
150 cyg_flag_setbits( &f1, 0x11); // wake other of t0,t2 |
|
|
151 CYG_TEST_CHECK(!cyg_flag_waiting( &f1 ),"waiting not false"); |
|
|
152 CYG_TEST_CHECK(0x0==cyg_flag_peek( &f1 ), "flag value wrong"); |
|
|
153 |
|
|
154 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
155 CYG_TEST_INFO("Testing cyg_flag_timed_wait()"); |
|
|
156 q=20; |
|
|
157 cyg_flag_setbits( &f2, 0x2); // synchronize with t0,t2 |
|
|
158 CYG_TEST_CHECK(20==q,"bad synchronization"); |
|
|
159 cyg_flag_timed_wait( &f2, 0x20, CYG_FLAG_WAITMODE_AND, |
|
|
160 cyg_current_time()+10); |
|
|
161 CYG_TEST_CHECK(22==q++,"bad synchronization"); |
|
|
162 #endif |
|
|
163 |
|
|
164 CYG_TEST_PASS_FINISH("Kernel C API Flag 1 OK"); |
|
|
165 } |
|
|
166 |
|
|
167 static void entry2( cyg_addrword_t data ) |
|
|
168 { |
|
|
169 cyg_flag_wait( &f1, 0x60, CYG_FLAG_WAITMODE_OR); |
|
|
170 cyg_flag_setbits( &f0, 0x4); |
|
|
171 |
|
|
172 cyg_flag_wait( &f1, 0x11, CYG_FLAG_WAITMODE_AND | CYG_FLAG_WAITMODE_CLR); |
|
|
173 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
174 cyg_flag_wait( &f2, 0x2, CYG_FLAG_WAITMODE_OR); |
|
|
175 CYG_TEST_CHECK(20==q,"bad synchronization"); |
|
|
176 CYG_TEST_CHECK(0==cyg_flag_timed_wait( &f2, 0x40, CYG_FLAG_WAITMODE_AND, |
|
|
177 cyg_current_time()+2), |
|
|
178 "timed wait() wrong"); |
|
|
179 CYG_TEST_CHECK(20==q++,"bad synchronization"); |
|
|
180 // Now wake t0 before it times out |
|
|
181 cyg_flag_setbits( &f2, 0x10); |
|
|
182 #endif |
|
|
183 cyg_flag_wait( &f0, 1, CYG_FLAG_WAITMODE_OR); |
|
|
184 |
|
|
185 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
186 } |
|
|
187 |
|
|
188 void kflag1_main( void ) |
|
|
189 { |
|
|
190 CYG_TEST_INIT(); |
|
|
191 |
|
|
192 cyg_flag_init( &f0 ); |
|
|
193 cyg_flag_init( &f1 ); |
|
|
194 #ifdef CYGFUN_KERNEL_THREADS_TIMER |
|
|
195 cyg_flag_init( &f2 ); |
|
|
196 #endif |
|
|
197 |
|
|
198 cyg_thread_create( 1, entry0 , (cyg_addrword_t)0, "kflag1-0", |
|
2
|
199 (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]); |
|
0
|
200 cyg_thread_resume(thread[0]); |
|
|
201 |
|
|
202 cyg_thread_create( 1, entry1 , (cyg_addrword_t)1, "kflag1-1", |
|
2
|
203 (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]); |
|
0
|
204 cyg_thread_resume(thread[1]); |
|
|
205 |
|
|
206 cyg_thread_create( 1, entry2 , (cyg_addrword_t)2, "kflag1-2", |
|
2
|
207 (void *)stack[2], STACKSIZE, &thread[2], &thread_obj[2]); |
|
0
|
208 cyg_thread_resume(thread[2]); |
|
|
209 |
|
|
210 cyg_scheduler_start(); |
|
|
211 |
|
|
212 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
213 } |
|
|
214 |
|
|
215 externC void |
|
|
216 cyg_start( void ) |
|
|
217 { |
|
|
218 kflag1_main(); |
|
|
219 } |
|
|
220 |
|
|
221 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
222 externC void |
|
|
223 cyg_start( void ) |
|
|
224 { |
|
|
225 CYG_TEST_INIT(); |
|
2
|
226 CYG_TEST_NA("Kernel C API layer disabled"); |
|
0
|
227 } |
|
|
228 #endif /* def CYGFUN_KERNEL_API_C */ |
|
|
229 |
|
|
230 // EOF flag1.cxx |