annotate packages/kernel/current/tests/kphilo.c @ 3403:8c8767bb9a62 default tip

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