|
0
|
1 //========================================================================== |
|
|
2 // |
|
|
3 // thread_gdb.c |
|
|
4 // |
|
|
5 // A test for thread support in GDB |
|
|
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 Cygnus Solutions. All Rights Reserved. |
|
|
26 // ------------------------------------------- |
|
|
27 // |
|
|
28 //####COPYRIGHTEND#### |
|
|
29 //========================================================================== |
|
|
30 //#####DESCRIPTIONBEGIN#### |
|
|
31 // |
|
|
32 // Author(s): nickg |
|
|
33 // Contributors: nickg |
|
|
34 // Date: 1998-09-21 |
|
|
35 // Description: GDB thread support test. |
|
|
36 //####DESCRIPTIONEND#### |
|
|
37 // |
|
|
38 |
|
|
39 #include <cyg/kernel/kapi.h> |
|
|
40 |
|
|
41 #include <cyg/infra/cyg_ass.h> |
|
|
42 |
|
|
43 #include <cyg/infra/testcase.h> |
|
|
44 |
|
|
45 #if defined(CYGFUN_KERNEL_API_C) && defined(CYGSEM_KERNEL_SCHED_MLQUEUE) |
|
|
46 |
|
|
47 // ------------------------------------------------------------------------- |
|
|
48 |
|
|
49 #define THREADS 10 |
|
|
50 |
|
|
51 #define STACKSIZE (2*1024) |
|
|
52 |
|
|
53 #define CONTROLLER_PRI_HI 0 |
|
|
54 #define CONTROLLER_PRI_LO 25 |
|
|
55 |
|
|
56 #define WORKER_PRI 3 |
|
|
57 #define WORKER_PRI_RANGE 20 |
|
|
58 |
|
|
59 |
|
|
60 // ------------------------------------------------------------------------- |
|
|
61 |
|
|
62 // array of stacks for threads |
|
|
63 char thread_stack[THREADS][STACKSIZE]; |
|
|
64 |
|
|
65 // array of threads. |
|
|
66 cyg_thread thread[THREADS]; |
|
|
67 |
|
|
68 cyg_handle_t thread_handle[THREADS]; |
|
|
69 |
|
|
70 volatile cyg_uint8 worker_state; |
|
|
71 |
|
|
72 #define WORKER_STATE_WAIT 1 |
|
|
73 #define WORKER_STATE_BREAK 2 |
|
|
74 #define WORKER_STATE_EXIT 9 |
|
|
75 |
|
|
76 cyg_mutex_t worker_mutex; |
|
|
77 cyg_cond_t worker_cv; |
|
|
78 |
|
|
79 cyg_count32 workers_asleep = 0; |
|
|
80 |
|
|
81 cyg_count32 thread_count[THREADS]; |
|
|
82 |
|
|
83 cyg_priority_t thread_pri[THREADS]; |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 // ------------------------------------------------------------------------- |
|
|
88 |
|
|
89 extern void breakme(void) |
|
|
90 { |
|
|
91 } |
|
|
92 |
|
|
93 // ------------------------------------------------------------------------- |
|
|
94 |
|
|
95 void worker( cyg_addrword_t id ) |
|
|
96 { |
|
|
97 for(;;) |
|
|
98 { |
|
|
99 thread_count[id]++; |
|
|
100 thread_pri[id] = cyg_thread_get_priority( cyg_thread_self() ); |
|
|
101 |
|
|
102 switch( worker_state ) |
|
|
103 { |
|
|
104 case WORKER_STATE_BREAK: |
|
|
105 if( 0 == (id % 4) ) |
|
|
106 breakme(); |
|
|
107 |
|
|
108 case WORKER_STATE_WAIT: |
|
|
109 cyg_mutex_lock( &worker_mutex ); |
|
|
110 workers_asleep++; |
|
|
111 cyg_cond_wait( &worker_cv ); |
|
|
112 workers_asleep--; |
|
|
113 cyg_mutex_unlock( &worker_mutex ); |
|
|
114 break; |
|
|
115 |
|
|
116 case WORKER_STATE_EXIT: |
|
|
117 cyg_thread_exit(); |
|
|
118 |
|
|
119 } |
|
|
120 } |
|
|
121 } |
|
|
122 |
|
|
123 // ------------------------------------------------------------------------- |
|
|
124 |
|
|
125 void controller( cyg_addrword_t id ) |
|
|
126 { |
|
|
127 cyg_priority_t pri; |
|
|
128 int i; |
|
|
129 |
|
|
130 cyg_mutex_init( &worker_mutex ); |
|
|
131 cyg_cond_init( &worker_cv, &worker_mutex ); |
|
|
132 |
|
|
133 // 1 thread, it is running, it calls BREAKME(); |
|
|
134 // +++ Thread status returned: |
|
|
135 // +++ 1 thread, running, is the current one |
|
|
136 |
|
|
137 breakme(); |
|
|
138 |
|
|
139 // Create N more threads; they are all suspended after creation. Adjust |
|
|
140 // the priorities of all the threads to lower than the controlling thread. |
|
|
141 // Make them all be distinct priorities as far as possible. BREAKME(); |
|
|
142 // +++ 1 thread, running, + N suspended ones of different prios. |
|
|
143 |
|
|
144 for( i = 1; i < THREADS; i++ ) |
|
|
145 { |
|
|
146 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; |
|
|
147 |
|
|
148 cyg_thread_create(pri, worker, (cyg_addrword_t)i, "worker", |
|
|
149 (void *)(&thread_stack[i]), STACKSIZE, |
|
|
150 &thread_handle[i], &thread[i]); |
|
|
151 |
|
|
152 } |
|
|
153 |
|
|
154 breakme(); |
|
|
155 |
|
|
156 // Adjust the priorities of all the threads to lower than the controlling |
|
|
157 // thread. Make them all be THE SAME priority. BREAKME(); |
|
|
158 // +++ 1 thread, running, + N suspended ones of same prio. |
|
|
159 |
|
|
160 for( i = 1; i < THREADS; i++ ) |
|
|
161 { |
|
|
162 cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); |
|
|
163 } |
|
|
164 |
|
|
165 breakme(); |
|
|
166 |
|
|
167 // Release all the N threads, BREAKME(); |
|
|
168 // +++ 1 thread, running, + N ready ones of same prio. |
|
|
169 |
|
|
170 for( i = 1; i < THREADS; i++ ) |
|
|
171 { |
|
|
172 cyg_thread_resume( thread_handle[i] ); |
|
|
173 } |
|
|
174 |
|
|
175 breakme(); |
|
|
176 |
|
|
177 // Adjust the priorities of all the threads, lower than the controlling |
|
|
178 // thread. Make them all be distinct priorities as far as possible. |
|
|
179 // BREAKME(); |
|
|
180 // +++ 1 thread, running, + N ready ones of different prios. |
|
|
181 |
|
|
182 for( i = 1; i < THREADS; i++ ) |
|
|
183 { |
|
|
184 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; |
|
|
185 |
|
|
186 cyg_thread_set_priority( thread_handle[i], pri ); |
|
|
187 } |
|
|
188 |
|
|
189 breakme(); |
|
|
190 |
|
|
191 // Command all the N threads to sleep; switch my own priority to lower |
|
|
192 // than theirs so that they all run and sleep, then I get back in and |
|
|
193 // BREAKME(); |
|
|
194 // +++ 1 thread, running, + N sleeping ones of different prios. |
|
|
195 |
|
|
196 worker_state = WORKER_STATE_WAIT; |
|
|
197 |
|
|
198 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); |
|
|
199 |
|
|
200 breakme(); |
|
|
201 |
|
|
202 // Make them all be THE SAME priority; BREAKME(); |
|
|
203 // +++ 1 thread, running, + N sleeping ones of same prio. |
|
|
204 |
|
|
205 for( i = 1; i < THREADS; i++ ) |
|
|
206 { |
|
|
207 cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); |
|
|
208 } |
|
|
209 |
|
|
210 breakme(); |
|
|
211 |
|
|
212 // Wake them all up, they'll loop once and sleep again; I get in and |
|
|
213 // BREAKME(); |
|
|
214 // +++ 1 thread, running, + N sleeping ones of same prio. |
|
|
215 |
|
|
216 cyg_cond_broadcast( &worker_cv ); |
|
|
217 |
|
|
218 breakme(); |
|
|
219 |
|
|
220 // Adjust the priorities of all the threads, higher than the controlling |
|
|
221 // thread. Make them all be distinct priorities as far as possible. |
|
|
222 // BREAKME(); |
|
|
223 // +++ 1 thread, running, + N sleeping ones of different prios. |
|
|
224 |
|
|
225 for( i = 1; i < THREADS; i++ ) |
|
|
226 { |
|
|
227 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; |
|
|
228 |
|
|
229 cyg_thread_set_priority( thread_handle[i], pri ); |
|
|
230 } |
|
|
231 |
|
|
232 breakme(); |
|
|
233 |
|
|
234 // Wake them all up, they'll loop once and sleep again; I get in and |
|
|
235 // BREAKME(); |
|
|
236 // +++ 1 thread, running, + N sleeping ones of different prios. |
|
|
237 |
|
|
238 cyg_cond_broadcast( &worker_cv ); |
|
|
239 |
|
|
240 breakme(); |
|
|
241 |
|
|
242 // Set them all the same prio, set me to the same prio, BREAKME(); |
|
|
243 // +++ 1 running, + N sleeping, *all* the same prio. |
|
|
244 |
|
|
245 for( i = 0; i < THREADS; i++ ) |
|
|
246 { |
|
|
247 cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); |
|
|
248 } |
|
|
249 |
|
|
250 breakme(); |
|
|
251 |
|
|
252 // Wake them all up, they'll loop once and sleep again; I get in and |
|
|
253 // BREAKME(); repeatedly until they have all slept again. |
|
|
254 // +++ 1 running, + some sleeping, some ready, *all* the same prio. |
|
|
255 |
|
|
256 cyg_cond_broadcast( &worker_cv ); |
|
|
257 |
|
|
258 // cyg_thread_yield(); |
|
|
259 |
|
|
260 do |
|
|
261 { |
|
|
262 breakme(); |
|
|
263 |
|
|
264 } while( workers_asleep != THREADS-1 ); |
|
|
265 |
|
|
266 breakme(); |
|
|
267 |
|
|
268 // Suspend some of the threads, BREAKME(); |
|
|
269 // +++ 1 running, + some sleeping, some suspended, *all* the same prio. |
|
|
270 |
|
|
271 for( i = 1; i < THREADS; i++ ) |
|
|
272 { |
|
|
273 // suspend every 3rd thread |
|
|
274 if( 0 == (i % 3) ) cyg_thread_suspend( thread_handle[i] ); |
|
|
275 } |
|
|
276 |
|
|
277 breakme(); |
|
|
278 |
|
|
279 |
|
|
280 // Change the prios all different, change my prio to highest , BREAKME(); |
|
|
281 // +++ 1 running, + some sleeping, some suspended, different prios. |
|
|
282 |
|
|
283 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_HI ); |
|
|
284 |
|
|
285 for( i = 1; i < THREADS; i++ ) |
|
|
286 { |
|
|
287 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; |
|
|
288 |
|
|
289 cyg_thread_set_priority( thread_handle[i], pri ); |
|
|
290 } |
|
|
291 |
|
|
292 breakme(); |
|
|
293 |
|
|
294 // Wake up all the threads, BREAKME(); |
|
|
295 // +++ 1 running, + some ready, some suspended/ready, different prios. |
|
|
296 |
|
|
297 cyg_cond_broadcast( &worker_cv ); |
|
|
298 |
|
|
299 breakme(); |
|
|
300 |
|
|
301 |
|
|
302 // Change my prio to lowest, let all the threads run, BREAKME(). |
|
|
303 // +++ 1 running + some sleeping, some suspended/ready, different prios. |
|
|
304 |
|
|
305 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); |
|
|
306 |
|
|
307 breakme(); |
|
|
308 |
|
|
309 // Resume all the threads, BREAKME(); |
|
|
310 // +++ 1 running, + N ready, different prios. |
|
|
311 |
|
|
312 for( i = 1; i < THREADS; i++ ) |
|
|
313 { |
|
|
314 cyg_thread_resume( thread_handle[i] ); |
|
|
315 } |
|
|
316 |
|
|
317 breakme(); |
|
|
318 |
|
|
319 // Command some of the N threads to call BREAKME(); themselves (then sleep |
|
|
320 // again). Change my prio to low, so that they all get to run and hit the |
|
|
321 // breakpoint. |
|
|
322 // +++ A different one running every time, others in a mixture of |
|
|
323 // ready and sleeping states. |
|
|
324 |
|
|
325 worker_state = WORKER_STATE_BREAK; |
|
|
326 |
|
|
327 cyg_cond_broadcast( &worker_cv ); |
|
|
328 |
|
|
329 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); |
|
|
330 |
|
|
331 breakme(); |
|
|
332 |
|
|
333 // Command all the threads to exit; switch my own priority to lower |
|
|
334 // than theirs so that they all run and exit, then I get back in and |
|
|
335 // BREAKME(); |
|
|
336 // +++ 1 thread, running, + N dormant ones. |
|
|
337 |
|
|
338 worker_state = WORKER_STATE_EXIT; |
|
|
339 |
|
|
340 cyg_cond_broadcast( &worker_cv ); |
|
|
341 |
|
|
342 breakme(); |
|
|
343 |
|
|
344 #if 0 |
|
|
345 |
|
|
346 // Cannot do this yet... |
|
|
347 |
|
|
348 // Destroy some of the N threads to invalidate their IDs. Re-create them |
|
|
349 // with new IDs, so that we get IDs 1,2,4,6,8,11,12,13,14,15 in use instead |
|
|
350 // of 1,2,3,4,5,6,7,8,9, if you see what I mean. Do all the above again. |
|
|
351 // Loop forever, or whatever... |
|
|
352 |
|
|
353 #endif |
|
|
354 |
|
|
355 breakme(); |
|
|
356 |
|
|
357 CYG_TEST_PASS_FINISH("GDB Thread test OK"); |
|
|
358 |
|
|
359 } |
|
|
360 |
|
|
361 // ------------------------------------------------------------------------- |
|
|
362 |
|
|
363 externC void |
|
|
364 cyg_start( void ) |
|
|
365 { |
|
|
366 |
|
|
367 CYG_TEST_INIT(); |
|
|
368 |
|
|
369 cyg_thread_create(CONTROLLER_PRI_HI, controller, (cyg_addrword_t)0, "controller", |
|
|
370 (void *)(&thread_stack[0]), STACKSIZE, |
|
|
371 &thread_handle[0], &thread[0]); |
|
|
372 |
|
|
373 // resume it |
|
|
374 cyg_thread_resume(thread_handle[0]); |
|
|
375 |
|
|
376 |
|
|
377 // Get the world going |
|
|
378 cyg_scheduler_start(); |
|
|
379 |
|
|
380 CYG_TEST_FAIL_FINISH("Not reached"); |
|
|
381 |
|
|
382 } |
|
|
383 |
|
|
384 #else /* def CYGFUN_KERNEL_API_C */ |
|
|
385 |
|
|
386 externC void |
|
|
387 cyg_start( void ) |
|
|
388 { |
|
|
389 CYG_TEST_INIT(); |
|
|
390 CYG_TEST_PASS_FINISH("Incorrect configuration for this test"); |
|
|
391 } |
|
|
392 #endif /* def CYGFUN_KERNEL_API_C && !def CYGSEM_KERNEL_SCHED_MLQUEUE */ |
|
|
393 |
|
|
394 // ------------------------------------------------------------------------- |
|
|
395 // EOF thread_gdb.c |