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