OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [kernel/] [v2_0/] [tests/] [kmutex4.c] - Blame information for rev 611

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//        kmutex4.c
4
//
5
//        Mutex test 4 - dynamic priority inheritance protocol
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
45
// Date:          2000-01-06, 2001-08-10, 2001-08-21
46
// Description:   Tests mutex priority inheritance.  This is an extension of
47
//                kmutex3.c, to test the new "set the protocol at run-time"
48
//                extensions.
49
//####DESCRIPTIONEND####
50
 
51
#include <pkgconf/hal.h>
52
#include <pkgconf/kernel.h>
53
 
54
#include <cyg/infra/testcase.h>
55
 
56
#include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
57
 
58
#include <cyg/infra/diag.h>             // diag_printf
59
 
60
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
61
externC void
62
cyg_hal_invoke_constructors();
63
#endif
64
 
65
// ------------------------------------------------------------------------
66
//
67
// These checks should be enough; any other scheduler which has priorities
68
// should manifest as having no priority inheritance, but otherwise fine,
69
// so the test should work correctly.
70
 
71
#if defined(CYGVAR_KERNEL_COUNTERS_CLOCK) &&                                    \
72
    (CYGNUM_KERNEL_SCHED_PRIORITIES > 20) &&                                    \
73
    defined(CYGFUN_KERNEL_API_C) &&                                             \
74
    !defined(CYGPKG_KERNEL_SMP_SUPPORT) &&                                      \
75
    defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC)      \
76
 
77
#include <cyg/kernel/kapi.h>
78
 
79
#include <cyg/infra/cyg_ass.h>
80
#include <cyg/infra/cyg_trac.h>
81
#include <cyg/infra/diag.h>             // diag_printf
82
 
83
// ------------------------------------------------------------------------
84
 
85
#define nVERBOSE
86
 
87
// ------------------------------------------------------------------------
88
// We have dynamic protocol choice, so we can set the protocol to whatever
89
// we want.  We'll do these combinations:
90
// NONE
91
// INHERIT
92
// CEILING = 4 = higher than any thread === INHERIT in behaviour
93
// CEILING = 11 = mixed in with threads === cannot check anything
94
// CEILING = 17 = lower than any threads === NONE in behaviour
95
 
96
#define PROTO_NONE            (0)
97
#define PROTO_INHERIT         (1)
98
#define PROTO_CEILING_HIGH    (2)
99
#define PROTO_CEILING_MID     (3)
100
#define PROTO_CEILING_LOW     (4)
101
 
102
int proto;
103
 
104
static char * protnames[] = {
105
    "none",
106
    "inherit",
107
    "high ceiling",
108
    "medium ceiling",
109
    "low ceiling",
110
};
111
 
112
// ------------------------------------------------------------------------
113
// Management functions
114
//
115
// Stolen from testaux.hxx and copied in here because I want to be able to
116
// reset the world also.
117
//
118
// Translated into KAPI also.
119
 
120
#define NTHREADS 7
121
 
122
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
123
 
124
static  cyg_handle_t thread[NTHREADS] = { 0 };
125
 
126
typedef cyg_uint64 CYG_ALIGNMENT_TYPE;
127
 
128
static cyg_thread thread_obj[NTHREADS];
129
 
130
static CYG_ALIGNMENT_TYPE stack[NTHREADS] [
131
   (STACKSIZE+sizeof(CYG_ALIGNMENT_TYPE)-1)
132
     / sizeof(CYG_ALIGNMENT_TYPE)                     ];
133
 
134
static volatile int nthreads = 0;
135
 
136
#undef NULL
137
#define NULL (0)
138
 
139
static cyg_handle_t new_thread( cyg_thread_entry_t *entry,
140
                                cyg_addrword_t data,
141
                                cyg_addrword_t priority,
142
                                int do_resume,
143
                                char *name )
144
{
145
    int _nthreads = nthreads++;
146
 
147
    CYG_ASSERT(_nthreads < NTHREADS,
148
               "Attempt to create more than NTHREADS threads");
149
 
150
    cyg_thread_create( priority,
151
                       entry,
152
                       data,
153
                       name,
154
                       (void *)(stack[_nthreads]),
155
                       STACKSIZE,
156
                       &thread[_nthreads],
157
                       &thread_obj[_nthreads] );
158
 
159
    if ( do_resume )
160
        cyg_thread_resume( thread[_nthreads] );
161
 
162
    return thread[_nthreads];
163
}
164
 
165
 
166
static void kill_threads( void )
167
{
168
    CYG_ASSERT(nthreads <= NTHREADS,
169
               "More than NTHREADS threads");
170
    CYG_ASSERT( cyg_thread_self() == thread[0],
171
                "kill_threads() not called from thread 0");
172
    while ( nthreads > 1 ) {
173
        nthreads--;
174
        if ( NULL != thread[nthreads] ) {
175
            do
176
                cyg_thread_kill( thread[nthreads] );
177
            while ( ! cyg_thread_delete ( thread[nthreads] ) );
178
            thread[nthreads] = NULL;
179
        }
180
    }
181
    CYG_ASSERT(nthreads == 1,
182
               "No threads left");
183
}
184
 
185
// ------------------------------------------------------------------------
186
 
187
#define DELAYFACTOR 1 // for debugging
188
 
189
// ------------------------------------------------------------------------
190
 
191
static cyg_mutex_t mutex_obj;
192
static cyg_mutex_t *mutex;
193
 
194
// These are for reporting back to the master thread
195
volatile int got_it  = 0;
196
volatile int t3ran   = 0;
197
volatile int t3ended = 0;
198
volatile int extras[4] = {0,0,0,0};
199
 
200
volatile int go_flag = 0; // but this one controls thread 3 from thread 2
201
 
202
// ------------------------------------------------------------------------
203
// 0 to 3 of these run generally to interfere with the other processing,
204
// to cause multiple prio inheritances, and clashes in any orders.
205
 
206
static void extra_thread( cyg_addrword_t data )
207
{
208
    cyg_handle_t self = cyg_thread_self();
209
 
210
 
211
#ifdef VERBOSE
212
#define xXINFO( z ) \
213
    do { z[13] = '0' + data; CYG_TEST_INFO( z ); } while ( 0 )
214
 
215
    static char running[]  = "Extra thread Xa running";
216
    static char exiting[]  = "Extra thread Xa exiting";
217
    static char resumed[]  = "Extra thread Xa resumed";
218
    static char locked[]   = "Extra thread Xa locked";
219
    static char unlocked[] = "Extra thread Xa unlocked";
220
#else
221
#define XINFO( z )  /* nothing */
222
#endif
223
 
224
    XINFO( running );
225
 
226
    cyg_thread_suspend( self );
227
 
228
    XINFO( resumed );
229
 
230
    cyg_mutex_lock( mutex );
231
 
232
    XINFO( locked );
233
 
234
    cyg_mutex_unlock( mutex );
235
 
236
    XINFO( unlocked );
237
 
238
    extras[ data ] ++;
239
 
240
    XINFO( exiting );
241
 
242
}
243
 
244
// ------------------------------------------------------------------------
245
 
246
static void t1( cyg_addrword_t data )
247
{
248
    cyg_handle_t self = cyg_thread_self();
249
#ifdef VERBOSE
250
    CYG_TEST_INFO( "Thread 1 running" );
251
#endif
252
    cyg_thread_suspend( self );
253
 
254
    cyg_mutex_lock( mutex );
255
 
256
    got_it++;
257
 
258
    CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,1]" );
259
 
260
    cyg_mutex_unlock( mutex );
261
 
262
    CYG_TEST_CHECK( 0 == t3ended, "T3 ended prematurely [T1,2]" );
263
 
264
    // That's all.
265
#ifdef VERBOSE
266
    CYG_TEST_INFO( "Thread 1 exit" );
267
#endif
268
}
269
 
270
// ------------------------------------------------------------------------
271
 
272
static void t2( cyg_addrword_t data )
273
{
274
    cyg_handle_t self = cyg_thread_self();
275
    int i;
276
    cyg_tick_count_t then, now;
277
#ifdef VERBOSE
278
    CYG_TEST_INFO( "Thread 2 running" );
279
#endif
280
    CYG_TEST_CHECK( 0 == (data & ~0x77), "Bad T2 arg: extra bits" );
281
    CYG_TEST_CHECK( 0 == (data & (data >> 4)), "Bad T2 arg: overlap" );
282
 
283
    cyg_thread_suspend( self );
284
 
285
    // depending on our config argument, optionally restart some of the
286
    // extra threads to throw noise into the scheduler:
287
    for ( i = 0; i < 3; i++ )
288
        if ( (1 << i) & data )          // bits 0-2 control
289
            cyg_thread_resume( thread[i+4] ); // extras are thread[4-6]
290
 
291
    cyg_thread_delay( DELAYFACTOR * 10 ); // let those threads run
292
 
293
    cyg_scheduler_lock();               // do this next lot atomically
294
 
295
    go_flag = 1;                        // unleash thread 3
296
    cyg_thread_resume( thread[1] );     // resume thread 1
297
 
298
    // depending on our config argument, optionally restart some of the
299
    // extra threads to throw noise into the scheduler at this later point:
300
    for ( i = 4; i < 7; i++ )
301
        if ( (1 << i) & data )          // bits 4-6 control
302
            cyg_thread_resume( thread[i] ); // extras are thread[4-6]
303
 
304
    cyg_scheduler_unlock();             // let scheduling proceed
305
 
306
    // Need a delay (but not a CPU yield) to allow t3 to awaken and act on
307
    // the go_flag, otherwise we check these details below too soon.
308
    // Actually, waiting for the clock to tick a couple of times would be
309
    // better, so that is what we will do.  Must be a busy-wait.
310
    then = cyg_current_time();
311
    do {
312
        now = cyg_current_time();
313
        // Wait longer than the delay in t3 waiting on go_flag
314
    } while ( now < (then + 3) );
315
 
316
    // Check for whatever result we expect from the protocol selected:
317
    // This mirrors what is done in configury in kmutex3.c and mutex3.cxx
318
    if ( PROTO_CEILING_MID == proto ) {
319
        CYG_TEST_INFO( "Not checking: ceiling mid value" );
320
    }
321
    else if ( PROTO_INHERIT == proto ||
322
              PROTO_CEILING_HIGH == proto ) {
323
        CYG_TEST_INFO( "Checking priority scheme operating" );
324
        CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
325
        CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
326
    }
327
    else {
328
        CYG_TEST_INFO( "Checking NO priority scheme operating" );
329
        CYG_TEST_CHECK( 0 == t3ran, "Thread 3 DID run" );
330
        CYG_TEST_CHECK( 0 == got_it, "Thread 1 DID get the mutex" );
331
    }
332
 
333
    CYG_TEST_CHECK( 0 == t3ended, "Thread 3 ended prematurely [T2,1]" );
334
 
335
    cyg_thread_delay( DELAYFACTOR * 20 ); // let those threads run
336
 
337
    CYG_TEST_CHECK( 1 == t3ran, "Thread 3 did not run" );
338
    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
339
    CYG_TEST_CHECK( 1 == t3ended, "Thread 3 has not ended" );
340
 
341
    for ( i = 0; i < 3; i++ )
342
        if ( (1 << i) & (data | data >> 4) ) // bits 0-2 and 4-6 control
343
            CYG_TEST_CHECK( 1 == extras[i+1], "Extra thread did not run" );
344
        else
345
            CYG_TEST_CHECK( 0 == extras[i+1], "Extra thread ran" );
346
 
347
    CYG_TEST_PASS( "Thread 2 exiting, AOK" );
348
    // That's all: restart the control thread.
349
    cyg_thread_resume( thread[0] );
350
}
351
 
352
// ------------------------------------------------------------------------
353
 
354
static void t3( cyg_addrword_t data )
355
{
356
#ifdef VERBOSE
357
    CYG_TEST_INFO( "Thread 3 running" );
358
#endif
359
    cyg_mutex_lock( mutex );
360
 
361
    cyg_thread_delay( DELAYFACTOR * 5 ); // let thread 3a run
362
 
363
    cyg_thread_resume( thread[2] );     // resume thread 2
364
 
365
    while ( 0 == go_flag )
366
        cyg_thread_delay(1);            // wait until we are told to go
367
 
368
    t3ran ++;                           // record the fact
369
 
370
    CYG_TEST_CHECK( 0 == got_it, "Thread 1 claims to have got my mutex" );
371
 
372
    cyg_mutex_unlock( mutex );
373
 
374
    t3ended ++;                         // record that we came back
375
 
376
    CYG_TEST_CHECK( 1 == got_it, "Thread 1 did not get the mutex" );
377
#ifdef VERBOSE
378
    CYG_TEST_INFO( "Thread 3 exit" );
379
#endif
380
}
381
 
382
// ------------------------------------------------------------------------
383
 
384
static void control_thread( cyg_addrword_t data )
385
{
386
    cyg_handle_t self = cyg_thread_self();
387
    int i, z;
388
 
389
    CYG_TEST_INIT();
390
    CYG_TEST_INFO( "Control Thread running" );
391
 
392
    // Go through the 27 possibilities of resuming the extra threads
393
    //     0: not at all
394
    //     1: early in the process
395
    //     2: later on
396
    // which are represented by bits 0-3 and 4-6 resp in the argument to
397
    // thread 2 (none set means no resume at all).
398
    for ( i = 0; i < 27; i++ ) {
399
        static int xx[] = { 0, 1, 16 };
400
        int j = i % 3;
401
        int k = (i / 3) % 3;
402
        int l = (i / 9) % 3;
403
 
404
        int d = xx[j] | (xx[k]<<1) | (xx[l]<<2) ;
405
 
406
        if ( cyg_test_is_simulator && (0 != i && 13 != i && 26 != i) )
407
            continue;    // 13 is 111 base 3, 26 is 222 base 3
408
 
409
        // Go through all these priority inversion prevention protocols:
410
        // (if supported in this configuration)
411
        // PROTO_NONE            (0)
412
        // PROTO_INHERIT         (1)
413
        // PROTO_CEILING_HIGH    (2)
414
        // PROTO_CEILING_MID     (3)
415
        // PROTO_CEILING_LOW     (4)
416
        for ( proto = PROTO_NONE; proto <= PROTO_CEILING_LOW; proto++ ) {
417
 
418
            // If no priority inheritance at all, running threads 1a and 2a is
419
            // OK, but not thread 3a; it blocks the world.
420
            if ( PROTO_NONE == proto ||
421
                 PROTO_CEILING_MID == proto ||
422
                 PROTO_CEILING_LOW == proto )
423
                if ( l )                // Cannot run thread 3a if no
424
                    continue;           // priority inheritance at all.
425
 
426
            mutex = &mutex_obj;
427
 
428
            switch ( proto ) {
429
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_NONE
430
            case PROTO_NONE:
431
                cyg_mutex_init( mutex );
432
                cyg_mutex_set_protocol( mutex, CYG_MUTEX_NONE );
433
                break;
434
#endif
435
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_INHERIT
436
            case PROTO_INHERIT:
437
                cyg_mutex_init( mutex );
438
                cyg_mutex_set_protocol( mutex, CYG_MUTEX_INHERIT );
439
                break;
440
#endif
441
#ifdef CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_CEILING
442
            case PROTO_CEILING_HIGH:
443
                cyg_mutex_init( mutex );
444
                cyg_mutex_set_protocol( mutex, CYG_MUTEX_CEILING );
445
                cyg_mutex_set_ceiling( mutex, (cyg_priority_t)  4 );
446
                break;
447
            case PROTO_CEILING_MID:
448
                cyg_mutex_init( mutex );
449
                cyg_mutex_set_protocol( mutex, CYG_MUTEX_CEILING );
450
                cyg_mutex_set_ceiling( mutex, (cyg_priority_t) 11 );
451
                break;
452
            case PROTO_CEILING_LOW:
453
                cyg_mutex_init( mutex );
454
                cyg_mutex_set_protocol( mutex, CYG_MUTEX_CEILING );
455
                cyg_mutex_set_ceiling( mutex, (cyg_priority_t) 17 );
456
                break;
457
#endif
458
            default:
459
                continue; // Break out of the prio for loop - do nothing
460
            }
461
 
462
            got_it  = 0;
463
            t3ran   = 0;
464
            t3ended = 0;
465
            for ( z = 0; z < 4; z++ ) extras[z] = 0;
466
            go_flag = 0;
467
 
468
            new_thread( t1, 0,  5, 1, "test 1" ); // Slot 1
469
            new_thread( t2, d, 10, 1, "test 2" ); // Slot 2
470
            new_thread( t3, 0, 15, 1, "test 3" ); // Slot 3
471
 
472
            new_thread( extra_thread, 1,  8, j, "extra 1" ); // Slot 4
473
            new_thread( extra_thread, 2, 12, k, "extra 2" ); // Slot 5
474
            new_thread( extra_thread, 3, 17, l, "extra 3" ); // Slot 6
475
 
476
            {
477
                static char *a[] = { "inactive", "run early", "run late" };
478
                diag_printf( "\n----- %s [%2d] New Cycle: 0x%02x, Threads 1a %s, 2a %s, 3a %s -----\n",
479
                             protnames[proto], i, d,  a[j], a[k], a[l] );
480
            }
481
 
482
            cyg_thread_suspend( self );
483
 
484
            kill_threads();
485
            cyg_mutex_destroy( mutex );
486
        }
487
    }
488
    CYG_TEST_EXIT( "Control Thread exit" );
489
}
490
 
491
// ------------------------------------------------------------------------
492
 
493
externC void
494
cyg_user_start( void )
495
{
496
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
497
    cyg_hal_invoke_constructors();
498
#endif
499
    new_thread( control_thread, 0, 2, 1, "control thread" );
500
}
501
 
502
#else // CYGVAR_KERNEL_COUNTERS_CLOCK &c
503
 
504
externC void
505
cyg_start( void )
506
{
507
    CYG_TEST_INIT();
508
    CYG_TEST_PASS_FINISH("KMutex4 test requires:\n"
509
                         "CYGFUN_KERNEL_API_C &&\n"
510
                         "CYGVAR_KERNEL_COUNTERS_CLOCK &&\n"
511
                         "(CYGNUM_KERNEL_SCHED_PRIORITIES > 20) &&\n"
512
                         "!defined(CYGPKG_KERNEL_SMP_SUPPORT) &&\n"
513
    "defined(CYGSEM_KERNEL_SYNCH_MUTEX_PRIORITY_INVERSION_PROTOCOL_DYNAMIC)\n"
514
        );
515
}
516
#endif // CYGVAR_KERNEL_COUNTERS_CLOCK &c
517
 
518
 
519
// ------------------------------------------------------------------------
520
// Documentation: enclosed is the design of this test.
521
//
522
// See mutex3.cxx or kmutex3.c
523
 
524
// ------------------------------------------------------------------------
525
// EOF mutex4.c

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.