Mercurial > ecos-v3_0-branch
annotate packages/kernel/current/tests/philo.cxx @ 130:eb9fd8c04db3 ecos-sw-2000-10-23
Merge from eCos master repository on 2000-10-23-17:01:37-BST
| author | jlarmour |
|---|---|
| date | Mon, 23 Oct 2000 17:10:57 +0000 |
| parents | bf00f99aec69 |
| children | e0c0827131d1 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 3 // philo.cxx | |
| 4 // | |
| 5 // A test of the dining philosophers problem | |
| 6 // | |
| 7 //========================================================================== | |
| 8 //####COPYRIGHTBEGIN#### | |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
9 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
10 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
11 // The contents of this file are subject to the Red Hat eCos Public License |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
12 // Version 1.1 (the "License"); you may not use this file except in |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
13 // compliance with the License. You may obtain a copy of the License at |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
14 // http://www.redhat.com/ |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
15 // |
|
66
bf00f99aec69
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
jlarmour
parents:
64
diff
changeset
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
64
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
18 // License for the specific language governing rights and limitations under |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
19 // the License. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
20 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
21 // The Original Code is eCos - Embedded Configurable Operating System, |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
22 // released September 30, 1998. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
23 // |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
24 // The Initial Developer of the Original Code is Red Hat. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
25 // Portions created by Red Hat are |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
27 // All Rights Reserved. |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
28 // ------------------------------------------- |
|
c38311975d4f
Merge from eCos master repository on 2000-01-28-04:28:11-GMT
jlarmour
parents:
2
diff
changeset
|
29 // |
| 0 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): dsm | |
| 35 // Contributors: dsm | |
| 36 // Date: 1998-02-24 | |
| 37 // Description: A test of the dining philosophers problem | |
| 38 //####DESCRIPTIONEND#### | |
| 39 // | |
| 40 | |
| 41 #include <cyg/kernel/kernel.hxx> | |
| 42 | |
| 43 #include <cyg/hal/hal_io.h> | |
| 44 | |
| 45 // ------------------------------------------------------------------------- | |
| 46 // Data for the philosophers problem | |
| 47 | |
| 2 | 48 #define PHILOSOPHERS 15 // number of philosophers |
| 0 | 49 #define STACKSIZE (2*1024) // size of thread stack |
| 50 | |
| 2 | 51 #define NTHREADS PHILOSOPHERS |
| 52 #include "testaux.hxx" | |
| 0 | 53 |
| 54 // array of chopsticks | |
| 55 Cyg_Binary_Semaphore chopstick[PHILOSOPHERS]; | |
| 56 | |
| 57 | |
| 58 //cyg_thread_entry Philosopher; | |
| 59 | |
| 60 // ------------------------------------------------------------------------- | |
| 61 // State recording and display | |
| 62 | |
| 63 static char pstate[PHILOSOPHERS+1]; // state vector showing what each | |
| 64 // philosopher is doing | |
| 65 | |
| 66 Cyg_Mutex state_mutex; | |
| 67 | |
| 68 #ifdef CYG_HAL_MN10300_MN103002 | |
| 69 static cyg_count8 eaters = 0; | |
| 70 #endif | |
| 71 | |
| 72 void change_state(int id, char newstate) | |
| 73 { | |
| 74 CYG_INSTRUMENT_USER( 1, 0, 0); | |
| 75 state_mutex.lock(); | |
| 76 CYG_INSTRUMENT_USER( 2, 0, 0); | |
| 77 | |
| 78 #ifdef CYG_HAL_MN10300_MN103002 | |
| 79 if( pstate[id] == 'E' ) eaters--; | |
| 80 if( newstate == 'E' ) eaters++; | |
| 81 // led(eaters); | |
| 82 #endif | |
| 83 | |
| 84 pstate[id] = newstate; | |
| 85 | |
| 86 diag_write_string(pstate); | |
| 87 #if 0 | |
| 88 diag_write_char(' '); | |
| 89 diag_write_dec(Cyg_Scheduler::get_thread_switches()); | |
| 90 #endif | |
| 91 diag_write_char('\n'); | |
| 92 | |
| 93 CYG_INSTRUMENT_USER( 3, 0, 0); | |
| 94 state_mutex.unlock(); | |
| 95 CYG_INSTRUMENT_USER( 4, 0, 0); | |
| 96 | |
| 97 } | |
| 98 | |
| 99 char get_state( int id) | |
| 100 { | |
| 101 state_mutex.lock(); | |
| 102 | |
| 103 char s = pstate[id]; | |
| 104 | |
| 105 state_mutex.unlock(); | |
| 106 | |
| 107 return s; | |
| 108 } | |
| 109 | |
| 110 // ------------------------------------------------------------------------- | |
| 111 // Thread to behave like a philosopher | |
| 112 | |
| 113 void Philosopher( CYG_ADDRESS id ) | |
| 114 { | |
| 115 Cyg_Thread *self = Cyg_Thread::self(); | |
| 116 Cyg_Binary_Semaphore *first_stick = &chopstick[id]; | |
| 117 Cyg_Binary_Semaphore *second_stick = &chopstick[(id+1)%PHILOSOPHERS]; | |
| 118 #ifdef CYGPKG_INFRA_DEBUG | |
| 119 int left_philo = ((id==0)?PHILOSOPHERS:id)-1; | |
| 120 int right_philo = (id==PHILOSOPHERS-1)?0:(id+1); | |
| 121 #endif | |
| 122 | |
| 123 CYG_ASSERT( id >= 0 && id < PHILOSOPHERS, "Bad id"); | |
| 124 | |
| 125 // Deadlock avoidance. The easiest way to make the philosophers | |
| 126 // behave is to make each pick up the lowest numbered stick | |
| 127 // first. This is how it works out anyway for all the philosophers | |
| 128 // except the last, who must have his sticks swapped. | |
| 129 | |
| 130 if( id == PHILOSOPHERS-1 ) | |
| 131 { | |
| 132 Cyg_Binary_Semaphore *t = first_stick; | |
| 133 first_stick = second_stick; | |
| 134 second_stick = t; | |
| 135 } | |
| 136 | |
| 137 // The following variable is shared by all philosophers. | |
| 138 // It is incremented unprotected, but this does not matter | |
| 139 // since it is only present to introduce a little variability | |
| 140 // into the think and eat times. | |
| 141 | |
| 142 static int cycle = 0; | |
| 143 | |
| 144 for(;;) | |
| 145 { | |
| 146 // Think for a bit | |
| 147 | |
| 148 self->delay((id+cycle++)%12); // Cogito ergo sum... | |
| 149 | |
| 150 // I am now hungry, try to get the chopsticks | |
| 151 | |
| 152 change_state(id,'H'); | |
| 153 | |
| 154 // Get the first stick | |
| 155 CYG_INSTRUMENT_USER( 5, 0, 0); | |
| 156 first_stick->wait(); | |
| 157 CYG_INSTRUMENT_USER( 6, 0, 0); | |
| 158 | |
| 159 // Get the second stick | |
| 160 CYG_INSTRUMENT_USER( 7, 0, 0); | |
| 161 second_stick->wait(); | |
| 162 CYG_INSTRUMENT_USER( 8, 0, 0); | |
| 163 | |
| 164 // Got them, now eat | |
| 165 | |
| 166 change_state(id,'E'); | |
| 167 | |
| 168 // Check that the world is as I think it is... | |
| 169 CYG_ASSERT( !first_stick->posted(), "Not got first stick"); | |
| 170 CYG_ASSERT( !second_stick->posted(), "Not got second stick"); | |
| 171 CYG_ASSERT( get_state(left_philo) != 'E', "Left neighbour also eating!!"); | |
| 172 CYG_ASSERT( get_state(right_philo) != 'E', "Right neighbour also eating!!"); | |
| 173 | |
| 174 self->delay((id+cycle++)%6); // munch munch | |
| 175 | |
| 176 // Finished eating, put down sticks. | |
| 177 | |
| 178 change_state(id,'T'); | |
| 179 | |
| 180 CYG_INSTRUMENT_USER( 9, 0, 0); | |
| 181 first_stick->post(); | |
| 182 CYG_INSTRUMENT_USER( 10, 0, 0); | |
| 183 second_stick->post(); | |
| 184 CYG_INSTRUMENT_USER( 11, 0, 0); | |
| 185 | |
| 186 // Cyg_Scheduler::lock(); | |
| 187 // Cyg_Scheduler::unlock(); | |
| 188 CYG_INSTRUMENT_USER( 12, 0, 0); | |
| 189 | |
| 190 } | |
| 191 } | |
| 192 | |
| 193 // ------------------------------------------------------------------------- | |
| 194 | |
| 195 externC void | |
| 196 cyg_start( void ) | |
| 197 { | |
|
130
eb9fd8c04db3
Merge from eCos master repository on 2000-10-23-17:01:37-BST
jlarmour
parents:
66
diff
changeset
|
198 #ifdef CYGSEM_HAL_STOP_CONSTRUCTORS_ON_FLAG |
|
eb9fd8c04db3
Merge from eCos master repository on 2000-10-23-17:01:37-BST
jlarmour
parents:
66
diff
changeset
|
199 cyg_hal_invoke_constructors(); |
|
eb9fd8c04db3
Merge from eCos master repository on 2000-10-23-17:01:37-BST
jlarmour
parents:
66
diff
changeset
|
200 #endif |
| 0 | 201 diag_init(); |
| 202 | |
| 203 diag_write_string("Philosophers\n"); | |
| 204 diag_write_string("Started\n"); | |
| 205 | |
| 206 // Zero last element in state so it acts like | |
| 207 // a string. | |
| 208 pstate[PHILOSOPHERS] = 0; | |
| 209 | |
| 210 #if 1 | |
| 211 for( int i = 0; i < PHILOSOPHERS; i++ ) | |
| 212 { | |
| 213 change_state(i,'T'); // starting state | |
| 214 | |
| 215 // Start the philosopher | |
| 2 | 216 Cyg_Thread *t = new_thread( Philosopher, i ); |
| 0 | 217 |
| 218 // resume it | |
| 219 t->resume(); | |
| 220 | |
| 221 // and make the matching chopstick present | |
| 222 chopstick[i].post(); | |
| 223 } | |
| 224 #endif | |
| 225 | |
| 226 // Get the world going | |
| 227 Cyg_Scheduler::scheduler.start(); | |
| 228 | |
| 229 } | |
| 230 | |
| 231 // ------------------------------------------------------------------------- | |
| 232 // EOF philo.cxx |
