changeset 181:d61da071934c

Merge from eCos master repository on 2001-08-22-06:38:55-BST
author jlarmour
date Wed, 22 Aug 2001 06:27:14 +0000
parents 2bdd82a025e1
children f62680ef1804
files packages/devs/eth/smsc/lan91cxx/current/ChangeLog packages/devs/eth/smsc/lan91cxx/current/src/if_lan91cxx.c packages/hal/arm/arch/current/ChangeLog packages/hal/arm/arch/current/include/hal_intr.h packages/hal/arm/sa11x0/var/current/ChangeLog packages/hal/arm/sa11x0/var/current/include/hal_var_ints.h packages/hal/arm/sa11x0/var/current/src/sa11x0_misc.c packages/io/eth/current/ChangeLog packages/kernel/current/ChangeLog packages/kernel/current/cdl/kernel.cdl packages/kernel/current/include/kapi.h packages/kernel/current/include/kapidata.h packages/kernel/current/src/sync/mutex.cxx packages/kernel/current/tests/kmutex3.c packages/kernel/current/tests/kmutex4.c packages/language/c/libc/string/current/ChangeLog packages/language/c/libc/string/current/cdl/string.cdl packages/templates/all/ChangeLog packages/templates/all/current.ect packages/templates/elix/ChangeLog packages/templates/elix/current.ect packages/templates/net/ChangeLog packages/templates/net/current.ect packages/templates/posix/ChangeLog packages/templates/posix/current.ect
diffstat 25 files changed, 883 insertions(+), 148 deletions(-) [+]
line wrap: on
line diff
--- a/packages/devs/eth/smsc/lan91cxx/current/ChangeLog
+++ b/packages/devs/eth/smsc/lan91cxx/current/ChangeLog
@@ -1,3 +1,12 @@
+2001-08-17  Hugo Tyson  <hmt@redhat.com>
+
+	* src/if_lan91cxx.c (lan91cxx_poll): The interrupt acknowledge
+	call only occurs in the ISR for this driver because the interrupt
+	via GPIO is edge triggered.  We now also acknowledge the interrupt
+	within the poll() routine - otherwise RedBoot net use never acks!
+	Which doesn't matter if the app uses the net, but in a net-free
+	app, it near enough wedges in the resulting interrupt loop.
+
 2001-08-13  Hugo Tyson  <hmt@redhat.com>
 
 	* src/smsc_lan91cxx.h (get_att,put_att): Condition out the inline
--- a/packages/devs/eth/smsc/lan91cxx/current/src/if_lan91cxx.c
+++ b/packages/devs/eth/smsc/lan91cxx/current/src/if_lan91cxx.c
@@ -1019,9 +1019,12 @@ static void
 lan91cxx_poll(struct eth_drv_sc *sc)
 {
     unsigned short event;
+    struct lan91cxx_priv_data *cpd = 
+        (struct lan91cxx_priv_data *)sc->driver_private;
 
     DEBUG_FUNCTION();
     while (1) {
+        cyg_drv_interrupt_acknowledge(cpd->interrupt);
         // Get the (unmasked) requests
         event = get_reg(sc, LAN91CXX_INTERRUPT);
         event = event & (event >> 8) & 0xff;
--- a/packages/hal/arm/arch/current/ChangeLog
+++ b/packages/hal/arm/arch/current/ChangeLog
@@ -1,3 +1,9 @@
+2001-08-21  Hugo Tyson  <hmt@redhat.com>
+
+	* include/hal_intr.h: Only define HAL_CLOCK_LATENCY() if it's not
+	defined already (if it's needed at all).  This is for platforms
+	which need a separate routine, such as sa11x0.
+
 2001-07-19  Gary Thomas  <gthomas@redhat.com>
 
 	* src/vectors.S: Support ROMRAM startup mode.  Note: most of
--- a/packages/hal/arm/arch/current/include/hal_intr.h
+++ b/packages/hal/arm/arch/current/include/hal_intr.h
@@ -392,7 +392,9 @@ externC void hal_clock_reset(cyg_uint32,
 #define HAL_CLOCK_RESET( _vec_, _period_ ) hal_clock_reset( _vec_, _period_ )
 #define HAL_CLOCK_READ( _pvalue_ )         hal_clock_read( _pvalue_ )
 #ifdef CYGVAR_KERNEL_COUNTERS_CLOCK_LATENCY
-#define HAL_CLOCK_LATENCY( _pvalue_ )      HAL_CLOCK_READ( (cyg_uint32 *)_pvalue_ )
+# ifndef HAL_CLOCK_LATENCY
+#  define HAL_CLOCK_LATENCY( _pvalue_ )    HAL_CLOCK_READ( (cyg_uint32 *)_pvalue_ )
+# endif
 #endif
 
 //--------------------------------------------------------------------------
--- a/packages/hal/arm/sa11x0/var/current/ChangeLog
+++ b/packages/hal/arm/sa11x0/var/current/ChangeLog
@@ -1,3 +1,20 @@
+2001-08-21  Hugo Tyson  <hmt@redhat.com>
+
+	* include/hal_var_ints.h (HAL_CLOCK_LATENCY()): Define this if
+	it's needed, for the routine below.
+
+	* src/sa11x0_misc.c (hal_clock_latency): A neater fix for the
+	below change: use a separate routine for measuring latency, which
+	assumes it is called in the ISR, before the clock is reset.
+
+2001-08-20  Hugo Tyson  <hmt@redhat.com>
+
+	* src/sa11x0_misc.c (hal_clock_read): Make hal_clock_read() return
+	the true value (as expected by the kernel latency test) from the
+	last interrupt if we just had that interrupt and didn't yet reset
+	the clock.  Otherwise ISR latencies are reported as one tick (1cS,
+	10000uS) too large.
+
 2001-07-27  Gary Thomas  <gthomas@redhat.com>
 
 	* src/redboot_linux_exec.c: Fix copyright disclaimers to satisfy
--- a/packages/hal/arm/sa11x0/var/current/include/hal_var_ints.h
+++ b/packages/hal/arm/sa11x0/var/current/include/hal_var_ints.h
@@ -110,6 +110,12 @@
 // The vector used by the Real time clock
 #define CYGNUM_HAL_INTERRUPT_RTC        CYGNUM_HAL_INTERRUPT_TIMER0
 
+// SA11x0 method for reading clock interrupt latency
+#ifdef CYGVAR_KERNEL_COUNTERS_CLOCK_LATENCY
+externC void hal_clock_latency(cyg_uint32 *);
+# define HAL_CLOCK_LATENCY( _pvalue_ ) \
+         hal_clock_latency( (cyg_uint32 *)(_pvalue_) )
+#endif
 
 
 //----------------------------------------------------------------------------
--- a/packages/hal/arm/sa11x0/var/current/src/sa11x0_misc.c
+++ b/packages/hal/arm/sa11x0/var/current/src/sa11x0_misc.c
@@ -167,7 +167,18 @@ void hal_clock_read(cyg_uint32 *pvalue)
     HAL_DISABLE_INTERRUPTS(orig);
     *pvalue = clock_period + *SA11X0_OSCR - *SA11X0_OSMR0;
     HAL_RESTORE_INTERRUPTS(orig);
+}
 
+// This is to cope with the test read used by tm_basic with
+// CYGVAR_KERNEL_COUNTERS_CLOCK_LATENCY defined; we read the count ASAP
+// in the ISR, *before* resetting the clock.  Which returns 1tick +
+// latency if we just use plain hal_clock_read().
+void hal_clock_latency(cyg_uint32 *pvalue)
+{
+    int orig;
+    HAL_DISABLE_INTERRUPTS(orig);
+    *pvalue = *SA11X0_OSCR - *SA11X0_OSMR0;
+    HAL_RESTORE_INTERRUPTS(orig);
 }
 
 //
--- a/packages/io/eth/current/ChangeLog
+++ b/packages/io/eth/current/ChangeLog
@@ -1,3 +1,8 @@
+2001-08-20  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* src/net/eth_drv.c (eth_drv_send): Move endif location to fix build
+	error.
+
 2001-08-17  Jonathan Larmour  <jlarmour@redhat.com>
 
 	* cdl/eth_drivers.cdl (CYGSEM_IO_ETH_DRIVERS_DEBUG): Now booldata.
--- a/packages/kernel/current/ChangeLog
+++ b/packages/kernel/current/ChangeLog
@@ -1,3 +1,28 @@
+2001-08-21  Hugo Tyson  <hmt@redhat.com>
+
+	* src/sync/mutex.cxx (lock): Bugfix: a ceiling priority mutex
+	wasn't elevated until after it had acquired the mutex.  This meant
+	is slept with a low priority, and so did not run when awakened by
+	the release of the mutex.  The fix is to elevate the potential
+	claimant before it sleeps in contention. 
+
+	* tests/kmutex4.c: New testcase - very similar to kmutex3 but it
+	loops a load more times using different mutex priority protocols.
+	This checks that dynamically set protos do in fact behave
+	differently from one another and as they are each intended to.
+
+	* tests/kmutex3.c: Remove FIXME comments - we now test dynamic
+	protocol.
+
+	* cdl/kernel.cdl: Build the new test.
+
+2001-08-20  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* include/kapidata.h: Reorganize most struct type definitions into
+	macros to allow compatible layout with all G++ implementations that
+	align non-POD types differently from included C structures.
+	* include/kapi.h: Similarly for cyg_resolution_t.
+
 2001-08-17  Nick Garnett  <nickg@redhat.com>
 
 	* src/sched/mlqueue.cxx (timeslice): Fix timeslice_count comparison.
--- a/packages/kernel/current/cdl/kernel.cdl
+++ b/packages/kernel/current/cdl/kernel.cdl
@@ -318,14 +318,14 @@ cdl_package CYGPKG_KERNEL {
             flavor  data
             no_define
             calculated { 
-                CYGPKG_HAL_ARM_AEB ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone" : \
-                CYGPKG_HAL_ARM_EBSA285 ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
-                CYGPKG_HAL_ARM_SA11X0 ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/clocktruth tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
-                CYGPKG_HAL_ARM_EDB7XXX ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
-                CYGPKG_HAL_ARM_CMA230 ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone" : \
-                CYGPKG_HAL_ARM_E7T ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
-                CYGPKG_HAL_ARM ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/clocktruth tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone" : \
-                "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/clocktruth tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2 tests/smp" 
+                CYGPKG_HAL_ARM_AEB ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone" : \
+                CYGPKG_HAL_ARM_EBSA285 ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
+                CYGPKG_HAL_ARM_SA11X0 ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/clocktruth tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
+                CYGPKG_HAL_ARM_EDB7XXX ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
+                CYGPKG_HAL_ARM_CMA230 ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone" : \
+                CYGPKG_HAL_ARM_E7T ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2" : \
+                CYGPKG_HAL_ARM ? "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/clocktruth tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone" : \
+                "tests/bin_sem0 tests/bin_sem1 tests/bin_sem2 tests/clock0 tests/clock1 tests/clockcnv tests/clocktruth tests/cnt_sem0 tests/cnt_sem1 tests/except1 tests/flag0 tests/flag1 tests/intr0 tests/kclock0 tests/kclock1 tests/kexcept1 tests/kintr0 tests/kmbox1 tests/kmutex0 tests/kmutex1 tests/kmutex3 tests/kmutex4 tests/ksched1 tests/ksem0 tests/ksem1 tests/kflag0 tests/kflag1 tests/klock tests/kthread0 tests/kthread1 tests/mbox1 tests/mqueue1 tests/mutex0 tests/mutex1 tests/mutex2 tests/mutex3 tests/sched1 tests/sync2 tests/sync3 tests/thread0 tests/thread1 tests/thread2 tests/release tests/kill tests/thread_gdb tests/tm_basic tests/dhrystone tests/stress_threads tests/kcache1 tests/kcache2 tests/smp" 
             }
             description   "
                 This option specifies the set of tests for the eCos kernel."
--- a/packages/kernel/current/include/kapi.h
+++ b/packages/kernel/current/include/kapi.h
@@ -333,10 +333,13 @@ void cyg_counter_set_value(
 void cyg_counter_tick(cyg_handle_t counter);
 
 
+#define CYG_RESOLUTION_T_MEMBERS  \
+    cyg_uint32  dividend;         \
+    cyg_uint32  divisor;
+
 typedef struct 
 {
-    cyg_uint32  dividend;
-    cyg_uint32  divisor;
+    CYG_RESOLUTION_T_MEMBERS
 } cyg_resolution_t;
 
 /* Create a clock object                */
--- a/packages/kernel/current/include/kapidata.h
+++ b/packages/kernel/current/include/kapidata.h
@@ -127,190 +127,279 @@ struct cyg_interrupt
 
 /*---------------------------------------------------------------------------*/
 
-struct cyg_counter
-{
+
 #if defined(CYGIMP_KERNEL_COUNTERS_SINGLE_LIST)
+# define CYG_COUNTER_ALARM_LIST_MEMBER \
     cyg_alarm           *alarm_list;
 #elif defined(CYGIMP_KERNEL_COUNTERS_MULTI_LIST)
+# define CYG_COUNTER_ALARM_LIST_MEMBER \
     cyg_alarm           *alarm_list[CYGNUM_KERNEL_COUNTERS_MULTI_LIST_SIZE];
+#else
+# define CYG_COUNTER_ALARM_LIST_MEMBER
 #endif
-    cyg_tick_count_t    counter;
+
+#define CYG_COUNTER_MEMBERS              \
+    CYG_COUNTER_ALARM_LIST_MEMBER        \
+    cyg_tick_count_t    counter;         \
     cyg_uint32          increment;
+
+struct cyg_counter
+{
+    CYG_COUNTER_MEMBERS
 };
 
 /*---------------------------------------------------------------------------*/
 
 struct cyg_clock
 {
-    cyg_counter         counter;
-    cyg_resolution_t    resolution;
+    CYG_COUNTER_MEMBERS
+    CYG_RESOLUTION_T_MEMBERS
 };
 
 /*---------------------------------------------------------------------------*/
 
+
+#if defined(CYGIMP_KERNEL_COUNTERS_SINGLE_LIST) ||  \
+    defined(CYGIMP_KERNEL_COUNTERS_MULTI_LIST)      
+# define CYG_ALARM_LIST_MEMBERS                     \
+    cyg_alarm           *next;                      \
+    cyg_alarm           *prev;
+#else 
+# define CYG_ALARM_LIST_MEMBERS
+#endif
+
+#define CYG_ALARM_MEMBERS           \
+    CYG_ALARM_LIST_MEMBERS          \
+    cyg_counter         *counter;   \
+    cyg_alarm_t         *alarm;     \
+    CYG_ADDRWORD        data;       \
+    cyg_tick_count_t    trigger;    \
+    cyg_tick_count_t    interval;   \
+    cyg_bool            enabled;
+
 struct cyg_alarm
 {
-#if defined(CYGIMP_KERNEL_COUNTERS_SINGLE_LIST) || defined(CYGIMP_KERNEL_COUNTERS_MULTI_LIST)
-    cyg_alarm           *next;    
-    cyg_alarm           *prev;    
-#endif
-    cyg_counter         *counter;
-    cyg_alarm_t         *alarm;
-    CYG_ADDRWORD        data;
-    cyg_tick_count_t    trigger;
-    cyg_tick_count_t    interval;
-    cyg_bool            enabled;
+    CYG_ALARM_MEMBERS
 };
 
 /*---------------------------------------------------------------------------*/
 /* Exception controller                                                      */
 
 #ifdef CYGPKG_KERNEL_EXCEPTIONS
+
+# ifdef CYGSEM_KERNEL_EXCEPTIONS_DECODE
+#  define CYG_EXCEPTION_CONTROL_MEMBERS                                     \
+    cyg_exception_handler_t *exception_handler[CYGNUM_HAL_EXCEPTION_COUNT]; \
+    CYG_ADDRWORD            exception_data[CYGNUM_HAL_EXCEPTION_COUNT];     
+# else
+#  define CYG_EXCEPTION_CONTROL_MEMBERS                                \
+    cyg_exception_handler_t *exception_handler; /* Handler function */ \
+    CYG_ADDRWORD            exception_data;     /* Handler data */
+# endif
+
 typedef struct
 {
-#ifdef CYGSEM_KERNEL_EXCEPTIONS_DECODE
-    cyg_exception_handler_t *exception_handler[CYGNUM_HAL_EXCEPTION_COUNT];
-    
-    CYG_ADDRWORD            exception_data[CYGNUM_HAL_EXCEPTION_COUNT];
-#else
-    cyg_exception_handler_t *exception_handler; // Handler function
-    
-    CYG_ADDRWORD            exception_data;     // Handler data
-#endif
-    
+    CYG_EXCEPTION_CONTROL_MEMBERS    
 } cyg_exception_control;
 
 #endif
 
 /*---------------------------------------------------------------------------*/
-/* Thread structure                                                          */
+/* Hardware Thread structure                                                 */
+
+#ifdef CYGFUN_KERNEL_THREADS_STACK_LIMIT
+# define CYG_HARDWARETHREAD_STACK_LIMIT_MEMBER \
+    CYG_ADDRESS         stack_limit;    /* movable stack limit */
+#else
+# define CYG_HARDWARETHREAD_STACK_LIMIT_MEMBER
+#endif
+
+#ifdef CYGDBG_KERNEL_DEBUG_GDB_THREAD_SUPPORT
+# define CYG_HARDWARETHREAD_SAVED_CONTEXT_MEMBER \
+    void                *saved_context; // If non-zero, this points at a more
+                                        // interesting context than stack_ptr.
+#else
+# define CYG_HARDWARETHREAD_SAVED_CONTEXT_MEMBER
+#endif
+
+#define CYG_HARDWARETHREAD_MEMBERS                                           \
+    CYG_ADDRESS         stack_base;   /* pointer to base of stack area */    \
+    cyg_uint32          stack_size;   /* size of stack area in bytes */      \
+    CYG_HARDWARETHREAD_STACK_LIMIT_MEMBER                                    \
+    CYG_ADDRESS         stack_ptr;    /* pointer to saved state on stack */  \
+    CYG_ADDRWORD        entry_point;  /* main entry point (code pointer!) */ \
+    CYG_ADDRWORD        entry_data;   /* entry point argument */             \
+    CYG_HARDWARETHREAD_SAVED_CONTEXT_MEMBER
 
 typedef struct
 {
-    CYG_ADDRESS         stack_base;     /* pointer to base of stack area */
-    cyg_uint32          stack_size;     /* size of stack area in bytes */
-#ifdef CYGFUN_KERNEL_THREADS_STACK_LIMIT
-    CYG_ADDRESS         stack_limit;    /* movable stack limit */
+    CYG_HARDWARETHREAD_MEMBERS
+} cyg_hardwarethread;
+
+/*---------------------------------------------------------------------------*/
+/* Scheduler Thread structure                                                */
+
+#ifdef CYGPKG_KERNEL_SMP_SUPPORT
+# define CYG_SCHEDTHREAD_CPU_MEMBER \
+    cyg_uint32          cpu;            // CPU id of cpu currently running
+#else
+# define CYG_SCHEDTHREAD_CPU_MEMBER
+#endif
+
+#ifdef CYGSEM_KERNEL_SCHED_TIMESLICE_ENABLE
+# define CYG_SCHEDTHREAD_TIMESLICE_ENABLED_MEMBER \
+    cyg_bool            timeslice_enabled; /* per-thread timeslice enable */
+#else
+# define CYG_SCHEDTHREAD_TIMESLICE_ENABLED_MEMBER
+#endif
+
+#if defined(CYGSEM_KERNEL_SCHED_BITMAP)
+# define CYG_SCHEDTHREAD_SCHEDIMP_MEMBERS \
+    cyg_priority_t      priority;       /* current thread priority */
+#elif defined(CYGSEM_KERNEL_SCHED_MLQUEUE)
+# define CYG_SCHEDTHREAD_SCHEDIMP_MEMBERS                                    \
+    cyg_thread *next;                                                        \
+    cyg_thread *prev;                                                        \
+    cyg_priority_t      priority;             /* current thread priority */  \
+    CYG_SCHEDTHREAD_CPU_MEMBER                                               \
+    CYG_SCHEDTHREAD_TIMESLICE_ENABLED_MEMBER
+#elif defined(CYGSEM_KERNEL_SCHED_LOTTERY)
+# define CYG_SCHEDTHREAD_SCHEDIMP_MEMBERS                                    \
+    cyg_thread *next;                                                        \
+    cyg_thread *prev;                                                        \
+    cyg_priority_t      priority;             /* current thread priority */  \
+    cyg_priority_t      compensation_tickets; /* sleep compensation */
+#else
+# error Undefined scheduler type
 #endif    
-    CYG_ADDRESS         stack_ptr;      /* pointer to saved state on stack */
-    CYG_ADDRWORD        entry_point;    /* main entry point (code pointer!) */
-    CYG_ADDRWORD        entry_data;     /* entry point argument */
-#ifdef CYGDBG_KERNEL_DEBUG_GDB_THREAD_SUPPORT
-    void                *saved_context; // If non-zero, this points at a more
-                                        // interesting context than stack_ptr.
+
+#ifndef CYGSEM_KERNEL_SCHED_ASR_GLOBAL
+#  define CYG_SCHEDTHREAD_ASR_NONGLOBAL_MEMBER \
+    void              (*asr)(CYG_ADDRWORD);   // ASR function
+#else
+#  define CYG_SCHEDTHREAD_ASR_NONGLOBAL_MEMBER
+#endif
+
+#ifndef CYGSEM_KERNEL_SCHED_ASR_DATA_GLOBAL
+#  define CYG_SCHEDTHREAD_ASR_DATA_NONGLOBAL_MEMBER \
+    CYG_ADDRWORD        asr_data;       // ASR data pointer
+#else
+#  define CYG_SCHEDTHREAD_ASR_DATA_NONGLOBAL_MEMBER
 #endif
-    
-} cyg_hardwarethread;
+
+#ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
+# define CYG_SCHEDTHREAD_ASR_MEMBER                                       \
+    volatile cyg_bool   asr_inhibit; /* If true, blocks calls to ASRs */  \
+    volatile cyg_bool   asr_pending; /* If true, this thread's ASR    */  \
+                                     /* should be called. */              \
+    CYG_SCHEDTHREAD_ASR_NONGLOBAL_MEMBER                                  \
+    CYG_SCHEDTHREAD_ASR_DATA_NONGLOBAL_MEMBER                             
+#else
+# define CYG_SCHEDTHREAD_ASR_MEMBER
+#endif
+
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE
+# define CYG_SCHEDTHREAD_MUTEX_INV_PROTO_SIMPLE_MEMBERS \
+    cyg_priority_t      original_priority;              \
+    cyg_bool            priority_inherited;
+#else
+# define CYG_SCHEDTHREAD_MUTEX_INV_PROTO_SIMPLE_MEMBERS
+#endif
+
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
+# define CYG_SCHEDTHREAD_MUTEX_INV_PROTO_MEMBERS   \
+    cyg_count32         mutex_count;               \
+    CYG_SCHEDTHREAD_MUTEX_INV_PROTO_SIMPLE_MEMBERS
+#else
+# define CYG_SCHEDTHREAD_MUTEX_INV_PROTO_MEMBERS
+#endif
+
+#define CYG_SCHEDTHREAD_MEMBERS               \
+    CYG_SCHEDTHREAD_SCHEDIMP_MEMBERS          \
+    cyg_threadqueue     *queue;               \
+    CYG_SCHEDTHREAD_ASR_MEMBER                \
+    CYG_SCHEDTHREAD_MUTEX_INV_PROTO_MEMBERS
+
     
 typedef struct 
 {
-#if defined(CYGSEM_KERNEL_SCHED_BITMAP)
-
-    cyg_priority_t      priority;       /* current thread priority */
-    
-#elif defined(CYGSEM_KERNEL_SCHED_MLQUEUE)
-
-    cyg_thread *next;
-    cyg_thread *prev;
-
-    cyg_priority_t      priority;       /* current thread priority */
-
-#ifdef CYGPKG_KERNEL_SMP_SUPPORT
-    cyg_uint32          cpu;            // CPU id of cpu currently running
-#endif
-    
-#ifdef CYGSEM_KERNEL_SCHED_TIMESLICE_ENABLE
-    cyg_bool            timeslice_enabled; /* per-thread timeslice enable */
-#endif    
-#elif defined(CYGSEM_KERNEL_SCHED_LOTTERY)
-
-    cyg_thread *next;
-    cyg_thread *prev;
-
-    cyg_priority_t      priority;       /* current thread priority */
+    CYG_SCHEDTHREAD_MEMBERS
+} cyg_schedthread;
 
-    cyg_priority_t      compensation_tickets;   /* sleep compensation */
-    
-#else
-
-#error Undefined scheduler type
-    
-#endif    
-
-    cyg_threadqueue     *queue;
+#define CYG_THREADTIMER_MEMBERS \
+    CYG_ALARM_MEMBERS           \
+    cyg_thread          *thread;
 
-#ifdef CYGSEM_KERNEL_SCHED_ASR_SUPPORT
-    volatile cyg_bool   asr_inhibit;    // If true, blocks calls to ASRs
-    volatile cyg_bool   asr_pending;    // If true, this thread's ASR should be called.
-#ifndef CYGSEM_KERNEL_SCHED_ASR_GLOBAL
-    void              (*asr)(CYG_ADDRWORD);   // ASR function
-#endif    
-#ifndef CYGSEM_KERNEL_SCHED_ASR_DATA_GLOBAL
-    CYG_ADDRWORD        asr_data;       // ASR data pointer
-#endif    
-#endif    
-#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
-    cyg_count32         mutex_count;
-#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE
-    cyg_priority_t      original_priority;
-    cyg_bool            priority_inherited;
-#endif
-#endif
-
-} cyg_schedthread;
+/*---------------------------------------------------------------------------*/
+/* Thread structure                                                          */
 
 typedef struct 
 {
-    cyg_alarm           alarm;
-    cyg_thread          *thread;
+    CYG_THREADTIMER_MEMBERS
 } cyg_threadtimer;
 
 
 typedef int cyg_reason_t; /* cyg_reason is originally an enum */
 
-struct cyg_thread
-{
-    cyg_hardwarethread  hwthread;
-    cyg_schedthread     schedthread;
-
-    cyg_uint32                  state;      
-    cyg_ucount32                suspend_count;
-    cyg_ucount32                wakeup_count;
-    CYG_ADDRWORD                wait_info;
-    cyg_uint16                  unique_id;
-    
-
 #if defined(CYGPKG_KERNEL_EXCEPTIONS) && !defined(CYGSEM_KERNEL_EXCEPTIONS_GLOBAL)
-    
+# define CYG_THREAD_EXCEPTION_CONTROL_MEMBER \
     cyg_exception_control       exception_control;
-
+#else
+# define CYG_THREAD_EXCEPTION_CONTROL_MEMBER
 #endif
 
 #ifdef CYGFUN_KERNEL_THREADS_TIMER
+# define CYG_THREAD_TIMER_MEMBER \
     cyg_threadtimer     timer;
+#else
+# define CYG_THREAD_TIMER_MEMBER
 #endif
 
-    cyg_reason_t        sleep_reason;
-    cyg_reason_t        wake_reason;
-
 #ifdef CYGVAR_KERNEL_THREADS_DATA
-
+# define CYG_THREAD_THREAD_DATA_MEMBER \
     CYG_ADDRWORD        thread_data[CYGNUM_KERNEL_THREADS_DATA_MAX];
-
+#else
+# define CYG_THREAD_THREAD_DATA_MEMBER
 #endif
 
 #ifdef CYGVAR_KERNEL_THREADS_NAME
-
+# define CYG_THREAD_NAME_MEMBER \
     char                *name;
-
+#else
+# define CYG_THREAD_NAME_MEMBER
 #endif
 
 #ifdef CYGVAR_KERNEL_THREADS_LIST
-
+# define CYG_THREAD_LIST_NEXT_MEMBER \
     cyg_thread          *list_next;
-    
+#else
+# define CYG_THREAD_LIST_NEXT_MEMBER
 #endif
-    
+
+#define CYG_THREAD_MEMBERS                        \
+    CYG_HARDWARETHREAD_MEMBERS                    \
+    CYG_SCHEDTHREAD_MEMBERS                       \
+                                                  \
+    cyg_uint32                  state;            \
+    cyg_ucount32                suspend_count;    \
+    cyg_ucount32                wakeup_count;     \
+    CYG_ADDRWORD                wait_info;        \
+    cyg_uint16                  unique_id;        \
+                                                  \
+    CYG_THREAD_EXCEPTION_CONTROL_MEMBER           \
+    CYG_THREAD_TIMER_MEMBER                       \
+                                                  \
+    cyg_reason_t        sleep_reason;             \
+    cyg_reason_t        wake_reason;              \
+                                                  \
+    CYG_THREAD_THREAD_DATA_MEMBER                 \
+    CYG_THREAD_NAME_MEMBER                        \
+    CYG_THREAD_LIST_NEXT_MEMBER                   
+
+
+struct cyg_thread
+{
+    CYG_THREAD_MEMBERS
 };
 
 /*---------------------------------------------------------------------------*/
--- a/packages/kernel/current/src/sync/mutex.cxx
+++ b/packages/kernel/current/src/sync/mutex.cxx
@@ -208,6 +208,13 @@ Cyg_Mutex::lock(void)
 
 #endif
 
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
+    
+    IF_PROTOCOL_CEILING
+        self->set_priority_ceiling(ceiling);
+
+#endif        
+               
     while( locked && result )
     {
         CYG_ASSERT( self != owner, "Locking mutex I already own");
@@ -254,25 +261,28 @@ Cyg_Mutex::lock(void)
         locked      = true;
         owner       = self;
 
+        CYG_INSTRUMENT_MUTEX(LOCKED, this, 0);
+    }
+    else
+    {
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL
+
+        self->uncount_mutex();
+
+#endif    
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
+
+        IF_PROTOCOL_INHERIT
+            self->disinherit_priority();
+
+#endif
 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
 
         IF_PROTOCOL_CEILING
-            self->set_priority_ceiling(ceiling);
-        
-#endif        
-               
-        CYG_INSTRUMENT_MUTEX(LOCKED, this, 0);
+            self->clear_priority_ceiling();
+
+#endif
     }
-#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
-    else
-    {
-        IF_PROTOCOL_INHERIT
-        {
-            self->uncount_mutex();
-            self->disinherit_priority();
-        }
-    }
-#endif
     
     // Unlock the scheduler and maybe switch threads
     Cyg_Scheduler::unlock();
--- a/packages/kernel/current/tests/kmutex3.c
+++ b/packages/kernel/current/tests/kmutex3.c
@@ -78,9 +78,6 @@ cyg_hal_invoke_constructors();
 // 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
@@ -386,7 +383,7 @@ static void control_thread( cyg_addrword
     CYG_TEST_INIT();
     CYG_TEST_INFO( "Control Thread running" );
 
-    // Go through the 27 possibilitied of resuming the extra threads
+    // Go through the 27 possibilities of resuming the extra threads
     //     0: not at all
     //     1: early in the process
     //     2: later on
@@ -627,4 +624,4 @@ cyg_start( void )
 // 
 // ------------------------------------------------------------------------
 
-// EOF mutex3.cxx
+// EOF mutex3.c
new file mode 100644
--- /dev/null
+++ b/packages/kernel/current/tests/kmutex4.c
@@ -0,0 +1,514 @@
+//==========================================================================
+//
+//        kmutex4.c
+//
+//        Mutex test 4 - dynamic priority inheritance protocol
+//
+//==========================================================================
+//####COPYRIGHTBEGIN####
+//                                                                          
+// -------------------------------------------                              
+// The contents of this file are subject to the Red Hat eCos Public License 
+// Version 1.1 (the "License"); you may not use this file except in         
+// compliance with the License.  You may obtain a copy of the License at    
+// http://www.redhat.com/                                                   
+//                                                                          
+// Software distributed under the License is distributed on an "AS IS"      
+// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See the 
+// License for the specific language governing rights and limitations under 
+// the License.                                                             
+//                                                                          
+// The Original Code is eCos - Embedded Configurable Operating System,      
+// released September 30, 1998.                                             
+//                                                                          
+// The Initial Developer of the Original Code is Red Hat.                   
+// Portions created by Red Hat are                                          
+// Copyright (C) 1998, 1999, 2000, 2001 Red Hat, Inc.
+// All Rights Reserved.                                                     
+// -------------------------------------------                              
+//                                                                          
+//####COPYRIGHTEND####
+//==========================================================================
+//#####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 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 )
+{
+    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.
+#ifdef VERBOSE
+    CYG_TEST_INFO( "Thread 1 exit" );
+#endif
+}
+
+// ------------------------------------------------------------------------
+
+static void t2( cyg_addrword_t data )
+{
+    cyg_handle_t self = cyg_thread_self();
+    int i;
+    cyg_tick_count_t then, now;
+#ifdef VERBOSE
+    CYG_TEST_INFO( "Thread 2 running" );
+#endif
+    CYG_TEST_CHECK( 0 == (data & ~0x77), "Bad T2 arg: extra bits" );
+    CYG_TEST_CHECK( 0 == (data & (data >> 4)), "Bad T2 arg: overlap" );
+
+    cyg_thread_suspend( self );
+
+    // 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
+            cyg_thread_resume( thread[i+4] ); // extras are thread[4-6]
+
+    cyg_thread_delay( DELAYFACTOR * 10 ); // let those threads run
+
+    cyg_scheduler_lock();               // do this next lot atomically
+
+    go_flag = 1;                        // unleash thread 3
+    cyg_thread_resume( thread[1] );     // 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
+            cyg_thread_resume( thread[i] ); // 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_current_time();
+    do {
+        now = cyg_current_time();
+        // Wait longer than the delay in t3 waiting on go_flag
+    } while ( now < (then + 3) );
+
+    // Check for whatever result we expect from the protocol selected:
+    // This mirrors what is done in configury in kmutex3.c and mutex3.cxx
+    if ( PROTO_CEILING_MID == proto ) {
+        CYG_TEST_INFO( "Not checking: ceiling mid value" );
+    }
+    else if ( PROTO_INHERIT == proto ||
+              PROTO_CEILING_HIGH == proto ) {
+        CYG_TEST_INFO( "Checking priority scheme operating" );
+        CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
+        CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
+    }
+    else {
+        CYG_TEST_INFO( "Checking NO priority scheme operating" );
+        CYG_TEST_CHECK( 0 == t3ran, "Thread 3 DID run" );
+        CYG_TEST_CHECK( 0 == got_it, "Thread 1 DID get the mutex" );
+    }
+
+    CYG_TEST_CHECK( 0 == t3ended, "Thread 3 ended prematurely [T2,1]" );
+
+    cyg_thread_delay( DELAYFACTOR * 20 ); // let those threads run
+
+    CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
+    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
+    CYG_TEST_CHECK( 1 == t3ended, "Thread 3 has not ended" );
+
+    for ( i = 0; i < 3; i++ )
+        if ( (1 << i) & (data | data >> 4) ) // bits 0-2 and 4-6 control
+            CYG_TEST_CHECK( 1 == extras[i+1], "Extra thread did not run" );
+        else
+            CYG_TEST_CHECK( 0 == extras[i+1], "Extra thread ran" );
+
+    CYG_TEST_PASS( "Thread 2 exiting, AOK" );
+    // That's all: restart the control thread.
+    cyg_thread_resume( thread[0] );
+}
+
+// ------------------------------------------------------------------------
+
+static void t3( cyg_addrword_t data )
+{
+#ifdef VERBOSE
+    CYG_TEST_INFO( "Thread 3 running" );
+#endif
+    cyg_mutex_lock( mutex );
+
+    cyg_thread_delay( DELAYFACTOR * 5 ); // let thread 3a run
+
+    cyg_thread_resume( thread[2] );     // resume thread 2
+
+    while ( 0 == go_flag )
+        cyg_thread_delay(1);            // wait until we are told to go
+
+    t3ran ++;                           // record the fact
+
+    CYG_TEST_CHECK( 0 == got_it, "Thread 1 claims to have got my mutex" );
+    
+    cyg_mutex_unlock( mutex );
+    
+    t3ended ++;                         // record that we came back
+
+    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
+#ifdef VERBOSE
+    CYG_TEST_INFO( "Thread 3 exit" );
+#endif
+}
+
+// ------------------------------------------------------------------------
+
+static void control_thread( cyg_addrword_t data )
+{
+    cyg_handle_t self = cyg_thread_self();
+    int i, z;
+
+    CYG_TEST_INIT();
+    CYG_TEST_INFO( "Control Thread running" );
+
+    // Go through the 27 possibilities of resuming the extra threads
+    //     0: not at all
+    //     1: early in the process
+    //     2: later on
+    // which are represented by bits 0-3 and 4-6 resp in the argument to
+    // thread 2 (none set means no resume at all).
+    for ( i = 0; i < 27; i++ ) {
+        static int xx[] = { 0, 1, 16 };
+        int j = i % 3;
+        int k = (i / 3) % 3;
+        int l = (i / 9) % 3;
+
+        int d = xx[j] | (xx[k]<<1) | (xx[l]<<2) ;
+
+        if ( cyg_test_is_simulator && (0 != i && 13 != i && 26 != i) )
+            continue;    // 13 is 111 base 3, 26 is 222 base 3
+
+        // Go through all these priority inversion prevention protocols:
+        // (if supported in this configuration)
+	// PROTO_NONE            (0)
+	// PROTO_INHERIT         (1)
+	// PROTO_CEILING_HIGH    (2)
+	// PROTO_CEILING_MID     (3)
+	// PROTO_CEILING_LOW     (4)
+        for ( proto = PROTO_NONE; proto <= PROTO_CEILING_LOW; proto++ ) {
+
+            // If no priority inheritance at all, running threads 1a and 2a is
+            // OK, but not thread 3a; it blocks the world.
+            if ( PROTO_NONE == proto ||
+                 PROTO_CEILING_MID == proto ||
+                 PROTO_CEILING_LOW == proto )
+                if ( l )                // Cannot run thread 3a if no
+                    continue;           // priority inheritance at all.
+
+            mutex = &mutex_obj;
+            
+            switch ( proto ) {
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_NONE
+            case PROTO_NONE:
+                cyg_mutex_init( mutex );
+                cyg_mutex_set_protocol( mutex, CYG_MUTEX_NONE );
+                break;
+#endif
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
+            case PROTO_INHERIT:
+                cyg_mutex_init( mutex );
+                cyg_mutex_set_protocol( mutex, CYG_MUTEX_INHERIT );
+                break;
+#endif
+#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
+            case PROTO_CEILING_HIGH:
+                cyg_mutex_init( mutex );
+                cyg_mutex_set_protocol( mutex, CYG_MUTEX_CEILING );
+                cyg_mutex_set_ceiling( mutex, (cyg_priority_t)  4 );
+                break;
+            case PROTO_CEILING_MID:
+                cyg_mutex_init( mutex );
+                cyg_mutex_set_protocol( mutex, CYG_MUTEX_CEILING );
+                cyg_mutex_set_ceiling( mutex, (cyg_priority_t) 11 );
+                break;
+            case PROTO_CEILING_LOW:
+                cyg_mutex_init( mutex );
+                cyg_mutex_set_protocol( mutex, CYG_MUTEX_CEILING );
+                cyg_mutex_set_ceiling( mutex, (cyg_priority_t) 17 );
+                break;
+#endif
+            default:
+                continue; // Break out of the prio for loop - do nothing
+            }
+
+            got_it  = 0;
+            t3ran   = 0;
+            t3ended = 0;
+            for ( z = 0; z < 4; z++ ) extras[z] = 0;
+            go_flag = 0;
+        
+            new_thread( t1, 0,  5, 1, "test 1" ); // Slot 1
+            new_thread( t2, d, 10, 1, "test 2" ); // Slot 2
+            new_thread( t3, 0, 15, 1, "test 3" ); // Slot 3
+        
+            new_thread( extra_thread, 1,  8, j, "extra 1" ); // Slot 4
+            new_thread( extra_thread, 2, 12, k, "extra 2" ); // Slot 5
+            new_thread( extra_thread, 3, 17, l, "extra 3" ); // Slot 6
+        
+            {
+                static char *a[] = { "inactive", "run early", "run late" };
+                diag_printf( "\n----- %s [%2d] New Cycle: 0x%02x, Threads 1a %s, 2a %s, 3a %s -----\n",
+                             protnames[proto], i, d,  a[j], a[k], a[l] );
+            }
+
+            cyg_thread_suspend( self );
+        
+            kill_threads();
+            cyg_mutex_destroy( mutex );
+        }
+    }
+    CYG_TEST_EXIT( "Control Thread exit" );
+}
+
+// ------------------------------------------------------------------------
+
+externC void
+cyg_user_start( void )
+{ 
+#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
+    cyg_hal_invoke_constructors();
+#endif
+    new_thread( control_thread, 0, 2, 1, "control thread" );
+}
+
+#else // CYGVAR_KERNEL_COUNTERS_CLOCK &c
+
+externC void
+cyg_start( void )
+{
+    CYG_TEST_INIT();
+    CYG_TEST_PASS_FINISH("KMutex4 test requires:\n"
+                         "CYGFUN_KERNEL_API_C &&\n"
+                         "CYGVAR_KERNEL_COUNTERS_CLOCK &&\n"
+                         "(CYGNUM_KERNEL_SCHED_PRIORITIES > 20) &&\n"
+                         "!defined(CYGPKG_KERNEL_SMP_SUPPORT) &&\n"
+    "defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC)\n"
+        );
+}
+#endif // CYGVAR_KERNEL_COUNTERS_CLOCK &c
+
+
+// ------------------------------------------------------------------------
+// Documentation: enclosed is the design of this test.
+//
+// See mutex3.cxx or kmutex3.c
+
+// ------------------------------------------------------------------------
+// EOF mutex4.c
--- a/packages/language/c/libc/string/current/ChangeLog
+++ b/packages/language/c/libc/string/current/ChangeLog
@@ -1,3 +1,8 @@
+2001-08-21  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* cdl/string.cdl (CYGSEM_LIBC_STRING_PER_THREAD_STRTOK): Improve
+	dependencies.
+
 2001-08-10  Robin Farine  <acnrf@dial.eunet.ch>
 
 	* src/memchr.cxx (__memchr): Don't check string validity if number
--- a/packages/language/c/libc/string/current/cdl/string.cdl
+++ b/packages/language/c/libc/string/current/cdl/string.cdl
@@ -105,8 +105,9 @@ cdl_package CYGPKG_LIBC_STRING {
     
         cdl_option CYGSEM_LIBC_STRING_PER_THREAD_STRTOK {
             display       "Per-thread strtok()"
+            active_if     CYGPKG_KERNEL
             requires      CYGVAR_KERNEL_THREADS_DATA
-            default_value 0
+            default_value CYGVAR_KERNEL_THREADS_DATA
             description   "
                 This option controls whether the string function
                 strtok() has its state recorded on a per-thread
--- a/packages/templates/all/ChangeLog
+++ b/packages/templates/all/ChangeLog
@@ -1,3 +1,7 @@
+2001-08-21  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* current.ect: Pre-infer CYGBLD_ISO_PMUTEXTYPES_HEADER.
+
 2000-11-03  Jonathan Larmour  <jlarmour@redhat.com>
 
 	* current.ect: Update inferred values for current CDL.
--- a/packages/templates/all/current.ect
+++ b/packages/templates/all/current.ect
@@ -173,3 +173,7 @@ cdl_option CYGBLD_ISO_SIGSETJMP_HEADER {
     inferred_value 1 <cyg/posix/sigsetjmp.h>
 };
 
+cdl_option CYGBLD_ISO_PMUTEXTYPES_HEADER {
+    inferred_value 1 <cyg/posix/muttypes.h>
+};
+
--- a/packages/templates/elix/ChangeLog
+++ b/packages/templates/elix/ChangeLog
@@ -1,3 +1,7 @@
+2001-08-21  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* current.ect: Pre-infer CYGBLD_ISO_PMUTEXTYPES_HEADER.
+
 2000-11-03  Jonathan Larmour  <jlarmour@redhat.com>
 
 	* current.ect: Update values for current CDL
--- a/packages/templates/elix/current.ect
+++ b/packages/templates/elix/current.ect
@@ -185,3 +185,7 @@ cdl_option CYGBLD_ISO_SIGSETJMP_HEADER {
 cdl_option CYGBLD_ISO_OPEN_MAX_HEADER {
     inferred_value 1 <cyg/fileio/limits.h>
 };
+
+cdl_option CYGBLD_ISO_PMUTEXTYPES_HEADER {
+    inferred_value 1 <cyg/posix/muttypes.h>
+};
--- a/packages/templates/net/ChangeLog
+++ b/packages/templates/net/ChangeLog
@@ -1,3 +1,7 @@
+2001-08-21  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* current.ect: Pre-infer CYGBLD_ISO_PMUTEXTYPES_HEADER.
+
 2000-11-03  Jonathan Larmour  <jlarmour@redhat.com>
 
 	* current.ect: Update inferred values for current CDL
--- a/packages/templates/net/current.ect
+++ b/packages/templates/net/current.ect
@@ -186,3 +186,7 @@ cdl_option CYGBLD_ISO_SIGSETJMP_HEADER {
 cdl_option CYGBLD_ISO_OPEN_MAX_HEADER {
     inferred_value 1 <cyg/fileio/limits.h>
 };
+
+cdl_option CYGBLD_ISO_PMUTEXTYPES_HEADER {
+    inferred_value 1 <cyg/posix/muttypes.h>
+};
--- a/packages/templates/posix/ChangeLog
+++ b/packages/templates/posix/ChangeLog
@@ -1,3 +1,7 @@
+2001-08-21  Jonathan Larmour  <jlarmour@redhat.com>
+
+	* current.ect: Pre-infer CYGBLD_ISO_PMUTEXTYPES_HEADER.
+
 2000-11-03  Jonathan Larmour  <jlarmour@redhat.com>
 
 	* current.ect: Update inferred values for current CDL
--- a/packages/templates/posix/current.ect
+++ b/packages/templates/posix/current.ect
@@ -119,3 +119,7 @@ cdl_option CYGBLD_ISO_SIGSETJMP_HEADER {
 cdl_option CYGBLD_ISO_OPEN_MAX_HEADER {
     inferred_value 1 <cyg/fileio/limits.h>
 };
+
+cdl_option CYGBLD_ISO_PMUTEXTYPES_HEADER {
+    inferred_value 1 <cyg/posix/muttypes.h>
+};