Changeset b91bb65 in mainline


Ignore:
Timestamp:
2006-06-05T14:11:18Z (19 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9c1c677
Parents:
e090e1bc
Message:

Kill task when its main thread exits.

Location:
generic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • generic/include/proc/task.h

    re090e1bc rb91bb65  
    4949       
    5050        char *name;
     51        thread_t *main_thread;  /**< Pointer to the main thread. */
    5152        link_t th_head;         /**< List of threads contained in this task. */
    5253        as_t *as;               /**< Address space. */
  • generic/src/proc/task.c

    re090e1bc rb91bb65  
    5959static task_id_t task_counter = 0;
    6060
    61 static void ktaskclnp(void *);
     61static void ktaskclnp(void *arg);
     62static void ktaskkill(void *arg);
    6263
    6364/** Initialize tasks
     
    9798        ta->as = as;
    9899        ta->name = name;
    99 
     100        ta->main_thread = NULL;
    100101        ta->refcount = 0;
    101102
     
    154155        as_area_t *a;
    155156        int rc;
    156         thread_t *t;
     157        thread_t *t1, *t2;
    157158        task_t *task;
    158159        uspace_arg_t *kernel_uarg;
     
    184185                USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
    185186
    186         t = thread_create(uinit, kernel_uarg, task, 0, "uinit");
    187         ASSERT(t);
    188         thread_ready(t);
    189        
     187        /*
     188         * Create the main thread.
     189         */
     190        t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
     191        ASSERT(t1);
     192       
     193        /*
     194         * Create killer thread for the new task.
     195         */
     196        t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
     197        ASSERT(t2);
     198        thread_ready(t2);
     199
     200        thread_ready(t1);
     201
    190202        return task;
    191203}
     
    253265        ta->accept_new_threads = false;
    254266        ta->refcount--;
    255        
     267
     268        /*
     269         * Interrupt all threads except this one.
     270         */     
    256271        for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
    257272                thread_t *thr;
     
    318333}
    319334
    320 /** Kernel thread used to cleanup the task. */
     335/** Kernel thread used to cleanup the task after it is killed. */
    321336void ktaskclnp(void *arg)
    322337{
    323338        ipl_t ipl;
    324         thread_t *t = NULL;
     339        thread_t *t = NULL, *main_thread;
    325340        link_t *cur;
    326341
     
    330345        ipl = interrupts_disable();
    331346        spinlock_lock(&TASK->lock);
     347       
     348        main_thread = TASK->main_thread;
    332349       
    333350        /*
     
    338355                if (t == THREAD)
    339356                        continue;
     357                else if (t == main_thread)
     358                        continue;
    340359                else
    341360                        break;
     
    346365       
    347366        if (t != THREAD) {
     367                ASSERT(t != main_thread);       /* uninit is joined and detached in ktaskkill */
    348368                thread_join(t);
    349369                thread_detach(t);
    350                 goto loop;
     370                goto loop;      /* go for another thread */
    351371        }
    352372       
     
    359379        futex_cleanup();
    360380}
     381
     382/** Kernel task used to kill a userspace task when its main thread exits.
     383 *
     384 * This thread waits until the main userspace thread (i.e. uninit) exits.
     385 * When this happens, the task is killed.
     386 *
     387 * @param arg Pointer to the thread structure of the task's main thread.
     388 */
     389void ktaskkill(void *arg)
     390{
     391        thread_t *t = (thread_t *) arg;
     392       
     393        /*
     394         * Userspace threads cannot detach themselves,
     395         * therefore the thread pointer is guaranteed to be valid.
     396         */
     397        thread_join(t); /* sleep uninterruptibly here! */
     398        thread_detach(t);
     399        task_kill(TASK->taskid);
     400}
  • generic/src/proc/thread.c

    re090e1bc rb91bb65  
    351351        }
    352352        list_append(&t->th_link, &task->th_head);
    353         task->refcount++;
     353        if (task->refcount++ == 0)
     354                task->main_thread = t;
    354355        spinlock_unlock(&task->lock);
    355356
Note: See TracChangeset for help on using the changeset viewer.