正在显示
1 个修改的文件
包含
66 行增加
和
54 行删除
| @@ -47,7 +47,6 @@ | @@ -47,7 +47,6 @@ | ||
| 47 | #include <errno.h> | 47 | #include <errno.h> |
| 48 | #include "common.h" | 48 | #include "common.h" |
| 49 | 49 | ||
| 50 | - | ||
| 51 | /* Global data */ | 50 | /* Global data */ |
| 52 | _st_vp_t _st_this_vp; /* This VP */ | 51 | _st_vp_t _st_this_vp; /* This VP */ |
| 53 | _st_thread_t *_st_this_thread; /* Current thread */ | 52 | _st_thread_t *_st_this_thread; /* Current thread */ |
| @@ -56,7 +55,6 @@ int _st_active_count = 0; /* Active thread count */ | @@ -56,7 +55,6 @@ int _st_active_count = 0; /* Active thread count */ | ||
| 56 | time_t _st_curr_time = 0; /* Current time as returned by time(2) */ | 55 | time_t _st_curr_time = 0; /* Current time as returned by time(2) */ |
| 57 | st_utime_t _st_last_tset; /* Last time it was fetched */ | 56 | st_utime_t _st_last_tset; /* Last time it was fetched */ |
| 58 | 57 | ||
| 59 | - | ||
| 60 | int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | 58 | int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) |
| 61 | { | 59 | { |
| 62 | struct pollfd *pd; | 60 | struct pollfd *pd; |
| @@ -71,16 +69,18 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | @@ -71,16 +69,18 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | ||
| 71 | return -1; | 69 | return -1; |
| 72 | } | 70 | } |
| 73 | 71 | ||
| 74 | - if ((*_st_eventsys->pollset_add)(pds, npds) < 0) | 72 | + if ((*_st_eventsys->pollset_add)(pds, npds) < 0) { |
| 75 | return -1; | 73 | return -1; |
| 74 | + } | ||
| 76 | 75 | ||
| 77 | pq.pds = pds; | 76 | pq.pds = pds; |
| 78 | pq.npds = npds; | 77 | pq.npds = npds; |
| 79 | pq.thread = me; | 78 | pq.thread = me; |
| 80 | pq.on_ioq = 1; | 79 | pq.on_ioq = 1; |
| 81 | _ST_ADD_IOQ(pq); | 80 | _ST_ADD_IOQ(pq); |
| 82 | - if (timeout != ST_UTIME_NO_TIMEOUT) | 81 | + if (timeout != ST_UTIME_NO_TIMEOUT) { |
| 83 | _ST_ADD_SLEEPQ(me, timeout); | 82 | _ST_ADD_SLEEPQ(me, timeout); |
| 83 | + } | ||
| 84 | me->state = _ST_ST_IO_WAIT; | 84 | me->state = _ST_ST_IO_WAIT; |
| 85 | 85 | ||
| 86 | _ST_SWITCH_CONTEXT(me); | 86 | _ST_SWITCH_CONTEXT(me); |
| @@ -93,10 +93,11 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | @@ -93,10 +93,11 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | ||
| 93 | } else { | 93 | } else { |
| 94 | /* Count the number of ready descriptors */ | 94 | /* Count the number of ready descriptors */ |
| 95 | for (pd = pds; pd < epd; pd++) { | 95 | for (pd = pds; pd < epd; pd++) { |
| 96 | - if (pd->revents) | 96 | + if (pd->revents) { |
| 97 | n++; | 97 | n++; |
| 98 | } | 98 | } |
| 99 | } | 99 | } |
| 100 | + } | ||
| 100 | 101 | ||
| 101 | if (me->flags & _ST_FL_INTERRUPT) { | 102 | if (me->flags & _ST_FL_INTERRUPT) { |
| 102 | me->flags &= ~_ST_FL_INTERRUPT; | 103 | me->flags &= ~_ST_FL_INTERRUPT; |
| @@ -107,7 +108,6 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | @@ -107,7 +108,6 @@ int st_poll(struct pollfd *pds, int npds, st_utime_t timeout) | ||
| 107 | return n; | 108 | return n; |
| 108 | } | 109 | } |
| 109 | 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 *thread; |
| @@ -127,7 +127,6 @@ void _st_vp_schedule(void) | @@ -127,7 +127,6 @@ void _st_vp_schedule(void) | ||
| 127 | _ST_RESTORE_CONTEXT(thread); | 127 | _ST_RESTORE_CONTEXT(thread); |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | - | ||
| 131 | /* | 130 | /* |
| 132 | * Initialize this Virtual Processor | 131 | * Initialize this Virtual Processor |
| 133 | */ | 132 | */ |
| @@ -143,8 +142,9 @@ int st_init(void) | @@ -143,8 +142,9 @@ int st_init(void) | ||
| 143 | /* We can ignore return value here */ | 142 | /* We can ignore return value here */ |
| 144 | st_set_eventsys(ST_EVENTSYS_DEFAULT); | 143 | st_set_eventsys(ST_EVENTSYS_DEFAULT); |
| 145 | 144 | ||
| 146 | - if (_st_io_init() < 0) | 145 | + if (_st_io_init() < 0) { |
| 147 | return -1; | 146 | return -1; |
| 147 | + } | ||
| 148 | 148 | ||
| 149 | memset(&_st_this_vp, 0, sizeof(_st_vp_t)); | 149 | memset(&_st_this_vp, 0, sizeof(_st_vp_t)); |
| 150 | 150 | ||
| @@ -155,8 +155,9 @@ int st_init(void) | @@ -155,8 +155,9 @@ int st_init(void) | ||
| 155 | ST_INIT_CLIST(&_ST_THREADQ); | 155 | ST_INIT_CLIST(&_ST_THREADQ); |
| 156 | #endif | 156 | #endif |
| 157 | 157 | ||
| 158 | - if ((*_st_eventsys->init)() < 0) | 158 | + if ((*_st_eventsys->init)() < 0) { |
| 159 | return -1; | 159 | return -1; |
| 160 | + } | ||
| 160 | 161 | ||
| 161 | _st_this_vp.pagesize = getpagesize(); | 162 | _st_this_vp.pagesize = getpagesize(); |
| 162 | _st_this_vp.last_clock = st_utime(); | 163 | _st_this_vp.last_clock = st_utime(); |
| @@ -164,10 +165,10 @@ int st_init(void) | @@ -164,10 +165,10 @@ int st_init(void) | ||
| 164 | /* | 165 | /* |
| 165 | * Create idle thread | 166 | * Create idle thread |
| 166 | */ | 167 | */ |
| 167 | - _st_this_vp.idle_thread = st_thread_create(_st_idle_thread_start, | ||
| 168 | - NULL, 0, 0); | ||
| 169 | - if (!_st_this_vp.idle_thread) | 168 | + _st_this_vp.idle_thread = st_thread_create(_st_idle_thread_start, NULL, 0, 0); |
| 169 | + if (!_st_this_vp.idle_thread) { | ||
| 170 | return -1; | 170 | return -1; |
| 171 | + } | ||
| 171 | _st_this_vp.idle_thread->flags = _ST_FL_IDLE_THREAD; | 172 | _st_this_vp.idle_thread->flags = _ST_FL_IDLE_THREAD; |
| 172 | _st_active_count--; | 173 | _st_active_count--; |
| 173 | _ST_DEL_RUNQ(_st_this_vp.idle_thread); | 174 | _ST_DEL_RUNQ(_st_this_vp.idle_thread); |
| @@ -177,8 +178,9 @@ int st_init(void) | @@ -177,8 +178,9 @@ int st_init(void) | ||
| 177 | */ | 178 | */ |
| 178 | thread = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + | 179 | thread = (_st_thread_t *) calloc(1, sizeof(_st_thread_t) + |
| 179 | (ST_KEYS_MAX * sizeof(void *))); | 180 | (ST_KEYS_MAX * sizeof(void *))); |
| 180 | - if (!thread) | 181 | + if (!thread) { |
| 181 | return -1; | 182 | return -1; |
| 183 | + } | ||
| 182 | thread->private_data = (void **) (thread + 1); | 184 | thread->private_data = (void **) (thread + 1); |
| 183 | thread->state = _ST_ST_RUNNING; | 185 | thread->state = _ST_ST_RUNNING; |
| 184 | thread->flags = _ST_FL_PRIMORDIAL; | 186 | thread->flags = _ST_FL_PRIMORDIAL; |
| @@ -191,7 +193,6 @@ int st_init(void) | @@ -191,7 +193,6 @@ int st_init(void) | ||
| 191 | return 0; | 193 | return 0; |
| 192 | } | 194 | } |
| 193 | 195 | ||
| 194 | - | ||
| 195 | #ifdef ST_SWITCH_CB | 196 | #ifdef ST_SWITCH_CB |
| 196 | st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb) | 197 | st_switch_cb_t st_set_switch_in_cb(st_switch_cb_t cb) |
| 197 | { | 198 | { |
| @@ -208,7 +209,6 @@ st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb) | @@ -208,7 +209,6 @@ st_switch_cb_t st_set_switch_out_cb(st_switch_cb_t cb) | ||
| 208 | } | 209 | } |
| 209 | #endif | 210 | #endif |
| 210 | 211 | ||
| 211 | - | ||
| 212 | /* | 212 | /* |
| 213 | * Start function for the idle thread | 213 | * Start function for the idle thread |
| 214 | */ | 214 | */ |
| @@ -235,7 +235,6 @@ void *_st_idle_thread_start(void *arg) | @@ -235,7 +235,6 @@ void *_st_idle_thread_start(void *arg) | ||
| 235 | return NULL; | 235 | return NULL; |
| 236 | } | 236 | } |
| 237 | 237 | ||
| 238 | - | ||
| 239 | void st_thread_exit(void *retval) | 238 | void st_thread_exit(void *retval) |
| 240 | { | 239 | { |
| 241 | _st_thread_t *thread = _ST_CURRENT_THREAD(); | 240 | _st_thread_t *thread = _ST_CURRENT_THREAD(); |
| @@ -263,15 +262,15 @@ void st_thread_exit(void *retval) | @@ -263,15 +262,15 @@ void st_thread_exit(void *retval) | ||
| 263 | _ST_DEL_THREADQ(thread); | 262 | _ST_DEL_THREADQ(thread); |
| 264 | #endif | 263 | #endif |
| 265 | 264 | ||
| 266 | - if (!(thread->flags & _ST_FL_PRIMORDIAL)) | 265 | + if (!(thread->flags & _ST_FL_PRIMORDIAL)) { |
| 267 | _st_stack_free(thread->stack); | 266 | _st_stack_free(thread->stack); |
| 267 | + } | ||
| 268 | 268 | ||
| 269 | /* Find another thread to run */ | 269 | /* Find another thread to run */ |
| 270 | _ST_SWITCH_CONTEXT(thread); | 270 | _ST_SWITCH_CONTEXT(thread); |
| 271 | /* Not going to land here */ | 271 | /* Not going to land here */ |
| 272 | } | 272 | } |
| 273 | 273 | ||
| 274 | - | ||
| 275 | int st_thread_join(_st_thread_t *thread, void **retvalp) | 274 | int st_thread_join(_st_thread_t *thread, void **retvalp) |
| 276 | { | 275 | { |
| 277 | _st_cond_t *term = thread->term; | 276 | _st_cond_t *term = thread->term; |
| @@ -293,12 +292,14 @@ int st_thread_join(_st_thread_t *thread, void **retvalp) | @@ -293,12 +292,14 @@ int st_thread_join(_st_thread_t *thread, void **retvalp) | ||
| 293 | } | 292 | } |
| 294 | 293 | ||
| 295 | while (thread->state != _ST_ST_ZOMBIE) { | 294 | while (thread->state != _ST_ST_ZOMBIE) { |
| 296 | - if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0) | 295 | + if (st_cond_timedwait(term, ST_UTIME_NO_TIMEOUT) != 0) { |
| 297 | return -1; | 296 | return -1; |
| 298 | } | 297 | } |
| 298 | + } | ||
| 299 | 299 | ||
| 300 | - if (retvalp) | 300 | + if (retvalp) { |
| 301 | *retvalp = thread->retval; | 301 | *retvalp = thread->retval; |
| 302 | + } | ||
| 302 | 303 | ||
| 303 | /* | 304 | /* |
| 304 | * Remove target thread from the zombie queue and make it runnable. | 305 | * Remove target thread from the zombie queue and make it runnable. |
| @@ -311,7 +312,6 @@ int st_thread_join(_st_thread_t *thread, void **retvalp) | @@ -311,7 +312,6 @@ int st_thread_join(_st_thread_t *thread, void **retvalp) | ||
| 311 | return 0; | 312 | return 0; |
| 312 | } | 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 *thread = _ST_CURRENT_THREAD(); |
| @@ -330,13 +330,13 @@ void _st_thread_main(void) | @@ -330,13 +330,13 @@ void _st_thread_main(void) | ||
| 330 | st_thread_exit(thread->retval); | 330 | st_thread_exit(thread->retval); |
| 331 | } | 331 | } |
| 332 | 332 | ||
| 333 | - | ||
| 334 | /* | 333 | /* |
| 335 | * Insert "thread" into the timeout heap, in the position | 334 | * Insert "thread" into the timeout heap, in the position |
| 336 | * specified by thread->heap_index. See docs/timeout_heap.txt | 335 | * specified by thread->heap_index. See docs/timeout_heap.txt |
| 337 | * for details about the timeout heap. | 336 | * for details about the timeout heap. |
| 338 | */ | 337 | */ |
| 339 | -static _st_thread_t **heap_insert(_st_thread_t *thread) { | 338 | +static _st_thread_t **heap_insert(_st_thread_t *thread) |
| 339 | +{ | ||
| 340 | int target = thread->heap_index; | 340 | int target = thread->heap_index; |
| 341 | int s = target; | 341 | int s = target; |
| 342 | _st_thread_t **p = &_ST_SLEEPQ; | 342 | _st_thread_t **p = &_ST_SLEEPQ; |
| @@ -348,6 +348,7 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) { | @@ -348,6 +348,7 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) { | ||
| 348 | s >>= 1; | 348 | s >>= 1; |
| 349 | bits++; | 349 | bits++; |
| 350 | } | 350 | } |
| 351 | + | ||
| 351 | for (bit = bits - 2; bit >= 0; bit--) { | 352 | for (bit = bits - 2; bit >= 0; bit--) { |
| 352 | if (thread->due < (*p)->due) { | 353 | if (thread->due < (*p)->due) { |
| 353 | _st_thread_t *t = *p; | 354 | _st_thread_t *t = *p; |
| @@ -365,17 +366,19 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) { | @@ -365,17 +366,19 @@ static _st_thread_t **heap_insert(_st_thread_t *thread) { | ||
| 365 | p = &((*p)->left); | 366 | p = &((*p)->left); |
| 366 | } | 367 | } |
| 367 | } | 368 | } |
| 369 | + | ||
| 368 | thread->heap_index = index; | 370 | thread->heap_index = index; |
| 369 | *p = thread; | 371 | *p = thread; |
| 370 | thread->left = thread->right = NULL; | 372 | thread->left = thread->right = NULL; |
| 373 | + | ||
| 371 | return p; | 374 | return p; |
| 372 | } | 375 | } |
| 373 | 376 | ||
| 374 | - | ||
| 375 | /* | 377 | /* |
| 376 | * Delete "thread" from the timeout heap. | 378 | * Delete "thread" from the timeout heap. |
| 377 | */ | 379 | */ |
| 378 | -static void heap_delete(_st_thread_t *thread) { | 380 | +static void heap_delete(_st_thread_t *thread) |
| 381 | +{ | ||
| 379 | _st_thread_t *t, **p; | 382 | _st_thread_t *t, **p; |
| 380 | int bits = 0; | 383 | int bits = 0; |
| 381 | int s, bit; | 384 | int s, bit; |
| @@ -387,6 +390,7 @@ static void heap_delete(_st_thread_t *thread) { | @@ -387,6 +390,7 @@ static void heap_delete(_st_thread_t *thread) { | ||
| 387 | s >>= 1; | 390 | s >>= 1; |
| 388 | bits++; | 391 | bits++; |
| 389 | } | 392 | } |
| 393 | + | ||
| 390 | for (bit = bits - 2; bit >= 0; bit--) { | 394 | for (bit = bits - 2; bit >= 0; bit--) { |
| 391 | if (_ST_SLEEPQ_SIZE & (1 << bit)) { | 395 | if (_ST_SLEEPQ_SIZE & (1 << bit)) { |
| 392 | p = &((*p)->right); | 396 | p = &((*p)->right); |
| @@ -394,6 +398,7 @@ static void heap_delete(_st_thread_t *thread) { | @@ -394,6 +398,7 @@ static void heap_delete(_st_thread_t *thread) { | ||
| 394 | p = &((*p)->left); | 398 | p = &((*p)->left); |
| 395 | } | 399 | } |
| 396 | } | 400 | } |
| 401 | + | ||
| 397 | t = *p; | 402 | t = *p; |
| 398 | *p = NULL; | 403 | *p = NULL; |
| 399 | --_ST_SLEEPQ_SIZE; | 404 | --_ST_SLEEPQ_SIZE; |
| @@ -413,14 +418,17 @@ static void heap_delete(_st_thread_t *thread) { | @@ -413,14 +418,17 @@ static void heap_delete(_st_thread_t *thread) { | ||
| 413 | for (;;) { | 418 | for (;;) { |
| 414 | _st_thread_t *y; /* The younger child */ | 419 | _st_thread_t *y; /* The younger child */ |
| 415 | int index_tmp; | 420 | int index_tmp; |
| 416 | - if (t->left == NULL) | 421 | + |
| 422 | + if (t->left == NULL) { | ||
| 417 | break; | 423 | break; |
| 418 | - else if (t->right == NULL) | 424 | + } else if (t->right == NULL) { |
| 419 | y = t->left; | 425 | y = t->left; |
| 420 | - else if (t->left->due < t->right->due) | 426 | + } else if (t->left->due < t->right->due) { |
| 421 | y = t->left; | 427 | y = t->left; |
| 422 | - else | 428 | + } else { |
| 423 | y = t->right; | 429 | y = t->right; |
| 430 | + } | ||
| 431 | + | ||
| 424 | if (t->due > y->due) { | 432 | if (t->due > y->due) { |
| 425 | _st_thread_t *tl = y->left; | 433 | _st_thread_t *tl = y->left; |
| 426 | _st_thread_t *tr = y->right; | 434 | _st_thread_t *tr = y->right; |
| @@ -444,10 +452,10 @@ static void heap_delete(_st_thread_t *thread) { | @@ -444,10 +452,10 @@ static void heap_delete(_st_thread_t *thread) { | ||
| 444 | } | 452 | } |
| 445 | } | 453 | } |
| 446 | } | 454 | } |
| 455 | + | ||
| 447 | thread->left = thread->right = NULL; | 456 | thread->left = thread->right = NULL; |
| 448 | } | 457 | } |
| 449 | 458 | ||
| 450 | - | ||
| 451 | void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout) | 459 | void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout) |
| 452 | { | 460 | { |
| 453 | thread->due = _ST_LAST_CLOCK + timeout; | 461 | thread->due = _ST_LAST_CLOCK + timeout; |
| @@ -456,14 +464,12 @@ void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout) | @@ -456,14 +464,12 @@ void _st_add_sleep_q(_st_thread_t *thread, st_utime_t timeout) | ||
| 456 | heap_insert(thread); | 464 | heap_insert(thread); |
| 457 | } | 465 | } |
| 458 | 466 | ||
| 459 | - | ||
| 460 | void _st_del_sleep_q(_st_thread_t *thread) | 467 | void _st_del_sleep_q(_st_thread_t *thread) |
| 461 | { | 468 | { |
| 462 | heap_delete(thread); | 469 | heap_delete(thread); |
| 463 | thread->flags &= ~_ST_FL_ON_SLEEPQ; | 470 | thread->flags &= ~_ST_FL_ON_SLEEPQ; |
| 464 | } | 471 | } |
| 465 | 472 | ||
| 466 | - | ||
| 467 | void _st_vp_check_clock(void) | 473 | void _st_vp_check_clock(void) |
| 468 | { | 474 | { |
| 469 | _st_thread_t *thread; | 475 | _st_thread_t *thread; |
| @@ -481,13 +487,15 @@ void _st_vp_check_clock(void) | @@ -481,13 +487,15 @@ void _st_vp_check_clock(void) | ||
| 481 | while (_ST_SLEEPQ != NULL) { | 487 | while (_ST_SLEEPQ != NULL) { |
| 482 | thread = _ST_SLEEPQ; | 488 | thread = _ST_SLEEPQ; |
| 483 | ST_ASSERT(thread->flags & _ST_FL_ON_SLEEPQ); | 489 | ST_ASSERT(thread->flags & _ST_FL_ON_SLEEPQ); |
| 484 | - if (thread->due > now) | 490 | + if (thread->due > now) { |
| 485 | break; | 491 | break; |
| 492 | + } | ||
| 486 | _ST_DEL_SLEEPQ(thread); | 493 | _ST_DEL_SLEEPQ(thread); |
| 487 | 494 | ||
| 488 | /* 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 */ |
| 489 | - if (thread->state == _ST_ST_COND_WAIT) | 496 | + if (thread->state == _ST_ST_COND_WAIT) { |
| 490 | thread->flags |= _ST_FL_TIMEDOUT; | 497 | thread->flags |= _ST_FL_TIMEDOUT; |
| 498 | + } | ||
| 491 | 499 | ||
| 492 | /* Make thread runnable */ | 500 | /* Make thread runnable */ |
| 493 | ST_ASSERT(!(thread->flags & _ST_FL_IDLE_THREAD)); | 501 | ST_ASSERT(!(thread->flags & _ST_FL_IDLE_THREAD)); |
| @@ -496,29 +504,29 @@ void _st_vp_check_clock(void) | @@ -496,29 +504,29 @@ void _st_vp_check_clock(void) | ||
| 496 | } | 504 | } |
| 497 | } | 505 | } |
| 498 | 506 | ||
| 499 | - | ||
| 500 | void st_thread_interrupt(_st_thread_t *thread) | 507 | void st_thread_interrupt(_st_thread_t *thread) |
| 501 | { | 508 | { |
| 502 | /* If thread is already dead */ | 509 | /* If thread is already dead */ |
| 503 | - if (thread->state == _ST_ST_ZOMBIE) | 510 | + if (thread->state == _ST_ST_ZOMBIE) { |
| 504 | return; | 511 | return; |
| 512 | + } | ||
| 505 | 513 | ||
| 506 | thread->flags |= _ST_FL_INTERRUPT; | 514 | thread->flags |= _ST_FL_INTERRUPT; |
| 507 | 515 | ||
| 508 | - if (thread->state == _ST_ST_RUNNING || thread->state == _ST_ST_RUNNABLE) | 516 | + if (thread->state == _ST_ST_RUNNING || thread->state == _ST_ST_RUNNABLE) { |
| 509 | return; | 517 | return; |
| 518 | + } | ||
| 510 | 519 | ||
| 511 | - if (thread->flags & _ST_FL_ON_SLEEPQ) | 520 | + if (thread->flags & _ST_FL_ON_SLEEPQ) { |
| 512 | _ST_DEL_SLEEPQ(thread); | 521 | _ST_DEL_SLEEPQ(thread); |
| 522 | + } | ||
| 513 | 523 | ||
| 514 | /* Make thread runnable */ | 524 | /* Make thread runnable */ |
| 515 | thread->state = _ST_ST_RUNNABLE; | 525 | thread->state = _ST_ST_RUNNABLE; |
| 516 | _ST_ADD_RUNQ(thread); | 526 | _ST_ADD_RUNQ(thread); |
| 517 | } | 527 | } |
| 518 | 528 | ||
| 519 | - | ||
| 520 | -_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | ||
| 521 | - int joinable, int stk_size) | 529 | +_st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, int joinable, int stk_size) |
| 522 | { | 530 | { |
| 523 | _st_thread_t *thread; | 531 | _st_thread_t *thread; |
| 524 | _st_stack_t *stack; | 532 | _st_stack_t *stack; |
| @@ -529,17 +537,19 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | @@ -529,17 +537,19 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | ||
| 529 | #endif | 537 | #endif |
| 530 | 538 | ||
| 531 | /* Adjust stack size */ | 539 | /* Adjust stack size */ |
| 532 | - if (stk_size == 0) | 540 | + if (stk_size == 0) { |
| 533 | stk_size = ST_DEFAULT_STACK_SIZE; | 541 | stk_size = ST_DEFAULT_STACK_SIZE; |
| 542 | + } | ||
| 534 | stk_size = ((stk_size + _ST_PAGE_SIZE - 1) / _ST_PAGE_SIZE) * _ST_PAGE_SIZE; | 543 | stk_size = ((stk_size + _ST_PAGE_SIZE - 1) / _ST_PAGE_SIZE) * _ST_PAGE_SIZE; |
| 535 | stack = _st_stack_new(stk_size); | 544 | stack = _st_stack_new(stk_size); |
| 536 | - if (!stack) | 545 | + if (!stack) { |
| 537 | return NULL; | 546 | return NULL; |
| 547 | + } | ||
| 538 | 548 | ||
| 539 | /* Allocate thread object and per-thread data off the stack */ | 549 | /* Allocate thread object and per-thread data off the stack */ |
| 540 | #if defined (MD_STACK_GROWS_DOWN) | 550 | #if defined (MD_STACK_GROWS_DOWN) |
| 541 | sp = stack->stk_top; | 551 | sp = stack->stk_top; |
| 542 | -#ifdef __ia64__ | 552 | + #ifdef __ia64__ |
| 543 | /* | 553 | /* |
| 544 | * The stack segment is split in the middle. The upper half is used | 554 | * The stack segment is split in the middle. The upper half is used |
| 545 | * as backing store for the register stack which grows upward. | 555 | * as backing store for the register stack which grows upward. |
| @@ -550,18 +560,20 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | @@ -550,18 +560,20 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | ||
| 550 | sp -= (stk_size >> 1); | 560 | sp -= (stk_size >> 1); |
| 551 | bsp = sp; | 561 | bsp = sp; |
| 552 | /* Make register stack 64-byte aligned */ | 562 | /* Make register stack 64-byte aligned */ |
| 553 | - if ((unsigned long)bsp & 0x3f) | 563 | + if ((unsigned long)bsp & 0x3f) { |
| 554 | bsp = bsp + (0x40 - ((unsigned long)bsp & 0x3f)); | 564 | bsp = bsp + (0x40 - ((unsigned long)bsp & 0x3f)); |
| 565 | + } | ||
| 555 | stack->bsp = bsp + _ST_STACK_PAD_SIZE; | 566 | stack->bsp = bsp + _ST_STACK_PAD_SIZE; |
| 556 | -#endif | 567 | + #endif |
| 557 | sp = sp - (ST_KEYS_MAX * sizeof(void *)); | 568 | sp = sp - (ST_KEYS_MAX * sizeof(void *)); |
| 558 | ptds = (void **) sp; | 569 | ptds = (void **) sp; |
| 559 | sp = sp - sizeof(_st_thread_t); | 570 | sp = sp - sizeof(_st_thread_t); |
| 560 | thread = (_st_thread_t *) sp; | 571 | thread = (_st_thread_t *) sp; |
| 561 | 572 | ||
| 562 | /* Make stack 64-byte aligned */ | 573 | /* Make stack 64-byte aligned */ |
| 563 | - if ((unsigned long)sp & 0x3f) | 574 | + if ((unsigned long)sp & 0x3f) { |
| 564 | sp = sp - ((unsigned long)sp & 0x3f); | 575 | sp = sp - ((unsigned long)sp & 0x3f); |
| 576 | + } | ||
| 565 | stack->sp = sp - _ST_STACK_PAD_SIZE; | 577 | stack->sp = sp - _ST_STACK_PAD_SIZE; |
| 566 | #elif defined (MD_STACK_GROWS_UP) | 578 | #elif defined (MD_STACK_GROWS_UP) |
| 567 | sp = stack->stk_bottom; | 579 | sp = stack->stk_bottom; |
| @@ -571,11 +583,12 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | @@ -571,11 +583,12 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | ||
| 571 | sp = sp + (ST_KEYS_MAX * sizeof(void *)); | 583 | sp = sp + (ST_KEYS_MAX * sizeof(void *)); |
| 572 | 584 | ||
| 573 | /* Make stack 64-byte aligned */ | 585 | /* Make stack 64-byte aligned */ |
| 574 | - if ((unsigned long)sp & 0x3f) | 586 | + if ((unsigned long)sp & 0x3f) { |
| 575 | sp = sp + (0x40 - ((unsigned long)sp & 0x3f)); | 587 | sp = sp + (0x40 - ((unsigned long)sp & 0x3f)); |
| 588 | + } | ||
| 576 | stack->sp = sp + _ST_STACK_PAD_SIZE; | 589 | stack->sp = sp + _ST_STACK_PAD_SIZE; |
| 577 | #else | 590 | #else |
| 578 | -#error Unknown OS | 591 | + #error Unknown OS |
| 579 | #endif | 592 | #endif |
| 580 | 593 | ||
| 581 | memset(thread, 0, sizeof(_st_thread_t)); | 594 | memset(thread, 0, sizeof(_st_thread_t)); |
| @@ -613,18 +626,15 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | @@ -613,18 +626,15 @@ _st_thread_t *st_thread_create(void *(*start)(void *arg), void *arg, | ||
| 613 | return thread; | 626 | return thread; |
| 614 | } | 627 | } |
| 615 | 628 | ||
| 616 | - | ||
| 617 | _st_thread_t *st_thread_self(void) | 629 | _st_thread_t *st_thread_self(void) |
| 618 | { | 630 | { |
| 619 | return _ST_CURRENT_THREAD(); | 631 | return _ST_CURRENT_THREAD(); |
| 620 | } | 632 | } |
| 621 | 633 | ||
| 622 | - | ||
| 623 | #ifdef DEBUG | 634 | #ifdef DEBUG |
| 624 | /* ARGSUSED */ | 635 | /* ARGSUSED */ |
| 625 | void _st_show_thread_stack(_st_thread_t *thread, const char *messg) | 636 | void _st_show_thread_stack(_st_thread_t *thread, const char *messg) |
| 626 | { | 637 | { |
| 627 | - | ||
| 628 | } | 638 | } |
| 629 | 639 | ||
| 630 | /* To be set from debugger */ | 640 | /* To be set from debugger */ |
| @@ -659,12 +669,14 @@ void _st_iterate_threads(void) | @@ -659,12 +669,14 @@ void _st_iterate_threads(void) | ||
| 659 | } | 669 | } |
| 660 | 670 | ||
| 661 | q = thread->tlink.next; | 671 | q = thread->tlink.next; |
| 662 | - if (q == &_ST_THREADQ) | 672 | + if (q == &_ST_THREADQ) { |
| 663 | q = q->next; | 673 | q = q->next; |
| 674 | + } | ||
| 664 | ST_ASSERT(q != &_ST_THREADQ); | 675 | ST_ASSERT(q != &_ST_THREADQ); |
| 665 | thread = _ST_THREAD_THREADQ_PTR(q); | 676 | thread = _ST_THREAD_THREADQ_PTR(q); |
| 666 | - if (thread == _ST_CURRENT_THREAD()) | 677 | + if (thread == _ST_CURRENT_THREAD()) { |
| 667 | MD_LONGJMP(orig_jb, 1); | 678 | MD_LONGJMP(orig_jb, 1); |
| 679 | + } | ||
| 668 | memcpy(save_jb, thread->context, sizeof(jmp_buf)); | 680 | memcpy(save_jb, thread->context, sizeof(jmp_buf)); |
| 669 | MD_LONGJMP(thread->context, 1); | 681 | MD_LONGJMP(thread->context, 1); |
| 670 | } | 682 | } |
-
请 注册 或 登录 后发表评论