Mercurial > ecos-v2_0-branch
annotate packages/kernel/current/tests/sync2.cxx @ 66:bf00f99aec69 ecos-sw-2000-02-02
Merge from eCos master repository on 2000-02-02-19:16:44-GMT
| author | jlarmour |
|---|---|
| date | Wed, 02 Feb 2000 19:57:02 +0000 |
| parents | c38311975d4f |
| children | eb9fd8c04db3 |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 3 // sync2.cxx | |
| 4 // | |
| 5 // Sync test 2 -- test of different locking mechanisms | |
| 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-18 | |
| 37 // Description: | |
| 38 // Creates some threads and tests the various synchronization | |
| 39 // mechanisms. Four threads are created t0..t3. t0 and t3 grab a | |
| 40 // mutex and check they have exclusive access to shared variable. | |
| 41 // t0,t1,t2 post each other in a loop with a semaphore so that | |
| 42 // only one is running at any time. t1,t2,t3 do a similar thing | |
| 43 // with counting semaphores, except that there are two active | |
| 44 // threads. | |
| 45 // Omissions: | |
| 46 // Doesn't test condition variables | |
| 47 // | |
| 48 //####DESCRIPTIONEND#### | |
| 49 | |
| 50 #include <pkgconf/kernel.h> | |
| 51 | |
| 52 #include <cyg/kernel/thread.hxx> | |
| 53 #include <cyg/kernel/thread.inl> | |
| 54 #include <cyg/kernel/sched.hxx> | |
| 55 #include <cyg/kernel/mutex.hxx> | |
| 56 #include <cyg/kernel/sema.hxx> | |
| 57 | |
| 58 #include <cyg/infra/testcase.h> | |
| 59 | |
| 60 #include <cyg/kernel/sched.inl> | |
| 61 | |
| 62 #define NTHREADS 4 | |
| 63 | |
| 64 #include "testaux.hxx" | |
| 65 | |
| 66 static Cyg_Mutex m0; | |
| 67 static Cyg_Binary_Semaphore s0, s1, s2(1); | |
| 68 static Cyg_Counting_Semaphore cs0, cs1, cs2, cs3; | |
| 69 | |
| 70 static const cyg_ucount16 n = 1000; | |
| 71 static cyg_ucount8 m0d=99, sd=2, cd0=99, cd1=99; | |
| 72 | |
| 73 static void entry0( CYG_ADDRWORD data ) | |
| 74 { | |
| 75 for(cyg_ucount16 i=0; i<n; i++) { | |
| 2 | 76 s2.wait(); |
| 77 CHECK( 2 == sd ); | |
| 78 sd = 0; | |
| 79 m0.lock(); { | |
| 80 m0d = 0; | |
| 81 s0.post(); | |
| 82 CHECK( 0 == m0d ); | |
| 83 } m0.unlock(); | |
| 0 | 84 } |
| 85 // wait for 3 explicit posts to indicate threads have stopped. | |
| 86 for(cyg_ucount8 i=0; i<3; i++) | |
| 2 | 87 cs3.wait(); |
| 0 | 88 |
| 89 CHECK( ! s0.posted() ); | |
| 90 CHECK( ! s1.posted() ); | |
| 91 CHECK( s2.posted() ); | |
| 92 | |
| 93 CHECK( 0 == cs0.peek() ); | |
| 94 CHECK( 0 == cs1.peek() ); | |
| 95 CHECK( 0 == cs2.peek() ); | |
| 96 CHECK( 0 == cs3.peek() ); | |
| 97 | |
| 98 CHECK( 0 == cd0 ); | |
| 99 CHECK( 0 == cd1 ); | |
| 100 CYG_TEST_PASS_FINISH("Sync 2 OK"); | |
| 101 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 102 } | |
| 103 | |
| 104 static void entry1( CYG_ADDRWORD data ) | |
| 105 { | |
| 106 for(cyg_ucount16 i=0; i<n; i++) { | |
| 2 | 107 s0.wait(); |
| 108 CHECK( 0 == sd ); | |
| 109 sd = 1; | |
| 110 cd0 = 1; | |
| 111 cs1.post(); | |
| 112 cd1 = 1; | |
| 113 cs1.post(); | |
| 114 s1.post(); | |
| 115 cs0.wait(); | |
| 116 CHECK( 0 == cd0 ); | |
| 117 cs0.wait(); | |
| 118 CHECK( 0 == cd1 ); | |
| 0 | 119 } |
| 120 cs3.post(); | |
| 121 s0.wait(); | |
| 122 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 123 } | |
| 124 | |
| 125 static void entry2( CYG_ADDRWORD data ) | |
| 126 { | |
| 127 for(cyg_ucount16 i=0; i<n; i++) { | |
| 2 | 128 s1.wait(); |
| 129 CHECK( 1 == sd ); | |
| 130 sd = 2; | |
| 131 cs1.wait(); | |
| 132 CHECK( 1 == cd0 ); | |
| 133 cd0 = 2; | |
| 134 cs2.post(); | |
| 135 s2.post(); | |
| 136 cs1.wait(); | |
| 137 CHECK( 1 == cd1 ); | |
| 138 cd1 = 2; | |
| 139 cs2.post(); | |
| 0 | 140 } |
| 141 cs3.post(); | |
| 142 s1.wait(); | |
| 143 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 144 } | |
| 145 | |
| 146 static void entry3( CYG_ADDRWORD data ) | |
| 147 { | |
| 148 for(cyg_ucount16 i=0; i < n*2; i++) { | |
| 2 | 149 cs2.wait(); |
| 150 CHECK( 2 == cd0 || 2 == cd1 ); | |
| 151 m0.lock(); { | |
| 152 m0d = 3; | |
| 153 if( 2 == cd0 ) | |
| 154 cd0 = 0; | |
| 155 else { | |
| 156 CHECK( 2 == cd1 ); | |
| 157 cd1 = 0; | |
| 158 } | |
| 159 cs0.post(); | |
| 160 CHECK( 3 == m0d ); | |
| 161 } m0.unlock(); | |
| 0 | 162 } |
| 163 cs3.post(); | |
| 164 cs1.wait(); | |
| 165 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 166 } | |
| 167 | |
| 168 | |
| 169 void sync2_main(void) | |
| 170 { | |
| 171 CYG_TEST_INIT(); | |
| 172 | |
| 173 new_thread(entry0, 0); | |
| 174 new_thread(entry1, 1); | |
| 175 new_thread(entry2, 2); | |
| 176 new_thread(entry3, 3); | |
| 177 | |
| 178 Cyg_Scheduler::start(); | |
| 179 | |
| 180 CYG_TEST_PASS_FINISH("Not reached"); | |
| 181 } | |
| 182 | |
| 183 externC void | |
| 184 cyg_start( void ) | |
| 185 { | |
| 186 sync2_main(); | |
| 187 } | |
| 188 | |
| 189 // EOF sync2.cxx |
