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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [kernel/] [current/] [tests/] [kphilo.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//        kphilo.c
4
//
5
//        A test of the dining philosophers problem
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-24
45
// Description:   A test of the dining philosophers problem
46
//####DESCRIPTIONEND####
47
// 
48
 
49
#include <cyg/kernel/kapi.h>
50
 
51
#include <cyg/infra/cyg_ass.h>
52
#include <cyg/kernel/diag.h>
53
 
54
// -------------------------------------------------------------------------
55
// Data for the philosophers problem
56
 
57
#define PHILOSOPHERS    15               // number of philosophers
58
#define STACKSIZE       (2*1024)        // size of thread stack
59
 
60
// array of stacks for philosopher threads
61
char thread_stack[PHILOSOPHERS][STACKSIZE];
62
 
63
// array of threads.
64
cyg_thread thread[PHILOSOPHERS];
65
 
66
cyg_handle_t thread_handle[PHILOSOPHERS];
67
 
68
// array of chopsticks
69
cyg_sem_t chopstick[PHILOSOPHERS];
70
 
71
cyg_ucount32 data_index;
72
 
73
// -------------------------------------------------------------------------
74
// State recording and display
75
 
76
static char pstate[PHILOSOPHERS+1];     // state vector showing what each
77
                                        // philosopher is doing
78
 
79
cyg_mutex_t state_mutex;
80
 
81
#ifdef CYG_HAL_MN10300_MN103002
82
static cyg_count8 eaters = 0;
83
#endif
84
 
85
void change_state(int id, char newstate)
86
{
87
    cyg_mutex_lock(&state_mutex);
88
 
89
#ifdef CYG_HAL_MN10300_MN103002
90
    if( pstate[id] == 'E' ) eaters--;
91
    if( newstate == 'E' ) eaters++;
92
//    led(eaters);
93
#endif
94
 
95
    pstate[id] = newstate;
96
 
97
    diag_write_string(pstate);
98
#if 0
99
    diag_write_char(' ');
100
    diag_write_dec(Cyg_Scheduler::get_thread_switches());
101
#endif    
102
    diag_write_char('\n');
103
 
104
    cyg_mutex_unlock(&state_mutex);
105
 
106
}
107
 
108
char get_state( int id)
109
{
110
    char s;
111
    cyg_mutex_lock(&state_mutex);
112
 
113
    s = pstate[id];
114
 
115
    cyg_mutex_unlock(&state_mutex);
116
 
117
    return s;
118
}
119
 
120
// -------------------------------------------------------------------------
121
// Thread to behave like a philosopher
122
 
123
void Philosopher( cyg_addrword_t vid )
124
{
125
    cyg_uint32 id = (cyg_uint32)vid;
126
    cyg_sem_t *first_stick = &chopstick[id];
127
    cyg_sem_t *second_stick = &chopstick[(id+1)%PHILOSOPHERS];
128
#ifdef CYGPKG_INFRA_DEBUG
129
    int left_philo = ((id==0)?PHILOSOPHERS:id)-1;
130
    int right_philo = (id==PHILOSOPHERS-1)?0:(id+1);
131
#endif
132
 
133
    CYG_ASSERT( id >= 0 && id < PHILOSOPHERS, "Bad id");
134
 
135
    // Deadlock avoidance. The easiest way to make the philosophers
136
    // behave is to make each pick up the lowest numbered stick
137
    // first. This is how it works out anyway for all the philosophers
138
    // except the last, who must have his sticks swapped.
139
 
140
    if( id == PHILOSOPHERS-1 )
141
    {
142
        cyg_sem_t *t = first_stick;
143
        first_stick = second_stick;
144
        second_stick = t;
145
    }
146
 
147
    for(;;)
148
    {
149
        cyg_ucount32 val;
150
 
151
        // The following variable is shared by all philosophers.
152
        // It is incremented unprotected, but this does not matter
153
        // since it is only present to introduce a little variability
154
        // into the think and eat times.
155
 
156
        static volatile int cycle = 0;
157
 
158
        // Think for a bit
159
 
160
        cyg_thread_delay((id+cycle++)%12);    // Cogito ergo sum...
161
 
162
        // I am now hungry, try to get the chopsticks
163
 
164
        change_state(id,'H');
165
 
166
        // Get the first stick
167
        cyg_semaphore_wait(first_stick);
168
 
169
        // Get the second stick
170
        cyg_semaphore_wait(second_stick);
171
 
172
        // Got them, now eat
173
 
174
        change_state(id,'E');
175
 
176
        // Check that the world is as I think it is...
177
        cyg_semaphore_peek( first_stick, &val);
178
        CYG_ASSERT( val == 0, "Not got first stick");
179
        cyg_semaphore_peek( second_stick, &val);
180
        CYG_ASSERT( val == 0, "Not got second stick");
181
        CYG_ASSERT( get_state(left_philo) != 'E', "Left neighbour also eating!!");
182
        CYG_ASSERT( get_state(right_philo) != 'E', "Right neighbour also eating!!");
183
 
184
        cyg_thread_delay((id+cycle++)%6);    // munch munch
185
 
186
        // Finished eating, put down sticks.
187
 
188
        change_state(id,'T');
189
 
190
        cyg_semaphore_post( first_stick );
191
        cyg_semaphore_post( second_stick );
192
 
193
    }
194
}
195
 
196
// -------------------------------------------------------------------------
197
 
198
externC void
199
cyg_start( void )
200
{
201
    int i;
202
 
203
    diag_init();
204
 
205
    diag_write_string("Philosophers\n");
206
    diag_write_string("Started\n");
207
 
208
    // Zero last element in state so it acts like
209
    // a string.
210
    pstate[PHILOSOPHERS] = 0;
211
 
212
#if 1
213
    for( i = 0; i < PHILOSOPHERS; i++ )
214
    {
215
        change_state(i,'T');            // starting state
216
 
217
        cyg_thread_create(4, Philosopher, (cyg_addrword_t)i, "philosopher",
218
            (void *)(&thread_stack[i]), STACKSIZE,
219
            &thread_handle[i], &thread[i]);
220
 
221
        // resume it
222
        cyg_thread_resume(thread_handle[i]);
223
 
224
        // and make the matching chopstick present
225
        cyg_semaphore_init( &chopstick[i], 1);
226
    }
227
#endif
228
 
229
    // Get the world going
230
    cyg_scheduler_start();
231
 
232
}
233
 
234
// -------------------------------------------------------------------------
235
// EOF kphilo.c

powered by: WebSVN 2.1.0

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