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