annotate packages/kernel/current/tests/bin_sem2.cxx @ 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 6724f8254bcb
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 // bin_sem2.cxx
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 // Binary semaphore test 2
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: 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
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): nickg,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-03-10
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
45 // Description:
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
46 // Dining philosophers test. Based on philo.cxx
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
47 //####DESCRIPTIONEND####
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 <pkgconf/kernel.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/kernel/sched.hxx>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
52 #include <cyg/kernel/thread.hxx>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
53 #include <cyg/kernel/thread.inl>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
54 #include <cyg/kernel/mutex.hxx>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
55
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
56 #include <cyg/kernel/sema.hxx>
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
57
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
58 #include <cyg/infra/testcase.h>
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 #include <cyg/kernel/sched.inl>
3120
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
61 #include CYGHWR_MEMORY_LAYOUT_H
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
62
2
443894e2e912 Block commit of eCos version 1.2.1
jlarmour
parents: 0
diff changeset
63 static cyg_ucount16 PHILO_LOOPS = 1000;
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
64
3120
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
65 #if (CYGMEM_REGION_ram_SIZE <= 32768)
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
66 # define PHILOSOPHERS 5
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
67 #elif (CYGMEM_REGION_ram_SIZE <= 49152)
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
68 # define PHILOSOPHERS 9
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
69 #elif (CYGMEM_REGION_ram_SIZE <= 65536)
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
70 # define PHILOSOPHERS 11
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
71 #else
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
72 # define PHILOSOPHERS 15
6724f8254bcb * tests/bin_sem2.cxx (PHILOSOPHERS):
sergeig
parents: 2729
diff changeset
73 #endif
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
74 #define NTHREADS PHILOSOPHERS
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
75 #include "testaux.hxx"
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
76
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
77 static Cyg_Binary_Semaphore chopstick[PHILOSOPHERS];
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 static char pstate[PHILOSOPHERS+1]; // state of each philosopher
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
80
2
443894e2e912 Block commit of eCos version 1.2.1
jlarmour
parents: 0
diff changeset
81 static cyg_ucount16 state_changes = 0;
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
82 // state_changes keep track of number of changes to pstate so
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
83 // we can exit after we've seen enough.
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
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
86 static Cyg_Mutex pstate_mutex;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
87 static Cyg_Mutex cycle_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 static inline int left(cyg_count8 i)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
90 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
91 return (0 == i) ? PHILOSOPHERS-1 : i-1 ;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
92 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
93 static inline int right(cyg_count8 i)
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 return (PHILOSOPHERS == i+1) ? 0 : i+1 ;
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
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
98 void change_state(int id, char newstate)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
99 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
100 if (PHILO_LOOPS == state_changes++)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
101 CYG_TEST_PASS_FINISH("Binary Semaphore 2 OK");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
102
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 pstate_mutex.lock(); {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
105 pstate[id] = newstate;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
106 bool all_hungry = true; // until proved otherwise
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
107 for(cyg_ucount8 i=0; i < PHILOSOPHERS; i++) {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
108 if('E' == pstate[i]) {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
109 CHECK('E' != pstate[left(i)]);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
110 CHECK('E' != pstate[right(i)]);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
111 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
112 if('H' != pstate[i]) {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
113 all_hungry = false;
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 }
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
116 // Theoretically it is possible for all the philosophers to be
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
117 // hungry but not waiting on semaphores. But in practice this
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
118 // means something is wrong.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
119 CHECK(false == all_hungry);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
120 } pstate_mutex.unlock();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
121 }
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 char get_state(int id)
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 pstate_mutex.lock();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
126
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
127 char s = pstate[id];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
128
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
129 pstate_mutex.unlock();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
130
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
131 return s;
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
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 // Thread to behave like a philosopher
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
136
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
137 void Philosopher( CYG_ADDRESS id )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
138 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
139 Cyg_Thread *self = Cyg_Thread::self();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
140 Cyg_Binary_Semaphore *first_stick = &chopstick[id];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
141 Cyg_Binary_Semaphore *second_stick = &chopstick[(id+1)%PHILOSOPHERS];
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
142
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
143 CHECK( id >= 0 && id < PHILOSOPHERS);
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
144
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
145 // Deadlock avoidance. The easiest way to make the philosophers
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
146 // behave is to make each pick up the lowest numbered stick
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
147 // first. This is how it works out anyway for all the philosophers
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
148 // except the last, who must have his sticks swapped.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
149
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
150 if( id == PHILOSOPHERS-1 )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
151 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
152 Cyg_Binary_Semaphore *t = first_stick;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
153 first_stick = second_stick;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
154 second_stick = t;
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
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 // The following variable is shared by all philosophers.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
159 // It is incremented unprotected, but this does not matter
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
160 // since it is only present to introduce a little variability
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
161 // into the think and eat times.
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
162
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
163 static int cycle = 0;
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
164
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
165 for(;;)
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
166 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
167 // Think for a bit
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 self->delay((id+cycle++)%12); // Cogito ergo sum...
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
170
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
171 // I am now hungry, try to get the chopsticks
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
172 change_state(id,'H');
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 // Get the sticks
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
175 first_stick->wait();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
176 second_stick->wait();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
177
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
178 // Got them, now eat
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
179 change_state(id,'E');
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
180
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
181 // Check that the world is as I think it is...
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
182 CYG_TEST_CHECK( !first_stick->posted(),
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
183 "Not got first stick");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
184 CYG_TEST_CHECK( !second_stick->posted(),
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
185 "Not got second stick");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
186 CYG_TEST_CHECK( get_state(left(id)) != 'E',
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
187 "Left neighbour also eating!!");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
188 CYG_TEST_CHECK( get_state(right(id)) != 'E',
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
189 "Right neighbour also eating!!");
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
190
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
191 self->delay((id+cycle++)%6); // munch munch
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 // Finished eating, put down sticks.
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 change_state(id,'T');
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 // put sticks back on table
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
198 first_stick->post();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
199 second_stick->post();
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 }
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 // -------------------------------------------------------------------------
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 void bin_sem2_main( void )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
206 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
207 CYG_TEST_INIT();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
208
2
443894e2e912 Block commit of eCos version 1.2.1
jlarmour
parents: 0
diff changeset
209 if (cyg_test_is_simulator)
443894e2e912 Block commit of eCos version 1.2.1
jlarmour
parents: 0
diff changeset
210 PHILO_LOOPS = 100;
443894e2e912 Block commit of eCos version 1.2.1
jlarmour
parents: 0
diff changeset
211
0
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
212 for( int i = 0; i < PHILOSOPHERS; i++ )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
213 {
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
214 pstate[i] = 'T'; // starting state
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
215 new_thread(Philosopher, i);
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 // make the matching chopstick present
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
218 chopstick[i].post();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
219 }
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 Cyg_Scheduler::scheduler.start();
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
222 }
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 externC void
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
225 cyg_start( void )
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
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
3111d98ba7b3 Initial commit of eCos version 1.1
jlarmour
parents:
diff changeset
230 bin_sem2_main();
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 // EOF bin_sem2.cxx