Mercurial > ecos
annotate packages/kernel/current/tests/thread_gdb.c @ 2729:74dbf4c3f2e1 after-copyright-change-20090129
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
| author | jlarmour |
|---|---|
| date | Thu, 29 Jan 2009 17:47:46 +0000 |
| parents | d2c90368aeef |
| children | 6724f8254bcb |
| rev | line source |
|---|---|
| 0 | 1 //========================================================================== |
| 2 // | |
| 3 // thread_gdb.c | |
| 4 // | |
| 5 // A test for thread support in GDB | |
| 6 // | |
| 7 //========================================================================== | |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
8 // ####ECOSGPLCOPYRIGHTBEGIN#### |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
9 // ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
10 // This file is part of eCos, the Embedded Configurable Operating System. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
66
diff
changeset
|
12 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
13 // eCos is free software; you can redistribute it and/or modify it under |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
14 // the terms of the GNU General Public License as published by the Free |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
15 // Software Foundation; either version 2 or (at your option) any later |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
16 // version. |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
66
diff
changeset
|
17 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
18 // eCos is distributed in the hope that it will be useful, but WITHOUT |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
19 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
20 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
21 // for more details. |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
66
diff
changeset
|
22 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
23 // You should have received a copy of the GNU General Public License |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
24 // along with eCos; if not, write to the Free Software Foundation, Inc., |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
25 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
66
diff
changeset
|
26 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
27 // As a special exception, if other files instantiate templates or use |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
28 // macros or inline functions from this file, or you compile this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
29 // and link it with other works to produce a work based on this file, |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
30 // this file does not by itself cause the resulting work to be covered by |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
31 // the GNU General Public License. However the source code for this file |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
32 // must still be made available in accordance with section (3) of the GNU |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
33 // General Public License v2. |
|
208
e0c0827131d1
Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
66
diff
changeset
|
34 // |
|
2729
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
35 // This exception does not invalidate any other reasons why a work based |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
36 // on this file might be covered by the GNU General Public License. |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
37 // ------------------------------------------- |
|
74dbf4c3f2e1
Update all copyright banners to reflect FSF ownership; fix and improve licence text.
jlarmour
parents:
210
diff
changeset
|
38 // ####ECOSGPLCOPYRIGHTEND#### |
| 0 | 39 //========================================================================== |
| 40 //#####DESCRIPTIONBEGIN#### | |
| 41 // | |
| 42 // Author(s): nickg | |
| 43 // Contributors: nickg | |
| 44 // Date: 1998-09-21 | |
| 45 // Description: GDB thread support test. | |
| 46 //####DESCRIPTIONEND#### | |
| 47 // | |
| 48 | |
| 49 #include <cyg/kernel/kapi.h> | |
| 50 | |
| 51 #include <cyg/infra/cyg_ass.h> | |
| 52 | |
| 53 #include <cyg/infra/testcase.h> | |
| 54 | |
| 2 | 55 #if defined(CYGFUN_KERNEL_API_C) && defined(CYGSEM_KERNEL_SCHED_MLQUEUE) &&\ |
| 56 (CYGNUM_KERNEL_SCHED_PRIORITIES > 26) | |
| 57 | |
| 58 #include <cyg/hal/hal_arch.h> // for CYGNUM_HAL_STACK_SIZE_TYPICAL | |
| 0 | 59 |
| 60 // ------------------------------------------------------------------------- | |
| 61 | |
| 62 #define THREADS 10 | |
| 63 | |
| 2 | 64 #ifdef CYGNUM_HAL_STACK_SIZE_TYPICAL |
| 65 #define STACKSIZE CYGNUM_HAL_STACK_SIZE_TYPICAL | |
| 66 #else | |
| 0 | 67 #define STACKSIZE (2*1024) |
| 2 | 68 #endif |
| 0 | 69 |
| 70 #define CONTROLLER_PRI_HI 0 | |
| 71 #define CONTROLLER_PRI_LO 25 | |
| 72 | |
| 73 #define WORKER_PRI 3 | |
| 74 #define WORKER_PRI_RANGE 20 | |
| 75 | |
| 76 | |
| 77 // ------------------------------------------------------------------------- | |
| 78 | |
| 79 // array of stacks for threads | |
| 80 char thread_stack[THREADS][STACKSIZE]; | |
| 81 | |
| 82 // array of threads. | |
| 83 cyg_thread thread[THREADS]; | |
| 84 | |
| 85 cyg_handle_t thread_handle[THREADS]; | |
| 86 | |
| 87 volatile cyg_uint8 worker_state; | |
| 88 | |
| 89 #define WORKER_STATE_WAIT 1 | |
| 90 #define WORKER_STATE_BREAK 2 | |
| 91 #define WORKER_STATE_EXIT 9 | |
| 92 | |
| 93 cyg_mutex_t worker_mutex; | |
| 94 cyg_cond_t worker_cv; | |
| 95 | |
| 96 cyg_count32 workers_asleep = 0; | |
| 97 | |
| 98 cyg_count32 thread_count[THREADS]; | |
| 99 | |
| 100 cyg_priority_t thread_pri[THREADS]; | |
| 101 | |
| 102 | |
| 103 | |
| 104 // ------------------------------------------------------------------------- | |
| 105 | |
| 106 extern void breakme(void) | |
| 107 { | |
| 108 } | |
| 109 | |
| 110 // ------------------------------------------------------------------------- | |
| 111 | |
| 112 void worker( cyg_addrword_t id ) | |
| 113 { | |
| 114 for(;;) | |
| 115 { | |
| 116 thread_count[id]++; | |
| 117 thread_pri[id] = cyg_thread_get_priority( cyg_thread_self() ); | |
| 118 | |
| 119 switch( worker_state ) | |
| 120 { | |
| 121 case WORKER_STATE_BREAK: | |
| 122 if( 0 == (id % 4) ) | |
| 123 breakme(); | |
| 124 | |
| 125 case WORKER_STATE_WAIT: | |
| 126 cyg_mutex_lock( &worker_mutex ); | |
| 127 workers_asleep++; | |
| 128 cyg_cond_wait( &worker_cv ); | |
| 129 workers_asleep--; | |
| 130 cyg_mutex_unlock( &worker_mutex ); | |
| 131 break; | |
| 132 | |
| 133 case WORKER_STATE_EXIT: | |
| 134 cyg_thread_exit(); | |
| 135 | |
| 136 } | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 // ------------------------------------------------------------------------- | |
| 141 | |
| 142 void controller( cyg_addrword_t id ) | |
| 143 { | |
| 144 cyg_priority_t pri; | |
| 145 int i; | |
| 146 | |
| 147 cyg_mutex_init( &worker_mutex ); | |
| 148 cyg_cond_init( &worker_cv, &worker_mutex ); | |
| 149 | |
| 150 // 1 thread, it is running, it calls BREAKME(); | |
| 151 // +++ Thread status returned: | |
| 152 // +++ 1 thread, running, is the current one | |
| 153 | |
| 154 breakme(); | |
| 155 | |
| 156 // Create N more threads; they are all suspended after creation. Adjust | |
| 157 // the priorities of all the threads to lower than the controlling thread. | |
| 158 // Make them all be distinct priorities as far as possible. BREAKME(); | |
| 159 // +++ 1 thread, running, + N suspended ones of different prios. | |
| 160 | |
| 161 for( i = 1; i < THREADS; i++ ) | |
| 162 { | |
| 163 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; | |
| 164 | |
| 165 cyg_thread_create(pri, worker, (cyg_addrword_t)i, "worker", | |
| 166 (void *)(&thread_stack[i]), STACKSIZE, | |
| 167 &thread_handle[i], &thread[i]); | |
| 168 | |
| 169 } | |
| 170 | |
| 171 breakme(); | |
| 172 | |
| 173 // Adjust the priorities of all the threads to lower than the controlling | |
| 174 // thread. Make them all be THE SAME priority. BREAKME(); | |
| 175 // +++ 1 thread, running, + N suspended ones of same prio. | |
| 176 | |
| 177 for( i = 1; i < THREADS; i++ ) | |
| 178 { | |
| 179 cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); | |
| 180 } | |
| 181 | |
| 182 breakme(); | |
| 183 | |
| 184 // Release all the N threads, BREAKME(); | |
| 185 // +++ 1 thread, running, + N ready ones of same prio. | |
| 186 | |
| 187 for( i = 1; i < THREADS; i++ ) | |
| 188 { | |
| 189 cyg_thread_resume( thread_handle[i] ); | |
| 190 } | |
| 191 | |
| 192 breakme(); | |
| 193 | |
| 194 // Adjust the priorities of all the threads, lower than the controlling | |
| 195 // thread. Make them all be distinct priorities as far as possible. | |
| 196 // BREAKME(); | |
| 197 // +++ 1 thread, running, + N ready ones of different prios. | |
| 198 | |
| 199 for( i = 1; i < THREADS; i++ ) | |
| 200 { | |
| 201 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; | |
| 202 | |
| 203 cyg_thread_set_priority( thread_handle[i], pri ); | |
| 204 } | |
| 205 | |
| 206 breakme(); | |
| 207 | |
| 208 // Command all the N threads to sleep; switch my own priority to lower | |
| 209 // than theirs so that they all run and sleep, then I get back in and | |
| 210 // BREAKME(); | |
| 211 // +++ 1 thread, running, + N sleeping ones of different prios. | |
| 212 | |
| 213 worker_state = WORKER_STATE_WAIT; | |
| 214 | |
| 215 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); | |
| 216 | |
| 217 breakme(); | |
| 218 | |
| 219 // Make them all be THE SAME priority; BREAKME(); | |
| 220 // +++ 1 thread, running, + N sleeping ones of same prio. | |
| 221 | |
| 222 for( i = 1; i < THREADS; i++ ) | |
| 223 { | |
| 224 cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); | |
| 225 } | |
| 226 | |
| 227 breakme(); | |
| 228 | |
| 229 // Wake them all up, they'll loop once and sleep again; I get in and | |
| 230 // BREAKME(); | |
| 231 // +++ 1 thread, running, + N sleeping ones of same prio. | |
| 232 | |
| 233 cyg_cond_broadcast( &worker_cv ); | |
| 234 | |
| 235 breakme(); | |
| 236 | |
| 237 // Adjust the priorities of all the threads, higher than the controlling | |
| 238 // thread. Make them all be distinct priorities as far as possible. | |
| 239 // BREAKME(); | |
| 240 // +++ 1 thread, running, + N sleeping ones of different prios. | |
| 241 | |
| 242 for( i = 1; i < THREADS; i++ ) | |
| 243 { | |
| 244 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; | |
| 245 | |
| 246 cyg_thread_set_priority( thread_handle[i], pri ); | |
| 247 } | |
| 248 | |
| 249 breakme(); | |
| 250 | |
| 251 // Wake them all up, they'll loop once and sleep again; I get in and | |
| 252 // BREAKME(); | |
| 253 // +++ 1 thread, running, + N sleeping ones of different prios. | |
| 254 | |
| 255 cyg_cond_broadcast( &worker_cv ); | |
| 256 | |
| 257 breakme(); | |
| 258 | |
| 259 // Set them all the same prio, set me to the same prio, BREAKME(); | |
| 260 // +++ 1 running, + N sleeping, *all* the same prio. | |
| 261 | |
| 262 for( i = 0; i < THREADS; i++ ) | |
| 263 { | |
| 264 cyg_thread_set_priority( thread_handle[i], WORKER_PRI ); | |
| 265 } | |
| 266 | |
| 267 breakme(); | |
| 268 | |
| 269 // Wake them all up, they'll loop once and sleep again; I get in and | |
| 270 // BREAKME(); repeatedly until they have all slept again. | |
| 271 // +++ 1 running, + some sleeping, some ready, *all* the same prio. | |
| 272 | |
| 273 cyg_cond_broadcast( &worker_cv ); | |
| 274 | |
| 275 // cyg_thread_yield(); | |
| 276 | |
| 277 do | |
| 278 { | |
| 279 breakme(); | |
| 280 | |
| 281 } while( workers_asleep != THREADS-1 ); | |
| 282 | |
| 283 breakme(); | |
| 284 | |
| 285 // Suspend some of the threads, BREAKME(); | |
| 286 // +++ 1 running, + some sleeping, some suspended, *all* the same prio. | |
| 287 | |
| 288 for( i = 1; i < THREADS; i++ ) | |
| 289 { | |
| 290 // suspend every 3rd thread | |
| 291 if( 0 == (i % 3) ) cyg_thread_suspend( thread_handle[i] ); | |
| 292 } | |
| 293 | |
| 294 breakme(); | |
| 295 | |
| 296 | |
| 297 // Change the prios all different, change my prio to highest , BREAKME(); | |
| 298 // +++ 1 running, + some sleeping, some suspended, different prios. | |
| 299 | |
| 300 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_HI ); | |
| 301 | |
| 302 for( i = 1; i < THREADS; i++ ) | |
| 303 { | |
| 304 pri = CONTROLLER_PRI_HI + 1 + i % WORKER_PRI_RANGE; | |
| 305 | |
| 306 cyg_thread_set_priority( thread_handle[i], pri ); | |
| 307 } | |
| 308 | |
| 309 breakme(); | |
| 310 | |
| 311 // Wake up all the threads, BREAKME(); | |
| 312 // +++ 1 running, + some ready, some suspended/ready, different prios. | |
| 313 | |
| 314 cyg_cond_broadcast( &worker_cv ); | |
| 315 | |
| 316 breakme(); | |
| 317 | |
| 318 | |
| 319 // Change my prio to lowest, let all the threads run, BREAKME(). | |
| 320 // +++ 1 running + some sleeping, some suspended/ready, different prios. | |
| 321 | |
| 322 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); | |
| 323 | |
| 324 breakme(); | |
| 325 | |
| 326 // Resume all the threads, BREAKME(); | |
| 327 // +++ 1 running, + N ready, different prios. | |
| 328 | |
| 329 for( i = 1; i < THREADS; i++ ) | |
| 330 { | |
| 331 cyg_thread_resume( thread_handle[i] ); | |
| 332 } | |
| 333 | |
| 334 breakme(); | |
| 335 | |
| 336 // Command some of the N threads to call BREAKME(); themselves (then sleep | |
| 337 // again). Change my prio to low, so that they all get to run and hit the | |
| 338 // breakpoint. | |
| 339 // +++ A different one running every time, others in a mixture of | |
| 2 | 340 // ready and sleeping states. |
| 0 | 341 |
| 342 worker_state = WORKER_STATE_BREAK; | |
| 343 | |
| 344 cyg_cond_broadcast( &worker_cv ); | |
| 345 | |
| 346 cyg_thread_set_priority( thread_handle[0], CONTROLLER_PRI_LO ); | |
| 347 | |
| 348 breakme(); | |
| 349 | |
| 350 // Command all the threads to exit; switch my own priority to lower | |
| 351 // than theirs so that they all run and exit, then I get back in and | |
| 352 // BREAKME(); | |
| 353 // +++ 1 thread, running, + N dormant ones. | |
| 354 | |
| 355 worker_state = WORKER_STATE_EXIT; | |
| 356 | |
| 357 cyg_cond_broadcast( &worker_cv ); | |
| 358 | |
| 359 breakme(); | |
| 360 | |
| 361 #if 0 | |
| 362 | |
| 363 // Cannot do this yet... | |
| 364 | |
| 365 // Destroy some of the N threads to invalidate their IDs. Re-create them | |
| 366 // with new IDs, so that we get IDs 1,2,4,6,8,11,12,13,14,15 in use instead | |
| 367 // of 1,2,3,4,5,6,7,8,9, if you see what I mean. Do all the above again. | |
| 368 // Loop forever, or whatever... | |
| 369 | |
| 370 #endif | |
| 371 | |
| 372 breakme(); | |
| 373 | |
| 374 CYG_TEST_PASS_FINISH("GDB Thread test OK"); | |
| 375 | |
| 376 } | |
| 377 | |
| 378 // ------------------------------------------------------------------------- | |
| 379 | |
| 380 externC void | |
| 381 cyg_start( void ) | |
| 382 { | |
| 383 | |
| 384 CYG_TEST_INIT(); | |
| 385 | |
| 386 cyg_thread_create(CONTROLLER_PRI_HI, controller, (cyg_addrword_t)0, "controller", | |
| 387 (void *)(&thread_stack[0]), STACKSIZE, | |
| 388 &thread_handle[0], &thread[0]); | |
| 389 | |
| 390 // resume it | |
| 391 cyg_thread_resume(thread_handle[0]); | |
| 392 | |
| 393 | |
| 394 // Get the world going | |
| 395 cyg_scheduler_start(); | |
| 396 | |
| 397 CYG_TEST_FAIL_FINISH("Not reached"); | |
| 398 | |
| 399 } | |
| 400 | |
| 401 #else /* def CYGFUN_KERNEL_API_C */ | |
| 402 | |
| 403 externC void | |
| 404 cyg_start( void ) | |
| 405 { | |
| 406 CYG_TEST_INIT(); | |
| 407 CYG_TEST_PASS_FINISH("Incorrect configuration for this test"); | |
| 408 } | |
| 2 | 409 #endif /* def CYGFUN_KERNEL_API_C && ... */ |
| 0 | 410 |
| 411 // ------------------------------------------------------------------------- | |
| 412 // EOF thread_gdb.c |
