/*=================================================================
|
/*=================================================================
|
//
|
//
|
// klock.c
|
// klock.c
|
//
|
//
|
// Kernel lock test
|
// Kernel lock test
|
//
|
//
|
//==========================================================================
|
//==========================================================================
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
//####ECOSGPLCOPYRIGHTBEGIN####
|
// -------------------------------------------
|
// -------------------------------------------
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
|
//
|
//
|
// eCos is free software; you can redistribute it and/or modify it under
|
// eCos is free software; you can redistribute it and/or modify it under
|
// the terms of the GNU General Public License as published by the Free
|
// the terms of the GNU General Public License as published by the Free
|
// Software Foundation; either version 2 or (at your option) any later version.
|
// Software Foundation; either version 2 or (at your option) any later version.
|
//
|
//
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
// for more details.
|
// for more details.
|
//
|
//
|
// You should have received a copy of the GNU General Public License along
|
// You should have received a copy of the GNU General Public License along
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// with eCos; if not, write to the Free Software Foundation, Inc.,
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
|
//
|
//
|
// As a special exception, if other files instantiate templates or use macros
|
// As a special exception, if other files instantiate templates or use macros
|
// or inline functions from this file, or you compile this file and link it
|
// or inline functions from this file, or you compile this file and link it
|
// with other works to produce a work based on this file, this file does not
|
// with other works to produce a work based on this file, this file does not
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// by itself cause the resulting work to be covered by the GNU General Public
|
// License. However the source code for this file must still be made available
|
// License. However the source code for this file must still be made available
|
// in accordance with section (3) of the GNU General Public License.
|
// in accordance with section (3) of the GNU General Public License.
|
//
|
//
|
// This exception does not invalidate any other reasons why a work based on
|
// This exception does not invalidate any other reasons why a work based on
|
// this file might be covered by the GNU General Public License.
|
// this file might be covered by the GNU General Public License.
|
//
|
//
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
|
// at http://sources.redhat.com/ecos/ecos-license/
|
// at http://sources.redhat.com/ecos/ecos-license/
|
// -------------------------------------------
|
// -------------------------------------------
|
//####ECOSGPLCOPYRIGHTEND####
|
//####ECOSGPLCOPYRIGHTEND####
|
//==========================================================================
|
//==========================================================================
|
//#####DESCRIPTIONBEGIN####
|
//#####DESCRIPTIONBEGIN####
|
//
|
//
|
// Author(s): dsm
|
// Author(s): dsm
|
// Contributors: dsm
|
// Contributors: dsm
|
// Date: 1998-03-18
|
// Date: 1998-03-18
|
// Description: Tests some basic thread functions.
|
// Description: Tests some basic thread functions.
|
//####DESCRIPTIONEND####
|
//####DESCRIPTIONEND####
|
*/
|
*/
|
//==========================================================================
|
//==========================================================================
|
|
|
#include <cyg/kernel/kapi.h>
|
#include <cyg/kernel/kapi.h>
|
|
|
#include <cyg/infra/testcase.h>
|
#include <cyg/infra/testcase.h>
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#ifdef CYGFUN_KERNEL_API_C
|
#ifdef CYGFUN_KERNEL_API_C
|
|
|
#if (CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0)
|
#if (CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0)
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#include "testaux.h"
|
#include "testaux.h"
|
|
|
#include <cyg/hal/hal_arch.h> // for CYGNUM_HAL_STACK_SIZE_TYPICAL
|
#include <cyg/hal/hal_arch.h> // for CYGNUM_HAL_STACK_SIZE_TYPICAL
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
|
#ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL
|
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
|
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
|
#else
|
#else
|
#define STACKSIZE 2000
|
#define STACKSIZE 2000
|
#endif
|
#endif
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
static char stack[2][STACKSIZE];
|
static char stack[2][STACKSIZE];
|
|
|
static cyg_thread thread[2];
|
static cyg_thread thread[2];
|
|
|
static cyg_handle_t pt0,pt1;
|
static cyg_handle_t pt0,pt1;
|
|
|
static cyg_mutex_t mx;
|
static cyg_mutex_t mx;
|
static cyg_cond_t cv;
|
static cyg_cond_t cv;
|
static cyg_sem_t sem;
|
static cyg_sem_t sem;
|
static cyg_flag_t fl;
|
static cyg_flag_t fl;
|
static cyg_mbox mbox;
|
static cyg_mbox mbox;
|
static cyg_handle_t mbh;
|
static cyg_handle_t mbh;
|
|
|
volatile static int thread0_state = 0;
|
volatile static int thread0_state = 0;
|
volatile static int thread1_state = 0;
|
volatile static int thread1_state = 0;
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
static void entry0( cyg_addrword_t data )
|
static void entry0( cyg_addrword_t data )
|
{
|
{
|
CHECK( 222 == (int)data );
|
CHECK( 222 == (int)data );
|
|
|
// Do everything with the scheduler locked.
|
// Do everything with the scheduler locked.
|
cyg_scheduler_lock();
|
cyg_scheduler_lock();
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Mutex test
|
// Mutex test
|
|
|
cyg_mutex_lock( &mx );
|
cyg_mutex_lock( &mx );
|
thread0_state = 1;
|
thread0_state = 1;
|
|
|
// Get thread 2 running.
|
// Get thread 2 running.
|
cyg_thread_resume(pt1);
|
cyg_thread_resume(pt1);
|
thread0_state = 2;
|
thread0_state = 2;
|
|
|
#ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
|
#ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
|
cyg_cond_wait( &cv );
|
cyg_cond_wait( &cv );
|
thread0_state = 3;
|
thread0_state = 3;
|
|
|
while( thread1_state < 2 ) cyg_thread_yield();
|
while( thread1_state < 2 ) cyg_thread_yield();
|
|
|
cyg_cond_broadcast( &cv );
|
cyg_cond_broadcast( &cv );
|
thread0_state = 4;
|
thread0_state = 4;
|
#endif
|
#endif
|
|
|
cyg_mutex_unlock( &mx );
|
cyg_mutex_unlock( &mx );
|
thread0_state = 5;
|
thread0_state = 5;
|
|
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Semaphore test
|
// Semaphore test
|
|
|
cyg_semaphore_wait( &sem );
|
cyg_semaphore_wait( &sem );
|
thread0_state = 6;
|
thread0_state = 6;
|
|
|
cyg_semaphore_post( &sem );
|
cyg_semaphore_post( &sem );
|
thread0_state = 7;
|
thread0_state = 7;
|
|
|
while( thread1_state < 7 ) cyg_thread_yield();
|
while( thread1_state < 7 ) cyg_thread_yield();
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Flags test
|
// Flags test
|
|
|
cyg_flag_wait( &fl, 1, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR);
|
cyg_flag_wait( &fl, 1, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR);
|
thread0_state = 8;
|
thread0_state = 8;
|
|
|
cyg_flag_setbits( &fl, 2 );
|
cyg_flag_setbits( &fl, 2 );
|
thread0_state = 9;
|
thread0_state = 9;
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Message box test
|
// Message box test
|
|
|
#ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
|
#ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
|
{
|
{
|
void *mbret;
|
void *mbret;
|
|
|
mbret = cyg_mbox_get( mbh );
|
mbret = cyg_mbox_get( mbh );
|
CYG_TEST_CHECK( mbret == (void *)0xAAAAAAAA , "bad result from cyg_mbox_timed_get()");
|
CYG_TEST_CHECK( mbret == (void *)0xAAAAAAAA , "bad result from cyg_mbox_timed_get()");
|
thread0_state = 10;
|
thread0_state = 10;
|
|
|
cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
|
cyg_mbox_put( mbh, (void *)0xBBBBBBBB );
|
thread0_state = 11;
|
thread0_state = 11;
|
}
|
}
|
#endif
|
#endif
|
// --------------------------------------------------
|
// --------------------------------------------------
|
|
|
thread0_state = 999;
|
thread0_state = 999;
|
|
|
cyg_thread_yield();
|
cyg_thread_yield();
|
cyg_thread_yield();
|
cyg_thread_yield();
|
cyg_thread_yield();
|
cyg_thread_yield();
|
|
|
CYG_TEST_CHECK( thread0_state == 999, "thread 0 not in exit state");
|
CYG_TEST_CHECK( thread0_state == 999, "thread 0 not in exit state");
|
CYG_TEST_CHECK( thread1_state == 999, "thread 1 not in exit state");
|
CYG_TEST_CHECK( thread1_state == 999, "thread 1 not in exit state");
|
CYG_TEST_PASS_FINISH("Kernel lock test OK");
|
CYG_TEST_PASS_FINISH("Kernel lock test OK");
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
static void entry1( cyg_addrword_t data )
|
static void entry1( cyg_addrword_t data )
|
{
|
{
|
cyg_bool res;
|
cyg_bool res;
|
|
|
CHECK( 333 == (int)data );
|
CHECK( 333 == (int)data );
|
|
|
// Do everything with the scheduler locked.
|
// Do everything with the scheduler locked.
|
cyg_scheduler_lock();
|
cyg_scheduler_lock();
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Mutex test
|
// Mutex test
|
#ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
|
#ifdef CYGMFN_KERNEL_SYNCH_CONDVAR_TIMED_WAIT
|
cyg_mutex_lock( &mx );
|
cyg_mutex_lock( &mx );
|
thread1_state = 1;
|
thread1_state = 1;
|
|
|
while( thread0_state < 2 ) cyg_thread_yield();
|
while( thread0_state < 2 ) cyg_thread_yield();
|
|
|
cyg_cond_signal( &cv );
|
cyg_cond_signal( &cv );
|
thread1_state = 2;
|
thread1_state = 2;
|
|
|
res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
|
res = cyg_cond_timed_wait( &cv, cyg_current_time()+10 );
|
CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
|
CYG_TEST_CHECK( res , "FALSE result from cyg_cond_timed_wait()" );
|
thread1_state = 3;
|
thread1_state = 3;
|
|
|
cyg_mutex_unlock( &mx );
|
cyg_mutex_unlock( &mx );
|
thread1_state = 4;
|
thread1_state = 4;
|
#endif
|
#endif
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Semaphore test
|
// Semaphore test
|
|
|
while( thread0_state < 5 ) cyg_thread_yield();
|
while( thread0_state < 5 ) cyg_thread_yield();
|
|
|
cyg_semaphore_post( &sem );
|
cyg_semaphore_post( &sem );
|
thread1_state = 5;
|
thread1_state = 5;
|
|
|
while( thread0_state < 6 ) cyg_thread_yield();
|
while( thread0_state < 6 ) cyg_thread_yield();
|
thread1_state = 6;
|
thread1_state = 6;
|
|
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
|
res = cyg_semaphore_timed_wait( &sem, cyg_current_time()+10 );
|
#else
|
#else
|
res = cyg_semaphore_wait( &sem );
|
res = cyg_semaphore_wait( &sem );
|
#endif
|
#endif
|
CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
|
CYG_TEST_CHECK( res , "FALSE result from cyg_semaphore[_timed]_wait()" );
|
thread1_state = 7;
|
thread1_state = 7;
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Flags test
|
// Flags test
|
|
|
cyg_flag_setbits( &fl, 1 );
|
cyg_flag_setbits( &fl, 1 );
|
thread1_state = 8;
|
thread1_state = 8;
|
|
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
cyg_flag_timed_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR,
|
cyg_flag_timed_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR,
|
cyg_current_time()+10 );
|
cyg_current_time()+10 );
|
#else
|
#else
|
cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
|
cyg_flag_wait( &fl, 2, CYG_FLAG_WAITMODE_OR|CYG_FLAG_WAITMODE_CLR );
|
#endif
|
#endif
|
thread1_state = 9;
|
thread1_state = 9;
|
|
|
// --------------------------------------------------
|
// --------------------------------------------------
|
// Message box test
|
// Message box test
|
#ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
|
#ifdef CYGMFN_KERNEL_SYNCH_MBOXT_PUT_CAN_WAIT
|
{
|
{
|
void *mbret;
|
void *mbret;
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
|
cyg_mbox_timed_put( mbh, (void *)0xAAAAAAAA, cyg_current_time()+10 );
|
#else
|
#else
|
cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
|
cyg_mbox_put( mbh, (void *)0xAAAAAAAA );
|
#endif
|
#endif
|
thread1_state = 10;
|
thread1_state = 10;
|
|
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
#ifdef CYGFUN_KERNEL_THREADS_TIMER
|
mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
|
mbret = cyg_mbox_timed_get( mbh, cyg_current_time()+10);
|
#else
|
#else
|
mbret = cyg_mbox_get( mbh );
|
mbret = cyg_mbox_get( mbh );
|
#endif
|
#endif
|
CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
|
CYG_TEST_CHECK( mbret == (void *)0xBBBBBBBB , "bad result from cyg_mbox[_timed]_get()");
|
thread1_state = 9;
|
thread1_state = 9;
|
}
|
}
|
#endif
|
#endif
|
// --------------------------------------------------
|
// --------------------------------------------------
|
|
|
thread1_state = 999;
|
thread1_state = 999;
|
cyg_thread_exit();
|
cyg_thread_exit();
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
void kthread1_main( void )
|
void kthread1_main( void )
|
{
|
{
|
CYG_TEST_INIT();
|
CYG_TEST_INIT();
|
|
|
cyg_thread_create(4, entry0, (cyg_addrword_t)222, "kthread1-0",
|
cyg_thread_create(4, entry0, (cyg_addrword_t)222, "kthread1-0",
|
(void *)stack[0], STACKSIZE, &pt0, &thread[0] );
|
(void *)stack[0], STACKSIZE, &pt0, &thread[0] );
|
cyg_thread_create(4, entry1, (cyg_addrword_t)333, "kthread1-1",
|
cyg_thread_create(4, entry1, (cyg_addrword_t)333, "kthread1-1",
|
(void *)stack[1], STACKSIZE, &pt1, &thread[1] );
|
(void *)stack[1], STACKSIZE, &pt1, &thread[1] );
|
|
|
// Init all the objects
|
// Init all the objects
|
|
|
cyg_mutex_init( &mx );
|
cyg_mutex_init( &mx );
|
cyg_cond_init( &cv, &mx );
|
cyg_cond_init( &cv, &mx );
|
cyg_semaphore_init( &sem, 0 );
|
cyg_semaphore_init( &sem, 0 );
|
cyg_flag_init( &fl );
|
cyg_flag_init( &fl );
|
cyg_mbox_create( &mbh, &mbox );
|
cyg_mbox_create( &mbh, &mbox );
|
|
|
cyg_thread_resume(pt0);
|
cyg_thread_resume(pt0);
|
|
|
cyg_scheduler_start();
|
cyg_scheduler_start();
|
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
CYG_TEST_FAIL_FINISH("Not reached");
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
externC void
|
externC void
|
cyg_start( void )
|
cyg_start( void )
|
{
|
{
|
kthread1_main();
|
kthread1_main();
|
}
|
}
|
|
|
//==========================================================================
|
//==========================================================================
|
|
|
#else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
|
#else /* CYGINT_KERNEL_SCHEDULER_UNIQUE_PRIORITIES == 0 */
|
# define NA_MSG "Schedule has unique priorities"
|
# define NA_MSG "Schedule has unique priorities"
|
#endif
|
#endif
|
|
|
#else /* def CYGFUN_KERNEL_API_C */
|
#else /* def CYGFUN_KERNEL_API_C */
|
# define NA_MSG "Kernel C API layer disabled"
|
# define NA_MSG "Kernel C API layer disabled"
|
#endif
|
#endif
|
|
|
#ifdef NA_MSG
|
#ifdef NA_MSG
|
externC void
|
externC void
|
cyg_start( void )
|
cyg_start( void )
|
{
|
{
|
CYG_TEST_INIT();
|
CYG_TEST_INIT();
|
CYG_TEST_NA(NA_MSG);
|
CYG_TEST_NA(NA_MSG);
|
}
|
}
|
#endif
|
#endif
|
|
|
//==========================================================================
|
//==========================================================================
|
/* EOF klock.c */
|
/* EOF klock.c */
|
|
|