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

Subversion Repositories openrisc

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

powered by: WebSVN 2.1.0

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