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

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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