]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/tests/klock.c
Initial revision
[karo-tx-redboot.git] / packages / kernel / v2_0 / tests / klock.c
1 /*=================================================================
2 //
3 //        klock.c
4 //
5 //        Kernel lock test
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12 //
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
16 //
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
20 // for more details.
21 //
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 //
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
32 //
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
35 //
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
42 //
43 // Author(s):     dsm
44 // Contributors:    dsm
45 // Date:          1998-03-18
46 // Description:   Tests some basic thread functions.
47 //####DESCRIPTIONEND####
48 */
49 //==========================================================================
50
51 #include <cyg/kernel/kapi.h>
52
53 #include <cyg/infra/testcase.h>
54
55 //==========================================================================
56
57 #ifdef CYGFUN_KERNEL_API_C
58
59 #if (CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0)
60
61 //==========================================================================
62
63 #include "testaux.h"
64
65 #include <cyg/hal/hal_arch.h>           // for CYGNUM_HAL_STACK_SIZE_TYPICAL
66
67 //==========================================================================
68
69 #ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
70 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
71 #else
72 #define STACKSIZE 2000
73 #endif
74
75 //==========================================================================
76
77 static char stack[2][STACKSIZE];
78
79 static cyg_thread thread[2];
80
81 static cyg_handle_t pt0,pt1;
82
83 static cyg_mutex_t mx;
84 static cyg_cond_t cv;
85 static cyg_sem_t sem;
86 static cyg_flag_t fl;
87 static cyg_mbox mbox;
88 static cyg_handle_t mbh;
89
90 volatile static int thread0_state = 0;
91 volatile static int thread1_state = 0;
92
93 //==========================================================================
94
95 static void entry0( cyg_addrword_t data )
96 {
97     CHECK( 222 == (int)data );
98
99     // Do everything with the scheduler locked.
100     cyg_scheduler_lock();
101
102     // --------------------------------------------------
103     // Mutex test
104     
105     cyg_mutex_lock( &mx );
106     thread0_state = 1;
107     
108     // Get thread 2 running.
109     cyg_thread_resume(pt1);
110     thread0_state = 2;
111
112 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
113     cyg_cond_wait( &cv );
114     thread0_state = 3;
115
116     while( thread1_state < 2 ) cyg_thread_yield();
117     
118     cyg_cond_broadcast( &cv );
119     thread0_state = 4;
120 #endif
121
122     cyg_mutex_unlock( &mx );
123     thread0_state = 5;
124
125
126     // --------------------------------------------------
127     // Semaphore test
128     
129     cyg_semaphore_wait( &sem );
130     thread0_state = 6;
131
132     cyg_semaphore_post( &sem );
133     thread0_state = 7;
134
135     while( thread1_state < 7 ) cyg_thread_yield();
136     
137     // --------------------------------------------------
138     // Flags test
139
140     cyg_flag_wait( &fl, 1, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR);
141     thread0_state = 8;    
142
143     cyg_flag_setbits( &fl, 2 );
144     thread0_state = 9;    
145     
146     // --------------------------------------------------
147     // Message box test
148
149 #ifdef  CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
150     {
151       void *mbret;
152
153       mbret = cyg_mbox_get( mbh );
154       CYG_TEST_CHECK( mbret == (void *)0xAAAAAAAA , "bad result from cyg_mbox_timed_get()");
155       thread0_state = 10;
156       
157       cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
158       thread0_state = 11;
159     }
160 #endif
161     // --------------------------------------------------    
162     
163     thread0_state = 999;
164
165     cyg_thread_yield();
166     cyg_thread_yield();
167     cyg_thread_yield();
168     
169     CYG_TEST_CHECK( thread0_state == 999, "thread 0 not in exit state");
170     CYG_TEST_CHECK( thread1_state == 999, "thread 1 not in exit state");
171     CYG_TEST_PASS_FINISH("Kernel lock test OK");    
172 }
173
174 //==========================================================================
175
176 static void entry1( cyg_addrword_t data )
177 {
178     cyg_bool res;
179     
180     CHECK( 333 == (int)data );
181
182     // Do everything with the scheduler locked.
183     cyg_scheduler_lock();
184
185     // --------------------------------------------------
186     // Mutex test
187 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
188     cyg_mutex_lock( &mx );
189     thread1_state = 1;
190
191     while( thread0_state < 2 ) cyg_thread_yield();
192     
193     cyg_cond_signal( &cv );
194     thread1_state = 2;
195
196     res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
197     CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
198     thread1_state = 3;
199
200     cyg_mutex_unlock( &mx );
201     thread1_state = 4;
202 #endif
203
204     // --------------------------------------------------
205     // Semaphore test
206     
207     while( thread0_state < 5 ) cyg_thread_yield();
208     
209     cyg_semaphore_post( &sem );
210     thread1_state = 5;
211
212     while( thread0_state < 6 ) cyg_thread_yield();
213     thread1_state = 6;
214     
215 #ifdef CYGFUN_KERNEL_THREADS_TIMER
216     res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
217 #else
218     res = cyg_semaphore_wait( &sem );
219 #endif
220     CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
221     thread1_state = 7;
222
223     // --------------------------------------------------
224     // Flags test
225
226     cyg_flag_setbits( &fl, 1 );
227     thread1_state = 8;
228
229 #ifdef CYGFUN_KERNEL_THREADS_TIMER
230     cyg_flag_timed_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR,
231                          cyg_current_time()+10 );
232 #else
233     cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
234 #endif
235     thread1_state = 9;
236     
237     // --------------------------------------------------
238     // Message box test
239 #ifdef  CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
240     {
241       void *mbret;
242 #ifdef CYGFUN_KERNEL_THREADS_TIMER
243       cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
244 #else
245       cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
246 #endif
247       thread1_state = 10;
248
249 #ifdef CYGFUN_KERNEL_THREADS_TIMER
250       mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
251 #else
252       mbret = cyg_mbox_get( mbh );
253 #endif
254       CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
255       thread1_state = 9;
256     }
257 #endif    
258     // --------------------------------------------------
259     
260     thread1_state = 999;
261     cyg_thread_exit();
262 }
263
264 //==========================================================================
265
266 void kthread1_main( void )
267 {
268     CYG_TEST_INIT();
269
270     cyg_thread_create(4, entry0, (cyg_addrword_t)222, "kthread1-0",
271         (void *)stack[0], STACKSIZE, &pt0, &thread[0] );
272     cyg_thread_create(4, entry1, (cyg_addrword_t)333, "kthread1-1",
273         (void *)stack[1], STACKSIZE, &pt1, &thread[1] );
274
275     // Init all the objects
276
277     cyg_mutex_init( &mx );
278     cyg_cond_init( &cv, &mx );
279     cyg_semaphore_init( &sem, 0 );
280     cyg_flag_init( &fl );
281     cyg_mbox_create( &mbh, &mbox );
282     
283     cyg_thread_resume(pt0);
284
285     cyg_scheduler_start();
286
287     CYG_TEST_FAIL_FINISH("Not reached");
288 }
289
290 //==========================================================================
291
292 externC void
293 cyg_start( void )
294 {
295     kthread1_main();
296 }
297
298 //==========================================================================
299
300 #else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
301 # define NA_MSG "Schedule has unique priorities"
302 #endif
303
304 #else /* def CYGFUN_KERNEL_API_C */
305 # define NA_MSG "Kernel C API layer disabled"
306 #endif
307
308 #ifdef NA_MSG
309 externC void
310 cyg_start( void )
311 {
312     CYG_TEST_INIT();
313     CYG_TEST_NA(NA_MSG);
314 }
315 #endif
316
317 //==========================================================================
318 /* EOF klock.c */