annotate packages/kernel/current/tests/kphilo.c @ 210:d2c90368aeef

Merge from eCos master repository on 2002-05-23-21:39:14-BST
author jlarmour
date Thu, 23 May 2002 22:59:51 +0000
parents e0c0827131d1
children 74dbf4c3f2e1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
1 //==========================================================================
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
2 //
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
3 // kphilo.c
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
4 //
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
5 // A test of the dining philosophers problem
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
6 //
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
7 //==========================================================================
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
8 //####ECOSGPLCOPYRIGHTBEGIN####
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
9 // -------------------------------------------
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
10 // This file is part of eCos, the Embedded Configurable Operating System.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
12 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
13 // eCos is free software; you can redistribute it and/or modify it under
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
14 // the terms of the GNU General Public License as published by the Free
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
15 // Software Foundation; either version 2 or (at your option) any later version.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
16 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
20 // for more details.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
21 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
22 // You should have received a copy of the GNU General Public License along
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
25 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
26 // As a special exception, if other files instantiate templates or use macros
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
27 // or inline functions from this file, or you compile this file and link it
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
28 // with other works to produce a work based on this file, this file does not
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
29 // by itself cause the resulting work to be covered by the GNU General Public
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
30 // License. However the source code for this file must still be made available
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
31 // in accordance with section (3) of the GNU General Public License.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
32 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
33 // This exception does not invalidate any other reasons why a work based on
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
34 // this file might be covered by the GNU General Public License.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
35 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
37 // at http://sources.redhat.com/ecos/ecos-license/
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
38 // -------------------------------------------
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents: 66
diff changeset
39 //####ECOSGPLCOPYRIGHTEND####
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
40 //==========================================================================
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
41 //#####DESCRIPTIONBEGIN####
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
42 //
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
43 // Author(s): dsm
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
44 // Contributors: dsm
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
45 // Date: 1998-02-24
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
46 // Description: A test of the dining philosophers problem
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
47 //####DESCRIPTIONEND####
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
48 //
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
49
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
50 #include <cyg/kernel/kapi.h>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
51
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
52 #include <cyg/infra/cyg_ass.h>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
53 #include <cyg/kernel/diag.h>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
54
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
55 // -------------------------------------------------------------------------
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
56 // Data for the philosophers problem
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
57
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
58 #define PHILOSOPHERS 15 // number of philosophers
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
59 #define STACKSIZE (2*1024) // size of thread stack
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
60
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
61 // array of stacks for philosopher threads
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
62 char thread_stack[PHILOSOPHERS][STACKSIZE];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
63
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
64 // array of threads.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
65 cyg_thread thread[PHILOSOPHERS];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
66
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
67 cyg_handle_t thread_handle[PHILOSOPHERS];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
68
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
69 // array of chopsticks
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
70 cyg_sem_t chopstick[PHILOSOPHERS];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
71
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
72 cyg_ucount32 data_index;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
73
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
74 // -------------------------------------------------------------------------
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
75 // State recording and display
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
76
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
77 static char pstate[PHILOSOPHERS+1]; // state vector showing what each
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
78 // philosopher is doing
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
79
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
80 cyg_mutex_t state_mutex;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
81
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
82 #ifdef CYG_HAL_MN10300_MN103002
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
83 static cyg_count8 eaters = 0;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
84 #endif
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
85
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
86 void change_state(int id, char newstate)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
87 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
88 cyg_mutex_lock(&state_mutex);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
89
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
90 #ifdef CYG_HAL_MN10300_MN103002
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
91 if( pstate[id] == 'E' ) eaters--;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
92 if( newstate == 'E' ) eaters++;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
93 // led(eaters);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
94 #endif
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
95
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
96 pstate[id] = newstate;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
97
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
98 diag_write_string(pstate);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
99 #if 0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
100 diag_write_char(' ');
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
101 diag_write_dec(Cyg_Scheduler::get_thread_switches());
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
102 #endif
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
103 diag_write_char('\n');
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
104
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
105 cyg_mutex_unlock(&state_mutex);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
106
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
107 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
108
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
109 char get_state( int id)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
110 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
111 char s;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
112 cyg_mutex_lock(&state_mutex);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
113
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
114 s = pstate[id];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
115
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
116 cyg_mutex_unlock(&state_mutex);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
117
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
118 return s;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
119 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
120
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
121 // -------------------------------------------------------------------------
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
122 // Thread to behave like a philosopher
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
123
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
124 void Philosopher( cyg_addrword_t vid )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
125 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
126 cyg_uint32 id = (cyg_uint32)vid;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
127 cyg_sem_t *first_stick = &chopstick[id];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
128 cyg_sem_t *second_stick = &chopstick[(id+1)%PHILOSOPHERS];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
129 #ifdef CYGPKG_INFRA_DEBUG
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
130 int left_philo = ((id==0)?PHILOSOPHERS:id)-1;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
131 int right_philo = (id==PHILOSOPHERS-1)?0:(id+1);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
132 #endif
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
133
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
134 CYG_ASSERT( id >= 0 && id < PHILOSOPHERS, "Bad id");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
135
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
136 // Deadlock avoidance. The easiest way to make the philosophers
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
137 // behave is to make each pick up the lowest numbered stick
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
138 // first. This is how it works out anyway for all the philosophers
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
139 // except the last, who must have his sticks swapped.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
140
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
141 if( id == PHILOSOPHERS-1 )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
142 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
143 cyg_sem_t *t = first_stick;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
144 first_stick = second_stick;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
145 second_stick = t;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
146 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
147
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
148 for(;;)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
149 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
150 cyg_ucount32 val;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
151
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
152 // The following variable is shared by all philosophers.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
153 // It is incremented unprotected, but this does not matter
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
154 // since it is only present to introduce a little variability
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
155 // into the think and eat times.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
156
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
157 static volatile int cycle = 0;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
158
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
159 // Think for a bit
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
160
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
161 cyg_thread_delay((id+cycle++)%12); // Cogito ergo sum...
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
162
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
163 // I am now hungry, try to get the chopsticks
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
164
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
165 change_state(id,'H');
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
166
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
167 // Get the first stick
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
168 cyg_semaphore_wait(first_stick);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
169
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
170 // Get the second stick
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
171 cyg_semaphore_wait(second_stick);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
172
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
173 // Got them, now eat
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
174
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
175 change_state(id,'E');
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
176
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
177 // Check that the world is as I think it is...
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
178 cyg_semaphore_peek( first_stick, &val);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
179 CYG_ASSERT( val == 0, "Not got first stick");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
180 cyg_semaphore_peek( second_stick, &val);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
181 CYG_ASSERT( val == 0, "Not got second stick");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
182 CYG_ASSERT( get_state(left_philo) != 'E', "Left neighbour also eating!!");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
183 CYG_ASSERT( get_state(right_philo) != 'E', "Right neighbour also eating!!");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
184
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
185 cyg_thread_delay((id+cycle++)%6); // munch munch
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
186
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
187 // Finished eating, put down sticks.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
188
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
189 change_state(id,'T');
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
190
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
191 cyg_semaphore_post( first_stick );
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
192 cyg_semaphore_post( second_stick );
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
193
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
194 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
195 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
196
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
197 // -------------------------------------------------------------------------
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
198
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
199 externC void
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
200 cyg_start( void )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
201 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
202 int i;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
203
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
204 diag_init();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
205
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
206 diag_write_string("Philosophers\n");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
207 diag_write_string("Started\n");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
208
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
209 // Zero last element in state so it acts like
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
210 // a string.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
211 pstate[PHILOSOPHERS] = 0;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
212
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
213 #if 1
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
214 for( i = 0; i < PHILOSOPHERS; i++ )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
215 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
216 change_state(i,'T'); // starting state
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
217
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
218 cyg_thread_create(4, Philosopher, (cyg_addrword_t)i, "philosopher",
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
219 (void *)(&thread_stack[i]), STACKSIZE,
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
220 &thread_handle[i], &thread[i]);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
221
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
222 // resume it
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
223 cyg_thread_resume(thread_handle[i]);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
224
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
225 // and make the matching chopstick present
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
226 cyg_semaphore_init( &chopstick[i], 1);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
227 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
228 #endif
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
229
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
230 // Get the world going
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
231 cyg_scheduler_start();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
232
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
233 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
234
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
235 // -------------------------------------------------------------------------
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
236 // EOF kphilo.c