Mercurial > ecos
annotate packages/kernel/current/tests/mutex2.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 |
|---|---|
| 2 | 1 //========================================================================== |
| 2 // | |
| 3 // mutex1.cxx | |
| 4 // | |
| 5 // Mutex test 1 | |
| 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 // |
| 2 | 30 //####COPYRIGHTEND#### |
| 31 //========================================================================== | |
| 32 //#####DESCRIPTIONBEGIN#### | |
| 33 // | |
| 34 // Author(s): nickg | |
| 35 // Contributors: nickg | |
| 36 // Date: 1999-02-19 | |
| 37 // Description: Tests mutex release functionality | |
| 38 //####DESCRIPTIONEND#### | |
| 39 | |
| 40 #include <pkgconf/kernel.h> | |
| 41 | |
| 42 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start() | |
| 43 #include <cyg/kernel/thread.hxx> // Cyg_Thread | |
| 44 | |
| 45 #include <cyg/kernel/mutex.hxx> | |
| 46 | |
| 47 #include <cyg/infra/testcase.h> | |
| 48 | |
| 49 #include <cyg/kernel/sched.inl> | |
| 50 #include <cyg/kernel/thread.inl> | |
| 51 | |
| 52 | |
| 53 #define NTHREADS 4 | |
| 54 #include "testaux.hxx" | |
| 55 #include "testaux.h" | |
| 56 | |
| 57 static Cyg_Mutex m0, m1; | |
| 58 static Cyg_Condition_Variable cvar0( m0 ), cvar1( m0 ), cvar2( m1 ); | |
| 59 | |
| 60 volatile int thread_state[NTHREADS]; | |
| 61 | |
| 62 // This thread is meant to get hung up trying to re-acquire m0 | |
| 63 // after waiting on the cv. | |
| 64 static void entry0( CYG_ADDRWORD data ) | |
| 65 { | |
| 66 CYG_TEST_INFO( "thread0: lock mutex 0"); | |
| 67 | |
| 68 m0.lock(); | |
| 69 | |
| 70 CYG_TEST_INFO( "thread0: wait cvar 0"); | |
| 71 | |
| 72 thread_state[data] = 1; | |
| 73 | |
| 74 cvar0.wait(); | |
| 75 | |
| 76 thread_state[data] = 2; | |
| 77 | |
| 78 CYG_TEST_INFO( "thread0: woke from cvar 0"); | |
| 79 | |
| 80 CYG_TEST_INFO( "thread0: unlock mutex 0"); | |
| 81 | |
| 82 m0.unlock(); | |
| 83 | |
| 84 thread_state[data] = 3; | |
| 85 | |
| 86 CYG_TEST_INFO( "thread0: exit"); | |
| 87 | |
| 88 thread[data]->exit(); | |
| 89 } | |
| 90 | |
| 91 // This thread is meant to claim and keep m0. | |
| 92 static void entry1( CYG_ADDRWORD data ) | |
| 93 { | |
| 94 CYG_TEST_INFO( "thread1: lock mutex 0"); | |
| 95 | |
| 96 m0.lock(); | |
| 97 | |
| 98 CYG_TEST_INFO( "thread1: lock mutex 1"); | |
| 99 | |
| 100 thread_state[data] = 1; | |
| 101 | |
| 102 m1.lock(); | |
| 103 | |
| 104 thread_state[data] = 2; | |
| 105 | |
| 106 CYG_TEST_INFO( "thread1: wait cvar 2"); | |
| 107 | |
| 108 cvar2.wait(); | |
| 109 | |
| 110 thread_state[data] = 3; | |
| 111 | |
| 112 CYG_TEST_INFO( "thread1: woke from cvar 2"); | |
| 113 | |
| 114 CYG_TEST_INFO( "thread1: unlock mutex 1"); | |
| 115 | |
| 116 m1.unlock(); | |
| 117 | |
| 118 thread_state[data] = 4; | |
| 119 | |
| 120 CYG_TEST_INFO( "thread1: unlock m0"); | |
| 121 | |
| 122 m0.unlock(); | |
| 123 | |
| 124 thread_state[data] = 5; | |
| 125 | |
| 126 CYG_TEST_INFO( "thread1: exit"); | |
| 127 | |
| 128 thread[data]->exit(); | |
| 129 } | |
| 130 | |
| 131 // This thread is meant to get hung trying to acquire m0, and then get | |
| 132 // released out of it by thread3. | |
| 133 | |
| 134 static void entry2( CYG_ADDRWORD data ) | |
| 135 { | |
| 136 CYG_TEST_INFO( "thread2: lock mutex 0"); | |
| 137 | |
| 138 thread_state[data] = 1; | |
| 139 | |
| 140 if( m0.lock() ) | |
| 141 { | |
| 142 thread_state[data] = 2; | |
| 143 | |
| 144 CYG_TEST_INFO( "thread2: lock mutex 0 - returned TRUE"); | |
| 145 CYG_TEST_FAIL_FINISH(" m0.lock() returned TRUE" ); | |
| 146 } | |
| 147 else | |
| 148 { | |
| 149 thread_state[data] = 3; | |
| 150 CYG_TEST_INFO( "thread2: lock mutex 0 - returned FALSE"); | |
| 151 } | |
| 152 | |
| 153 CYG_TEST_INFO( "thread2: exit"); | |
| 154 | |
| 155 thread[data]->exit(); | |
| 156 } | |
| 157 | |
| 158 static void entry3( CYG_ADDRWORD data ) | |
| 159 { | |
| 160 | |
| 161 CHECK( thread_state[0] == 1 ); | |
| 162 CHECK( thread_state[1] == 2 ); | |
| 163 CHECK( thread_state[2] == 1 ); | |
| 164 | |
| 165 CYG_TEST_INFO( "thread3: signal cvar 0"); | |
| 166 | |
| 167 cvar0.signal(); | |
| 168 | |
| 169 CHECK( thread_state[0] == 1 ); | |
| 170 CHECK( thread_state[1] == 2 ); | |
| 171 CHECK( thread_state[2] == 1 ); | |
| 172 | |
| 173 CYG_TEST_INFO( "thread3: release mutex 0"); | |
| 174 | |
| 175 m0.release(); | |
| 176 | |
| 177 CHECK( thread_state[0] == 1 ); | |
| 178 CHECK( thread_state[1] == 2 ); | |
| 179 CHECK( thread_state[2] == 3 ); | |
| 180 | |
| 181 CYG_TEST_INFO( "thread3: signal cvar 2"); | |
| 182 | |
| 183 cvar2.signal(); | |
| 184 | |
| 185 CHECK( thread_state[0] == 3 ); | |
| 186 CHECK( thread_state[1] == 5 ); | |
| 187 CHECK( thread_state[2] == 3 ); | |
| 188 | |
| 189 CYG_TEST_PASS_FINISH( "mutex2 finished OK"); | |
| 190 | |
| 191 CYG_TEST_INFO( "thread3: exit"); | |
| 192 | |
| 193 thread[data]->exit(); | |
| 194 } | |
| 195 | |
| 196 void mutex2_main( void ) | |
| 197 { | |
| 198 CYG_TEST_INIT(); | |
| 199 | |
| 200 new_thread(entry0, 0); | |
| 201 new_thread(entry1, 1); | |
| 202 new_thread(entry2, 2); | |
| 203 new_thread(entry3, 3); | |
| 204 | |
| 205 // Set priorities from the top to prevent two threads getting | |
| 206 // the same priority: this causes an ASSERT on some configurations. | |
| 207 thread[3]->set_priority( 5 ); | |
| 208 thread[2]->set_priority( 4 ); | |
| 209 thread[1]->set_priority( 3 ); | |
| 210 thread[0]->set_priority( 2 ); | |
| 211 | |
| 212 Cyg_Scheduler::start(); | |
| 213 | |
| 214 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 215 } | |
| 216 | |
| 217 externC void | |
| 218 cyg_start( void ) | |
| 219 { | |
| 220 mutex2_main(); | |
| 221 } | |
| 222 | |
| 223 // EOF mutex2.cxx |
