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