Mercurial > ecos
annotate packages/kernel/current/tests/bin_sem2.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 // bin_sem2.cxx | |
| 4 // | |
| 5 // Binary semaphore test 2 | |
| 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): nickg,dsm | |
| 35 // Contributors: dsm | |
| 36 // Date: 1998-03-10 | |
| 37 // Description: | |
| 38 // Dining philosophers test. Based on philo.cxx | |
| 39 //####DESCRIPTIONEND#### | |
| 40 | |
| 41 #include <pkgconf/kernel.h> | |
| 42 | |
| 43 #include <cyg/kernel/sched.hxx> | |
| 44 #include <cyg/kernel/thread.hxx> | |
| 45 #include <cyg/kernel/thread.inl> | |
| 46 #include <cyg/kernel/mutex.hxx> | |
| 47 | |
| 48 #include <cyg/kernel/sema.hxx> | |
| 49 | |
| 50 #include <cyg/infra/testcase.h> | |
| 51 | |
| 52 #include <cyg/kernel/sched.inl> | |
| 53 | |
| 2 | 54 static cyg_ucount16 PHILO_LOOPS = 1000; |
| 0 | 55 |
| 56 #define PHILOSOPHERS 15 | |
| 57 #define NTHREADS PHILOSOPHERS | |
| 58 #include "testaux.hxx" | |
| 59 | |
| 60 static Cyg_Binary_Semaphore chopstick[PHILOSOPHERS]; | |
| 61 | |
| 62 static char pstate[PHILOSOPHERS+1]; // state of each philosopher | |
| 63 | |
| 2 | 64 static cyg_ucount16 state_changes = 0; |
| 0 | 65 // state_changes keep track of number of changes to pstate so |
| 66 // we can exit after we've seen enough. | |
| 67 | |
| 68 | |
| 69 static Cyg_Mutex pstate_mutex; | |
| 70 static Cyg_Mutex cycle_mutex; | |
| 71 | |
| 72 static inline int left(cyg_count8 i) | |
| 73 { | |
| 74 return (0 == i) ? PHILOSOPHERS-1 : i-1 ; | |
| 75 } | |
| 76 static inline int right(cyg_count8 i) | |
| 77 { | |
| 78 return (PHILOSOPHERS == i+1) ? 0 : i+1 ; | |
| 79 } | |
| 80 | |
| 81 void change_state(int id, char newstate) | |
| 82 { | |
| 83 if (PHILO_LOOPS == state_changes++) | |
| 84 CYG_TEST_PASS_FINISH("Binary Semaphore 2 OK"); | |
| 85 | |
| 86 | |
| 87 pstate_mutex.lock(); { | |
| 88 pstate[id] = newstate; | |
| 89 bool all_hungry = true; // until proved otherwise | |
| 90 for(cyg_ucount8 i=0; i < PHILOSOPHERS; i++) { | |
| 91 if('E' == pstate[i]) { | |
| 92 CHECK('E' != pstate[left(i)]); | |
| 93 CHECK('E' != pstate[right(i)]); | |
| 94 } | |
| 95 if('H' != pstate[i]) { | |
| 96 all_hungry = false; | |
| 97 } | |
| 98 } | |
| 99 // Theoretically it is possible for all the philosophers to be | |
| 100 // hungry but not waiting on semaphores. But in practice this | |
| 101 // means something is wrong. | |
| 102 CHECK(false == all_hungry); | |
| 103 } pstate_mutex.unlock(); | |
| 104 } | |
| 105 | |
| 106 char get_state(int id) | |
| 107 { | |
| 108 pstate_mutex.lock(); | |
| 109 | |
| 110 char s = pstate[id]; | |
| 111 | |
| 112 pstate_mutex.unlock(); | |
| 113 | |
| 114 return s; | |
| 115 } | |
| 116 | |
| 117 // ------------------------------------------------------------------------- | |
| 118 // Thread to behave like a philosopher | |
| 119 | |
| 120 void Philosopher( CYG_ADDRESS id ) | |
| 121 { | |
| 122 Cyg_Thread *self = Cyg_Thread::self(); | |
| 123 Cyg_Binary_Semaphore *first_stick = &chopstick[id]; | |
| 124 Cyg_Binary_Semaphore *second_stick = &chopstick[(id+1)%PHILOSOPHERS]; | |
| 125 | |
| 126 CHECK( id >= 0 && id < PHILOSOPHERS); | |
| 127 | |
| 128 // Deadlock avoidance. The easiest way to make the philosophers | |
| 129 // behave is to make each pick up the lowest numbered stick | |
| 130 // first. This is how it works out anyway for all the philosophers | |
| 131 // except the last, who must have his sticks swapped. | |
| 132 | |
| 133 if( id == PHILOSOPHERS-1 ) | |
| 134 { | |
| 135 Cyg_Binary_Semaphore *t = first_stick; | |
| 136 first_stick = second_stick; | |
| 137 second_stick = t; | |
| 138 } | |
| 139 | |
| 140 | |
| 141 // The following variable is shared by all philosophers. | |
| 142 // It is incremented unprotected, but this does not matter | |
| 143 // since it is only present to introduce a little variability | |
| 144 // into the think and eat times. | |
| 145 | |
| 146 static int cycle = 0; | |
| 147 | |
| 148 for(;;) | |
| 149 { | |
| 150 // Think for a bit | |
| 151 | |
| 152 self->delay((id+cycle++)%12); // Cogito ergo sum... | |
| 153 | |
| 154 // I am now hungry, try to get the chopsticks | |
| 155 change_state(id,'H'); | |
| 156 | |
| 157 // Get the sticks | |
| 158 first_stick->wait(); | |
| 159 second_stick->wait(); | |
| 160 | |
| 161 // Got them, now eat | |
| 162 change_state(id,'E'); | |
| 163 | |
| 164 // Check that the world is as I think it is... | |
| 165 CYG_TEST_CHECK( !first_stick->posted(), | |
| 166 "Not got first stick"); | |
| 167 CYG_TEST_CHECK( !second_stick->posted(), | |
| 168 "Not got second stick"); | |
| 169 CYG_TEST_CHECK( get_state(left(id)) != 'E', | |
| 170 "Left neighbour also eating!!"); | |
| 171 CYG_TEST_CHECK( get_state(right(id)) != 'E', | |
| 172 "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 // put sticks back on table | |
| 181 first_stick->post(); | |
| 182 second_stick->post(); | |
| 183 } | |
| 184 } | |
| 185 | |
| 186 // ------------------------------------------------------------------------- | |
| 187 | |
| 188 void bin_sem2_main( void ) | |
| 189 { | |
| 190 CYG_TEST_INIT(); | |
| 191 | |
| 2 | 192 if (cyg_test_is_simulator) |
| 193 PHILO_LOOPS = 100; | |
| 194 | |
| 0 | 195 for( int i = 0; i < PHILOSOPHERS; i++ ) |
| 196 { | |
| 197 pstate[i] = 'T'; // starting state | |
| 198 new_thread(Philosopher, i); | |
| 199 | |
| 200 // make the matching chopstick present | |
| 201 chopstick[i].post(); | |
| 202 } | |
| 203 | |
| 204 Cyg_Scheduler::scheduler.start(); | |
| 205 } | |
| 206 | |
| 207 externC void | |
| 208 cyg_start( void ) | |
| 209 { | |
|
130
eb9fd8c04db3
Merge from eCos master repository on 2000-10-23-17:01:37-BST
jlarmour
parents:
66
diff
changeset
|
210 #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
|
211 cyg_hal_invoke_constructors(); |
|
eb9fd8c04db3
Merge from eCos master repository on 2000-10-23-17:01:37-BST
jlarmour
parents:
66
diff
changeset
|
212 #endif |
| 0 | 213 bin_sem2_main(); |
| 214 } | |
| 215 // EOF bin_sem2.cxx |
