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