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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [kmutex4.c] - Blame information for rev 851

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

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

powered by: WebSVN 2.1.0

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