Changeset ea7890e7 in mainline for kernel/generic/src/proc/thread.c
- Timestamp:
- 2007-06-01T15:47:46Z (18 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 07be3c4
- Parents:
- ff3a34b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/proc/thread.c
rff3a34b rea7890e7 68 68 #include <syscall/copy.h> 69 69 #include <errno.h> 70 #include <console/klog.h> 70 71 71 72 … … 78 79 "Entering", 79 80 "Exiting", 80 " Undead"81 "JoinMe" 81 82 }; 82 83 … … 329 330 330 331 t->interrupted = false; 331 t->join_type = None;332 332 t->detached = false; 333 333 waitq_initialize(&t->join_wq); … … 343 343 thread_create_arch(t); 344 344 345 ipl = interrupts_disable();346 spinlock_lock(&task->lock);347 if (!task->accept_new_threads) {348 spinlock_unlock(&task->lock);349 slab_free(thread_slab, t);350 interrupts_restore(ipl);351 return NULL;352 } else {353 /*354 * Bump the reference count so that this task cannot be355 * destroyed while the new thread is being attached to it.356 */357 task->refcount++;358 }359 spinlock_unlock(&task->lock);360 interrupts_restore(ipl);361 362 345 if (!(flags & THREAD_FLAG_NOATTACH)) 363 346 thread_attach(t, task); … … 374 357 void thread_destroy(thread_t *t) 375 358 { 376 bool destroy_task = false; 377 378 ASSERT(t->state == Exiting || t->state == Undead); 359 ASSERT(t->state == Exiting || t->state == JoinMe); 379 360 ASSERT(t->task); 380 361 ASSERT(t->cpu); … … 396 377 spinlock_lock(&t->task->lock); 397 378 list_remove(&t->th_link); 398 if (--t->task->refcount == 0) {399 t->task->accept_new_threads = false;400 destroy_task = true;401 }402 379 spinlock_unlock(&t->task->lock); 403 404 if (destroy_task) 380 381 /* 382 * t is guaranteed to be the very last thread of its task. 383 * It is safe to destroy the task. 384 */ 385 if (atomic_predec(&t->task->refcount) == 0) 405 386 task_destroy(t->task); 406 387 … … 432 413 * Attach to the current task. 433 414 */ 434 ipl = interrupts_disable(); 415 ipl = interrupts_disable(); 435 416 spinlock_lock(&task->lock); 436 ASSERT(task->refcount); 417 atomic_inc(&task->refcount); 418 atomic_inc(&task->lifecount); 437 419 list_append(&t->th_link, &task->th_head); 438 if (task->refcount == 1)439 task->main_thread = t;440 420 spinlock_unlock(&task->lock); 441 421 … … 459 439 { 460 440 ipl_t ipl; 441 442 if (atomic_predec(&TASK->lifecount) == 0) { 443 /* 444 * We are the last thread in the task that still has not exited. 445 * With the exception of the moment the task was created, new 446 * threads can only be created by threads of the same task. 447 * We are safe to perform cleanup. 448 */ 449 if (THREAD->flags & THREAD_FLAG_USPACE) { 450 ipc_cleanup(); 451 futex_cleanup(); 452 klog_printf("Cleanup of task %llu completed.", 453 TASK->taskid); 454 } 455 } 461 456 462 457 restart: … … 469 464 goto restart; 470 465 } 466 471 467 THREAD->state = Exiting; 472 468 spinlock_unlock(&THREAD->lock); … … 525 521 /** Detach thread. 526 522 * 527 * Mark the thread as detached, if the thread is already in the Undeadstate,523 * Mark the thread as detached, if the thread is already in the JoinMe state, 528 524 * deallocate its resources. 529 525 * … … 541 537 spinlock_lock(&t->lock); 542 538 ASSERT(!t->detached); 543 if (t->state == Undead) {539 if (t->state == JoinMe) { 544 540 thread_destroy(t); /* unlocks &t->lock */ 545 541 interrupts_restore(ipl); … … 703 699 sizeof(t->tid)); 704 700 if (rc != 0) { 705 ipl_t ipl;706 707 701 /* 708 702 * We have encountered a failure, but the thread … … 712 706 713 707 /* 714 * The new thread structure is initialized, 715 * butis still not visible to the system.708 * The new thread structure is initialized, but 709 * is still not visible to the system. 716 710 * We can safely deallocate it. 717 711 */ 718 712 slab_free(thread_slab, t); 719 713 free(kernel_uarg); 720 721 /*722 * Now we need to decrement the task reference723 * counter. Because we are running within the724 * same task, thread t is not the last thread725 * in the task, so it is safe to merely726 * decrement the counter.727 */728 ipl = interrupts_disable();729 spinlock_lock(&TASK->lock);730 TASK->refcount--;731 spinlock_unlock(&TASK->lock);732 interrupts_restore(ipl);733 714 734 715 return (unative_t) rc;
Note:
See TracChangeset
for help on using the changeset viewer.