research st, rename variable thread to trd, for thread is a keyword.
正在显示
3 个修改的文件
包含
189 行增加
和
120 行删除
| @@ -95,8 +95,8 @@ extern "C" { | @@ -95,8 +95,8 @@ extern "C" { | ||
| 95 | 95 | ||
| 96 | extern st_thread_t st_thread_self(void); | 96 | extern st_thread_t st_thread_self(void); |
| 97 | extern void st_thread_exit(void *retval); | 97 | extern void st_thread_exit(void *retval); |
| 98 | - extern int st_thread_join(st_thread_t thread, void **retvalp); | ||
| 99 | - extern void st_thread_interrupt(st_thread_t thread); | 98 | + extern int st_thread_join(st_thread_t trd, void **retvalp); |
| 99 | + extern void st_thread_interrupt(st_thread_t trd); | ||
| 100 | extern st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size); | 100 | extern st_thread_t st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stack_size); |
| 101 | extern int st_randomize_stacks(int on); | 101 | extern int st_randomize_stacks(int on); |
| 102 | extern int st_set_utime_function(st_utime_t (*func)(void)); | 102 | extern int st_set_utime_function(st_utime_t (*func)(void)); |
| @@ -110,21 +110,21 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | @@ -110,21 +110,21 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | ||
| 110 | 110 | ||
| 111 | void _st_vp_schedule(void) | 111 | void _st_vp_schedule(void) |
| 112 | { | 112 | { |
| 113 | - _st_thread_t *thread; | 113 | + _st_thread_t *trd; |
| 114 | 114 | ||
| 115 | if (_ST_RUNQ.next != &_ST_RUNQ) { | 115 | if (_ST_RUNQ.next != &_ST_RUNQ) { |
| 116 | /* Pull thread off of the run queue */ | 116 | /* Pull thread off of the run queue */ |
| 117 | - thread = _ST_THREAD_PTR(_ST_RUNQ.next); | ||
| 118 | - _ST_DEL_RUNQ(thread); | 117 | + trd = _ST_THREAD_PTR(_ST_RUNQ.next); |
| 118 | + _ST_DEL_RUNQ(trd); | ||
| 119 | } else { | 119 | } else { |
| 120 | /* If there are no threads to run, switch to the idle thread */ | 120 | /* If there are no threads to run, switch to the idle thread */ |
| 121 | - thread = _st_this_vp.idle_thread; | 121 | + trd = _st_this_vp.idle_thread; |
| 122 | } | 122 | } |
| 123 | - ST_ASSERT(thread->state == _ST_ST_RUNNABLE); | 123 | + ST_ASSERT(trd->state == _ST_ST_RUNNABLE); |
| 124 | 124 | ||
| 125 | /* Resume the thread */ | 125 | /* Resume the thread */ |
| 126 | - thread->state = _ST_ST_RUNNING; | ||
| 127 | - _ST_RESTORE_CONTEXT(thread); | 126 | + trd->state = _ST_ST_RUNNING; |
| 127 | + _ST_RESTORE_CONTEXT(trd); | ||
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | /* | 130 | /* |
| @@ -132,7 +132,7 @@ void _st_vp_schedule(void) | @@ -132,7 +132,7 @@ void _st_vp_schedule(void) | ||
| 132 | */ | 132 | */ |
| 133 | int st_init(void) | 133 | int st_init(void) |
| 134 | { | 134 | { |
| 135 | - _st_thread_t *thread; | 135 | + _st_thread_t *trd; |
| 136 | 136 | ||
| 137 | if (_st_active_count) { | 137 | if (_st_active_count) { |
| 138 | /* Already initialized */ | 138 | /* Already initialized */ |
| @@ -176,18 +176,18 @@ int st_init(void) | @@ -176,18 +176,18 @@ int st_init(void) | ||
| 176 | /* | 176 | /* |
| 177 | * Initialize primordial thread | 177 | * Initialize primordial thread |
| 178 | */ | 178 | */ |
| 179 | - thread = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + | 179 | + trd = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + |
| 180 | (ST_KEYS_MAX * sizeof(void *))); | 180 | (ST_KEYS_MAX * sizeof(void *))); |
| 181 | - if (!thread) { | 181 | + if (!trd) { |
| 182 | return -1; | 182 | return -1; |
| 183 | } | 183 | } |
| 184 | - thread->private_data = (void **) (thread + 1); | ||
| 185 | - thread->state = _ST_ST_RUNNING; | ||
| 186 | - thread->flags = _ST_FL_PRIMORDIAL; | ||
| 187 | - _ST_SET_CURRENT_THREAD(thread); | 184 | + trd->private_data = (void **) (trd + 1); |
| 185 | + trd->state = _ST_ST_RUNNING; | ||
| 186 | + trd->flags = _ST_FL_PRIMORDIAL; | ||
| 187 | + _ST_SET_CURRENT_THREAD(trd); | ||
| 188 | _st_active_count++; | 188 | _st_active_count++; |
| 189 | #ifdef DEBUG | 189 | #ifdef DEBUG |
| 190 | - _ST_ADD_THREADQ(thread); | 190 | + _ST_ADD_THREADQ(trd); |
| 191 | #endif | 191 | #endif |
| 192 | 192 | ||
| 193 | return 0; | 193 | return 0; |
| @@ -237,50 +237,50 @@ void *_st_idle_thread_start(void *arg) | @@ -237,50 +237,50 @@ void *_st_idle_thread_start(void *arg) | ||
| 237 | 237 | ||
| 238 | void st_thread_exit(void *retval) | 238 | void st_thread_exit(void *retval) |
| 239 | { | 239 | { |
| 240 | - _st_thread_t *thread = _ST_CURRENT_THREAD(); | 240 | + _st_thread_t *trd = _ST_CURRENT_THREAD(); |
| 241 | 241 | ||
| 242 | - thread->retval = retval; | ||
| 243 | - _st_thread_cleanup(thread); | 242 | + trd->retval = retval; |
| 243 | + _st_thread_cleanup(trd); | ||
| 244 | _st_active_count--; | 244 | _st_active_count--; |
| 245 | - if (thread->term) { | 245 | + if (trd->term) { |
| 246 | /* Put thread on the zombie queue */ | 246 | /* Put thread on the zombie queue */ |
| 247 | - thread->state = _ST_ST_ZOMBIE; | ||
| 248 | - _ST_ADD_ZOMBIEQ(thread); | 247 | + trd->state = _ST_ST_ZOMBIE; |
| 248 | + _ST_ADD_ZOMBIEQ(trd); | ||
| 249 | 249 | ||
| 250 | /* Notify on our termination condition variable */ | 250 | /* Notify on our termination condition variable */ |
| 251 | - st_cond_signal(thread->term); | 251 | + st_cond_signal(trd->term); |
| 252 | 252 | ||
| 253 | /* Switch context and come back later */ | 253 | /* Switch context and come back later */ |
| 254 | - _ST_SWITCH_CONTEXT(thread); | 254 | + _ST_SWITCH_CONTEXT(trd); |
| 255 | 255 | ||
| 256 | /* Continue the cleanup */ | 256 | /* Continue the cleanup */ |
| 257 | - st_cond_destroy(thread->term); | ||
| 258 | - thread->term = NULL; | 257 | + st_cond_destroy(trd->term); |
| 258 | + trd->term = NULL; | ||
| 259 | } | 259 | } |
| 260 | 260 | ||
| 261 | #ifdef DEBUG | 261 | #ifdef DEBUG |
| 262 | - _ST_DEL_THREADQ(thread); | 262 | + _ST_DEL_THREADQ(trd); |
| 263 | #endif | 263 | #endif |
| 264 | 264 | ||
| 265 | - if (!(thread->flags & _ST_FL_PRIMORDIAL)) { | ||
| 266 | - _st_stack_free(thread->stack); | 265 | + if (!(trd->flags & _ST_FL_PRIMORDIAL)) { |
| 266 | + _st_stack_free(trd->stack); | ||
| 267 | } | 267 | } |
| 268 | 268 | ||
| 269 | /* Find another thread to run */ | 269 | /* Find another thread to run */ |
| 270 | - _ST_SWITCH_CONTEXT(thread); | 270 | + _ST_SWITCH_CONTEXT(trd); |
| 271 | /* Not going to land here */ | 271 | /* Not going to land here */ |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | -int st_thread_join(_st_thread_t *thread, void **retvalp) | 274 | +int st_thread_join(_st_thread_t *trd, void **retvalp) |
| 275 | { | 275 | { |
| 276 | - _st_cond_t *term = thread->term; | 276 | + _st_cond_t *term = trd->term; |
| 277 | 277 | ||
| 278 | /* Can't join a non-joinable thread */ | 278 | /* Can't join a non-joinable thread */ |
| 279 | if (term == NULL) { | 279 | if (term == NULL) { |
| 280 | errno = EINVAL; | 280 | errno = EINVAL; |
| 281 | return -1; | 281 | return -1; |
| 282 | } | 282 | } |
| 283 | - if (_ST_CURRENT_THREAD() == thread) { | 283 | + if (_ST_CURRENT_THREAD() == trd) { |
| 284 | errno = EDEADLK; | 284 | errno = EDEADLK; |
| 285 | return -1; | 285 | return -1; |
| 286 | } | 286 | } |
| @@ -291,43 +291,43 @@ int st_thread_join(_st_thread_t *thread, void **retvalp) | @@ -291,43 +291,43 @@ int st_thread_join(_st_thread_t *thread, void **retvalp) | ||
| 291 | return -1; | 291 | return -1; |
| 292 | } | 292 | } |
| 293 | 293 | ||
| 294 | - while (thread->state != _ST_ST_ZOMBIE) { | 294 | + while (trd->state != _ST_ST_ZOMBIE) { |
| 295 | if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0) { | 295 | if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0) { |
| 296 | return -1; | 296 | return -1; |
| 297 | } | 297 | } |
| 298 | } | 298 | } |
| 299 | 299 | ||
| 300 | if (retvalp) { | 300 | if (retvalp) { |
| 301 | - *retvalp = thread->retval; | 301 | + *retvalp = trd->retval; |
| 302 | } | 302 | } |
| 303 | 303 | ||
| 304 | /* | 304 | /* |
| 305 | * Remove target thread from the zombie queue and make it runnable. | 305 | * Remove target thread from the zombie queue and make it runnable. |
| 306 | * When it gets scheduled later, it will do the clean up. | 306 | * When it gets scheduled later, it will do the clean up. |
| 307 | */ | 307 | */ |
| 308 | - thread->state = _ST_ST_RUNNABLE; | ||
| 309 | - _ST_DEL_ZOMBIEQ(thread); | ||
| 310 | - _ST_ADD_RUNQ(thread); | 308 | + trd->state = _ST_ST_RUNNABLE; |
| 309 | + _ST_DEL_ZOMBIEQ(trd); | ||
| 310 | + _ST_ADD_RUNQ(trd); | ||
| 311 | 311 | ||
| 312 | return 0; | 312 | return 0; |
| 313 | } | 313 | } |
| 314 | 314 | ||
| 315 | void _st_thread_main(void) | 315 | void _st_thread_main(void) |
| 316 | { | 316 | { |
| 317 | - _st_thread_t *thread = _ST_CURRENT_THREAD(); | 317 | + _st_thread_t *trd = _ST_CURRENT_THREAD(); |
| 318 | 318 | ||
| 319 | /* | 319 | /* |
| 320 | * Cap the stack by zeroing out the saved return address register | 320 | * Cap the stack by zeroing out the saved return address register |
| 321 | * value. This allows some debugging/profiling tools to know when | 321 | * value. This allows some debugging/profiling tools to know when |
| 322 | * to stop unwinding the stack. It's a no-op on most platforms. | 322 | * to stop unwinding the stack. It's a no-op on most platforms. |
| 323 | */ | 323 | */ |
| 324 | - MD_CAP_STACK(&thread); | 324 | + MD_CAP_STACK(&trd); |
| 325 | 325 | ||
| 326 | /* Run thread main */ | 326 | /* Run thread main */ |
| 327 | - thread->retval = (*thread->start)(thread->arg); | 327 | + trd->retval = (*trd->start)(trd->arg); |
| 328 | 328 | ||
| 329 | /* All done, time to go away */ | 329 | /* All done, time to go away */ |
| 330 | - st_thread_exit(thread->retval); | 330 | + st_thread_exit(trd->retval); |
| 331 | } | 331 | } |
| 332 | 332 | ||
| 333 | /* | 333 | /* |
| @@ -335,9 +335,9 @@ void _st_thread_main(void) | @@ -335,9 +335,9 @@ void _st_thread_main(void) | ||
| 335 | * specified by thread->heap_index. See docs/timeout_heap.txt | 335 | * specified by thread->heap_index. See docs/timeout_heap.txt |
| 336 | * for details about the timeout heap. | 336 | * for details about the timeout heap. |
| 337 | */ | 337 | */ |
| 338 | -static _st_thread_t **heap_insert(_st_thread_t *thread) | 338 | +static _st_thread_t **heap_insert(_st_thread_t *trd) |
| 339 | { | 339 | { |
| 340 | - int target = thread->heap_index; | 340 | + int target = trd->heap_index; |
| 341 | int s = target; | 341 | int s = target; |
| 342 | _st_thread_t **p = &_ST_SLEEPQ; | 342 | _st_thread_t **p = &_ST_SLEEPQ; |
| 343 | int bits = 0; | 343 | int bits = 0; |
| @@ -350,13 +350,13 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) | @@ -350,13 +350,13 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) | ||
| 350 | } | 350 | } |
| 351 | 351 | ||
| 352 | for (bit = bits - 2; bit >= 0; bit--) { | 352 | for (bit = bits - 2; bit >= 0; bit--) { |
| 353 | - if (thread->due < (*p)->due) { | 353 | + if (trd->due < (*p)->due) { |
| 354 | _st_thread_t *t = *p; | 354 | _st_thread_t *t = *p; |
| 355 | - thread->left = t->left; | ||
| 356 | - thread->right = t->right; | ||
| 357 | - *p = thread; | ||
| 358 | - thread->heap_index = index; | ||
| 359 | - thread = t; | 355 | + trd->left = t->left; |
| 356 | + trd->right = t->right; | ||
| 357 | + *p = trd; | ||
| 358 | + trd->heap_index = index; | ||
| 359 | + trd = t; | ||
| 360 | } | 360 | } |
| 361 | index <<= 1; | 361 | index <<= 1; |
| 362 | if (target & (1 << bit)) { | 362 | if (target & (1 << bit)) { |
| @@ -367,9 +367,9 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) | @@ -367,9 +367,9 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) | ||
| 367 | } | 367 | } |
| 368 | } | 368 | } |
| 369 | 369 | ||
| 370 | - thread->heap_index = index; | ||
| 371 | - *p = thread; | ||
| 372 | - thread->left = thread->right = NULL; | 370 | + trd->heap_index = index; |
| 371 | + *p = trd; | ||
| 372 | + trd->left = trd->right = NULL; | ||
| 373 | 373 | ||
| 374 | return p; | 374 | return p; |
| 375 | } | 375 | } |
| @@ -377,7 +377,7 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) | @@ -377,7 +377,7 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) | ||
| 377 | /* | 377 | /* |
| 378 | * Delete "thread" from the timeout heap. | 378 | * Delete "thread" from the timeout heap. |
| 379 | */ | 379 | */ |
| 380 | -static void heap_delete(_st_thread_t *thread) | 380 | +static void heap_delete(_st_thread_t *trd) |
| 381 | { | 381 | { |
| 382 | _st_thread_t *t, **p; | 382 | _st_thread_t *t, **p; |
| 383 | int bits = 0; | 383 | int bits = 0; |
| @@ -402,15 +402,15 @@ static void heap_delete(_st_thread_t *thread) | @@ -402,15 +402,15 @@ static void heap_delete(_st_thread_t *thread) | ||
| 402 | t = *p; | 402 | t = *p; |
| 403 | *p = NULL; | 403 | *p = NULL; |
| 404 | --_ST_SLEEPQ_SIZE; | 404 | --_ST_SLEEPQ_SIZE; |
| 405 | - if (t != thread) { | 405 | + if (t != trd) { |
| 406 | /* | 406 | /* |
| 407 | * Insert the unlinked last element in place of the element we are deleting | 407 | * Insert the unlinked last element in place of the element we are deleting |
| 408 | */ | 408 | */ |
| 409 | - t->heap_index = thread->heap_index; | 409 | + t->heap_index = trd->heap_index; |
| 410 | p = heap_insert(t); | 410 | p = heap_insert(t); |
| 411 | t = *p; | 411 | t = *p; |
| 412 | - t->left = thread->left; | ||
| 413 | - t->right = thread->right; | 412 | + t->left = trd->left; |
| 413 | + t->right = trd->right; | ||
| 414 | 414 | ||
| 415 | /* | 415 | /* |
| 416 | * Reestablish the heap invariant. | 416 | * Reestablish the heap invariant. |
| @@ -453,26 +453,26 @@ static void heap_delete(_st_thread_t *thread) | @@ -453,26 +453,26 @@ static void heap_delete(_st_thread_t *thread) | ||
| 453 | } | 453 | } |
| 454 | } | 454 | } |
| 455 | 455 | ||
| 456 | - thread->left = thread->right = NULL; | 456 | + trd->left = trd->right = NULL; |
| 457 | } | 457 | } |
| 458 | 458 | ||
| 459 | -void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout) | 459 | +void _st_add_sleep_q(_st_thread_t *trd, st_utime_t timeout) |
| 460 | { | 460 | { |
| 461 | - thread->due = _ST_LAST_CLOCK + timeout; | ||
| 462 | - thread->flags |= _ST_FL_ON_SLEEPQ; | ||
| 463 | - thread->heap_index = ++_ST_SLEEPQ_SIZE; | ||
| 464 | - heap_insert(thread); | 461 | + trd->due = _ST_LAST_CLOCK + timeout; |
| 462 | + trd->flags |= _ST_FL_ON_SLEEPQ; | ||
| 463 | + trd->heap_index = ++_ST_SLEEPQ_SIZE; | ||
| 464 | + heap_insert(trd); | ||
| 465 | } | 465 | } |
| 466 | 466 | ||
| 467 | -void _st_del_sleep_q(_st_thread_t *thread) | 467 | +void _st_del_sleep_q(_st_thread_t *trd) |
| 468 | { | 468 | { |
| 469 | - heap_delete(thread); | ||
| 470 | - thread->flags &= ~_ST_FL_ON_SLEEPQ; | 469 | + heap_delete(trd); |
| 470 | + trd->flags &= ~_ST_FL_ON_SLEEPQ; | ||
| 471 | } | 471 | } |
| 472 | 472 | ||
| 473 | void _st_vp_check_clock(void) | 473 | void _st_vp_check_clock(void) |
| 474 | { | 474 | { |
| 475 | - _st_thread_t *thread; | 475 | + _st_thread_t *trd; |
| 476 | st_utime_t elapsed, now; | 476 | st_utime_t elapsed, now; |
| 477 | 477 | ||
| 478 | now = st_utime(); | 478 | now = st_utime(); |
| @@ -485,50 +485,50 @@ void _st_vp_check_clock(void) | @@ -485,50 +485,50 @@ void _st_vp_check_clock(void) | ||
| 485 | } | 485 | } |
| 486 | 486 | ||
| 487 | while (_ST_SLEEPQ != NULL) { | 487 | while (_ST_SLEEPQ != NULL) { |
| 488 | - thread = _ST_SLEEPQ; | ||
| 489 | - ST_ASSERT(thread->flags & _ST_FL_ON_SLEEPQ); | ||
| 490 | - if (thread->due > now) { | 488 | + trd = _ST_SLEEPQ; |
| 489 | + ST_ASSERT(trd->flags & _ST_FL_ON_SLEEPQ); | ||
| 490 | + if (trd->due > now) { | ||
| 491 | break; | 491 | break; |
| 492 | } | 492 | } |
| 493 | - _ST_DEL_SLEEPQ(thread); | 493 | + _ST_DEL_SLEEPQ(trd); |
| 494 | 494 | ||
| 495 | /* If thread is waiting on condition variable, set the time out flag */ | 495 | /* If thread is waiting on condition variable, set the time out flag */ |
| 496 | - if (thread->state == _ST_ST_COND_WAIT) { | ||
| 497 | - thread->flags |= _ST_FL_TIMEDOUT; | 496 | + if (trd->state == _ST_ST_COND_WAIT) { |
| 497 | + trd->flags |= _ST_FL_TIMEDOUT; | ||
| 498 | } | 498 | } |
| 499 | 499 | ||
| 500 | /* Make thread runnable */ | 500 | /* Make thread runnable */ |
| 501 | - ST_ASSERT(!(thread->flags & _ST_FL_IDLE_THREAD)); | ||
| 502 | - thread->state = _ST_ST_RUNNABLE; | ||
| 503 | - _ST_ADD_RUNQ(thread); | 501 | + ST_ASSERT(!(trd->flags & _ST_FL_IDLE_THREAD)); |
| 502 | + trd->state = _ST_ST_RUNNABLE; | ||
| 503 | + _ST_ADD_RUNQ(trd); | ||
| 504 | } | 504 | } |
| 505 | } | 505 | } |
| 506 | 506 | ||
| 507 | -void st_thread_interrupt(_st_thread_t *thread) | 507 | +void st_thread_interrupt(_st_thread_t* trd) |
| 508 | { | 508 | { |
| 509 | /* If thread is already dead */ | 509 | /* If thread is already dead */ |
| 510 | - if (thread->state == _ST_ST_ZOMBIE) { | 510 | + if (trd->state == _ST_ST_ZOMBIE) { |
| 511 | return; | 511 | return; |
| 512 | } | 512 | } |
| 513 | 513 | ||
| 514 | - thread->flags |= _ST_FL_INTERRUPT; | 514 | + trd->flags |= _ST_FL_INTERRUPT; |
| 515 | 515 | ||
| 516 | - if (thread->state == _ST_ST_RUNNING || thread->state == _ST_ST_RUNNABLE) { | 516 | + if (trd->state == _ST_ST_RUNNING || trd->state == _ST_ST_RUNNABLE) { |
| 517 | return; | 517 | return; |
| 518 | } | 518 | } |
| 519 | 519 | ||
| 520 | - if (thread->flags & _ST_FL_ON_SLEEPQ) { | ||
| 521 | - _ST_DEL_SLEEPQ(thread); | 520 | + if (trd->flags & _ST_FL_ON_SLEEPQ) { |
| 521 | + _ST_DEL_SLEEPQ(trd); | ||
| 522 | } | 522 | } |
| 523 | 523 | ||
| 524 | /* Make thread runnable */ | 524 | /* Make thread runnable */ |
| 525 | - thread->state = _ST_ST_RUNNABLE; | ||
| 526 | - _ST_ADD_RUNQ(thread); | 525 | + trd->state = _ST_ST_RUNNABLE; |
| 526 | + _ST_ADD_RUNQ(trd); | ||
| 527 | } | 527 | } |
| 528 | 528 | ||
| 529 | _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size) | 529 | _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size) |
| 530 | { | 530 | { |
| 531 | - _st_thread_t *thread; | 531 | + _st_thread_t *trd; |
| 532 | _st_stack_t *stack; | 532 | _st_stack_t *stack; |
| 533 | void **ptds; | 533 | void **ptds; |
| 534 | char *sp; | 534 | char *sp; |
| @@ -549,7 +549,7 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl | @@ -549,7 +549,7 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl | ||
| 549 | sp = sp - (ST_KEYS_MAX * sizeof(void *)); | 549 | sp = sp - (ST_KEYS_MAX * sizeof(void *)); |
| 550 | ptds = (void **) sp; | 550 | ptds = (void **) sp; |
| 551 | sp = sp - sizeof(_st_thread_t); | 551 | sp = sp - sizeof(_st_thread_t); |
| 552 | - thread = (_st_thread_t *) sp; | 552 | + trd = (_st_thread_t *) sp; |
| 553 | 553 | ||
| 554 | /* Make stack 64-byte aligned */ | 554 | /* Make stack 64-byte aligned */ |
| 555 | if ((unsigned long)sp & 0x3f) { | 555 | if ((unsigned long)sp & 0x3f) { |
| @@ -560,35 +560,35 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl | @@ -560,35 +560,35 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinabl | ||
| 560 | #error Unknown Stack Grown | 560 | #error Unknown Stack Grown |
| 561 | #endif | 561 | #endif |
| 562 | 562 | ||
| 563 | - memset(thread, 0, sizeof(_st_thread_t)); | 563 | + memset(trd, 0, sizeof(_st_thread_t)); |
| 564 | memset(ptds, 0, ST_KEYS_MAX * sizeof(void *)); | 564 | memset(ptds, 0, ST_KEYS_MAX * sizeof(void *)); |
| 565 | 565 | ||
| 566 | /* Initialize thread */ | 566 | /* Initialize thread */ |
| 567 | - thread->private_data = ptds; | ||
| 568 | - thread->stack = stack; | ||
| 569 | - thread->start = start; | ||
| 570 | - thread->arg = arg; | 567 | + trd->private_data = ptds; |
| 568 | + trd->stack = stack; | ||
| 569 | + trd->start = start; | ||
| 570 | + trd->arg = arg; | ||
| 571 | 571 | ||
| 572 | - _ST_INIT_CONTEXT(thread, stack->sp, _st_thread_main); | 572 | + _ST_INIT_CONTEXT(trd, stack->sp, _st_thread_main); |
| 573 | 573 | ||
| 574 | /* If thread is joinable, allocate a termination condition variable */ | 574 | /* If thread is joinable, allocate a termination condition variable */ |
| 575 | if (joinable) { | 575 | if (joinable) { |
| 576 | - thread->term = st_cond_new(); | ||
| 577 | - if (thread->term == NULL) { | ||
| 578 | - _st_stack_free(thread->stack); | 576 | + trd->term = st_cond_new(); |
| 577 | + if (trd->term == NULL) { | ||
| 578 | + _st_stack_free(trd->stack); | ||
| 579 | return NULL; | 579 | return NULL; |
| 580 | } | 580 | } |
| 581 | } | 581 | } |
| 582 | 582 | ||
| 583 | /* Make thread runnable */ | 583 | /* Make thread runnable */ |
| 584 | - thread->state = _ST_ST_RUNNABLE; | 584 | + trd->state = _ST_ST_RUNNABLE; |
| 585 | _st_active_count++; | 585 | _st_active_count++; |
| 586 | - _ST_ADD_RUNQ(thread); | 586 | + _ST_ADD_RUNQ(trd); |
| 587 | #ifdef DEBUG | 587 | #ifdef DEBUG |
| 588 | - _ST_ADD_THREADQ(thread); | 588 | + _ST_ADD_THREADQ(trd); |
| 589 | #endif | 589 | #endif |
| 590 | 590 | ||
| 591 | - return thread; | 591 | + return trd; |
| 592 | } | 592 | } |
| 593 | 593 | ||
| 594 | _st_thread_t *st_thread_self(void) | 594 | _st_thread_t *st_thread_self(void) |
| @@ -598,7 +598,7 @@ _st_thread_t *st_thread_self(void) | @@ -598,7 +598,7 @@ _st_thread_t *st_thread_self(void) | ||
| 598 | 598 | ||
| 599 | #ifdef DEBUG | 599 | #ifdef DEBUG |
| 600 | /* ARGSUSED */ | 600 | /* ARGSUSED */ |
| 601 | -void _st_show_thread_stack(_st_thread_t *thread, const char *messg) | 601 | +void _st_show_thread_stack(_st_thread_t *trd, const char *messg) |
| 602 | { | 602 | { |
| 603 | } | 603 | } |
| 604 | 604 | ||
| @@ -607,43 +607,43 @@ int _st_iterate_threads_flag = 0; | @@ -607,43 +607,43 @@ int _st_iterate_threads_flag = 0; | ||
| 607 | 607 | ||
| 608 | void _st_iterate_threads(void) | 608 | void _st_iterate_threads(void) |
| 609 | { | 609 | { |
| 610 | - static _st_thread_t *thread = NULL; | 610 | + static _st_thread_t *trd = NULL; |
| 611 | static jmp_buf orig_jb, save_jb; | 611 | static jmp_buf orig_jb, save_jb; |
| 612 | _st_clist_t *q; | 612 | _st_clist_t *q; |
| 613 | 613 | ||
| 614 | if (!_st_iterate_threads_flag) { | 614 | if (!_st_iterate_threads_flag) { |
| 615 | - if (thread) { | ||
| 616 | - memcpy(thread->context, save_jb, sizeof(jmp_buf)); | 615 | + if (trd) { |
| 616 | + memcpy(trd->context, save_jb, sizeof(jmp_buf)); | ||
| 617 | MD_LONGJMP(orig_jb, 1); | 617 | MD_LONGJMP(orig_jb, 1); |
| 618 | } | 618 | } |
| 619 | return; | 619 | return; |
| 620 | } | 620 | } |
| 621 | 621 | ||
| 622 | - if (thread) { | ||
| 623 | - memcpy(thread->context, save_jb, sizeof(jmp_buf)); | ||
| 624 | - _st_show_thread_stack(thread, NULL); | 622 | + if (trd) { |
| 623 | + memcpy(trd->context, save_jb, sizeof(jmp_buf)); | ||
| 624 | + _st_show_thread_stack(trd, NULL); | ||
| 625 | } else { | 625 | } else { |
| 626 | if (MD_SETJMP(orig_jb)) { | 626 | if (MD_SETJMP(orig_jb)) { |
| 627 | _st_iterate_threads_flag = 0; | 627 | _st_iterate_threads_flag = 0; |
| 628 | - thread = NULL; | ||
| 629 | - _st_show_thread_stack(thread, "Iteration completed"); | 628 | + trd = NULL; |
| 629 | + _st_show_thread_stack(trd, "Iteration completed"); | ||
| 630 | return; | 630 | return; |
| 631 | } | 631 | } |
| 632 | - thread = _ST_CURRENT_THREAD(); | ||
| 633 | - _st_show_thread_stack(thread, "Iteration started"); | 632 | + trd = _ST_CURRENT_THREAD(); |
| 633 | + _st_show_thread_stack(trd, "Iteration started"); | ||
| 634 | } | 634 | } |
| 635 | 635 | ||
| 636 | - q = thread->tlink.next; | 636 | + q = trd->tlink.next; |
| 637 | if (q == &_ST_THREADQ) { | 637 | if (q == &_ST_THREADQ) { |
| 638 | q = q->next; | 638 | q = q->next; |
| 639 | } | 639 | } |
| 640 | ST_ASSERT(q != &_ST_THREADQ); | 640 | ST_ASSERT(q != &_ST_THREADQ); |
| 641 | - thread = _ST_THREAD_THREADQ_PTR(q); | ||
| 642 | - if (thread == _ST_CURRENT_THREAD()) { | 641 | + trd = _ST_THREAD_THREADQ_PTR(q); |
| 642 | + if (trd == _ST_CURRENT_THREAD()) { | ||
| 643 | MD_LONGJMP(orig_jb, 1); | 643 | MD_LONGJMP(orig_jb, 1); |
| 644 | } | 644 | } |
| 645 | - memcpy(save_jb, thread->context, sizeof(jmp_buf)); | ||
| 646 | - MD_LONGJMP(thread->context, 1); | 645 | + memcpy(save_jb, trd->context, sizeof(jmp_buf)); |
| 646 | + MD_LONGJMP(trd->context, 1); | ||
| 647 | } | 647 | } |
| 648 | #endif /* DEBUG */ | 648 | #endif /* DEBUG */ |
| 649 | 649 |
| 1 | #include <unistd.h> | 1 | #include <unistd.h> |
| 2 | +#include <stdlib.h> | ||
| 2 | #include <stdio.h> | 3 | #include <stdio.h> |
| 3 | 4 | ||
| 4 | #include <sys/types.h> | 5 | #include <sys/types.h> |
| @@ -14,6 +15,64 @@ | @@ -14,6 +15,64 @@ | ||
| 14 | #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") | 15 | #define srs_trace(msg, ...) printf(msg, ##__VA_ARGS__);printf("\n") |
| 15 | 16 | ||
| 16 | int io_port = 1990; | 17 | int io_port = 1990; |
| 18 | +int sleep_ms = 100; | ||
| 19 | + | ||
| 20 | +void stack_print(long int previous_sp, int level) | ||
| 21 | +{ | ||
| 22 | + if (level <= 0) { | ||
| 23 | + return; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + register long int rsp asm("sp"); | ||
| 27 | + char buf[level * 1024]; | ||
| 28 | + | ||
| 29 | + stack_print(rsp, level - 1); | ||
| 30 | + | ||
| 31 | + srs_trace("%d. psp=%#lx, sp=%#lx, size=%dB(%dB+%dKB)", | ||
| 32 | + level, previous_sp, rsp, (int)(previous_sp - rsp), | ||
| 33 | + (int)(previous_sp - rsp - sizeof(buf)), (int)(sizeof(buf) / 1024)); | ||
| 34 | +} | ||
| 35 | + | ||
| 36 | +int huge_stack_test() | ||
| 37 | +{ | ||
| 38 | + srs_trace("==================================================="); | ||
| 39 | + srs_trace("huge_stack test: start"); | ||
| 40 | + | ||
| 41 | + register long int rsp asm("sp"); | ||
| 42 | + stack_print(rsp, 10); | ||
| 43 | + | ||
| 44 | + srs_trace("huge_stack test: end"); | ||
| 45 | + | ||
| 46 | + return 0; | ||
| 47 | +} | ||
| 48 | + | ||
| 49 | +void* thread_func(void* arg) | ||
| 50 | +{ | ||
| 51 | + srs_trace("1. thread run"); | ||
| 52 | + st_usleep(sleep_ms * 1000); | ||
| 53 | + srs_trace("2. thread completed"); | ||
| 54 | + return NULL; | ||
| 55 | +} | ||
| 56 | + | ||
| 57 | +int thread_test() | ||
| 58 | +{ | ||
| 59 | + srs_trace("==================================================="); | ||
| 60 | + srs_trace("thread test: start"); | ||
| 61 | + | ||
| 62 | + st_thread_t trd = st_thread_create(thread_func, NULL, 1, 0); | ||
| 63 | + if (trd == NULL) { | ||
| 64 | + srs_trace("st_thread_create failed"); | ||
| 65 | + return -1; | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + st_thread_join(trd, NULL); | ||
| 69 | + srs_trace("3. thread joined"); | ||
| 70 | + | ||
| 71 | + srs_trace("thread test: end"); | ||
| 72 | + exit(0); | ||
| 73 | + | ||
| 74 | + return 0; | ||
| 75 | +} | ||
| 17 | 76 | ||
| 18 | st_mutex_t sync_start = NULL; | 77 | st_mutex_t sync_start = NULL; |
| 19 | st_cond_t sync_cond = NULL; | 78 | st_cond_t sync_cond = NULL; |
| @@ -26,14 +85,14 @@ void* sync_master(void* arg) | @@ -26,14 +85,14 @@ void* sync_master(void* arg) | ||
| 26 | st_mutex_lock(sync_start); | 85 | st_mutex_lock(sync_start); |
| 27 | st_mutex_unlock(sync_start); | 86 | st_mutex_unlock(sync_start); |
| 28 | 87 | ||
| 29 | - st_usleep(100 * 1000); | 88 | + st_usleep(sleep_ms * 1000); |
| 30 | st_cond_signal(sync_cond); | 89 | st_cond_signal(sync_cond); |
| 31 | 90 | ||
| 32 | st_mutex_lock(sync_mutex); | 91 | st_mutex_lock(sync_mutex); |
| 33 | srs_trace("2. st mutex is ok"); | 92 | srs_trace("2. st mutex is ok"); |
| 34 | st_mutex_unlock(sync_mutex); | 93 | st_mutex_unlock(sync_mutex); |
| 35 | 94 | ||
| 36 | - st_usleep(100 * 1000); | 95 | + st_usleep(sleep_ms * 1000); |
| 37 | srs_trace("3. st thread is ok"); | 96 | srs_trace("3. st thread is ok"); |
| 38 | st_cond_signal(sync_cond); | 97 | st_cond_signal(sync_cond); |
| 39 | 98 | ||
| @@ -54,7 +113,7 @@ void* sync_slave(void* arg) | @@ -54,7 +113,7 @@ void* sync_slave(void* arg) | ||
| 54 | srs_trace("1. st cond is ok"); | 113 | srs_trace("1. st cond is ok"); |
| 55 | 114 | ||
| 56 | // release mutex to control thread | 115 | // release mutex to control thread |
| 57 | - st_usleep(100 * 1000); | 116 | + st_usleep(sleep_ms * 1000); |
| 58 | st_mutex_unlock(sync_mutex); | 117 | st_mutex_unlock(sync_mutex); |
| 59 | 118 | ||
| 60 | // wait thread to exit. | 119 | // wait thread to exit. |
| @@ -278,6 +337,16 @@ int main(int argc, char** argv) | @@ -278,6 +337,16 @@ int main(int argc, char** argv) | ||
| 278 | return -1; | 337 | return -1; |
| 279 | } | 338 | } |
| 280 | 339 | ||
| 340 | + if (huge_stack_test() < 0) { | ||
| 341 | + srs_trace("huge_stack_test failed"); | ||
| 342 | + return -1; | ||
| 343 | + } | ||
| 344 | + | ||
| 345 | + if (thread_test() < 0) { | ||
| 346 | + srs_trace("thread_test failed"); | ||
| 347 | + return -1; | ||
| 348 | + } | ||
| 349 | + | ||
| 281 | if (sync_test() < 0) { | 350 | if (sync_test() < 0) { |
| 282 | srs_trace("sync_test failed"); | 351 | srs_trace("sync_test failed"); |
| 283 | return -1; | 352 | return -1; |
-
请 注册 或 登录 后发表评论