|
2
|
1 //========================================================================== |
|
|
2 // |
|
|
3 // mutex1.cxx |
|
|
4 // |
|
|
5 // Mutex test 1 |
|
|
6 // |
|
|
7 //========================================================================== |
|
|
8 //####COPYRIGHTBEGIN#### |
|
|
9 // |
|
|
10 // ------------------------------------------- |
|
|
11 // The contents of this file are subject to the Cygnus eCos Public License |
|
|
12 // Version 1.0 (the "License"); you may not use this file except in |
|
|
13 // compliance with the License. You may obtain a copy of the License at |
|
|
14 // http://sourceware.cygnus.com/ecos |
|
|
15 // |
|
|
16 // Software distributed under the License is distributed on an "AS IS" |
|
|
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|
|
18 // License for the specific language governing rights and limitations under |
|
|
19 // the License. |
|
|
20 // |
|
|
21 // The Original Code is eCos - Embedded Cygnus Operating System, released |
|
|
22 // September 30, 1998. |
|
|
23 // |
|
|
24 // The Initial Developer of the Original Code is Cygnus. Portions created |
|
|
25 // by Cygnus are Copyright (C) 1998,1999 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): nickg |
|
|
33 // Contributors: nickg |
|
|
34 // Date: 1999-02-19 |
|
|
35 // Description: Tests mutex release functionality |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 |
|
|
38 #include <pkgconf/kernel.h> |
|
|
39 |
|
|
40 #include <cyg/kernel/sched.hxx> // Cyg_Scheduler::start() |
|
|
41 #include <cyg/kernel/thread.hxx> // Cyg_Thread |
|
|
42 |
|
|
43 #include <cyg/kernel/mutex.hxx> |
|
|
44 |
|
|
45 #include <cyg/infra/testcase.h> |
|
|
46 |
|
|
47 #include <cyg/kernel/sched.inl> |
|
|
48 #include <cyg/kernel/thread.inl> |
|
|
49 |
|
|
50 |
|
|
51 #define NTHREADS 4 |
|
|
52 #include "testaux.hxx" |
|
|
53 #include "testaux.h" |
|
|
54 |
|
|
55 static Cyg_Mutex m0, m1; |
|
|
56 static Cyg_Condition_Variable cvar0( m0 ), cvar1( m0 ), cvar2( m1 ); |
|
|
57 |
|
|
58 volatile int thread_state[NTHREADS]; |
|
|
59 |
|
|
60 // This thread is meant to get hung up trying to re-acquire m0 |
|
|
61 // after waiting on the cv. |
|
|
62 static void entry0( CYG_ADDRWORD data ) |
|
|
63 { |
|
|
64 CYG_TEST_INFO( "thread0: lock mutex 0"); |
|
|
65 |
|
|
66 m0.lock(); |
|
|
67 |
|
|
68 CYG_TEST_INFO( "thread0: wait cvar 0"); |
|
|
69 |
|
|
70 thread_state[data] = 1; |
|
|
71 |
|
|
72 cvar0.wait(); |
|
|
73 |
|
|
74 thread_state[data] = 2; |
|
|
75 |
|
|
76 CYG_TEST_INFO( "thread0: woke from cvar 0"); |
|
|
77 |
|
|
78 CYG_TEST_INFO( "thread0: unlock mutex 0"); |
|
|
79 |
|
|
80 m0.unlock(); |
|
|
81 |
|
|
82 thread_state[data] = 3; |
|
|
83 |
|
|
84 CYG_TEST_INFO( "thread0: exit"); |
|
|
85 |
|
|
86 thread[data]->exit(); |
|
|
87 } |
|
|
88 |
|
|
89 // This thread is meant to claim and keep m0. |
|
|
90 static void entry1( CYG_ADDRWORD data ) |
|
|
91 { |
|
|
92 CYG_TEST_INFO( "thread1: lock mutex 0"); |
|
|
93 |
|
|
94 m0.lock(); |
|
|
95 |
|
|
96 CYG_TEST_INFO( "thread1: lock mutex 1"); |
|
|
97 |
|
|
98 thread_state[data] = 1; |
|
|
99 |
|
|
100 m1.lock(); |
|
|
101 |
|
|
102 thread_state[data] = 2; |
|
|
103 |
|
|
104 CYG_TEST_INFO( "thread1: wait cvar 2"); |
|
|
105 |
|
|
106 cvar2.wait(); |
|
|
107 |
|
|
108 thread_state[data] = 3; |
|
|
109 |
|
|
110 CYG_TEST_INFO( "thread1: woke from cvar 2"); |
|
|
111 |
|
|
112 CYG_TEST_INFO( "thread1: unlock mutex 1"); |
|
|
113 |
|
|
114 m1.unlock(); |
|
|
115 |
|
|
116 thread_state[data] = 4; |
|
|
117 |
|
|
118 CYG_TEST_INFO( "thread1: unlock m0"); |
|
|
119 |
|
|
120 m0.unlock(); |
|
|
121 |
|
|
122 thread_state[data] = 5; |
|
|
123 |
|
|
124 CYG_TEST_INFO( "thread1: exit"); |
|
|
125 |
|
|
126 thread[data]->exit(); |
|
|
127 } |
|
|
128 |
|
|
129 // This thread is meant to get hung trying to acquire m0, and then get |
|
|
130 // released out of it by thread3. |
|
|
131 |
|
|
132 static void entry2( CYG_ADDRWORD data ) |
|
|
133 { |
|
|
134 CYG_TEST_INFO( "thread2: lock mutex 0"); |
|
|
135 |
|
|
136 thread_state[data] = 1; |
|
|
137 |
|
|
138 if( m0.lock() ) |
|
|
139 { |
|
|
140 thread_state[data] = 2; |
|
|
141 |
|
|
142 CYG_TEST_INFO( "thread2: lock mutex 0 - returned TRUE"); |
|
|
143 CYG_TEST_FAIL_FINISH(" m0.lock() returned TRUE" ); |
|
|
144 } |
|
|
145 else |
|
|
146 { |
|
|
147 thread_state[data] = 3; |
|
|
148 CYG_TEST_INFO( "thread2: lock mutex 0 - returned FALSE"); |
|
|
149 } |
|
|
150 |
|
|
151 CYG_TEST_INFO( "thread2: exit"); |
|
|
152 |
|
|
153 thread[data]->exit(); |
|
|
154 } |
|
|
155 |
|
|
156 static void entry3( CYG_ADDRWORD data ) |
|
|
157 { |
|
|
158 |
|
|
159 CHECK( thread_state[0] == 1 ); |
|
|
160 CHECK( thread_state[1] == 2 ); |
|
|
161 CHECK( thread_state[2] == 1 ); |
|
|
162 |
|
|
163 CYG_TEST_INFO( "thread3: signal cvar 0"); |
|
|
164 |
|
|
165 cvar0.signal(); |
|
|
166 |
|
|
167 CHECK( thread_state[0] == 1 ); |
|
|
168 CHECK( thread_state[1] == 2 ); |
|
|
169 CHECK( thread_state[2] == 1 ); |
|
|
170 |
|
|
171 CYG_TEST_INFO( "thread3: release mutex 0"); |
|
|
172 |
|
|
173 m0.release(); |
|
|
174 |
|
|
175 CHECK( thread_state[0] == 1 ); |
|
|
176 CHECK( thread_state[1] == 2 ); |
|
|
177 CHECK( thread_state[2] == 3 ); |
|
|
178 |
|
|
179 CYG_TEST_INFO( "thread3: signal cvar 2"); |
|
|
180 |
|
|
181 cvar2.signal(); |
|
|
182 |
|
|
183 CHECK( thread_state[0] == 3 ); |
|
|
184 CHECK( thread_state[1] == 5 ); |
|
|
185 CHECK( thread_state[2] == 3 ); |
|
|
186 |
|
|
187 CYG_TEST_PASS_FINISH( "mutex2 finished OK"); |
|
|
188 |
|
|
189 CYG_TEST_INFO( "thread3: exit"); |
|
|
190 |
|
|
191 thread[data]->exit(); |
|
|
192 } |
|
|
193 |
|
|
194 void mutex2_main( void ) |
|
|
195 { |
|
|
196 CYG_TEST_INIT(); |
|
|
197 |
|
|
198 new_thread(entry0, 0); |
|
|
199 new_thread(entry1, 1); |
|
|
200 new_thread(entry2, 2); |
|
|
201 new_thread(entry3, 3); |
|
|
202 |
|
|
203 // Set priorities from the top to prevent two threads getting |
|
|
204 // the same priority: this causes an ASSERT on some configurations. |
|
|
205 thread[3]->set_priority( 5 ); |
|
|
206 thread[2]->set_priority( 4 ); |
|
|
207 thread[1]->set_priority( 3 ); |
|
|
208 thread[0]->set_priority( 2 ); |
|
|
209 |
|
|
210 Cyg_Scheduler::start(); |
|
|
211 |
|
|
212 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
213 } |
|
|
214 |
|
|
215 externC void |
|
|
216 cyg_start( void ) |
|
|
217 { |
|
|
218 mutex2_main(); |
|
|
219 } |
|
|
220 |
|
|
221 // EOF mutex2.cxx |