1 /*=================================================================
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.
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.
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
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.
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.
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.
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####
46 // Description: Tests some basic thread functions.
47 //####DESCRIPTIONEND####
49 //==========================================================================
51 #include <cyg/kernel/kapi.h>
53 #include <cyg/infra/testcase.h>
55 //==========================================================================
57 #ifdef CYGFUN_KERNEL_API_C
59 #if (CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0)
61 //==========================================================================
65 #include <cyg/hal/hal_arch.h> // for CYGNUM_HAL_STACK_SIZE_TYPICAL
67 //==========================================================================
69 #ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
70 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
72 #define STACKSIZE 2000
75 //==========================================================================
77 static char stack[2][STACKSIZE];
79 static cyg_thread thread[2];
81 static cyg_handle_t pt0,pt1;
83 static cyg_mutex_t mx;
88 static cyg_handle_t mbh;
90 volatile static int thread0_state = 0;
91 volatile static int thread1_state = 0;
93 //==========================================================================
95 static void entry0( cyg_addrword_t data )
97 CHECK( 222 == (int)data );
99 // Do everything with the scheduler locked.
100 cyg_scheduler_lock();
102 // --------------------------------------------------
105 cyg_mutex_lock( &mx );
108 // Get thread 2 running.
109 cyg_thread_resume(pt1);
112 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
113 cyg_cond_wait( &cv );
116 while( thread1_state < 2 ) cyg_thread_yield();
118 cyg_cond_broadcast( &cv );
122 cyg_mutex_unlock( &mx );
126 // --------------------------------------------------
129 cyg_semaphore_wait( &sem );
132 cyg_semaphore_post( &sem );
135 while( thread1_state < 7 ) cyg_thread_yield();
137 // --------------------------------------------------
140 cyg_flag_wait( &fl, 1, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR);
143 cyg_flag_setbits( &fl, 2 );
146 // --------------------------------------------------
149 #ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
153 mbret = cyg_mbox_get( mbh );
154 CYG_TEST_CHECK( mbret == (void *)0xAAAAAAAA , "bad result from cyg_mbox_timed_get()");
157 while( thread1_state < 10 ) cyg_thread_yield();
159 cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
163 // --------------------------------------------------
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");
176 //==========================================================================
178 static void entry1( cyg_addrword_t data )
182 CHECK( 333 == (int)data );
184 // Do everything with the scheduler locked.
185 cyg_scheduler_lock();
187 // --------------------------------------------------
189 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
190 cyg_mutex_lock( &mx );
193 while( thread0_state < 2 ) cyg_thread_yield();
195 cyg_cond_signal( &cv );
198 res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
199 CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
202 cyg_mutex_unlock( &mx );
206 // --------------------------------------------------
209 while( thread0_state < 5 ) cyg_thread_yield();
211 cyg_semaphore_post( &sem );
214 while( thread0_state < 6 ) cyg_thread_yield();
217 #ifdef CYGFUN_KERNEL_THREADS_TIMER
218 res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
220 res = cyg_semaphore_wait( &sem );
222 CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
225 // --------------------------------------------------
228 cyg_flag_setbits( &fl, 1 );
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 );
235 cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
239 // --------------------------------------------------
241 #ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
244 #ifdef CYGFUN_KERNEL_THREADS_TIMER
245 cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
247 cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
251 while( thread0_state < 10 ) cyg_thread_yield();
253 #ifdef CYGFUN_KERNEL_THREADS_TIMER
254 mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
256 mbret = cyg_mbox_get( mbh );
259 CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
263 // --------------------------------------------------
269 //==========================================================================
271 void kthread1_main( void )
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] );
280 // Init all the objects
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 );
288 cyg_thread_resume(pt0);
290 cyg_scheduler_start();
292 CYG_TEST_FAIL_FINISH("Not reached");
295 //==========================================================================
303 //==========================================================================
305 #else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
306 # define NA_MSG "Schedule has unique priorities"
309 #else /* def CYGFUN_KERNEL_API_C */
310 # define NA_MSG "Kernel C API layer disabled"
322 //==========================================================================