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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [mutex2.cxx] - 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
//        mutex1.cxx
4
//
5
//        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 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):     nickg
43
// Contributors:  nickg
44
// Date:          1999-02-19
45
// Description:   Tests mutex release functionality
46
//####DESCRIPTIONEND####
47
 
48
#include <pkgconf/kernel.h>
49
 
50
#include <cyg/kernel/sched.hxx>        // Cyg_Scheduler::start()
51
#include <cyg/kernel/thread.hxx>       // Cyg_Thread
52
 
53
#include <cyg/kernel/mutex.hxx>
54
 
55
#include <cyg/infra/testcase.h>
56
 
57
#include <cyg/kernel/sched.inl>
58
#include <cyg/kernel/thread.inl>
59
 
60
// ------------------------------------------------------------------------
61
 
62
#if !defined(CYGPKG_KERNEL_SMP_SUPPORT)
63
 
64
// ------------------------------------------------------------------------
65
 
66
#define NTHREADS 4
67
#include "testaux.hxx"
68
#include "testaux.h"
69
 
70
// ------------------------------------------------------------------------
71
 
72
static Cyg_Mutex m0, m1;
73
static Cyg_Condition_Variable cvar0( m0 ), cvar1( m0 ), cvar2( m1 );
74
 
75
volatile int thread_state[NTHREADS];
76
 
77
// ------------------------------------------------------------------------
78
// This thread is meant to get hung up trying to re-acquire m0
79
// after waiting on the cv.
80
 
81
static void entry0( CYG_ADDRWORD data )
82
{
83
    CYG_TEST_INFO( "thread0: lock mutex 0");
84
 
85
    m0.lock();
86
 
87
    CYG_TEST_INFO( "thread0: wait cvar 0");
88
 
89
    thread_state[data] = 1;
90
 
91
    cvar0.wait();
92
 
93
    thread_state[data] = 2;
94
 
95
    CYG_TEST_INFO( "thread0: woke from cvar 0");
96
 
97
    CYG_TEST_INFO( "thread0: unlock mutex 0");
98
 
99
    m0.unlock();
100
 
101
    thread_state[data] = 3;
102
 
103
    CYG_TEST_INFO( "thread0: exit");
104
 
105
    thread[data]->exit();
106
}
107
 
108
// ------------------------------------------------------------------------
109
// This thread is meant to claim and keep m0.
110
 
111
static void entry1( CYG_ADDRWORD data )
112
{
113
    CYG_TEST_INFO( "thread1: lock mutex 0");
114
 
115
    m0.lock();
116
 
117
    CYG_TEST_INFO( "thread1: lock mutex 1");
118
 
119
    thread_state[data] = 1;
120
 
121
    m1.lock();
122
 
123
    thread_state[data] = 2;
124
 
125
    CYG_TEST_INFO( "thread1: wait cvar 2");
126
 
127
    cvar2.wait();
128
 
129
    thread_state[data] = 3;
130
 
131
    CYG_TEST_INFO( "thread1: woke from cvar 2");
132
 
133
    CYG_TEST_INFO( "thread1: unlock mutex 1");
134
 
135
    m1.unlock();
136
 
137
    thread_state[data] = 4;
138
 
139
    CYG_TEST_INFO( "thread1: unlock m0");
140
 
141
    m0.unlock();
142
 
143
    thread_state[data] = 5;
144
 
145
    CYG_TEST_INFO( "thread1: exit");
146
 
147
    thread[data]->exit();
148
}
149
 
150
// ------------------------------------------------------------------------
151
// This thread is meant to get hung trying to acquire m0, and then get
152
// released out of it by thread3.
153
 
154
static void entry2( CYG_ADDRWORD data )
155
{
156
    CYG_TEST_INFO( "thread2: lock mutex 0");
157
 
158
    thread_state[data] = 1;
159
 
160
    if( m0.lock() )
161
    {
162
        thread_state[data] = 2;
163
 
164
        CYG_TEST_INFO( "thread2: lock mutex 0 - returned TRUE");
165
        CYG_TEST_FAIL_FINISH(" m0.lock() returned TRUE" );
166
    }
167
    else
168
    {
169
        thread_state[data] = 3;
170
        CYG_TEST_INFO( "thread2: lock mutex 0 - returned FALSE");
171
    }
172
 
173
    CYG_TEST_INFO( "thread2: exit");
174
 
175
    thread[data]->exit();
176
}
177
 
178
// ------------------------------------------------------------------------
179
 
180
static void entry3( CYG_ADDRWORD data )
181
{
182
 
183
    CHECK( thread_state[0] == 1 );
184
    CHECK( thread_state[1] == 2 );
185
    CHECK( thread_state[2] == 1 );
186
 
187
    CYG_TEST_INFO( "thread3: signal  cvar 0");
188
 
189
    cvar0.signal();
190
 
191
    CHECK( thread_state[0] == 1 );
192
    CHECK( thread_state[1] == 2 );
193
    CHECK( thread_state[2] == 1 );
194
 
195
    CYG_TEST_INFO( "thread3: release mutex 0");
196
 
197
    m0.release();
198
 
199
    CHECK( thread_state[0] == 1 );
200
    CHECK( thread_state[1] == 2 );
201
    CHECK( thread_state[2] == 3 );
202
 
203
    CYG_TEST_INFO( "thread3: signal cvar 2");
204
 
205
    cvar2.signal();
206
 
207
    CHECK( thread_state[0] == 3 );
208
    CHECK( thread_state[1] == 5 );
209
    CHECK( thread_state[2] == 3 );
210
 
211
    CYG_TEST_PASS_FINISH( "mutex2 finished OK");
212
 
213
    CYG_TEST_INFO( "thread3: exit");
214
 
215
    thread[data]->exit();
216
}
217
 
218
// ------------------------------------------------------------------------
219
 
220
void mutex2_main( void )
221
{
222
    CYG_TEST_INIT();
223
 
224
    new_thread(entry0, 0);
225
    new_thread(entry1, 1);
226
    new_thread(entry2, 2);
227
    new_thread(entry3, 3);
228
 
229
    // Set priorities from the top to prevent two threads getting
230
    // the same priority: this causes an ASSERT on some configurations.
231
    thread[3]->set_priority( 5 );
232
    thread[2]->set_priority( 4 );
233
    thread[1]->set_priority( 3 );
234
    thread[0]->set_priority( 2 );
235
 
236
    Cyg_Scheduler::start();
237
 
238
    CYG_TEST_FAIL_FINISH("Not reached");
239
}
240
 
241
// ------------------------------------------------------------------------
242
 
243
externC void
244
cyg_start( void )
245
{
246
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
247
    cyg_hal_invoke_constructors();
248
#endif
249
    mutex2_main();
250
}
251
 
252
// ------------------------------------------------------------------------
253
 
254
#else // CYGPKG_KERNEL_SMP_SUPPORT
255
 
256
// ------------------------------------------------------------------------
257
 
258
externC void
259
cyg_start( void )
260
{
261
    CYG_TEST_INIT();
262
    CYG_TEST_NA("Mutex2 test requires: !defined(CYGPKG_KERNEL_SMP_SUPPORT)");
263
 
264
}
265
 
266
// ------------------------------------------------------------------------
267
 
268
#endif // CYGPKG_KERNEL_SMP_SUPPORT
269
 
270
// ------------------------------------------------------------------------
271
// EOF mutex2.cxx

powered by: WebSVN 2.1.0

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