1 |
27 |
unneback |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// mutex3.cxx
|
4 |
|
|
//
|
5 |
|
|
// Mutex test 3 - 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 Red Hat, 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 version.
|
16 |
|
|
//
|
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
|
20 |
|
|
// for more details.
|
21 |
|
|
//
|
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.
|
25 |
|
|
//
|
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.
|
32 |
|
|
//
|
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.
|
35 |
|
|
//
|
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####
|
42 |
|
|
//
|
43 |
|
|
// Author(s): hmt
|
44 |
|
|
// Contributors: hmt, nickg, jlarmour
|
45 |
|
|
// Date: 2000-01-06
|
46 |
|
|
// Description: Tests mutex priority inheritance. This is simply a translation
|
47 |
|
|
// of the similarly named kernel test to the POSIX API
|
48 |
|
|
//####DESCRIPTIONEND####
|
49 |
|
|
|
50 |
|
|
// ------------------------------------------------------------------------
|
51 |
|
|
|
52 |
|
|
#include <cyg/infra/testcase.h>
|
53 |
|
|
#include <pkgconf/posix.h>
|
54 |
|
|
#include <pkgconf/system.h>
|
55 |
|
|
#ifdef CYGPKG_KERNEL
|
56 |
|
|
#include <pkgconf/kernel.h>
|
57 |
|
|
#endif
|
58 |
|
|
|
59 |
|
|
#ifdef CYGPKG_ISOINFRA
|
60 |
|
|
# include <sys/types.h>
|
61 |
|
|
# include <pthread.h>
|
62 |
|
|
# include <semaphore.h>
|
63 |
|
|
# include <time.h>
|
64 |
|
|
# include <unistd.h>
|
65 |
|
|
#endif
|
66 |
|
|
|
67 |
|
|
#if !defined(CYGPKG_POSIX_PTHREAD)
|
68 |
|
|
#define NA_MSG "POSIX threads not enabled"
|
69 |
|
|
|
70 |
|
|
// ------------------------------------------------------------------------
|
71 |
|
|
//
|
72 |
|
|
// These checks should be enough; any other scheduler which has priorities
|
73 |
|
|
// should manifest as having no priority inheritance, but otherwise fine,
|
74 |
|
|
// so the test should work correctly.
|
75 |
|
|
|
76 |
|
|
#elif !defined(_POSIX_THREAD_PRIORITY_SCHEDULING)
|
77 |
|
|
#define NA_MSG "No POSIX thread priority scheduling enabled"
|
78 |
|
|
#elif !defined(_POSIX_THREAD_PRIO_INHERIT)
|
79 |
|
|
#define NA_MSG "No POSIX thread priority inheritance enabled"
|
80 |
|
|
#elif !defined(_POSIX_SEMAPHORES)
|
81 |
|
|
#define NA_MSG "No POSIX sempaphore support enabled enabled"
|
82 |
|
|
#elif !defined(CYGFUN_KERNEL_API_C)
|
83 |
|
|
#define NA_MSG "Kernel C API not enabled"
|
84 |
|
|
#elif defined(CYGPKG_KERNEL_SMP_SUPPORT)
|
85 |
|
|
#define NA_MSG "Test cannot run with SMP support"
|
86 |
|
|
#endif
|
87 |
|
|
|
88 |
|
|
#ifdef NA_MSG
|
89 |
|
|
void
|
90 |
|
|
cyg_start(void)
|
91 |
|
|
{
|
92 |
|
|
CYG_TEST_INIT();
|
93 |
|
|
CYG_TEST_NA(NA_MSG);
|
94 |
|
|
}
|
95 |
|
|
#else
|
96 |
|
|
|
97 |
|
|
#include <cyg/infra/cyg_ass.h>
|
98 |
|
|
#include <cyg/infra/cyg_trac.h>
|
99 |
|
|
#include <cyg/infra/diag.h> // diag_printf
|
100 |
|
|
|
101 |
|
|
#include <cyg/kernel/kapi.h> // Some extras
|
102 |
|
|
|
103 |
|
|
// ------------------------------------------------------------------------
|
104 |
|
|
// Management functions
|
105 |
|
|
//
|
106 |
|
|
// Stolen from testaux.hxx and copied in here because I want to be able to
|
107 |
|
|
// reset the world also.
|
108 |
|
|
// ... and subsequently POSIXized out of all similarly with its progenitors.
|
109 |
|
|
|
110 |
|
|
#define NTHREADS 7
|
111 |
|
|
|
112 |
|
|
#define STACKSIZE (PTHREAD_STACK_MIN*2)
|
113 |
|
|
|
114 |
|
|
static pthread_t thread[NTHREADS] = { 0 };
|
115 |
|
|
|
116 |
|
|
typedef CYG_WORD64 CYG_ALIGNMENT_TYPE;
|
117 |
|
|
|
118 |
|
|
static CYG_ALIGNMENT_TYPE stack[NTHREADS] [
|
119 |
|
|
(STACKSIZE+sizeof(CYG_ALIGNMENT_TYPE)-1)
|
120 |
|
|
/ sizeof(CYG_ALIGNMENT_TYPE) ];
|
121 |
|
|
|
122 |
|
|
// Semaphores to halt execution of threads
|
123 |
|
|
static sem_t hold[NTHREADS];
|
124 |
|
|
|
125 |
|
|
// Flag to tell all threads to exit
|
126 |
|
|
static int all_exit;
|
127 |
|
|
|
128 |
|
|
// Application thread data is passed here, the thread
|
129 |
|
|
// argument is
|
130 |
|
|
static CYG_ADDRWORD thread_data[NTHREADS];
|
131 |
|
|
|
132 |
|
|
static volatile int nthreads = 0;
|
133 |
|
|
|
134 |
|
|
// Sleep for 1 tick...
|
135 |
|
|
static struct timespec sleeptime;
|
136 |
|
|
|
137 |
|
|
|
138 |
|
|
static pthread_t new_thread( void *(*entry)(void *),
|
139 |
|
|
CYG_ADDRWORD data,
|
140 |
|
|
int priority,
|
141 |
|
|
int do_resume)
|
142 |
|
|
{
|
143 |
|
|
pthread_attr_t attr;
|
144 |
|
|
int _nthreads = nthreads++;
|
145 |
|
|
|
146 |
|
|
struct sched_param schedparam;
|
147 |
|
|
schedparam.sched_priority = priority;
|
148 |
|
|
|
149 |
|
|
pthread_attr_init( &attr );
|
150 |
|
|
pthread_attr_setstackaddr( &attr, (void *)((char *)(&stack[_nthreads])+STACKSIZE) );
|
151 |
|
|
pthread_attr_setstacksize( &attr, STACKSIZE );
|
152 |
|
|
pthread_attr_setinheritsched( &attr, PTHREAD_EXPLICIT_SCHED );
|
153 |
|
|
pthread_attr_setschedpolicy( &attr, SCHED_RR );
|
154 |
|
|
pthread_attr_setschedparam( &attr, &schedparam );
|
155 |
|
|
|
156 |
|
|
CYG_ASSERT(_nthreads < NTHREADS,
|
157 |
|
|
"Attempt to create more than NTHREADS threads");
|
158 |
|
|
|
159 |
|
|
thread_data[_nthreads] = data;
|
160 |
|
|
|
161 |
|
|
sem_init( &hold[_nthreads], 0, do_resume ? 1 : 0 );
|
162 |
|
|
all_exit = 0;
|
163 |
|
|
|
164 |
|
|
pthread_create( &thread[_nthreads],
|
165 |
|
|
&attr,
|
166 |
|
|
entry,
|
167 |
|
|
(void *)_nthreads);
|
168 |
|
|
|
169 |
|
|
return thread[_nthreads];
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
|
173 |
|
|
static void kill_threads( void )
|
174 |
|
|
{
|
175 |
|
|
CYG_ASSERT(nthreads <= NTHREADS,
|
176 |
|
|
"More than NTHREADS threads");
|
177 |
|
|
CYG_ASSERT( pthread_equal(pthread_self(),thread[0]),
|
178 |
|
|
"kill_threads() not called from thread 0");
|
179 |
|
|
all_exit = 1;
|
180 |
|
|
while ( nthreads > 1 ) {
|
181 |
|
|
nthreads--;
|
182 |
|
|
if ( 0 != thread[nthreads] ) {
|
183 |
|
|
sem_post( &hold[nthreads] );
|
184 |
|
|
pthread_cancel( thread[nthreads] );
|
185 |
|
|
pthread_join( thread[nthreads], NULL );
|
186 |
|
|
thread[nthreads] = 0;
|
187 |
|
|
sem_destroy( &hold[nthreads] );
|
188 |
|
|
}
|
189 |
|
|
}
|
190 |
|
|
CYG_ASSERT(nthreads == 1,
|
191 |
|
|
"No threads left");
|
192 |
|
|
}
|
193 |
|
|
|
194 |
|
|
// ------------------------------------------------------------------------
|
195 |
|
|
|
196 |
|
|
#define DELAYFACTOR 1 // for debugging
|
197 |
|
|
|
198 |
|
|
// ------------------------------------------------------------------------
|
199 |
|
|
|
200 |
|
|
pthread_mutex_t mutex;
|
201 |
|
|
|
202 |
|
|
// These are for reporting back to the master thread
|
203 |
|
|
volatile int got_it = 0;
|
204 |
|
|
volatile int t3ran = 0;
|
205 |
|
|
volatile int t3ended = 0;
|
206 |
|
|
volatile int extras[4] = {0,0,0,0};
|
207 |
|
|
|
208 |
|
|
volatile int go_flag = 0; // but this one controls thread 3 from thread 2
|
209 |
|
|
|
210 |
|
|
// ------------------------------------------------------------------------
|
211 |
|
|
// 0 to 3 of these run generally to interfere with the other processing,
|
212 |
|
|
// to cause multiple prio inheritances, and clashes in any orders.
|
213 |
|
|
|
214 |
|
|
static void *extra_thread( void *arg )
|
215 |
|
|
{
|
216 |
|
|
#define XINFO( z ) \
|
217 |
|
|
do { z[13] = '0' + data; CYG_TEST_INFO( z ); } while ( 0 )
|
218 |
|
|
|
219 |
|
|
static char running[] = "Extra thread Xa running";
|
220 |
|
|
static char exiting[] = "Extra thread Xa exiting";
|
221 |
|
|
static char resumed[] = "Extra thread Xa resumed";
|
222 |
|
|
static char locked[] = "Extra thread Xa locked";
|
223 |
|
|
static char unlocked[] = "Extra thread Xa unlocked";
|
224 |
|
|
|
225 |
|
|
int id = (int)arg;
|
226 |
|
|
CYG_ADDRWORD data = thread_data[id];
|
227 |
|
|
|
228 |
|
|
CYG_ASSERT( (id >= 4 && id <= 6), "extra_thread invalid id" );
|
229 |
|
|
|
230 |
|
|
// Emulate resume behaviour
|
231 |
|
|
sem_wait( &hold[id] );
|
232 |
|
|
if( all_exit ) return 0;
|
233 |
|
|
|
234 |
|
|
XINFO( running );
|
235 |
|
|
|
236 |
|
|
sem_wait( &hold[id] );
|
237 |
|
|
|
238 |
|
|
XINFO( resumed );
|
239 |
|
|
|
240 |
|
|
pthread_mutex_lock( &mutex );
|
241 |
|
|
|
242 |
|
|
XINFO( locked );
|
243 |
|
|
|
244 |
|
|
pthread_mutex_unlock( &mutex );
|
245 |
|
|
|
246 |
|
|
XINFO( unlocked );
|
247 |
|
|
|
248 |
|
|
extras[ data ] ++;
|
249 |
|
|
|
250 |
|
|
XINFO( exiting );
|
251 |
|
|
|
252 |
|
|
return NULL;
|
253 |
|
|
}
|
254 |
|
|
|
255 |
|
|
// ------------------------------------------------------------------------
|
256 |
|
|
|
257 |
|
|
static void *t1( void *arg )
|
258 |
|
|
{
|
259 |
|
|
int id = (int)arg;
|
260 |
|
|
//CYG_ADDRWORD data = thread_data[id];
|
261 |
|
|
|
262 |
|
|
// Emulate resume behaviour
|
263 |
|
|
sem_wait( &hold[id] );
|
264 |
|
|
if( all_exit ) return 0;
|
265 |
|
|
|
266 |
|
|
CYG_TEST_INFO( "Thread 1 running" );
|
267 |
|
|
|
268 |
|
|
sem_wait( &hold[id] );
|
269 |
|
|
|
270 |
|
|
pthread_mutex_lock( &mutex );
|
271 |
|
|
|
272 |
|
|
got_it++;
|
273 |
|
|
|
274 |
|
|
CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,1]" );
|
275 |
|
|
|
276 |
|
|
pthread_mutex_unlock( &mutex );
|
277 |
|
|
|
278 |
|
|
CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,2]" );
|
279 |
|
|
|
280 |
|
|
// That's all.
|
281 |
|
|
|
282 |
|
|
CYG_TEST_INFO( "Thread 1 exit" );
|
283 |
|
|
|
284 |
|
|
return 0;
|
285 |
|
|
}
|
286 |
|
|
|
287 |
|
|
// ------------------------------------------------------------------------
|
288 |
|
|
|
289 |
|
|
static void *t2( void *arg )
|
290 |
|
|
{
|
291 |
|
|
int i;
|
292 |
|
|
int id = (int)arg;
|
293 |
|
|
CYG_ADDRWORD data = thread_data[id];
|
294 |
|
|
cyg_tick_count_t now, then;
|
295 |
|
|
|
296 |
|
|
// Emulate resume behaviour
|
297 |
|
|
sem_wait( &hold[id] );
|
298 |
|
|
if( all_exit ) return 0;
|
299 |
|
|
|
300 |
|
|
CYG_TEST_INFO( "Thread 2 running" );
|
301 |
|
|
|
302 |
|
|
CYG_TEST_CHECK( 0 == (data & ~0x77), "Bad T2 arg: extra bits" );
|
303 |
|
|
CYG_TEST_CHECK( 0 == (data & (data >> 4)), "Bad T2 arg: overlap" );
|
304 |
|
|
|
305 |
|
|
sem_wait( &hold[id] );
|
306 |
|
|
|
307 |
|
|
// depending on our config argument, optionally restart some of the
|
308 |
|
|
// extra threads to throw noise into the scheduler:
|
309 |
|
|
for ( i = 0; i < 3; i++ )
|
310 |
|
|
if ( (1 << i) & data ) // bits 0-2 control
|
311 |
|
|
sem_post( &hold[i+4] ); // made sure extras are thread[4-6]
|
312 |
|
|
|
313 |
|
|
// let those threads run
|
314 |
|
|
for( i = 0; i < DELAYFACTOR * 10; i++ )
|
315 |
|
|
nanosleep( &sleeptime, NULL );
|
316 |
|
|
|
317 |
|
|
cyg_scheduler_lock(); // do this next lot atomically
|
318 |
|
|
|
319 |
|
|
go_flag = 1; // unleash thread 3
|
320 |
|
|
sem_post( &hold[1] ); // resume thread 1
|
321 |
|
|
|
322 |
|
|
// depending on our config argument, optionally restart some of the
|
323 |
|
|
// extra threads to throw noise into the scheduler at this later point:
|
324 |
|
|
for ( i = 4; i < 7; i++ )
|
325 |
|
|
if ( (1 << i) & data ) // bits 4-6 control
|
326 |
|
|
sem_post( &hold[i] ); // made sure extras are thread[4-6]
|
327 |
|
|
|
328 |
|
|
cyg_scheduler_unlock(); // let scheduling proceed
|
329 |
|
|
|
330 |
|
|
// Need a delay (but not a CPU yield) to allow t3 to awaken and act on
|
331 |
|
|
// the go_flag, otherwise we check these details below too soon.
|
332 |
|
|
// Actually, waiting for the clock to tick a couple of times would be
|
333 |
|
|
// better, so that is what we will do. Must be a busy-wait.
|
334 |
|
|
then = cyg_current_time();
|
335 |
|
|
do {
|
336 |
|
|
now = cyg_current_time();
|
337 |
|
|
// Wait longer than the delay in t3 waiting on go_flag
|
338 |
|
|
} while ( now < (then + 3) );
|
339 |
|
|
|
340 |
|
|
#ifdef _POSIX_THREAD_PRIO_INHERIT
|
341 |
|
|
CYG_TEST_INFO( "Checking for mutex priority inheritance" );
|
342 |
|
|
CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
|
343 |
|
|
CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
|
344 |
|
|
#else
|
345 |
|
|
CYG_TEST_INFO( "Checking for NO mutex priority inheritance" );
|
346 |
|
|
CYG_TEST_CHECK( 0 == t3ran, "Thread 3 DID run" );
|
347 |
|
|
CYG_TEST_CHECK( 0 == got_it, "Thread 1 DID get the mutex" );
|
348 |
|
|
#endif
|
349 |
|
|
|
350 |
|
|
CYG_TEST_CHECK( 0 == t3ended, "Thread 3 ended prematurely [T2,1]" );
|
351 |
|
|
|
352 |
|
|
for( i = 0; i < DELAYFACTOR * 20; i++ )
|
353 |
|
|
nanosleep( &sleeptime, NULL ); // let those threads run
|
354 |
|
|
|
355 |
|
|
CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
|
356 |
|
|
CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
|
357 |
|
|
CYG_TEST_CHECK( 1 == t3ended, "Thread 3 has not ended" );
|
358 |
|
|
|
359 |
|
|
for ( i = 0; i < 3; i++ )
|
360 |
|
|
if ( (1 << i) & (data | data >> 4) ) // bits 0-2 and 4-6 control
|
361 |
|
|
CYG_TEST_CHECK( 1 == extras[i+1], "Extra thread did not run" );
|
362 |
|
|
else
|
363 |
|
|
CYG_TEST_CHECK( 0 == extras[i+1], "Extra thread ran" );
|
364 |
|
|
|
365 |
|
|
CYG_TEST_PASS( "Thread 2 exiting, AOK" );
|
366 |
|
|
// That's all: restart the control thread.
|
367 |
|
|
sem_post( &hold[0] );
|
368 |
|
|
|
369 |
|
|
return 0;
|
370 |
|
|
}
|
371 |
|
|
|
372 |
|
|
// ------------------------------------------------------------------------
|
373 |
|
|
|
374 |
|
|
static void *t3( void *arg )
|
375 |
|
|
{
|
376 |
|
|
int i;
|
377 |
|
|
int id = (int)arg;
|
378 |
|
|
//CYG_ADDRWORD data = thread_data[id];
|
379 |
|
|
|
380 |
|
|
// Emulate resume behaviour
|
381 |
|
|
sem_wait( &hold[id] );
|
382 |
|
|
if( all_exit ) return 0;
|
383 |
|
|
|
384 |
|
|
CYG_TEST_INFO( "Thread 3 running" );
|
385 |
|
|
|
386 |
|
|
pthread_mutex_lock( &mutex );
|
387 |
|
|
|
388 |
|
|
for( i = 0; i < DELAYFACTOR * 5; i++ )
|
389 |
|
|
nanosleep( &sleeptime, NULL ); // let thread 3a run
|
390 |
|
|
|
391 |
|
|
sem_post( &hold[2] ); // resume thread 2
|
392 |
|
|
|
393 |
|
|
while ( 0 == go_flag )
|
394 |
|
|
nanosleep( &sleeptime, NULL ); // wait until we are told to go
|
395 |
|
|
|
396 |
|
|
t3ran ++; // record the fact
|
397 |
|
|
|
398 |
|
|
CYG_TEST_CHECK( 0 == got_it, "Thread 1 claims to have got my mutex" );
|
399 |
|
|
|
400 |
|
|
pthread_mutex_unlock( &mutex );
|
401 |
|
|
|
402 |
|
|
t3ended ++; // record that we came back
|
403 |
|
|
|
404 |
|
|
CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
|
405 |
|
|
|
406 |
|
|
CYG_TEST_INFO( "Thread 3 exit" );
|
407 |
|
|
|
408 |
|
|
return 0;
|
409 |
|
|
}
|
410 |
|
|
|
411 |
|
|
// ------------------------------------------------------------------------
|
412 |
|
|
|
413 |
|
|
static void *control_thread( void *arg )
|
414 |
|
|
{
|
415 |
|
|
int i,z;
|
416 |
|
|
int id = (int)arg;
|
417 |
|
|
//CYG_ADDRWORD data = thread_data[id];
|
418 |
|
|
|
419 |
|
|
// Emulate resume behaviour
|
420 |
|
|
sem_wait( &hold[id] );
|
421 |
|
|
if( all_exit ) return 0;
|
422 |
|
|
|
423 |
|
|
// one tick sleep time
|
424 |
|
|
sleeptime.tv_nsec = 10000000;
|
425 |
|
|
sleeptime.tv_sec = 0;
|
426 |
|
|
|
427 |
|
|
CYG_TEST_INIT();
|
428 |
|
|
CYG_TEST_INFO( "Control Thread running" );
|
429 |
|
|
|
430 |
|
|
// Go through the 27 possibilitied of resuming the extra threads
|
431 |
|
|
// 0: not at all
|
432 |
|
|
// 1: early in the process
|
433 |
|
|
// 2: later on
|
434 |
|
|
// which are represented by bits 0-3 and 4-6 resp in the argument to
|
435 |
|
|
// thread 2 (none set means no resume at all).
|
436 |
|
|
for ( i = 0; i < 27; i++ ) {
|
437 |
|
|
static int xx[] = { 0, 1, 16 };
|
438 |
|
|
int j = i % 3;
|
439 |
|
|
int k = (i / 3) % 3;
|
440 |
|
|
int l = (i / 9) % 3;
|
441 |
|
|
|
442 |
|
|
int d = xx[j] | (xx[k]<<1) | (xx[l]<<2) ;
|
443 |
|
|
|
444 |
|
|
if ( cyg_test_is_simulator && (0 != i && 13 != i && 26 != i) )
|
445 |
|
|
continue; // 13 is 111 base 3, 26 is 222 base 3
|
446 |
|
|
|
447 |
|
|
#ifdef _POSIX_THREAD_PRIO_INHERIT
|
448 |
|
|
// If the simple scheme plus relay enhancement, or any other
|
449 |
|
|
// *complete* scheme, we can run all three ancillary threads no
|
450 |
|
|
// problem, so no special action here.
|
451 |
|
|
|
452 |
|
|
#else
|
453 |
|
|
// If no priority inheritance at all, running threads 1a and 2a is
|
454 |
|
|
// OK, but not thread 3a; it blocks the world.
|
455 |
|
|
if ( l ) // Cannot run thread 3a if no
|
456 |
|
|
break; // priority inheritance at all.
|
457 |
|
|
#endif
|
458 |
|
|
|
459 |
|
|
// Reinitialize mutex to provide priority inheritance
|
460 |
|
|
{
|
461 |
|
|
pthread_mutexattr_t attr;
|
462 |
|
|
pthread_mutexattr_init( &attr );
|
463 |
|
|
pthread_mutexattr_setprotocol( &attr, PTHREAD_PRIO_INHERIT );
|
464 |
|
|
pthread_mutex_init( &mutex, &attr );
|
465 |
|
|
}
|
466 |
|
|
|
467 |
|
|
got_it = 0;
|
468 |
|
|
t3ran = 0;
|
469 |
|
|
t3ended = 0;
|
470 |
|
|
for ( z = 0; z < 4; z++ ) extras[z] = 0;
|
471 |
|
|
go_flag = 0;
|
472 |
|
|
|
473 |
|
|
new_thread( t1, 0, 15, 1 ); // Slot 1
|
474 |
|
|
new_thread( t2, d, 10, 1 ); // Slot 2
|
475 |
|
|
new_thread( t3, 0, 5, 1 ); // Slot 3
|
476 |
|
|
|
477 |
|
|
new_thread( extra_thread, 1, 17, j ); // Slot 4
|
478 |
|
|
new_thread( extra_thread, 2, 12, k ); // Slot 5
|
479 |
|
|
new_thread( extra_thread, 3, 8, l ); // Slot 6
|
480 |
|
|
|
481 |
|
|
{
|
482 |
|
|
static char *a[] = { "inactive", "run early", "run late" };
|
483 |
|
|
diag_printf( "\n----- [%2d] New Cycle: 0x%02x, Threads 1a %s, 2a %s, 3a %s -----\n",
|
484 |
|
|
i, d, a[j], a[k], a[l] );
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
sem_wait( &hold[0] );
|
488 |
|
|
|
489 |
|
|
kill_threads();
|
490 |
|
|
pthread_mutex_destroy( &mutex );
|
491 |
|
|
}
|
492 |
|
|
CYG_TEST_EXIT( "Control Thread exit" );
|
493 |
|
|
|
494 |
|
|
return 0;
|
495 |
|
|
}
|
496 |
|
|
|
497 |
|
|
// ------------------------------------------------------------------------
|
498 |
|
|
|
499 |
|
|
static sem_t main_sem;
|
500 |
|
|
|
501 |
|
|
externC int
|
502 |
|
|
main( int argc, char **argv )
|
503 |
|
|
{
|
504 |
|
|
new_thread( control_thread, 0, 20, 1 );
|
505 |
|
|
|
506 |
|
|
// We have nothing for main to do here, so put it to sleep on
|
507 |
|
|
// its own semaphore. We cannot let it just exit since that
|
508 |
|
|
// will end the whole program.
|
509 |
|
|
|
510 |
|
|
sem_init( &main_sem, 0, 0 );
|
511 |
|
|
|
512 |
|
|
for(;;) sem_wait( &main_sem );
|
513 |
|
|
}
|
514 |
|
|
|
515 |
|
|
// ------------------------------------------------------------------------
|
516 |
|
|
// Documentation: enclosed is the design of this test.
|
517 |
|
|
//
|
518 |
|
|
// It has been carefully constructed so that it does NOT use other kernel
|
519 |
|
|
// facilities (aside from delay-task) to test that priority inheritance is
|
520 |
|
|
// working, or not, as intended by the configuration.
|
521 |
|
|
//
|
522 |
|
|
// These notes describe the flow of control in one run of the test with the
|
523 |
|
|
// ancillary tasks optionally interspersed. The details of how those extra
|
524 |
|
|
// tasks are or are not allowed to run are not described.
|
525 |
|
|
//
|
526 |
|
|
//
|
527 |
|
|
//
|
528 |
|
|
// The only change in the test that depends on whether there is inheritance or
|
529 |
|
|
// not is the check in thread 2 on "3-ran" and "got it" flags marked ****
|
530 |
|
|
//
|
531 |
|
|
//
|
532 |
|
|
// volatile &c booleans:
|
533 |
|
|
// "got it" = FALSE
|
534 |
|
|
// "3-ran" = FALSE
|
535 |
|
|
// "3-ended" = FALSE
|
536 |
|
|
// "extras"[3] = FALSE
|
537 |
|
|
//
|
538 |
|
|
// thread 1. prio 5, self-suspend.
|
539 |
|
|
//
|
540 |
|
|
// thread 1a, prio 8, self-suspend.
|
541 |
|
|
//
|
542 |
|
|
// thread 2. prio 10, self-suspend.
|
543 |
|
|
//
|
544 |
|
|
// thread 2a, prio 12, self-suspend.
|
545 |
|
|
//
|
546 |
|
|
// thread 3. prio 15, runs, lock mutex, resume(2)
|
547 |
|
|
//
|
548 |
|
|
// thread 3a, prio 17, self-suspend.
|
549 |
|
|
//
|
550 |
|
|
// 2. runs,
|
551 |
|
|
// 2. resume(3a) +++OPTIONAL
|
552 |
|
|
// 2. resume(2a) +++OPTIONAL
|
553 |
|
|
// 2. resume(1a) +++OPTIONAL
|
554 |
|
|
// [1a lock-fail] thread 3->prio := 8
|
555 |
|
|
//
|
556 |
|
|
// [3. runs maybe, does the looping thing]
|
557 |
|
|
//
|
558 |
|
|
// 2. sleep a while...
|
559 |
|
|
//
|
560 |
|
|
// [2a lock-fail] thread 3->prio := 12
|
561 |
|
|
//
|
562 |
|
|
// [3. runs maybe, does the looping thing]
|
563 |
|
|
//
|
564 |
|
|
// [3a lock-fail] thread 3->prio unchanged
|
565 |
|
|
//
|
566 |
|
|
// [3. runs maybe, does the looping thing]
|
567 |
|
|
//
|
568 |
|
|
// 2. lock scheduler
|
569 |
|
|
// 2. set "go-flag"
|
570 |
|
|
// 2. resume(1)
|
571 |
|
|
// 2. resume(1a) +++OPTIONAL
|
572 |
|
|
// 2. resume(2a) +++OPTIONAL
|
573 |
|
|
// 2. resume(3a) +++OPTIONAL
|
574 |
|
|
// 2. unlock scheduler
|
575 |
|
|
//
|
576 |
|
|
// 1. runs, lock mutex - thread 3 has it locked
|
577 |
|
|
//
|
578 |
|
|
// 2. busy-waits a bit for thread 3 to come out of its delay() loop.
|
579 |
|
|
// This must be a *busy*wait so that 3 can only run via the
|
580 |
|
|
// inherited raised priority.
|
581 |
|
|
//
|
582 |
|
|
// [xa. all do the same: lock mutex, ]
|
583 |
|
|
// [xa. unlock mutex ]
|
584 |
|
|
// [xa. set a flag "extras"[x] to say we are done. ]
|
585 |
|
|
// [xa. exit ]
|
586 |
|
|
//
|
587 |
|
|
//
|
588 |
|
|
//
|
589 |
|
|
// INHERIT
|
590 |
|
|
// -------
|
591 |
|
|
//
|
592 |
|
|
// thread 3->prio := 5
|
593 |
|
|
//
|
594 |
|
|
// 3. runs,
|
595 |
|
|
// 3. set a flag to say "3-ran",
|
596 |
|
|
// 3. loop with a sleep(1) until "go-flag" is set.
|
597 |
|
|
// 3. check "got it" is false,
|
598 |
|
|
// 3. then unlock mutex,
|
599 |
|
|
//
|
600 |
|
|
// thread 3->prio := 15
|
601 |
|
|
//
|
602 |
|
|
// 1. runs, set a flag to say "got it",
|
603 |
|
|
// 1. check "3-ended" flag is false
|
604 |
|
|
// 1. unlock mutex,
|
605 |
|
|
// 1. check "3-ended" flag is still false
|
606 |
|
|
// 1. exit.
|
607 |
|
|
//
|
608 |
|
|
// [1a locks, unlocks, exits]
|
609 |
|
|
//
|
610 |
|
|
// 2. runs, check "3-ran" and "got it" flags are TRUE ****
|
611 |
|
|
// 2. check "3-ended" flag is false
|
612 |
|
|
// 2. sleeps for a while so that...
|
613 |
|
|
//
|
614 |
|
|
// [2a locks, unlocks, exits]
|
615 |
|
|
//
|
616 |
|
|
// 3. runs, set "3-ended" flag,
|
617 |
|
|
// 3. check "3-ran" and "got it" flags
|
618 |
|
|
// 3. exit
|
619 |
|
|
//
|
620 |
|
|
// [3a locks, unlocks, exits]
|
621 |
|
|
//
|
622 |
|
|
// 2. awakens, checks all flags true,
|
623 |
|
|
// 2. check that all "extra" threads that we started have indeed run
|
624 |
|
|
// 2. end of test.
|
625 |
|
|
//
|
626 |
|
|
//
|
627 |
|
|
//
|
628 |
|
|
//
|
629 |
|
|
// NO-INHERIT
|
630 |
|
|
// ----------
|
631 |
|
|
// thread 1 is waiting on the mutex
|
632 |
|
|
//
|
633 |
|
|
// [1a lock-fail]
|
634 |
|
|
//
|
635 |
|
|
// 2. runs, checks that "3-ran" and "got it" flags are FALSE ****
|
636 |
|
|
// 2. check "3-ended" flag is false
|
637 |
|
|
// 2. sleeps for a while so that...
|
638 |
|
|
//
|
639 |
|
|
// [2a. lock-fail]
|
640 |
|
|
//
|
641 |
|
|
// 3. runs, set a flag to say "3-ran",
|
642 |
|
|
// 3. check "got it" is false,
|
643 |
|
|
// 3. then unlock mutex,
|
644 |
|
|
//
|
645 |
|
|
// 1. runs, set a flag to say "got it",
|
646 |
|
|
// 1. check "3-ended" flag is false
|
647 |
|
|
// 1. unlock mutex,
|
648 |
|
|
// 1. check "3-ended" flag is still false
|
649 |
|
|
// 1. exit.
|
650 |
|
|
//
|
651 |
|
|
// [1a locks, unlocks, exits]
|
652 |
|
|
// [2a locks, unlocks, exits]
|
653 |
|
|
//
|
654 |
|
|
// 3. runs, set "3-ended" flag,
|
655 |
|
|
// 3. check "3-ran" and "got it" flags
|
656 |
|
|
// 3. exit
|
657 |
|
|
//
|
658 |
|
|
// [3a locks, unlocks, exits]
|
659 |
|
|
//
|
660 |
|
|
// 2. awakens, checks all flags true,
|
661 |
|
|
// 2. check that all "extra" threads that we started have indeed run
|
662 |
|
|
// 2. end of test.
|
663 |
|
|
//
|
664 |
|
|
//
|
665 |
|
|
// (the end)
|
666 |
|
|
//
|
667 |
|
|
//
|
668 |
|
|
// ------------------------------------------------------------------------
|
669 |
|
|
|
670 |
|
|
#endif
|
671 |
|
|
|
672 |
|
|
// EOF mutex3.cxx
|