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/] [sync2.cxx] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//        sync2.cxx
4
//
5
//        Sync test 2 -- test of different locking mechanisms
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-02-18
46
// Description: 
47
//     Creates some threads and tests the various synchronization
48
//     mechanisms.  Four threads are created t0..t3.  t0 and t3 grab a
49
//     mutex and check they have exclusive access to shared variable.
50
//     t0,t1,t2 post each other in a loop with a semaphore so that
51
//     only one is running at any time.  t1,t2,t3 do a similar thing
52
//     with counting semaphores, except that there are two active
53
//     threads.
54
// Omissions:
55
//     Doesn't test condition variables
56
//                
57
//####DESCRIPTIONEND####
58
 
59
#include <pkgconf/kernel.h>
60
 
61
#include <cyg/kernel/thread.hxx>
62
#include <cyg/kernel/thread.inl>
63
#include <cyg/kernel/sched.hxx>
64
#include <cyg/kernel/mutex.hxx>
65
#include <cyg/kernel/sema.hxx>
66
 
67
#include <cyg/infra/testcase.h>
68
 
69
#include <cyg/kernel/sched.inl>
70
 
71
#define NTHREADS 4
72
 
73
#include "testaux.hxx"
74
 
75
static Cyg_Mutex m0;
76
static Cyg_Binary_Semaphore s0, s1, s2(1);
77
static Cyg_Counting_Semaphore cs0, cs1, cs2, cs3;
78
 
79
static const cyg_ucount16 n = 1000;
80
static cyg_ucount8 m0d=99, sd=2, cd0=99, cd1=99;
81
 
82
static void entry0( CYG_ADDRWORD data )
83
{
84
    for(cyg_ucount16 i=0; i<n; i++) {
85
        s2.wait();
86
        CHECK( 2 == sd );
87
        sd = 0;
88
        m0.lock(); {
89
            m0d = 0;
90
            s0.post();
91
            CHECK( 0 == m0d );
92
        } m0.unlock();
93
    }
94
    // wait for 3 explicit posts to indicate threads have stopped.
95
    for(cyg_ucount8 i=0; i<3; i++)
96
        cs3.wait();
97
 
98
    CHECK( ! s0.posted() );
99
    CHECK( ! s1.posted() );
100
    CHECK(   s2.posted() );
101
 
102
    CHECK( 0 == cs0.peek() );
103
    CHECK( 0 == cs1.peek() );
104
    CHECK( 0 == cs2.peek() );
105
    CHECK( 0 == cs3.peek() );
106
 
107
    CHECK( 0 == cd0 );
108
    CHECK( 0 == cd1 );
109
    CYG_TEST_PASS_FINISH("Sync 2 OK");
110
    CYG_TEST_FAIL_FINISH("Not reached");
111
}
112
 
113
static void entry1( CYG_ADDRWORD data )
114
{
115
    for(cyg_ucount16 i=0; i<n; i++) {
116
        s0.wait();
117
        CHECK( 0 == sd );
118
        sd = 1;
119
        cd0 = 1;
120
        cs1.post();
121
        cd1 = 1;
122
        cs1.post();
123
        s1.post();
124
        cs0.wait();
125
        CHECK( 0 == cd0 );
126
        cs0.wait();
127
        CHECK( 0 == cd1 );
128
    }
129
    cs3.post();
130
    s0.wait();
131
    CYG_TEST_FAIL_FINISH("Not reached");
132
}
133
 
134
static void entry2( CYG_ADDRWORD data )
135
{
136
    for(cyg_ucount16 i=0; i<n; i++) {
137
        s1.wait();
138
        CHECK( 1 == sd );
139
        sd = 2;
140
        cs1.wait();
141
        CHECK( 1 == cd0 );
142
        cd0 = 2;
143
        cs2.post();
144
        s2.post();
145
        cs1.wait();
146
        CHECK( 1 == cd1 );
147
        cd1 = 2;
148
        cs2.post();
149
    }
150
    cs3.post();
151
    s1.wait();
152
    CYG_TEST_FAIL_FINISH("Not reached");
153
}
154
 
155
static void entry3( CYG_ADDRWORD data )
156
{
157
    for(cyg_ucount16 i=0; i < n*2; i++)  {
158
        cs2.wait();
159
        CHECK( 2 == cd0 || 2 == cd1 );
160
        m0.lock(); {
161
            m0d = 3;
162
            if( 2 == cd0 )
163
                cd0 = 0;
164
            else {
165
                CHECK( 2 == cd1 );
166
                cd1 = 0;
167
            }
168
            cs0.post();
169
            CHECK( 3 == m0d );
170
        } m0.unlock();
171
    }
172
    cs3.post();
173
    cs1.wait();
174
    CYG_TEST_FAIL_FINISH("Not reached");
175
}
176
 
177
 
178
void sync2_main(void)
179
{
180
    CYG_TEST_INIT();
181
 
182
    new_thread(entry0, 0);
183
    new_thread(entry1, 1);
184
    new_thread(entry2, 2);
185
    new_thread(entry3, 3);
186
 
187
    Cyg_Scheduler::start();
188
 
189
    CYG_TEST_PASS_FINISH("Not reached");
190
}
191
 
192
externC void
193
cyg_start( void )
194
{
195
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
196
    cyg_hal_invoke_constructors();
197
#endif
198
    sync2_main();
199
}
200
 
201
// EOF sync2.cxx

powered by: WebSVN 2.1.0

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