Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/proc/task.c

    r5a5269d r07d4271  
    158158                return rc;
    159159
    160         atomic_store(&task->refcount, 0);
    161160        atomic_store(&task->lifecount, 0);
    162161
     
    201200        if (!task)
    202201                return NULL;
     202
     203        refcount_init(&task->refcount);
    203204
    204205        task_create_arch(task);
     
    224225
    225226        task->answerbox.active = true;
     227
     228        task->debug_sections = NULL;
    226229
    227230#ifdef CONFIG_UDEBUG
     
    266269 *
    267270 */
    268 void task_destroy(task_t *task)
     271static void task_destroy(task_t *task)
    269272{
    270273        /*
     
    297300void task_hold(task_t *task)
    298301{
    299         atomic_inc(&task->refcount);
     302        refcount_up(&task->refcount);
    300303}
    301304
     
    309312void task_release(task_t *task)
    310313{
    311         if ((atomic_predec(&task->refcount)) == 0)
     314        if (refcount_down(&task->refcount))
    312315                task_destroy(task);
    313316}
     
    385388        irq_spinlock_lock(&tasks_lock, true);
    386389        irq_spinlock_lock(&TASK->lock, false);
    387         irq_spinlock_lock(&threads_lock, false);
    388390
    389391        /* Set task name */
    390392        str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
    391393
    392         irq_spinlock_unlock(&threads_lock, false);
    393394        irq_spinlock_unlock(&TASK->lock, false);
    394395        irq_spinlock_unlock(&tasks_lock, true);
     
    416417/** Find task structure corresponding to task ID.
    417418 *
    418  * The tasks_lock must be already held by the caller of this function and
    419  * interrupts must be disabled.
    420  *
    421419 * @param id Task ID.
    422420 *
    423  * @return Task structure address or NULL if there is no such task ID.
     421 * @return Task reference or NULL if there is no such task ID.
    424422 *
    425423 */
    426424task_t *task_find_by_id(task_id_t id)
    427425{
    428         assert(interrupts_disabled());
    429         assert(irq_spinlock_locked(&tasks_lock));
     426        task_t *task = NULL;
     427
     428        irq_spinlock_lock(&tasks_lock, true);
    430429
    431430        odlink_t *odlink = odict_find_eq(&tasks, &id, NULL);
    432         if (odlink != NULL)
    433                 return odict_get_instance(odlink, task_t, ltasks);
    434 
    435         return NULL;
     431        if (odlink != NULL) {
     432                task = odict_get_instance(odlink, task_t, ltasks);
     433
     434                /*
     435                 * The directory of tasks can't hold a reference, since that would
     436                 * prevent task from ever being destroyed. That means we have to
     437                 * check for the case where the task is already being destroyed, but
     438                 * not yet removed from the directory.
     439                 */
     440                if (!refcount_try_up(&task->refcount))
     441                        task = NULL;
     442        }
     443
     444        irq_spinlock_unlock(&tasks_lock, true);
     445
     446        return task;
    436447}
    437448
     
    506517        /* Current values of threads */
    507518        list_foreach(task->threads, th_link, thread_t, thread) {
    508                 irq_spinlock_lock(&thread->lock, false);
    509 
    510519                /* Process only counted threads */
    511520                if (!thread->uncounted) {
     
    515524                        }
    516525
    517                         uret += thread->ucycles;
    518                         kret += thread->kcycles;
     526                        uret += atomic_time_read(&thread->ucycles);
     527                        kret += atomic_time_read(&thread->kcycles);
    519528                }
    520 
    521                 irq_spinlock_unlock(&thread->lock, false);
    522529        }
    523530
     
    528535static void task_kill_internal(task_t *task)
    529536{
    530         irq_spinlock_lock(&task->lock, false);
    531         irq_spinlock_lock(&threads_lock, false);
     537        irq_spinlock_lock(&task->lock, true);
    532538
    533539        /*
     
    536542
    537543        list_foreach(task->threads, th_link, thread_t, thread) {
    538                 bool sleeping = false;
    539 
    540                 irq_spinlock_lock(&thread->lock, false);
    541 
    542                 thread->interrupted = true;
    543                 if (thread->state == Sleeping)
    544                         sleeping = true;
    545 
    546                 irq_spinlock_unlock(&thread->lock, false);
    547 
    548                 if (sleeping)
    549                         waitq_interrupt_sleep(thread);
     544                thread_interrupt(thread);
    550545        }
    551546
    552         irq_spinlock_unlock(&threads_lock, false);
    553         irq_spinlock_unlock(&task->lock, false);
     547        irq_spinlock_unlock(&task->lock, true);
    554548}
    555549
     
    569563                return EPERM;
    570564
    571         irq_spinlock_lock(&tasks_lock, true);
    572 
    573565        task_t *task = task_find_by_id(id);
    574         if (!task) {
    575                 irq_spinlock_unlock(&tasks_lock, true);
     566        if (!task)
    576567                return ENOENT;
    577         }
    578568
    579569        task_kill_internal(task);
    580         irq_spinlock_unlock(&tasks_lock, true);
    581 
     570        task_release(task);
    582571        return EOK;
    583572}
     
    609598        }
    610599
    611         irq_spinlock_lock(&tasks_lock, true);
    612600        task_kill_internal(TASK);
    613         irq_spinlock_unlock(&tasks_lock, true);
    614 
    615601        thread_exit();
    616602}
     
    641627        if (additional)
    642628                printf("%-8" PRIu64 " %9zu", task->taskid,
    643                     atomic_load(&task->refcount));
     629                    atomic_load(&task->lifecount));
    644630        else
    645631                printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
     
    653639                printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
    654640                    "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
    655                     ksuffix, atomic_load(&task->refcount));
     641                    ksuffix, atomic_load(&task->lifecount));
    656642        else
    657643                printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
Note: See TracChangeset for help on using the changeset viewer.