1 //==========================================================================
5 // Sync test 3 -- tests priorities and 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####
47 // Creates mutexes and threads to set up starvation condition.
48 // Checks simple priority inheritance cures this.
50 // The starvation condition is caused by the highest priority
51 // thread, t0 waiting on a mutex which is never released because
52 // it is held by t2. t2 never releases it because t1 will be
53 // running at a priority level higher than t2 (but lower than t0).
55 // With priority inheritance enabled, t2 will inherit its priority
56 // from t0 when t0 tries to grab the mutex.
59 // CYGIMP_THREAD_PRIORITY
60 // CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
61 // CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE
62 //####DESCRIPTIONEND####
64 #include <pkgconf/kernel.h>
66 #include <cyg/kernel/thread.hxx>
67 #include <cyg/kernel/thread.inl>
68 #include <cyg/kernel/sched.hxx>
69 #include <cyg/kernel/mutex.hxx>
70 #include <cyg/kernel/sema.hxx>
72 #include <cyg/infra/testcase.h>
74 #include <cyg/kernel/sched.inl>
76 #if defined(CYGIMP_THREAD_PRIORITY) && \
77 !defined(CYGPKG_KERNEL_SMP_SUPPORT)
79 // ------------------------------------------------------------------------
80 // Manufacture a simpler feature test macro for priority inheritance than
81 // the configuration gives us. We have priority inheritance if it is configured
82 // as the only protocol, or if it is the default protocol for dynamic protocol
84 // FIXME: If we have dynamic protocol choice, we can also set priority inheritance
85 // as the protocol to be used on the mutexes we are interested in. At present we
88 #ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
89 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
90 # ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_INHERIT
91 # define PRIORITY_INHERITANCE
93 # undef PRIORITY_INHERITANCE
96 # define PRIORITY_INHERITANCE
99 # undef PRIORITY_INHERITANCE
102 // ------------------------------------------------------------------------
106 #include "testaux.hxx"
109 static Cyg_Binary_Semaphore s0, s1, s2;
111 static cyg_ucount8 m0d = 9;
113 static void check_priorities_normal()
115 CHECK( 5 == thread[0]->get_priority());
116 CHECK( 6 == thread[1]->get_priority());
117 CHECK( 7 == thread[2]->get_priority());
120 static void check_priorities_inherited()
122 CHECK( 5 == thread[0]->get_priority());
123 CHECK( 6 == thread[1]->get_priority());
124 #ifdef PRIORITY_INHERITANCE
125 CHECK( 5 == thread[2]->get_current_priority());
127 CHECK( 7 == thread[2]->get_priority());
131 static void entry0( CYG_ADDRWORD data )
133 s0.wait(); // wait until t2 has gained m0.lock
134 check_priorities_normal();
136 check_priorities_normal();
140 check_priorities_normal();
141 #ifdef PRIORITY_INHERITANCE
142 CYG_TEST_PASS_FINISH("Sync 3 OK -- priority inheritance worked");
144 CYG_TEST_FAIL_FINISH("Sync 3: thread not starved");
149 static void entry1( CYG_ADDRWORD data )
152 // The delay below will allow testing of the priority inheritance
153 // mechanism when scheduler does not guarantee to schedule threads
154 // in strict priority order.
155 for ( volatile cyg_ucount32 i=0; i < 100000; i++ )
158 #ifdef PRIORITY_INHERITANCE
159 // thread0 should have stopped by this point
160 CYG_TEST_FAIL_FINISH("Sync 3: priority inheritance mechanism failed");
162 // With strict priority scheduling and no priority inheritance
163 // this is expected to happen.
164 CYG_TEST_PASS_FINISH("Sync 3 OK");
166 CYG_TEST_FAIL_FINISH("Not reached");
169 void entry2( CYG_ADDRWORD data )
173 check_priorities_normal();
174 s0.post(); // Now I have lock on m0, wake t0 then t1
175 check_priorities_inherited();
177 check_priorities_inherited();
180 check_priorities_normal();
182 check_priorities_normal();
185 s2.wait(); // never posted
191 void sync3_main(void)
195 new_thread( entry0, 0);
196 new_thread( entry1, 1);
197 new_thread( entry2, 2);
199 thread[0]->set_priority(5);
200 thread[1]->set_priority(6);
201 thread[2]->set_priority(7);
203 Cyg_Scheduler::start();
205 CYG_TEST_FAIL_FINISH("Not reached");
211 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
212 cyg_hal_invoke_constructors();
217 #else // defined(CYGIMP_THREAD_PRIORITY) etc
223 CYG_TEST_INFO("Sync3 test requires:\n"
224 "defined(CYGIMP_THREAD_PRIORITY) &&\n"
225 "!defined(CYGPKG_KERNEL_SMP_SUPPORT)\n");
226 CYG_TEST_NA("Sync3 test requirements");
230 #endif // defined(CYGIMP_THREAD_PRIORITY) etc