📄 kmutex4.c
字号:
//==========================================================================
//
// kmutex4.c
//
// Mutex test 4 - dynamic priority inheritance protocol
//
//==========================================================================
//####ECOSGPLCOPYRIGHTBEGIN####
// -------------------------------------------
// This file is part of eCos, the Embedded Configurable Operating System.
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
//
// 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
// 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
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with eCos; if not, write to the Free Software Foundation, Inc.,
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
//
// 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
// 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
// License. However the source code for this file must still be made available
// 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 file might be covered by the GNU General Public License.
//
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
// at http://sources.redhat.com/ecos/ecos-license/
// -------------------------------------------
//####ECOSGPLCOPYRIGHTEND####
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s): hmt
// Contributors: hmt
// Date: 2000-01-06, 2001-08-10, 2001-08-21
// Description: Tests mutex priority inheritance. This is an extension of
// kmutex3.c, to test the new "set the protocol at run-time"
// extensions.
//####DESCRIPTIONEND####
#include <pkgconf/hal.h>
#include <pkgconf/kernel.h>
#include <cyg/infra/testcase.h>
#include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
#include <cyg/infra/diag.h> // diag_printf
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
externC void
cyg_hal_invoke_constructors();
#endif
// ------------------------------------------------------------------------
//
// These checks should be enough; any other scheduler which has priorities
// should manifest as having no priority inheritance, but otherwise fine,
// so the test should work correctly.
#if defined(CYGVAR_KERNEL_COUNTERS_CLOCK) && \ (CYGNUM_KERNEL_SCHED_PRIORITIES > 20) && \ defined(CYGFUN_KERNEL_API_C) && \ !defined(CYGPKG_KERNEL_SMP_SUPPORT) && \ defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC) \
#include <cyg/kernel/kapi.h>
#include <cyg/infra/cyg_ass.h>
#include <cyg/infra/cyg_trac.h>
#include <cyg/infra/diag.h> // diag_printf
// ------------------------------------------------------------------------
#define nVERBOSE
// ------------------------------------------------------------------------
// We have dynamic protocol choice, so we can set the protocol to whatever
// we want. We'll do these combinations:
// NONE
// INHERIT
// CEILING = 4 = higher than any thread === INHERIT in behaviour
// CEILING = 11 = mixed in with threads === cannot check anything
// CEILING = 17 = lower than any threads === NONE in behaviour
#define PROTO_NONE (0)
#define PROTO_INHERIT (1)
#define PROTO_CEILING_HIGH (2)
#define PROTO_CEILING_MID (3)
#define PROTO_CEILING_LOW (4)
int proto;
static char * protnames[] = {
"none",
"inherit",
"high ceiling",
"medium ceiling",
"low ceiling",
};
// ------------------------------------------------------------------------
// Management functions
//
// Stolen from testaux.hxx and copied in here because I want to be able to
// reset the world also.
//
// Translated into KAPI also.
#define NTHREADS 7
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
static cyg_handle_t thread[NTHREADS] = { 0 };
typedef cyg_uint64 CYG_ALIGNMENT_TYPE;
static cyg_thread thread_obj[NTHREADS];
static CYG_ALIGNMENT_TYPE stack[NTHREADS] [
(STACKSIZE+sizeof(CYG_ALIGNMENT_TYPE)-1)
/ sizeof(CYG_ALIGNMENT_TYPE) ];
static volatile int nthreads = 0;
#undef NULL
#define NULL (0)
static cyg_handle_t new_thread( cyg_thread_entry_t *entry,
cyg_addrword_t data,
cyg_addrword_t priority,
int do_resume,
char *name )
{
int _nthreads = nthreads++;
CYG_ASSERT(_nthreads < NTHREADS,
"Attempt to create more than NTHREADS threads");
cyg_thread_create( priority,
entry,
data,
name,
(void *)(stack[_nthreads]),
STACKSIZE,
&thread[_nthreads],
&thread_obj[_nthreads] );
if ( do_resume )
cyg_thread_resume( thread[_nthreads] );
return thread[_nthreads];
}
static void kill_threads( void )
{
CYG_ASSERT(nthreads <= NTHREADS,
"More than NTHREADS threads");
CYG_ASSERT( cyg_thread_self() == thread[0],
"kill_threads() not called from thread 0");
while ( nthreads > 1 ) {
nthreads--;
if ( NULL != thread[nthreads] ) {
do
cyg_thread_kill( thread[nthreads] );
while ( ! cyg_thread_delete ( thread[nthreads] ) );
thread[nthreads] = NULL;
}
}
CYG_ASSERT(nthreads == 1,
"No threads left");
}
// ------------------------------------------------------------------------
#define DELAYFACTOR 1 // for debugging
// ------------------------------------------------------------------------
static cyg_mutex_t mutex_obj;
static cyg_mutex_t *mutex;
// These are for reporting back to the master thread
volatile int got_it = 0;
volatile int t3ran = 0;
volatile int t3ended = 0;
volatile int extras[4] = {0,0,0,0};
volatile int go_flag = 0; // but this one controls thread 3 from thread 2
// ------------------------------------------------------------------------
// 0 to 3 of these run generally to interfere with the other processing,
// to cause multiple prio inheritances, and clashes in any orders.
static void extra_thread( cyg_addrword_t data )
{
cyg_handle_t self = cyg_thread_self();
#ifdef VERBOSE
#define xXINFO( z ) \ do { z[13] = '0' + data; CYG_TEST_INFO( z ); } while ( 0 )
static char running[] = "Extra thread Xa running";
static char exiting[] = "Extra thread Xa exiting";
static char resumed[] = "Extra thread Xa resumed";
static char locked[] = "Extra thread Xa locked";
static char unlocked[] = "Extra thread Xa unlocked";
#else
#define XINFO( z ) /* nothing */
#endif
XINFO( running );
cyg_thread_suspend( self );
XINFO( resumed );
cyg_mutex_lock( mutex );
XINFO( locked );
cyg_mutex_unlock( mutex );
XINFO( unlocked );
extras[ data ] ++;
XINFO( exiting );
}
// ------------------------------------------------------------------------
static void t1( cyg_addrword_t data )
{
cyg_handle_t self = cyg_thread_self();
#ifdef VERBOSE
CYG_TEST_INFO( "Thread 1 running" );
#endif
cyg_thread_suspend( self );
cyg_mutex_lock( mutex );
got_it++;
CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,1]" );
cyg_mutex_unlock( mutex );
CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,2]" );
// That's all.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -