1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// sync3.cxx
|
4 |
|
|
//
|
5 |
|
|
// Sync test 3 -- tests priorities and priority inheritance
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
|
12 |
|
|
//
|
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
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): dsm
|
43 |
|
|
// Contributors: dsm
|
44 |
|
|
// Date: 1998-02-18
|
45 |
|
|
// Description:
|
46 |
|
|
// Creates mutexes and threads to set up starvation condition.
|
47 |
|
|
// Checks simple priority inheritance cures this.
|
48 |
|
|
//
|
49 |
|
|
// The starvation condition is caused by the highest priority
|
50 |
|
|
// thread, t0 waiting on a mutex which is never released because
|
51 |
|
|
// it is held by t2. t2 never releases it because t1 will be
|
52 |
|
|
// running at a priority level higher than t2 (but lower than t0).
|
53 |
|
|
//
|
54 |
|
|
// With priority inheritance enabled, t2 will inherit its priority
|
55 |
|
|
// from t0 when t0 tries to grab the mutex.
|
56 |
|
|
//
|
57 |
|
|
// Options:
|
58 |
|
|
// CYGIMP_THREAD_PRIORITY
|
59 |
|
|
// CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
|
60 |
|
|
// CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_SIMPLE
|
61 |
|
|
//####DESCRIPTIONEND####
|
62 |
|
|
|
63 |
|
|
#include <pkgconf/kernel.h>
|
64 |
|
|
|
65 |
|
|
#include <cyg/kernel/thread.hxx>
|
66 |
|
|
#include <cyg/kernel/thread.inl>
|
67 |
|
|
#include <cyg/kernel/sched.hxx>
|
68 |
|
|
#include <cyg/kernel/mutex.hxx>
|
69 |
|
|
#include <cyg/kernel/sema.hxx>
|
70 |
|
|
|
71 |
|
|
#include <cyg/infra/testcase.h>
|
72 |
|
|
|
73 |
|
|
#include <cyg/kernel/sched.inl>
|
74 |
|
|
|
75 |
|
|
#if defined(CYGIMP_THREAD_PRIORITY) && \
|
76 |
|
|
!defined(CYGPKG_KERNEL_SMP_SUPPORT)
|
77 |
|
|
|
78 |
|
|
// ------------------------------------------------------------------------
|
79 |
|
|
// Manufacture a simpler feature test macro for priority inheritance than
|
80 |
|
|
// the configuration gives us. We have priority inheritance if it is configured
|
81 |
|
|
// as the only protocol, or if it is the default protocol for dynamic protocol
|
82 |
|
|
// choice.
|
83 |
|
|
// FIXME: If we have dynamic protocol choice, we can also set priority inheritance
|
84 |
|
|
// as the protocol to be used on the mutexes we are interested in. At present we
|
85 |
|
|
// do not do this.
|
86 |
|
|
|
87 |
|
|
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
|
88 |
|
|
# ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC
|
89 |
|
|
# ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DEFAULT_INHERIT
|
90 |
|
|
# define PRIORITY_INHERITANCE
|
91 |
|
|
# else
|
92 |
|
|
# undef PRIORITY_INHERITANCE
|
93 |
|
|
# endif
|
94 |
|
|
# else
|
95 |
|
|
# define PRIORITY_INHERITANCE
|
96 |
|
|
# endif
|
97 |
|
|
#else
|
98 |
|
|
# undef PRIORITY_INHERITANCE
|
99 |
|
|
#endif
|
100 |
|
|
|
101 |
|
|
// ------------------------------------------------------------------------
|
102 |
|
|
|
103 |
|
|
#define NTHREADS 3
|
104 |
|
|
|
105 |
|
|
#include "testaux.hxx"
|
106 |
|
|
|
107 |
|
|
static Cyg_Mutex m0;
|
108 |
|
|
static Cyg_Binary_Semaphore s0, s1, s2;
|
109 |
|
|
|
110 |
|
|
static cyg_ucount8 m0d = 9;
|
111 |
|
|
|
112 |
|
|
static void check_priorities_normal()
|
113 |
|
|
{
|
114 |
|
|
CHECK( 5 == thread[0]->get_priority());
|
115 |
|
|
CHECK( 6 == thread[1]->get_priority());
|
116 |
|
|
CHECK( 7 == thread[2]->get_priority());
|
117 |
|
|
}
|
118 |
|
|
|
119 |
|
|
static void check_priorities_inherited()
|
120 |
|
|
{
|
121 |
|
|
CHECK( 5 == thread[0]->get_priority());
|
122 |
|
|
CHECK( 6 == thread[1]->get_priority());
|
123 |
|
|
#ifdef PRIORITY_INHERITANCE
|
124 |
|
|
CHECK( 5 == thread[2]->get_current_priority());
|
125 |
|
|
#endif
|
126 |
|
|
CHECK( 7 == thread[2]->get_priority());
|
127 |
|
|
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
static void entry0( CYG_ADDRWORD data )
|
131 |
|
|
{
|
132 |
|
|
s0.wait(); // wait until t2 has gained m0.lock
|
133 |
|
|
check_priorities_normal();
|
134 |
|
|
m0.lock(); {
|
135 |
|
|
check_priorities_normal();
|
136 |
|
|
CHECK( 2 == m0d );
|
137 |
|
|
m0d = 0;
|
138 |
|
|
} m0.unlock();
|
139 |
|
|
check_priorities_normal();
|
140 |
|
|
#ifdef PRIORITY_INHERITANCE
|
141 |
|
|
CYG_TEST_PASS_FINISH("Sync 3 OK -- priority inheritance worked");
|
142 |
|
|
#else
|
143 |
|
|
CYG_TEST_FAIL_FINISH("Sync 3: thread not starved");
|
144 |
|
|
#endif
|
145 |
|
|
// NOT REACHED
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
static void entry1( CYG_ADDRWORD data )
|
149 |
|
|
{
|
150 |
|
|
s1.wait();
|
151 |
|
|
// The delay below will allow testing of the priority inheritance
|
152 |
|
|
// mechanism when scheduler does not guarantee to schedule threads
|
153 |
|
|
// in strict priority order.
|
154 |
|
|
for ( volatile cyg_ucount32 i=0; i < 100000; i++ )
|
155 |
|
|
; // math is hard
|
156 |
|
|
|
157 |
|
|
#ifdef PRIORITY_INHERITANCE
|
158 |
|
|
// thread0 should have stopped by this point
|
159 |
|
|
CYG_TEST_FAIL_FINISH("Sync 3: priority inheritance mechanism failed");
|
160 |
|
|
#else
|
161 |
|
|
// With strict priority scheduling and no priority inheritance
|
162 |
|
|
// this is expected to happen.
|
163 |
|
|
CYG_TEST_PASS_FINISH("Sync 3 OK");
|
164 |
|
|
#endif
|
165 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
void entry2( CYG_ADDRWORD data )
|
169 |
|
|
{
|
170 |
|
|
m0.lock(); {
|
171 |
|
|
CHECK( 9 == m0d );
|
172 |
|
|
check_priorities_normal();
|
173 |
|
|
s0.post(); // Now I have lock on m0, wake t0 then t1
|
174 |
|
|
check_priorities_inherited();
|
175 |
|
|
s1.post();
|
176 |
|
|
check_priorities_inherited();
|
177 |
|
|
m0d = 2;
|
178 |
|
|
} m0.unlock();
|
179 |
|
|
check_priorities_normal();
|
180 |
|
|
m0.lock(); {
|
181 |
|
|
check_priorities_normal();
|
182 |
|
|
CHECK( 0 == m0d );
|
183 |
|
|
m0d = 21;
|
184 |
|
|
s2.wait(); // never posted
|
185 |
|
|
} m0.unlock();
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
|
189 |
|
|
|
190 |
|
|
void sync3_main(void)
|
191 |
|
|
{
|
192 |
|
|
CYG_TEST_INIT();
|
193 |
|
|
|
194 |
|
|
new_thread( entry0, 0);
|
195 |
|
|
new_thread( entry1, 1);
|
196 |
|
|
new_thread( entry2, 2);
|
197 |
|
|
|
198 |
|
|
thread[0]->set_priority(5);
|
199 |
|
|
thread[1]->set_priority(6);
|
200 |
|
|
thread[2]->set_priority(7);
|
201 |
|
|
|
202 |
|
|
Cyg_Scheduler::start();
|
203 |
|
|
|
204 |
|
|
CYG_TEST_FAIL_FINISH("Not reached");
|
205 |
|
|
}
|
206 |
|
|
|
207 |
|
|
externC void
|
208 |
|
|
cyg_start( void )
|
209 |
|
|
{
|
210 |
|
|
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
|
211 |
|
|
cyg_hal_invoke_constructors();
|
212 |
|
|
#endif
|
213 |
|
|
sync3_main();
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
#else // defined(CYGIMP_THREAD_PRIORITY) etc
|
217 |
|
|
|
218 |
|
|
externC void
|
219 |
|
|
cyg_start( void )
|
220 |
|
|
{
|
221 |
|
|
CYG_TEST_INIT();
|
222 |
|
|
CYG_TEST_INFO("Sync3 test requires:\n"
|
223 |
|
|
"defined(CYGIMP_THREAD_PRIORITY) &&\n"
|
224 |
|
|
"!defined(CYGPKG_KERNEL_SMP_SUPPORT)\n");
|
225 |
|
|
CYG_TEST_NA("Sync3 test requirements");
|
226 |
|
|
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
#endif // defined(CYGIMP_THREAD_PRIORITY) etc
|
230 |
|
|
|
231 |
|
|
// EOF sync3.cxx
|