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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 27 unneback
/*=================================================================
2
//
3
//        kmutex1.c
4
//
5
//        Kernel C API Mutex test 1
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):     dsm
44
// Contributors:    dsm
45
// Date:          1998-03-23
46
// Description:   Tests basic mutex functionality.
47
// Omissions:     Timed wait.
48
//####DESCRIPTIONEND####
49
*/
50
 
51
#include <cyg/hal/hal_arch.h>           // CYGNUM_HAL_STACK_SIZE_TYPICAL
52
 
53
#include <cyg/kernel/kapi.h>
54
 
55
#include <cyg/infra/testcase.h>
56
 
57
#ifdef CYGFUN_KERNEL_API_C
58
 
59
#include "testaux.h"
60
 
61
#define NTHREADS 3
62
#define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL
63
 
64
static cyg_handle_t thread[NTHREADS];
65
 
66
static cyg_thread thread_obj[NTHREADS];
67
static char stack[NTHREADS][STACKSIZE];
68
 
69
 
70
static cyg_mutex_t m0, m1;
71
static cyg_cond_t cvar0, cvar1, cvar2;
72
 
73
static cyg_ucount8 m0d=0, m1d=0;
74
 
75
static void finish( cyg_ucount8 t )
76
{
77
    cyg_mutex_lock( &m1 ); {
78
        m1d |= 1<<t;
79
        if( 0x7 == m1d )
80
            CYG_TEST_PASS_FINISH("Kernel C API Mutex 1 OK");
81
        cyg_cond_wait( &cvar2 );
82
    } /* cyg_mutex_unlock( &m1 ); */
83
    CYG_TEST_FAIL_FINISH("Not reached");
84
}
85
 
86
static void entry0( cyg_addrword_t data )
87
{
88
    cyg_mutex_lock( &m0 ); {
89
        CHECK( ! cyg_mutex_trylock( &m0 ) );
90
        cyg_mutex_lock( &m1 ); {
91
            CHECK( ! cyg_mutex_trylock( &m0 ) );
92
        } cyg_mutex_unlock( &m1 );
93
    } cyg_mutex_unlock( &m0 );
94
 
95
    cyg_mutex_lock( &m0 ); {
96
        while ( 0 == m0d )
97
            cyg_cond_wait( &cvar0 );
98
        CHECK( 1 == m0d++ );
99
        cyg_cond_signal( &cvar0 );
100
        while ( 4 != m0d )
101
            cyg_cond_wait( &cvar1 );
102
        CHECK( 4 == m0d );
103
    } cyg_mutex_unlock( &m0 );
104
 
105
    finish( (cyg_ucount8)data );
106
}
107
 
108
static void entry1( cyg_addrword_t data )
109
{
110
    cyg_mutex_lock( &m0 ); {
111
        CHECK( cyg_mutex_trylock( &m1 ) ); {
112
        } cyg_mutex_unlock( &m1 );
113
    } cyg_mutex_unlock( &m0 );
114
 
115
    cyg_mutex_lock( &m0 ); {
116
        CHECK( 0 == m0d++ );
117
        cyg_cond_broadcast( &cvar0 );
118
    } cyg_mutex_unlock( &m0 );
119
 
120
    cyg_mutex_lock( &m0 ); {
121
        while( 1 == m0d )
122
            cyg_cond_wait( &cvar0 );
123
        CHECK( 2 == m0d++ );
124
        cyg_cond_signal( &cvar0 );
125
        while( 3 == m0d )
126
            cyg_cond_wait( &cvar1 );
127
    } cyg_mutex_unlock( &m0 );
128
 
129
    finish( (cyg_ucount8)data );
130
}
131
 
132
static void entry2( cyg_addrword_t data )
133
{
134
    cyg_mutex_lock( &m0 ); {
135
        while( 3 != m0d ) {
136
            cyg_cond_wait( &cvar0 );
137
        }
138
        CHECK( 3 == m0d++ );
139
        cyg_cond_broadcast( &cvar1 );
140
    } cyg_mutex_unlock( &m0 );
141
 
142
    finish( (cyg_ucount8)data );
143
}
144
 
145
void kmutex1_main( void )
146
{
147
    CYG_TEST_INIT();
148
 
149
    cyg_thread_create(4, entry0 , (cyg_addrword_t)0, "kmutex1-0",
150
        (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
151
    cyg_thread_resume(thread[0]);
152
 
153
    cyg_thread_create(4, entry1 , (cyg_addrword_t)1, "kmutex1-1",
154
        (void *)stack[1], STACKSIZE, &thread[1], &thread_obj[1]);
155
    cyg_thread_resume(thread[1]);
156
 
157
    cyg_thread_create(4, entry2 , (cyg_addrword_t)2, "kmutex1-2",
158
        (void *)stack[2], STACKSIZE, &thread[2], &thread_obj[2]);
159
    cyg_thread_resume(thread[2]);
160
 
161
    cyg_mutex_init( &m0 );
162
    cyg_mutex_init( &m1 );
163
 
164
    cyg_cond_init( &cvar0, &m0 );
165
    cyg_cond_init( &cvar1, &m0 );
166
    cyg_cond_init( &cvar2, &m1 );
167
 
168
    cyg_scheduler_start();
169
 
170
    CYG_TEST_FAIL_FINISH("Not reached");
171
}
172
 
173
externC void
174
cyg_start( void )
175
{
176
    kmutex1_main();
177
}
178
 
179
#else /* def CYGFUN_KERNEL_API_C */
180
externC void
181
cyg_start( void )
182
{
183
    CYG_TEST_INIT();
184
    CYG_TEST_NA("Kernel C API layer disabled");
185
}
186
#endif /* def CYGFUN_KERNEL_API_C */
187
 
188
/* EOF kmutex1.c */

powered by: WebSVN 2.1.0

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