📄 mutex3.cxx
字号:
//==========================================================================//// mutex3.cxx//// Mutex test 3 - priority inheritance////==========================================================================//####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// Description: Tests mutex priority inheritance//####DESCRIPTIONEND#####include <pkgconf/hal.h>#include <pkgconf/kernel.h>#include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start()#include <cyg/kernel/thread.hxx> // Cyg_Thread#include <cyg/kernel/mutex.hxx>#include <cyg/infra/testcase.h>#include <cyg/kernel/sched.inl>#include <cyg/kernel/thread.inl>#include <cyg/infra/diag.h> // diag_printf#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAGexternC voidcyg_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(CYGPKG_KERNEL_SMP_SUPPORT)// ------------------------------------------------------------------------// Manufacture a simpler feature test macro for priority inheritance than// the configuration gives us. We have priority inheritance if it is configured// as the only protocol, or if it is the default protocol for dynamic protocol// choice.// FIXME: If we have dynamic protocol choice, we can also set priority inheritance// as the protocol to be used on the mutexes we are interested in. At present we// do not do this.#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT# ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC# ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_INHERIT# define PRIORITY_INHERITANCE "dynamic-default-inherit"# endif# else# define PRIORITY_INHERITANCE "static-inherit"# endif#endif#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING# ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC# ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_CEILING# if CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY <= 5# define PRIORITY_INHERITANCE "dynamic-default-ceiling-high"# elif CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY >= 15# define NO_PRIORITY_INHERITANCE "dynamic-default-ceiling-low"# else# define PRIORITY_UNKNOWN "dynamic-default-ceiling-mid"# endif# endif# else# if CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY <= 5# define PRIORITY_INHERITANCE "static-ceiling-high"# elif CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY >= 15# define NO_PRIORITY_INHERITANCE "static-ceiling-low"# else# define PRIORITY_UNKNOWN "static-ceiling-mid"# endif# endif#endif#ifndef PRIORITY_INHERITANCE# ifndef NO_PRIORITY_INHERITANCE# define NO_PRIORITY_INHERITANCE "no scheme selected"# endif#endif// ------------------------------------------------------------------------// Management functions//// Stolen from testaux.hxx and copied in here because I want to be able to// reset the world also.#define NTHREADS 7static inline void *operator new(size_t size, void *ptr) { return ptr; };#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICALstatic Cyg_Thread *thread[NTHREADS] = { 0 };typedef CYG_WORD64 CYG_ALIGNMENT_TYPE;static CYG_ALIGNMENT_TYPE thread_obj[NTHREADS] [ (sizeof(Cyg_Thread)+sizeof(CYG_ALIGNMENT_TYPE)-1) / sizeof(CYG_ALIGNMENT_TYPE) ];static CYG_ALIGNMENT_TYPE stack[NTHREADS] [ (STACKSIZE+sizeof(CYG_ALIGNMENT_TYPE)-1) / sizeof(CYG_ALIGNMENT_TYPE) ];static volatile int nthreads = 0;static Cyg_Thread *new_thread( cyg_thread_entry *entry, CYG_ADDRWORD data, CYG_ADDRWORD priority, int do_resume ){ int _nthreads = nthreads++; CYG_ASSERT(_nthreads < NTHREADS, "Attempt to create more than NTHREADS threads"); thread[_nthreads] = new( (void *)&thread_obj[_nthreads] ) Cyg_Thread(priority, entry, data, NULL, // no name (CYG_ADDRESS)stack[_nthreads], STACKSIZE ); if ( do_resume ) thread[_nthreads]->resume(); 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] ) { thread[nthreads]->kill(); thread[nthreads]->~Cyg_Thread(); thread[nthreads] = NULL; } } CYG_ASSERT(nthreads == 1, "No threads left");}// ------------------------------------------------------------------------#define DELAYFACTOR 1 // for debugging// ------------------------------------------------------------------------static Cyg_Mutex mutex;// These are for reporting back to the master threadvolatile 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 data ){#define XINFO( 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"; XINFO( running ); Cyg_Thread *self = Cyg_Thread::self(); self->suspend(); XINFO( resumed ); mutex.lock(); XINFO( locked ); mutex.unlock(); XINFO( unlocked ); extras[ data ] ++; XINFO( exiting );}// ------------------------------------------------------------------------static void t1( CYG_ADDRWORD data ){ Cyg_Thread *self = Cyg_Thread::self(); CYG_TEST_INFO( "Thread 1 running" ); self->suspend(); mutex.lock(); got_it++; CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,1]" ); mutex.unlock(); CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,2]" ); // That's all. CYG_TEST_INFO( "Thread 1 exit" );}// ------------------------------------------------------------------------static void t2( CYG_ADDRWORD data ){ Cyg_Thread *self = Cyg_Thread::self(); int i; cyg_tick_count then, now; CYG_TEST_INFO( "Thread 2 running" ); CYG_TEST_CHECK( 0 == (data & ~0x77), "Bad T2 arg: extra bits" ); CYG_TEST_CHECK( 0 == (data & (data >> 4)), "Bad T2 arg: overlap" ); self->suspend(); // depending on our config argument, optionally restart some of the // extra threads to throw noise into the scheduler: for ( i = 0; i < 3; i++ ) if ( (1 << i) & data ) // bits 0-2 control thread[i+4]->resume(); // made sure extras are thread[4-6] self->delay( DELAYFACTOR * 10 ); // let those threads run Cyg_Scheduler::lock(); // do this next lot atomically go_flag = 1; // unleash thread 3 thread[1]->resume(); // resume thread 1 // depending on our config argument, optionally restart some of the // extra threads to throw noise into the scheduler at this later point: for ( i = 4; i < 7; i++ ) if ( (1 << i) & data ) // bits 4-6 control thread[i]->resume(); // made sure extras are thread[4-6] Cyg_Scheduler::unlock(); // let scheduling proceed // Need a delay (but not a CPU yield) to allow t3 to awaken and act on // the go_flag, otherwise we check these details below too soon. // Actually, waiting for the clock to tick a couple of times would be // better, so that is what we will do. Must be a busy-wait. then = Cyg_Clock::real_time_clock->current_value(); do { now = Cyg_Clock::real_time_clock->current_value(); // Wait longer than the delay in t3 waiting on go_flag } while ( now < (then + 3) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -