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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [kernel/] [v2_0/] [tests/] [mutex2.cxx] - Blame information for rev 228

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

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

powered by: WebSVN 2.1.0

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