1 //==========================================================================
5 // Mutex test 3 - priority inheritance
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
13 // eCos is free software; you can redistribute it and/or modify it under
14 // the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 2 or (at your option) any later version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //==========================================================================
41 //#####DESCRIPTIONBEGIN####
45 // Date: 2000-01-06, 2001-08-10
46 // Description: Tests mutex priority inheritance. This is simply a
47 // translation of the similarly named kernel test to the
48 // KAPI, with the intention of also testing the new
49 // "set the protocol at run-time" extensions.
50 //####DESCRIPTIONEND####
52 #include <pkgconf/hal.h>
53 #include <pkgconf/kernel.h>
55 #include <cyg/infra/testcase.h>
57 #include <cyg/hal/hal_arch.h> // CYGNUM_HAL_STACK_SIZE_TYPICAL
59 #include <cyg/infra/diag.h> // diag_printf
61 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
63 cyg_hal_invoke_constructors();
66 // ------------------------------------------------------------------------
68 // These checks should be enough; any other scheduler which has priorities
69 // should manifest as having no priority inheritance, but otherwise fine,
70 // so the test should work correctly.
72 #if defined(CYGVAR_KERNEL_COUNTERS_CLOCK) && \
73 (CYGNUM_KERNEL_SCHED_PRIORITIES > 20) && \
74 defined(CYGFUN_KERNEL_API_C) && \
75 !defined(CYGPKG_KERNEL_SMP_SUPPORT)
78 #include <cyg/kernel/kapi.h>
80 #include <cyg/infra/cyg_ass.h>
81 #include <cyg/infra/cyg_trac.h>
82 #include <cyg/infra/diag.h> // diag_printf
85 // ------------------------------------------------------------------------
86 // manufacture a simpler feature test macro for priority inheritance than
87 // the configuration gives us. We have priority inheritance if it is configured
88 // as the only protocol, or if it is the default protocol for dynamic protocol
91 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
92 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
93 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_INHERIT
94 # define PRIORITY_INHERITANCE "dynamic-default-inherit"
97 # define PRIORITY_INHERITANCE "static-inherit"
101 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
102 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
103 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_CEILING
104 # if CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY <= 5
105 # define PRIORITY_INHERITANCE "dynamic-default-ceiling-high"
106 # elif CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY >= 15
107 # define NO_PRIORITY_INHERITANCE "dynamic-default-ceiling-low"
109 # define PRIORITY_UNKNOWN "dynamic-default-ceiling-mid"
113 # if CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY <= 5
114 # define PRIORITY_INHERITANCE "static-ceiling-high"
115 # elif CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_PRIORITY >= 15
116 # define NO_PRIORITY_INHERITANCE "static-ceiling-low"
118 # define PRIORITY_UNKNOWN "static-ceiling-mid"
123 #ifndef PRIORITY_INHERITANCE
124 # ifndef NO_PRIORITY_INHERITANCE
125 # define NO_PRIORITY_INHERITANCE "no scheme selected"
129 // ------------------------------------------------------------------------
130 // Management functions
132 // Stolen from testaux.hxx and copied in here because I want to be able to
133 // reset the world also.
135 // Translated into KAPI also.
139 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
141 static cyg_handle_t thread[NTHREADS] = { 0 };
143 typedef cyg_uint64 CYG_ALIGNMENT_TYPE;
145 static cyg_thread thread_obj[NTHREADS];
147 static CYG_ALIGNMENT_TYPE stack[NTHREADS] [
148 (STACKSIZE+sizeof(CYG_ALIGNMENT_TYPE)-1)
149 / sizeof(CYG_ALIGNMENT_TYPE) ];
151 static volatile int nthreads = 0;
156 static cyg_handle_t new_thread( cyg_thread_entry_t *entry,
158 cyg_addrword_t priority,
161 int _nthreads = nthreads++;
163 CYG_ASSERT(_nthreads < NTHREADS,
164 "Attempt to create more than NTHREADS threads");
166 cyg_thread_create( priority,
170 (void *)(stack[_nthreads]),
173 &thread_obj[_nthreads] );
176 cyg_thread_resume( thread[_nthreads] );
178 return thread[_nthreads];
182 static void kill_threads( void )
184 CYG_ASSERT(nthreads <= NTHREADS,
185 "More than NTHREADS threads");
186 CYG_ASSERT( cyg_thread_self() == thread[0],
187 "kill_threads() not called from thread 0");
188 while ( nthreads > 1 ) {
190 if ( NULL != thread[nthreads] ) {
192 cyg_thread_kill( thread[nthreads] );
193 while ( ! cyg_thread_delete ( thread[nthreads] ) );
194 thread[nthreads] = NULL;
197 CYG_ASSERT(nthreads == 1,
201 // ------------------------------------------------------------------------
203 #define DELAYFACTOR 1 // for debugging
205 // ------------------------------------------------------------------------
207 static cyg_mutex_t mutex_obj;
208 static cyg_mutex_t *mutex;
210 // These are for reporting back to the master thread
211 volatile int got_it = 0;
212 volatile int t3ran = 0;
213 volatile int t3ended = 0;
214 volatile int extras[4] = {0,0,0,0};
216 volatile int go_flag = 0; // but this one controls thread 3 from thread 2
218 // ------------------------------------------------------------------------
219 // 0 to 3 of these run generally to interfere with the other processing,
220 // to cause multiple prio inheritances, and clashes in any orders.
222 static void extra_thread( cyg_addrword_t data )
224 cyg_handle_t self = cyg_thread_self();
227 do { z[13] = '0' + data; CYG_TEST_INFO( z ); } while ( 0 )
229 static char running[] = "Extra thread Xa running";
230 static char exiting[] = "Extra thread Xa exiting";
231 static char resumed[] = "Extra thread Xa resumed";
232 static char locked[] = "Extra thread Xa locked";
233 static char unlocked[] = "Extra thread Xa unlocked";
237 cyg_thread_suspend( self );
241 cyg_mutex_lock( mutex );
245 cyg_mutex_unlock( mutex );
255 // ------------------------------------------------------------------------
257 static void t1( cyg_addrword_t data )
259 cyg_handle_t self = cyg_thread_self();
261 CYG_TEST_INFO( "Thread 1 running" );
263 cyg_thread_suspend( self );
265 cyg_mutex_lock( mutex );
269 CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,1]" );
271 cyg_mutex_unlock( mutex );
273 CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,2]" );
277 CYG_TEST_INFO( "Thread 1 exit" );
280 // ------------------------------------------------------------------------
282 static void t2( cyg_addrword_t data )
284 cyg_handle_t self = cyg_thread_self();
286 cyg_tick_count_t then, now;
288 CYG_TEST_INFO( "Thread 2 running" );
290 CYG_TEST_CHECK( 0 == (data & ~0x77), "Bad T2 arg: extra bits" );
291 CYG_TEST_CHECK( 0 == (data & (data >> 4)), "Bad T2 arg: overlap" );
293 cyg_thread_suspend( self );
295 // depending on our config argument, optionally restart some of the
296 // extra threads to throw noise into the scheduler:
297 for ( i = 0; i < 3; i++ )
298 if ( (1 << i) & data ) // bits 0-2 control
299 cyg_thread_resume( thread[i+4] ); // extras are thread[4-6]
301 cyg_thread_delay( DELAYFACTOR * 10 ); // let those threads run
303 cyg_scheduler_lock(); // do this next lot atomically
305 go_flag = 1; // unleash thread 3
306 cyg_thread_resume( thread[1] ); // resume thread 1
308 // depending on our config argument, optionally restart some of the
309 // extra threads to throw noise into the scheduler at this later point:
310 for ( i = 4; i < 7; i++ )
311 if ( (1 << i) & data ) // bits 4-6 control
312 cyg_thread_resume( thread[i] ); // extras are thread[4-6]
314 cyg_scheduler_unlock(); // let scheduling proceed
316 // Need a delay (but not a CPU yield) to allow t3 to awaken and act on
317 // the go_flag, otherwise we check these details below too soon.
318 // Actually, waiting for the clock to tick a couple of times would be
319 // better, so that is what we will do. Must be a busy-wait.
320 then = cyg_current_time();
322 now = cyg_current_time();
323 // Wait longer than the delay in t3 waiting on go_flag
324 } while ( now < (then + 3) );
326 #ifdef PRIORITY_UNKNOWN
327 CYG_TEST_INFO( "Not checking: " PRIORITY_UNKNOWN );
329 #ifdef PRIORITY_INHERITANCE
330 CYG_TEST_INFO( "Checking priority scheme: " PRIORITY_INHERITANCE );
331 CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
332 CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
334 CYG_TEST_INFO( "Checking NO priority scheme: " NO_PRIORITY_INHERITANCE );
335 CYG_TEST_CHECK( 0 == t3ran, "Thread 3 DID run" );
336 CYG_TEST_CHECK( 0 == got_it, "Thread 1 DID get the mutex" );
340 CYG_TEST_CHECK( 0 == t3ended, "Thread 3 ended prematurely [T2,1]" );
342 cyg_thread_delay( DELAYFACTOR * 20 ); // let those threads run
344 CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
345 CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
346 CYG_TEST_CHECK( 1 == t3ended, "Thread 3 has not ended" );
348 for ( i = 0; i < 3; i++ )
349 if ( (1 << i) & (data | data >> 4) ) // bits 0-2 and 4-6 control
350 CYG_TEST_CHECK( 1 == extras[i+1], "Extra thread did not run" );
352 CYG_TEST_CHECK( 0 == extras[i+1], "Extra thread ran" );
354 CYG_TEST_PASS( "Thread 2 exiting, AOK" );
355 // That's all: restart the control thread.
356 cyg_thread_resume( thread[0] );
359 // ------------------------------------------------------------------------
361 static void t3( cyg_addrword_t data )
363 CYG_TEST_INFO( "Thread 3 running" );
365 cyg_mutex_lock( mutex );
367 cyg_thread_delay( DELAYFACTOR * 5 ); // let thread 3a run
369 cyg_thread_resume( thread[2] ); // resume thread 2
371 while ( 0 == go_flag )
372 cyg_thread_delay(1); // wait until we are told to go
374 t3ran ++; // record the fact
376 CYG_TEST_CHECK( 0 == got_it, "Thread 1 claims to have got my mutex" );
378 cyg_mutex_unlock( mutex );
380 t3ended ++; // record that we came back
382 CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
384 CYG_TEST_INFO( "Thread 3 exit" );
387 // ------------------------------------------------------------------------
389 static void control_thread( cyg_addrword_t data )
391 cyg_handle_t self = cyg_thread_self();
395 CYG_TEST_INFO( "Control Thread running" );
397 // Go through the 27 possibilities of resuming the extra threads
399 // 1: early in the process
401 // which are represented by bits 0-3 and 4-6 resp in the argument to
402 // thread 2 (none set means no resume at all).
403 for ( i = 0; i < 27; i++ ) {
404 static int xx[] = { 0, 1, 16 };
409 int d = xx[j] | (xx[k]<<1) | (xx[l]<<2) ;
411 if ( cyg_test_is_simulator && (0 != i && 13 != i && 26 != i) )
412 continue; // 13 is 111 base 3, 26 is 222 base 3
414 #ifdef PRIORITY_INHERITANCE
415 // If the simple scheme plus relay enhancement, or any other
416 // *complete* scheme, we can run all three ancillary threads no
417 // problem, so no special action here.
420 // If no priority inheritance at all, running threads 1a and 2a is
421 // OK, but not thread 3a; it blocks the world.
422 if ( l ) // Cannot run thread 3a if no
423 break; // priority inheritance at all.
427 cyg_mutex_init( mutex );
432 for ( z = 0; z < 4; z++ ) extras[z] = 0;
435 new_thread( t1, 0, 5, 1 ); // Slot 1
436 new_thread( t2, d, 10, 1 ); // Slot 2
437 new_thread( t3, 0, 15, 1 ); // Slot 3
439 new_thread( extra_thread, 1, 8, j ); // Slot 4
440 new_thread( extra_thread, 2, 12, k ); // Slot 5
441 new_thread( extra_thread, 3, 17, l ); // Slot 6
444 static char *a[] = { "inactive", "run early", "run late" };
445 diag_printf( "\n----- [%2d] New Cycle: 0x%02x, Threads 1a %s, 2a %s, 3a %s -----\n",
446 i, d, a[j], a[k], a[l] );
449 cyg_thread_suspend( self );
452 cyg_mutex_destroy( mutex );
454 CYG_TEST_EXIT( "Control Thread exit" );
457 // ------------------------------------------------------------------------
460 cyg_user_start( void )
462 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
463 cyg_hal_invoke_constructors();
465 new_thread( control_thread, 0, 2, 1 );
468 #else // CYGVAR_KERNEL_COUNTERS_CLOCK &c
474 CYG_TEST_INFO("KMutex3 test requires:\n"
475 "CYGFUN_KERNEL_API_C &&\n"
476 "CYGVAR_KERNEL_COUNTERS_CLOCK &&\n"
477 "(CYGNUM_KERNEL_SCHED_PRIORITIES > 20) &&\n"
478 "!defined(CYGPKG_KERNEL_SMP_SUPPORT)\n");
479 CYG_TEST_NA("KMutex3 test requirements");
481 #endif // CYGVAR_KERNEL_COUNTERS_CLOCK &c
484 // ------------------------------------------------------------------------
485 // Documentation: enclosed is the design of this test.
487 // It has been carefully constructed so that it does NOT use other kernel
488 // facilities (aside from delay-task) to test that priority inheritance is
489 // working, or not, as intended by the configuration.
491 // These notes describe the flow of control in one run of the test with the
492 // ancillary tasks optionally interspersed. The details of how those extra
493 // tasks are or are not allowed to run are not described.
497 // The only change in the test that depends on whether there is inheritance or
498 // not is the check in thread 2 on "3-ran" and "got it" flags marked ****
501 // volatile &c booleans:
505 // "extras"[3] = FALSE
507 // thread 1. prio 5, self-suspend.
509 // thread 1a, prio 8, self-suspend.
511 // thread 2. prio 10, self-suspend.
513 // thread 2a, prio 12, self-suspend.
515 // thread 3. prio 15, runs, lock mutex, resume(2)
517 // thread 3a, prio 17, self-suspend.
520 // 2. resume(3a) +++OPTIONAL
521 // 2. resume(2a) +++OPTIONAL
522 // 2. resume(1a) +++OPTIONAL
523 // [1a lock-fail] thread 3->prio := 8
525 // [3. runs maybe, does the looping thing]
527 // 2. sleep a while...
529 // [2a lock-fail] thread 3->prio := 12
531 // [3. runs maybe, does the looping thing]
533 // [3a lock-fail] thread 3->prio unchanged
535 // [3. runs maybe, does the looping thing]
540 // 2. resume(1a) +++OPTIONAL
541 // 2. resume(2a) +++OPTIONAL
542 // 2. resume(3a) +++OPTIONAL
543 // 2. unlock scheduler
545 // 1. runs, lock mutex - thread 3 has it locked
547 // 2. busy-waits a bit for thread 3 to come out of its delay() loop.
548 // This must be a *busy*wait so that 3 can only run via the
549 // inherited raised priority.
551 // [xa. all do the same: lock mutex, ]
552 // [xa. unlock mutex ]
553 // [xa. set a flag "extras"[x] to say we are done. ]
561 // thread 3->prio := 5
564 // 3. set a flag to say "3-ran",
565 // 3. loop with a sleep(1) until "go-flag" is set.
566 // 3. check "got it" is false,
567 // 3. then unlock mutex,
569 // thread 3->prio := 15
571 // 1. runs, set a flag to say "got it",
572 // 1. check "3-ended" flag is false
574 // 1. check "3-ended" flag is still false
577 // [1a locks, unlocks, exits]
579 // 2. runs, check "3-ran" and "got it" flags are TRUE ****
580 // 2. check "3-ended" flag is false
581 // 2. sleeps for a while so that...
583 // [2a locks, unlocks, exits]
585 // 3. runs, set "3-ended" flag,
586 // 3. check "3-ran" and "got it" flags
589 // [3a locks, unlocks, exits]
591 // 2. awakens, checks all flags true,
592 // 2. check that all "extra" threads that we started have indeed run
600 // thread 1 is waiting on the mutex
604 // 2. runs, checks that "3-ran" and "got it" flags are FALSE ****
605 // 2. check "3-ended" flag is false
606 // 2. sleeps for a while so that...
610 // 3. runs, set a flag to say "3-ran",
611 // 3. check "got it" is false,
612 // 3. then unlock mutex,
614 // 1. runs, set a flag to say "got it",
615 // 1. check "3-ended" flag is false
617 // 1. check "3-ended" flag is still false
620 // [1a locks, unlocks, exits]
621 // [2a locks, unlocks, exits]
623 // 3. runs, set "3-ended" flag,
624 // 3. check "3-ran" and "got it" flags
627 // [3a locks, unlocks, exits]
629 // 2. awakens, checks all flags true,
630 // 2. check that all "extra" threads that we started have indeed run
637 // ------------------------------------------------------------------------