Ignore:
File:
1 edited

Legend:

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

    r07d4271 r40eab9f  
    158158                return rc;
    159159
     160        atomic_store(&task->refcount, 0);
    160161        atomic_store(&task->lifecount, 0);
    161162
     
    200201        if (!task)
    201202                return NULL;
    202 
    203         refcount_init(&task->refcount);
    204203
    205204        task_create_arch(task);
     
    269268 *
    270269 */
    271 static void task_destroy(task_t *task)
     270void task_destroy(task_t *task)
    272271{
    273272        /*
     
    300299void task_hold(task_t *task)
    301300{
    302         refcount_up(&task->refcount);
     301        atomic_inc(&task->refcount);
    303302}
    304303
     
    312311void task_release(task_t *task)
    313312{
    314         if (refcount_down(&task->refcount))
     313        if ((atomic_predec(&task->refcount)) == 0)
    315314                task_destroy(task);
    316315}
     
    417416/** Find task structure corresponding to task ID.
    418417 *
     418 * The tasks_lock must be already held by the caller of this function and
     419 * interrupts must be disabled.
     420 *
    419421 * @param id Task ID.
    420422 *
    421  * @return Task reference or NULL if there is no such task ID.
     423 * @return Task structure address or NULL if there is no such task ID.
    422424 *
    423425 */
    424426task_t *task_find_by_id(task_id_t id)
    425427{
    426         task_t *task = NULL;
    427 
    428         irq_spinlock_lock(&tasks_lock, true);
     428        assert(interrupts_disabled());
     429        assert(irq_spinlock_locked(&tasks_lock));
    429430
    430431        odlink_t *odlink = odict_find_eq(&tasks, &id, 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;
     432        if (odlink != NULL)
     433                return odict_get_instance(odlink, task_t, ltasks);
     434
     435        return NULL;
    447436}
    448437
     
    517506        /* Current values of threads */
    518507        list_foreach(task->threads, th_link, thread_t, thread) {
     508                irq_spinlock_lock(&thread->lock, false);
     509
    519510                /* Process only counted threads */
    520511                if (!thread->uncounted) {
     
    524515                        }
    525516
    526                         uret += atomic_time_read(&thread->ucycles);
    527                         kret += atomic_time_read(&thread->kcycles);
     517                        uret += thread->ucycles;
     518                        kret += thread->kcycles;
    528519                }
     520
     521                irq_spinlock_unlock(&thread->lock, false);
    529522        }
    530523
     
    535528static void task_kill_internal(task_t *task)
    536529{
    537         irq_spinlock_lock(&task->lock, true);
     530        irq_spinlock_lock(&task->lock, false);
    538531
    539532        /*
     
    545538        }
    546539
    547         irq_spinlock_unlock(&task->lock, true);
     540        irq_spinlock_unlock(&task->lock, false);
    548541}
    549542
     
    563556                return EPERM;
    564557
     558        irq_spinlock_lock(&tasks_lock, true);
     559
    565560        task_t *task = task_find_by_id(id);
    566         if (!task)
     561        if (!task) {
     562                irq_spinlock_unlock(&tasks_lock, true);
    567563                return ENOENT;
     564        }
    568565
    569566        task_kill_internal(task);
    570         task_release(task);
     567        irq_spinlock_unlock(&tasks_lock, true);
     568
    571569        return EOK;
    572570}
     
    598596        }
    599597
     598        irq_spinlock_lock(&tasks_lock, true);
    600599        task_kill_internal(TASK);
     600        irq_spinlock_unlock(&tasks_lock, true);
     601
    601602        thread_exit();
    602603}
     
    627628        if (additional)
    628629                printf("%-8" PRIu64 " %9zu", task->taskid,
    629                     atomic_load(&task->lifecount));
     630                    atomic_load(&task->refcount));
    630631        else
    631632                printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %10p %10p"
     
    639640                printf("%-8" PRIu64 " %9" PRIu64 "%c %9" PRIu64 "%c "
    640641                    "%9zu\n", task->taskid, ucycles, usuffix, kcycles,
    641                     ksuffix, atomic_load(&task->lifecount));
     642                    ksuffix, atomic_load(&task->refcount));
    642643        else
    643644                printf("%-8" PRIu64 " %-14s %-5" PRIu32 " %18p %18p\n",
Note: See TracChangeset for help on using the changeset viewer.