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 cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
161 // --------------------------------------------------
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");
174 //==========================================================================
176 static void entry1( cyg_addrword_t data )
180 CHECK( 333 == (int)data );
182 // Do everything with the scheduler locked.
183 cyg_scheduler_lock();
185 // --------------------------------------------------
187 #ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
188 cyg_mutex_lock( &mx );
191 while( thread0_state < 2 ) cyg_thread_yield();
193 cyg_cond_signal( &cv );
196 res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
197 CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
200 cyg_mutex_unlock( &mx );
204 // --------------------------------------------------
207 while( thread0_state < 5 ) cyg_thread_yield();
209 cyg_semaphore_post( &sem );
212 while( thread0_state < 6 ) cyg_thread_yield();
215 #ifdef CYGFUN_KERNEL_THREADS_TIMER
216 res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
218 res = cyg_semaphore_wait( &sem );
220 CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
223 // --------------------------------------------------
226 cyg_flag_setbits( &fl, 1 );
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 );
233 cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
237 // --------------------------------------------------
239 #ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
242 #ifdef CYGFUN_KERNEL_THREADS_TIMER
243 cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
245 cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
249 #ifdef CYGFUN_KERNEL_THREADS_TIMER
250 mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
252 mbret = cyg_mbox_get( mbh );
254 CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
258 // --------------------------------------------------
264 //==========================================================================
266 void kthread1_main( void )
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] );
275 // Init all the objects
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 );
283 cyg_thread_resume(pt0);
285 cyg_scheduler_start();
287 CYG_TEST_FAIL_FINISH("Not reached");
290 //==========================================================================
298 //==========================================================================
300 #else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
301 # define NA_MSG "Schedule has unique priorities"
304 #else /* def CYGFUN_KERNEL_API_C */
305 # define NA_MSG "Kernel C API layer disabled"
317 //==========================================================================