]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/kernel/v2_0/tests/klock.c
unified MX27, MX25, MX37 trees
[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       while( thread1_state < 10 ) cyg_thread_yield();
158       
159       cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
160       thread0_state = 11;
161     }
162 #endif
163     // --------------------------------------------------    
164     
165     thread0_state = 999;
166
167     cyg_thread_yield();
168     cyg_thread_yield();
169     cyg_thread_yield();
170     
171     CYG_TEST_CHECK( thread0_state == 999, "thread 0 not in exit state");
172     CYG_TEST_CHECK( thread1_state == 999, "thread 1 not in exit state");
173     CYG_TEST_PASS_FINISH("Kernel lock test OK");    
174 }
175
176 //==========================================================================
177
178 static void entry1( cyg_addrword_t data )
179 {
180     cyg_bool res;
181     
182     CHECK( 333 == (int)data );
183
184     // Do everything with the scheduler locked.
185     cyg_scheduler_lock();
186
187     // --------------------------------------------------
188     // Mutex test
189 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
190     cyg_mutex_lock( &mx );
191     thread1_state = 1;
192
193     while( thread0_state < 2 ) cyg_thread_yield();
194     
195     cyg_cond_signal( &cv );
196     thread1_state = 2;
197
198     res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
199     CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
200     thread1_state = 3;
201
202     cyg_mutex_unlock( &mx );
203     thread1_state = 4;
204 #endif
205
206     // --------------------------------------------------
207     // Semaphore test
208     
209     while( thread0_state < 5 ) cyg_thread_yield();
210     
211     cyg_semaphore_post( &sem );
212     thread1_state = 5;
213
214     while( thread0_state < 6 ) cyg_thread_yield();
215     thread1_state = 6;
216     
217 #ifdef CYGFUN_KERNEL_THREADS_TIMER
218     res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
219 #else
220     res = cyg_semaphore_wait( &sem );
221 #endif
222     CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
223     thread1_state = 7;
224
225     // --------------------------------------------------
226     // Flags test
227
228     cyg_flag_setbits( &fl, 1 );
229     thread1_state = 8;
230
231 #ifdef CYGFUN_KERNEL_THREADS_TIMER
232     cyg_flag_timed_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR,
233                          cyg_current_time()+10 );
234 #else
235     cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
236 #endif
237     thread1_state = 9;
238     
239     // --------------------------------------------------
240     // Message box test
241 #ifdef  CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
242     {
243       void *mbret;
244 #ifdef CYGFUN_KERNEL_THREADS_TIMER
245       cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
246 #else
247       cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
248 #endif
249       thread1_state = 10;
250
251       while( thread0_state < 10 ) cyg_thread_yield();
252       
253 #ifdef CYGFUN_KERNEL_THREADS_TIMER
254       mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
255 #else
256       mbret = cyg_mbox_get( mbh );
257 #endif
258       thread1_state = 11;
259       CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
260       thread1_state = 12;
261     }
262 #endif    
263     // --------------------------------------------------
264     
265     thread1_state = 999;
266     cyg_thread_exit();
267 }
268
269 //==========================================================================
270
271 void kthread1_main( void )
272 {
273     CYG_TEST_INIT();
274
275     cyg_thread_create(4, entry0, (cyg_addrword_t)222, "kthread1-0",
276         (void *)stack[0], STACKSIZE, &pt0, &thread[0] );
277     cyg_thread_create(4, entry1, (cyg_addrword_t)333, "kthread1-1",
278         (void *)stack[1], STACKSIZE, &pt1, &thread[1] );
279
280     // Init all the objects
281
282     cyg_mutex_init( &mx );
283     cyg_cond_init( &cv, &mx );
284     cyg_semaphore_init( &sem, 0 );
285     cyg_flag_init( &fl );
286     cyg_mbox_create( &mbh, &mbox );
287     
288     cyg_thread_resume(pt0);
289
290     cyg_scheduler_start();
291
292     CYG_TEST_FAIL_FINISH("Not reached");
293 }
294
295 //==========================================================================
296
297 externC void
298 cyg_start( void )
299 {
300     kthread1_main();
301 }
302
303 //==========================================================================
304
305 #else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
306 # define NA_MSG "Schedule has unique priorities"
307 #endif
308
309 #else /* def CYGFUN_KERNEL_API_C */
310 # define NA_MSG "Kernel C API layer disabled"
311 #endif
312
313 #ifdef NA_MSG
314 externC void
315 cyg_start( void )
316 {
317     CYG_TEST_INIT();
318     CYG_TEST_NA(NA_MSG);
319 }
320 #endif
321
322 //==========================================================================
323 /* EOF klock.c */