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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [bin_sem2.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
//        bin_sem2.cxx
4
//
5
//        Binary semaphore test 2
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,dsm
43
// Contributors:    dsm
44
// Date:          1998-03-10
45
// Description:
46
//     Dining philosophers test.  Based on philo.cxx
47
//####DESCRIPTIONEND####
48
 
49
#include <pkgconf/kernel.h>
50
 
51
#include <cyg/kernel/sched.hxx>
52
#include <cyg/kernel/thread.hxx>
53
#include <cyg/kernel/thread.inl>
54
#include <cyg/kernel/mutex.hxx>
55
 
56
#include <cyg/kernel/sema.hxx>
57
 
58
#include <cyg/infra/testcase.h>
59
 
60
#include <cyg/kernel/sched.inl>
61
#include CYGHWR_MEMORY_LAYOUT_H
62
 
63
static cyg_ucount16 PHILO_LOOPS = 1000;
64
 
65
#if (CYGMEM_REGION_ram_SIZE <= 32768)
66
# define PHILOSOPHERS 5
67
#elif (CYGMEM_REGION_ram_SIZE <= 49152)
68
# define PHILOSOPHERS 9
69
#elif (CYGMEM_REGION_ram_SIZE <= 65536)
70
# define PHILOSOPHERS 11
71
#else
72
# define PHILOSOPHERS 15
73
#endif
74
#define NTHREADS PHILOSOPHERS
75
#include "testaux.hxx"
76
 
77
static Cyg_Binary_Semaphore chopstick[PHILOSOPHERS];
78
 
79
static char pstate[PHILOSOPHERS+1];       // state of each philosopher
80
 
81
static cyg_ucount16 state_changes = 0;
82
// state_changes keep track of number of changes to pstate so
83
// we can exit after we've seen enough.
84
 
85
 
86
static Cyg_Mutex pstate_mutex;
87
static Cyg_Mutex cycle_mutex;
88
 
89
static inline int left(cyg_count8 i)
90
{
91
    return (0 == i) ? PHILOSOPHERS-1 : i-1 ;
92
}
93
static inline int right(cyg_count8 i)
94
{
95
    return (PHILOSOPHERS == i+1) ? 0 : i+1 ;
96
}
97
 
98
void change_state(int id, char newstate)
99
{
100
    if (PHILO_LOOPS == state_changes++)
101
        CYG_TEST_PASS_FINISH("Binary Semaphore 2 OK");
102
 
103
 
104
    pstate_mutex.lock(); {
105
        pstate[id] = newstate;
106
        bool all_hungry = true;         // until proved otherwise
107
        for(cyg_ucount8 i=0; i < PHILOSOPHERS; i++) {
108
            if('E' == pstate[i]) {
109
                CHECK('E' != pstate[left(i)]);
110
                CHECK('E' != pstate[right(i)]);
111
            }
112
            if('H' != pstate[i]) {
113
                all_hungry = false;
114
            }
115
        }
116
        // Theoretically it is possible for all the philosophers to be
117
        // hungry but not waiting on semaphores.  But in practice this
118
        // means something is wrong.
119
        CHECK(false == all_hungry);
120
    } pstate_mutex.unlock();
121
}
122
 
123
char get_state(int id)
124
{
125
    pstate_mutex.lock();
126
 
127
    char s = pstate[id];
128
 
129
    pstate_mutex.unlock();
130
 
131
    return s;
132
}
133
 
134
// -------------------------------------------------------------------------
135
// Thread to behave like a philosopher
136
 
137
void Philosopher( CYG_ADDRESS id )
138
{
139
    Cyg_Thread *self = Cyg_Thread::self();
140
    Cyg_Binary_Semaphore *first_stick = &chopstick[id];
141
    Cyg_Binary_Semaphore *second_stick = &chopstick[(id+1)%PHILOSOPHERS];
142
 
143
    CHECK( id >= 0 && id < PHILOSOPHERS);
144
 
145
    // Deadlock avoidance. The easiest way to make the philosophers
146
    // behave is to make each pick up the lowest numbered stick
147
    // first. This is how it works out anyway for all the philosophers
148
    // except the last, who must have his sticks swapped.
149
 
150
    if( id == PHILOSOPHERS-1 )
151
    {
152
        Cyg_Binary_Semaphore *t = first_stick;
153
        first_stick = second_stick;
154
        second_stick = t;
155
    }
156
 
157
 
158
    // The following variable is shared by all philosophers.
159
    // It is incremented unprotected, but this does not matter
160
    // since it is only present to introduce a little variability
161
    // into the think and eat times.
162
 
163
    static int cycle = 0;
164
 
165
    for(;;)
166
    {
167
        // Think for a bit
168
 
169
        self->delay((id+cycle++)%12);    // Cogito ergo sum...
170
 
171
        // I am now hungry, try to get the chopsticks
172
        change_state(id,'H');
173
 
174
        // Get the sticks
175
        first_stick->wait();
176
        second_stick->wait();
177
 
178
        // Got them, now eat
179
        change_state(id,'E');
180
 
181
        // Check that the world is as I think it is...
182
        CYG_TEST_CHECK( !first_stick->posted(),
183
                        "Not got first stick");
184
        CYG_TEST_CHECK( !second_stick->posted(),
185
                        "Not got second stick");
186
        CYG_TEST_CHECK( get_state(left(id)) != 'E',
187
                        "Left neighbour also eating!!");
188
        CYG_TEST_CHECK( get_state(right(id)) != 'E',
189
                        "Right neighbour also eating!!");
190
 
191
        self->delay((id+cycle++)%6);    // munch munch
192
 
193
        // Finished eating, put down sticks.
194
 
195
        change_state(id,'T');
196
 
197
        // put sticks back on table
198
        first_stick->post();
199
        second_stick->post();
200
    }
201
}
202
 
203
// -------------------------------------------------------------------------
204
 
205
void bin_sem2_main( void )
206
{
207
    CYG_TEST_INIT();
208
 
209
    if (cyg_test_is_simulator)
210
        PHILO_LOOPS = 100;
211
 
212
    for( int i = 0; i < PHILOSOPHERS; i++ )
213
    {
214
        pstate[i] = 'T';            // starting state
215
        new_thread(Philosopher, i);
216
 
217
        // make the matching chopstick present
218
        chopstick[i].post();
219
    }
220
 
221
    Cyg_Scheduler::scheduler.start();
222
}
223
 
224
externC void
225
cyg_start( void )
226
{
227
#ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG
228
    cyg_hal_invoke_constructors();
229
#endif
230
    bin_sem2_main();
231
}
232
// EOF bin_sem2.cxx

powered by: WebSVN 2.1.0

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