Changeset 8565a42 in mainline for kernel/generic/src/proc


Ignore:
Timestamp:
2018-03-02T20:34:50Z (7 years ago)
Author:
GitHub <noreply@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
a1a81f69, d5e5fd1
Parents:
3061bc1 (diff), 34e1206 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2018-03-02 20:34:50)
git-committer:
GitHub <noreply@…> (2018-03-02 20:34:50)
Message:

Remove all trailing whitespace, everywhere.

See individual commit messages for details.

Location:
kernel/generic/src/proc
Files:
4 edited

Legend:

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

    r3061bc1 r8565a42  
    7575        if (!prg->task)
    7676                return ELIMIT;
    77        
     77
    7878        /*
    7979         * Create the stack address space area.
     
    9494                return ENOMEM;
    9595        }
    96        
     96
    9797        uspace_arg_t *kernel_uarg = (uspace_arg_t *)
    9898            malloc(sizeof(uspace_arg_t), 0);
    99        
     99
    100100        kernel_uarg->uspace_entry = (void *) entry_addr;
    101101        kernel_uarg->uspace_stack = (void *) virt;
     
    104104        kernel_uarg->uspace_thread_arg = NULL;
    105105        kernel_uarg->uspace_uarg = NULL;
    106        
     106
    107107        /*
    108108         * Create the main thread.
     
    117117                return ELIMIT;
    118118        }
    119        
     119
    120120        return EOK;
    121121}
     
    142142        if (!as)
    143143                return ENOMEM;
    144        
     144
    145145        prg->loader_status = elf_load((elf_header_t *) image_addr, as, 0);
    146146        if (prg->loader_status != EE_OK) {
     
    148148                prg->task = NULL;
    149149                prg->main_thread = NULL;
    150                
     150
    151151                if (prg->loader_status != EE_LOADER)
    152152                        return ENOTSUP;
    153                
     153
    154154                /* Register image as the program loader */
    155155                if (program_loader != NULL)
    156156                        return ELIMIT;
    157                
     157
    158158                program_loader = image_addr;
    159159                log(LF_OTHER, LVL_NOTE, "Program loader at %p", (void *) image_addr);
    160                
     160
    161161                return EOK;
    162162        }
    163        
     163
    164164        return program_create(as, ((elf_header_t *) image_addr)->e_entry,
    165165            name, prg);
     
    179179        if (!as)
    180180                return ENOMEM;
    181        
     181
    182182        void *loader = program_loader;
    183183        if (!loader) {
     
    187187                return ENOENT;
    188188        }
    189        
     189
    190190        prg->loader_status = elf_load((elf_header_t *) program_loader, as,
    191191            ELD_F_LOADER);
     
    196196                return ENOENT;
    197197        }
    198        
     198
    199199        return program_create(as, ((elf_header_t *) program_loader)->e_entry,
    200200            name, prg);
     
    230230        if (name_len > TASK_NAME_BUFLEN - 1)
    231231                name_len = TASK_NAME_BUFLEN - 1;
    232        
     232
    233233        char namebuf[TASK_NAME_BUFLEN];
    234234        errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
    235235        if (rc != EOK)
    236236                return (sys_errno_t) rc;
    237        
     237
    238238        namebuf[name_len] = 0;
    239        
     239
    240240        /* Spawn the new task. */
    241241        program_t prg;
     
    243243        if (rc != EOK)
    244244                return rc;
    245        
     245
    246246        // FIXME: control the permissions
    247247        perm_set(prg.task, perm_get(TASK));
    248248        program_ready(&prg);
    249        
     249
    250250        return EOK;
    251251}
  • kernel/generic/src/proc/scheduler.c

    r3061bc1 r8565a42  
    9090        before_thread_runs_arch();
    9191        rcu_before_thread_runs();
    92        
     92
    9393#ifdef CONFIG_FPU_LAZY
    9494        if (THREAD == CPU->fpu_owner)
     
    105105        }
    106106#endif
    107        
     107
    108108#ifdef CONFIG_UDEBUG
    109109        if (THREAD->btrace) {
     
    113113                        stack_trace_istate(istate);
    114114                }
    115                
     115
    116116                THREAD->btrace = false;
    117117        }
     
    141141        fpu_enable();
    142142        irq_spinlock_lock(&CPU->lock, false);
    143        
     143
    144144        /* Save old context */
    145145        if (CPU->fpu_owner != NULL) {
    146146                irq_spinlock_lock(&CPU->fpu_owner->lock, false);
    147147                fpu_context_save(CPU->fpu_owner->saved_fpu_context);
    148                
     148
    149149                /* Don't prevent migration */
    150150                CPU->fpu_owner->fpu_context_engaged = false;
     
    152152                CPU->fpu_owner = NULL;
    153153        }
    154        
     154
    155155        irq_spinlock_lock(&THREAD->lock, false);
    156156        if (THREAD->fpu_context_exists) {
     
    164164                        THREAD->saved_fpu_context =
    165165                            (fpu_context_t *) slab_alloc(fpu_context_cache, 0);
    166                        
     166
    167167                        /* We may have switched CPUs during slab_alloc */
    168168                        goto restart;
     
    171171                THREAD->fpu_context_exists = true;
    172172        }
    173        
     173
    174174        CPU->fpu_owner = THREAD;
    175175        THREAD->fpu_context_engaged = true;
    176176        irq_spinlock_unlock(&THREAD->lock, false);
    177        
     177
    178178        irq_spinlock_unlock(&CPU->lock, false);
    179179}
     
    201201{
    202202        assert(CPU != NULL);
    203        
     203
    204204loop:
    205        
     205
    206206        if (atomic_get(&CPU->nrdy) == 0) {
    207207                /*
     
    214214                irq_spinlock_unlock(&CPU->lock, false);
    215215                interrupts_enable();
    216                
     216
    217217                /*
    218218                 * An interrupt might occur right now and wake up a thread.
     
    226226
    227227        assert(!CPU->idle);
    228        
     228
    229229        unsigned int i;
    230230        for (i = 0; i < RQ_COUNT; i++) {
     
    237237                        continue;
    238238                }
    239                
     239
    240240                atomic_dec(&CPU->nrdy);
    241241                atomic_dec(&nrdy);
    242242                CPU->rq[i].n--;
    243                
     243
    244244                /*
    245245                 * Take the first thread from the queue.
     
    248248                    list_first(&CPU->rq[i].rq), thread_t, rq_link);
    249249                list_remove(&thread->rq_link);
    250                
     250
    251251                irq_spinlock_pass(&(CPU->rq[i].lock), &thread->lock);
    252                
     252
    253253                thread->cpu = CPU;
    254254                thread->ticks = us2ticks((i + 1) * 10000);
    255255                thread->priority = i;  /* Correct rq index */
    256                
     256
    257257                /*
    258258                 * Clear the stolen flag so that it can be migrated
     
    261261                thread->stolen = false;
    262262                irq_spinlock_unlock(&thread->lock, false);
    263                
     263
    264264                return thread;
    265265        }
    266        
     266
    267267        goto loop;
    268268}
     
    282282{
    283283        list_t list;
    284        
     284
    285285        list_initialize(&list);
    286286        irq_spinlock_lock(&CPU->lock, false);
    287        
     287
    288288        if (CPU->needs_relink > NEEDS_RELINK_MAX) {
    289289                int i;
    290290                for (i = start; i < RQ_COUNT - 1; i++) {
    291291                        /* Remember and empty rq[i + 1] */
    292                        
     292
    293293                        irq_spinlock_lock(&CPU->rq[i + 1].lock, false);
    294294                        list_concat(&list, &CPU->rq[i + 1].rq);
     
    296296                        CPU->rq[i + 1].n = 0;
    297297                        irq_spinlock_unlock(&CPU->rq[i + 1].lock, false);
    298                        
     298
    299299                        /* Append rq[i + 1] to rq[i] */
    300                        
     300
    301301                        irq_spinlock_lock(&CPU->rq[i].lock, false);
    302302                        list_concat(&CPU->rq[i].rq, &list);
     
    304304                        irq_spinlock_unlock(&CPU->rq[i].lock, false);
    305305                }
    306                
     306
    307307                CPU->needs_relink = 0;
    308308        }
    309        
     309
    310310        irq_spinlock_unlock(&CPU->lock, false);
    311311}
     
    321321{
    322322        volatile ipl_t ipl;
    323        
     323
    324324        assert(CPU != NULL);
    325        
     325
    326326        ipl = interrupts_disable();
    327        
     327
    328328        if (atomic_get(&haltstate))
    329329                halt();
    330        
     330
    331331        if (THREAD) {
    332332                irq_spinlock_lock(&THREAD->lock, false);
    333                
     333
    334334                /* Update thread kernel accounting */
    335335                THREAD->kcycles += get_cycle() - THREAD->last_cycle;
    336                
     336
    337337#if (defined CONFIG_FPU) && (!defined CONFIG_FPU_LAZY)
    338338                fpu_context_save(THREAD->saved_fpu_context);
     
    342342                         * This is the place where threads leave scheduler();
    343343                         */
    344                        
     344
    345345                        /* Save current CPU cycle */
    346346                        THREAD->last_cycle = get_cycle();
    347                        
     347
    348348                        irq_spinlock_unlock(&THREAD->lock, false);
    349349                        interrupts_restore(THREAD->saved_context.ipl);
    350                        
     350
    351351                        return;
    352352                }
    353                
     353
    354354                /*
    355355                 * Interrupt priority level of preempted thread is recorded
     
    360360                THREAD->saved_context.ipl = ipl;
    361361        }
    362        
     362
    363363        /*
    364364         * Through the 'THE' structure, we keep track of THREAD, TASK, CPU, AS
     
    368368         */
    369369        the_copy(THE, (the_t *) CPU->stack);
    370        
     370
    371371        /*
    372372         * We may not keep the old stack.
     
    386386            (uintptr_t) CPU->stack, STACK_SIZE);
    387387        context_restore(&CPU->saved_context);
    388        
     388
    389389        /* Not reached */
    390390}
     
    402402        task_t *old_task = TASK;
    403403        as_t *old_as = AS;
    404        
     404
    405405        assert((!THREAD) || (irq_spinlock_locked(&THREAD->lock)));
    406406        assert(CPU != NULL);
    407407        assert(interrupts_disabled());
    408        
     408
    409409        /*
    410410         * Hold the current task and the address space to prevent their
     
    414414        if (old_task)
    415415                task_hold(old_task);
    416        
     416
    417417        if (old_as)
    418418                as_hold(old_as);
    419        
     419
    420420        if (THREAD) {
    421421                /* Must be run after the switch to scheduler stack */
    422422                after_thread_ran();
    423                
     423
    424424                switch (THREAD->state) {
    425425                case Running:
     
    427427                        thread_ready(THREAD);
    428428                        break;
    429                
     429
    430430                case Exiting:
    431431                        rcu_thread_exiting();
     
    452452                                    WAKEUP_FIRST);
    453453                                irq_spinlock_unlock(&THREAD->join_wq.lock, false);
    454                                
     454
    455455                                THREAD->state = Lingering;
    456456                                irq_spinlock_unlock(&THREAD->lock, false);
    457457                        }
    458458                        break;
    459                        
     459
    460460                case Sleeping:
    461461                        /*
     
    463463                         */
    464464                        THREAD->priority = -1;
    465                        
     465
    466466                        /*
    467467                         * We need to release wq->lock which we locked in
     
    470470                         */
    471471                        irq_spinlock_unlock(&THREAD->sleep_queue->lock, false);
    472                        
     472
    473473                        irq_spinlock_unlock(&THREAD->lock, false);
    474474                        break;
    475                
     475
    476476                default:
    477477                        /*
     
    482482                        break;
    483483                }
    484                
     484
    485485                THREAD = NULL;
    486486        }
    487        
     487
    488488        THREAD = find_best_thread();
    489        
     489
    490490        irq_spinlock_lock(&THREAD->lock, false);
    491491        int priority = THREAD->priority;
    492492        irq_spinlock_unlock(&THREAD->lock, false);
    493        
     493
    494494        relink_rq(priority);
    495        
     495
    496496        /*
    497497         * If both the old and the new task are the same,
     
    500500        if (TASK != THREAD->task) {
    501501                as_t *new_as = THREAD->task->as;
    502                
     502
    503503                /*
    504504                 * Note that it is possible for two tasks
     
    512512                        as_switch(old_as, new_as);
    513513                }
    514                
     514
    515515                TASK = THREAD->task;
    516516                before_task_runs();
    517517        }
    518        
     518
    519519        if (old_task)
    520520                task_release(old_task);
    521        
     521
    522522        if (old_as)
    523523                as_release(old_as);
    524        
     524
    525525        irq_spinlock_lock(&THREAD->lock, false);
    526526        THREAD->state = Running;
    527        
     527
    528528#ifdef SCHEDULER_VERBOSE
    529529        log(LF_OTHER, LVL_DEBUG,
     
    532532            THREAD->ticks, atomic_get(&CPU->nrdy));
    533533#endif
    534        
     534
    535535        /*
    536536         * Some architectures provide late kernel PA2KA(identity)
     
    542542         */
    543543        before_thread_runs();
    544        
     544
    545545        /*
    546546         * Copy the knowledge of CPU, TASK, THREAD and preemption counter to
     
    548548         */
    549549        the_copy(THE, (the_t *) THREAD->kstack);
    550        
     550
    551551        context_restore(&THREAD->saved_context);
    552        
     552
    553553        /* Not reached */
    554554}
     
    567567        atomic_count_t average;
    568568        atomic_count_t rdy;
    569        
     569
    570570        /*
    571571         * Detach kcpulb as nobody will call thread_join_timeout() on it.
    572572         */
    573573        thread_detach(THREAD);
    574        
     574
    575575loop:
    576576        /*
     
    578578         */
    579579        thread_sleep(1);
    580        
     580
    581581not_satisfied:
    582582        /*
     
    588588        average = atomic_get(&nrdy) / config.cpu_active + 1;
    589589        rdy = atomic_get(&CPU->nrdy);
    590        
     590
    591591        if (average <= rdy)
    592592                goto satisfied;
    593        
     593
    594594        atomic_count_t count = average - rdy;
    595        
     595
    596596        /*
    597597         * Searching least priority queues on all CPU's first and most priority
     
    601601        size_t acpu_bias = 0;
    602602        int rq;
    603        
     603
    604604        for (rq = RQ_COUNT - 1; rq >= 0; rq--) {
    605605                for (acpu = 0; acpu < config.cpu_active; acpu++) {
    606606                        cpu_t *cpu = &cpus[(acpu + acpu_bias) % config.cpu_active];
    607                        
     607
    608608                        /*
    609609                         * Not interested in ourselves.
     
    614614                        if (CPU == cpu)
    615615                                continue;
    616                        
     616
    617617                        if (atomic_get(&cpu->nrdy) <= average)
    618618                                continue;
    619                        
     619
    620620                        irq_spinlock_lock(&(cpu->rq[rq].lock), true);
    621621                        if (cpu->rq[rq].n == 0) {
     
    623623                                continue;
    624624                        }
    625                        
     625
    626626                        thread_t *thread = NULL;
    627                        
     627
    628628                        /* Search rq from the back */
    629629                        link_t *link = cpu->rq[rq].rq.head.prev;
    630                        
     630
    631631                        while (link != &(cpu->rq[rq].rq.head)) {
    632632                                thread = (thread_t *) list_get_instance(link,
    633633                                    thread_t, rq_link);
    634                                
     634
    635635                                /*
    636636                                 * Do not steal CPU-wired threads, threads
     
    640640                                 */
    641641                                irq_spinlock_lock(&thread->lock, false);
    642                                
     642
    643643                                if ((!thread->wired) && (!thread->stolen) &&
    644644                                    (!thread->nomigrate) &&
     
    649649                                        irq_spinlock_unlock(&thread->lock,
    650650                                            false);
    651                                        
     651
    652652                                        atomic_dec(&cpu->nrdy);
    653653                                        atomic_dec(&nrdy);
    654                                        
     654
    655655                                        cpu->rq[rq].n--;
    656656                                        list_remove(&thread->rq_link);
    657                                        
     657
    658658                                        break;
    659659                                }
    660                                
     660
    661661                                irq_spinlock_unlock(&thread->lock, false);
    662                                
     662
    663663                                link = link->prev;
    664664                                thread = NULL;
    665665                        }
    666                        
     666
    667667                        if (thread) {
    668668                                /*
    669669                                 * Ready thread on local CPU
    670670                                 */
    671                                
     671
    672672                                irq_spinlock_pass(&(cpu->rq[rq].lock),
    673673                                    &thread->lock);
    674                                
     674
    675675#ifdef KCPULB_VERBOSE
    676676                                log(LF_OTHER, LVL_DEBUG,
     
    680680                                    atomic_get(&nrdy) / config.cpu_active);
    681681#endif
    682                                
     682
    683683                                thread->stolen = true;
    684684                                thread->state = Entering;
    685                                
     685
    686686                                irq_spinlock_unlock(&thread->lock, true);
    687687                                thread_ready(thread);
    688                                
     688
    689689                                if (--count == 0)
    690690                                        goto satisfied;
    691                                
     691
    692692                                /*
    693693                                 * We are not satisfied yet, focus on another
     
    696696                                 */
    697697                                acpu_bias++;
    698                                
     698
    699699                                continue;
    700700                        } else
    701701                                irq_spinlock_unlock(&(cpu->rq[rq].lock), true);
    702                        
    703                 }
    704         }
    705        
     702
     703                }
     704        }
     705
    706706        if (atomic_get(&CPU->nrdy)) {
    707707                /*
     
    718718                goto loop;
    719719        }
    720        
     720
    721721        goto not_satisfied;
    722        
     722
    723723satisfied:
    724724        goto loop;
     
    735735                if (!cpus[cpu].active)
    736736                        continue;
    737                
     737
    738738                irq_spinlock_lock(&cpus[cpu].lock, true);
    739                
     739
    740740                printf("cpu%u: address=%p, nrdy=%" PRIua ", needs_relink=%zu\n",
    741741                    cpus[cpu].id, &cpus[cpu], atomic_get(&cpus[cpu].nrdy),
    742742                    cpus[cpu].needs_relink);
    743                
     743
    744744                unsigned int i;
    745745                for (i = 0; i < RQ_COUNT; i++) {
     
    749749                                continue;
    750750                        }
    751                        
     751
    752752                        printf("\trq[%u]: ", i);
    753753                        list_foreach(cpus[cpu].rq[i].rq, rq_link, thread_t,
     
    757757                        }
    758758                        printf("\n");
    759                        
     759
    760760                        irq_spinlock_unlock(&(cpus[cpu].rq[i].lock), false);
    761761                }
    762                
     762
    763763                irq_spinlock_unlock(&cpus[cpu].lock, true);
    764764        }
  • kernel/generic/src/proc/task.c

    r3061bc1 r8565a42  
    107107        task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
    108108        size_t *cnt = (size_t *) arg;
    109        
     109
    110110        if (task != TASK) {
    111111                (*cnt)++;
    112                
     112
    113113#ifdef CONFIG_DEBUG
    114114                printf("[%"PRIu64"] ", task->taskid);
    115115#endif
    116                
     116
    117117                task_kill_internal(task);
    118118        }
    119        
     119
    120120        /* Continue the walk */
    121121        return true;
     
    138138                task_release(task_0);
    139139        }
    140        
     140
    141141        /* Repeat until there are any tasks except TASK */
    142142        do {
     
    144144                printf("Killing tasks... ");
    145145#endif
    146                
     146
    147147                irq_spinlock_lock(&tasks_lock, true);
    148148                tasks_left = 0;
    149149                avltree_walk(&tasks_tree, task_done_walker, &tasks_left);
    150150                irq_spinlock_unlock(&tasks_lock, true);
    151                
     151
    152152                thread_sleep(1);
    153                
     153
    154154#ifdef CONFIG_DEBUG
    155155                printf("\n");
     
    165165        if (rc != EOK)
    166166                return rc;
    167        
     167
    168168        atomic_set(&task->refcount, 0);
    169169        atomic_set(&task->lifecount, 0);
    170        
     170
    171171        irq_spinlock_initialize(&task->lock, "task_t_lock");
    172        
     172
    173173        list_initialize(&task->threads);
    174        
     174
    175175        ipc_answerbox_init(&task->answerbox, task);
    176        
     176
    177177        spinlock_initialize(&task->active_calls_lock, "active_calls_lock");
    178178        list_initialize(&task->active_calls);
    179                
     179
    180180#ifdef CONFIG_UDEBUG
    181181        /* Init kbox stuff */
     
    184184        mutex_initialize(&task->kb.cleanup_lock, MUTEX_PASSIVE);
    185185#endif
    186        
     186
    187187        return EOK;
    188188}
     
    191191{
    192192        task_t *task = (task_t *) obj;
    193        
     193
    194194        caps_task_free(task);
    195195        return 0;
     
    210210                return NULL;
    211211        }
    212        
     212
    213213        task_create_arch(task);
    214        
     214
    215215        task->as = as;
    216216        str_cpy(task->name, TASK_NAME_BUFLEN, name);
    217        
     217
    218218        task->container = CONTAINER;
    219219        task->perms = 0;
     
    231231
    232232        event_task_init(task);
    233        
     233
    234234        task->answerbox.active = true;
    235235
     
    237237        /* Init debugging stuff */
    238238        udebug_task_init(&task->udebug);
    239        
     239
    240240        /* Init kbox stuff */
    241241        task->kb.box.active = true;
    242242        task->kb.finished = false;
    243243#endif
    244        
     244
    245245        if ((ipc_phone_0) &&
    246246            (container_check(ipc_phone_0->task->container, task->container))) {
     
    253253                        return NULL;
    254254                }
    255                
     255
    256256                kobject_t *phone_obj = kobject_get(task, phone_handle,
    257257                    KOBJECT_TYPE_PHONE);
    258258                (void) ipc_phone_connect(phone_obj->phone, ipc_phone_0);
    259259        }
    260        
     260
    261261        futex_task_init(task);
    262        
     262
    263263        /*
    264264         * Get a reference to the address space.
    265265         */
    266266        as_hold(task->as);
    267        
     267
    268268        irq_spinlock_lock(&tasks_lock, true);
    269        
     269
    270270        task->taskid = ++task_counter;
    271271        avltree_node_initialize(&task->tasks_tree_node);
    272272        task->tasks_tree_node.key = task->taskid;
    273273        avltree_insert(&tasks_tree, &task->tasks_tree_node);
    274        
     274
    275275        irq_spinlock_unlock(&tasks_lock, true);
    276        
     276
    277277        return task;
    278278}
     
    291291        avltree_delete(&tasks_tree, &task->tasks_tree_node);
    292292        irq_spinlock_unlock(&tasks_lock, true);
    293        
     293
    294294        /*
    295295         * Perform architecture specific task destruction.
    296296         */
    297297        task_destroy_arch(task);
    298        
     298
    299299        /*
    300300         * Free up dynamically allocated state.
    301301         */
    302302        futex_task_deinit(task);
    303        
     303
    304304        /*
    305305         * Drop our reference to the address space.
    306306         */
    307307        as_release(task->as);
    308        
     308
    309309        slab_free(task_cache, task);
    310310}
     
    388388{
    389389        char namebuf[TASK_NAME_BUFLEN];
    390        
     390
    391391        /* Cap length of name and copy it from userspace. */
    392392        if (name_len > TASK_NAME_BUFLEN - 1)
    393393                name_len = TASK_NAME_BUFLEN - 1;
    394        
     394
    395395        errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
    396396        if (rc != EOK)
    397397                return (sys_errno_t) rc;
    398        
     398
    399399        namebuf[name_len] = '\0';
    400        
     400
    401401        /*
    402402         * As the task name is referenced also from the
     
    404404         * of the update.
    405405         */
    406        
     406
    407407        irq_spinlock_lock(&tasks_lock, true);
    408408        irq_spinlock_lock(&TASK->lock, false);
    409409        irq_spinlock_lock(&threads_lock, false);
    410        
     410
    411411        /* Set task name */
    412412        str_cpy(TASK->name, TASK_NAME_BUFLEN, namebuf);
    413        
     413
    414414        irq_spinlock_unlock(&threads_lock, false);
    415415        irq_spinlock_unlock(&TASK->lock, false);
    416416        irq_spinlock_unlock(&tasks_lock, true);
    417        
     417
    418418        return EOK;
    419419}
     
    432432        if (rc != EOK)
    433433                return (sys_errno_t) rc;
    434        
     434
    435435        return (sys_errno_t) task_kill(taskid);
    436436}
     
    453453        avltree_node_t *node =
    454454            avltree_search(&tasks_tree, (avltree_key_t) id);
    455        
     455
    456456        if (node)
    457457                return avltree_get_instance(node, task_t, tasks_tree_node);
    458        
     458
    459459        return NULL;
    460460}
     
    478478        uint64_t uret = task->ucycles;
    479479        uint64_t kret = task->kcycles;
    480        
     480
    481481        /* Current values of threads */
    482482        list_foreach(task->threads, th_link, thread_t, thread) {
    483483                irq_spinlock_lock(&thread->lock, false);
    484                
     484
    485485                /* Process only counted threads */
    486486                if (!thread->uncounted) {
     
    489489                                thread_update_accounting(false);
    490490                        }
    491                        
     491
    492492                        uret += thread->ucycles;
    493493                        kret += thread->kcycles;
    494494                }
    495                
     495
    496496                irq_spinlock_unlock(&thread->lock, false);
    497497        }
    498        
     498
    499499        *ucycles = uret;
    500500        *kcycles = kret;
     
    505505        irq_spinlock_lock(&task->lock, false);
    506506        irq_spinlock_lock(&threads_lock, false);
    507        
     507
    508508        /*
    509509         * Interrupt all threads.
    510510         */
    511        
     511
    512512        list_foreach(task->threads, th_link, thread_t, thread) {
    513513                bool sleeping = false;
    514                
     514
    515515                irq_spinlock_lock(&thread->lock, false);
    516                
     516
    517517                thread->interrupted = true;
    518518                if (thread->state == Sleeping)
    519519                        sleeping = true;
    520                
     520
    521521                irq_spinlock_unlock(&thread->lock, false);
    522                
     522
    523523                if (sleeping)
    524524                        waitq_interrupt_sleep(thread);
    525525        }
    526        
     526
    527527        irq_spinlock_unlock(&threads_lock, false);
    528528        irq_spinlock_unlock(&task->lock, false);
     
    543543        if (id == 1)
    544544                return EPERM;
    545        
     545
    546546        irq_spinlock_lock(&tasks_lock, true);
    547        
     547
    548548        task_t *task = task_find_by_id(id);
    549549        if (!task) {
     
    551551                return ENOENT;
    552552        }
    553        
     553
    554554        task_kill_internal(task);
    555555        irq_spinlock_unlock(&tasks_lock, true);
    556        
     556
    557557        return EOK;
    558558}
     
    583583                }
    584584        }
    585        
     585
    586586        irq_spinlock_lock(&tasks_lock, true);
    587587        task_kill_internal(TASK);
    588588        irq_spinlock_unlock(&tasks_lock, true);
    589        
     589
    590590        thread_exit();
    591591}
     
    599599{
    600600        task_kill_self(notify);
    601        
     601
    602602        /* Unreachable */
    603603        return EOK;
     
    609609        task_t *task = avltree_get_instance(node, task_t, tasks_tree_node);
    610610        irq_spinlock_lock(&task->lock, false);
    611        
     611
    612612        uint64_t ucycles;
    613613        uint64_t kcycles;
     
    616616        order_suffix(ucycles, &ucycles, &usuffix);
    617617        order_suffix(kcycles, &kcycles, &ksuffix);
    618        
     618
    619619#ifdef __32_BITS__
    620620        if (*additional)
     
    627627                    ucycles, usuffix, kcycles, ksuffix);
    628628#endif
    629        
     629
    630630#ifdef __64_BITS__
    631631        if (*additional)
     
    637637                    task->taskid, task->name, task->container, task, task->as);
    638638#endif
    639        
     639
    640640        irq_spinlock_unlock(&task->lock, false);
    641641        return true;
     
    651651        /* Messing with task structures, avoid deadlock */
    652652        irq_spinlock_lock(&tasks_lock, true);
    653        
     653
    654654#ifdef __32_BITS__
    655655        if (additional)
     
    659659                    " [ucycles ] [kcycles ]\n");
    660660#endif
    661        
     661
    662662#ifdef __64_BITS__
    663663        if (additional)
     
    668668                    " [as              ]\n");
    669669#endif
    670        
     670
    671671        avltree_walk(&tasks_tree, task_print_walker, &additional);
    672        
     672
    673673        irq_spinlock_unlock(&tasks_lock, true);
    674674}
  • kernel/generic/src/proc/thread.c

    r3061bc1 r8565a42  
    122122        void *arg = THREAD->thread_arg;
    123123        THREAD->last_cycle = get_cycle();
    124        
     124
    125125        /* This is where each thread wakes up after its creation */
    126126        irq_spinlock_unlock(&THREAD->lock, false);
    127127        interrupts_enable();
    128        
     128
    129129        f(arg);
    130        
     130
    131131        /* Accumulate accounting to the task */
    132132        irq_spinlock_lock(&THREAD->lock, true);
     
    137137                uint64_t kcycles = THREAD->kcycles;
    138138                THREAD->kcycles = 0;
    139                
     139
    140140                irq_spinlock_pass(&THREAD->lock, &TASK->lock);
    141141                TASK->ucycles += ucycles;
     
    144144        } else
    145145                irq_spinlock_unlock(&THREAD->lock, true);
    146        
     146
    147147        thread_exit();
    148        
     148
    149149        /* Not reached */
    150150}
     
    156156{
    157157        thread_t *thread = (thread_t *) obj;
    158        
     158
    159159        irq_spinlock_initialize(&thread->lock, "thread_t_lock");
    160160        link_initialize(&thread->rq_link);
    161161        link_initialize(&thread->wq_link);
    162162        link_initialize(&thread->th_link);
    163        
     163
    164164        /* call the architecture-specific part of the constructor */
    165165        thr_constructor_arch(thread);
    166        
     166
    167167#ifdef CONFIG_FPU
    168168#ifdef CONFIG_FPU_LAZY
     
    174174#endif /* CONFIG_FPU_LAZY */
    175175#endif /* CONFIG_FPU */
    176        
     176
    177177        /*
    178178         * Allocate the kernel stack from the low-memory to prevent an infinite
     
    193193        kmflags |= FRAME_LOWMEM;
    194194        kmflags &= ~FRAME_HIGHMEM;
    195        
     195
    196196        uintptr_t stack_phys =
    197197            frame_alloc(STACK_FRAMES, kmflags, STACK_SIZE - 1);
     
    203203                return ENOMEM;
    204204        }
    205        
     205
    206206        thread->kstack = (uint8_t *) PA2KA(stack_phys);
    207        
     207
    208208#ifdef CONFIG_UDEBUG
    209209        mutex_initialize(&thread->udebug.lock, MUTEX_PASSIVE);
    210210#endif
    211        
     211
    212212        return EOK;
    213213}
     
    217217{
    218218        thread_t *thread = (thread_t *) obj;
    219        
     219
    220220        /* call the architecture-specific part of the destructor */
    221221        thr_destructor_arch(thread);
    222        
     222
    223223        frame_free(KA2PA(thread->kstack), STACK_FRAMES);
    224        
     224
    225225#ifdef CONFIG_FPU
    226226        if (thread->saved_fpu_context)
    227227                slab_free(fpu_context_cache, thread->saved_fpu_context);
    228228#endif
    229        
     229
    230230        return STACK_FRAMES;  /* number of frames freed */
    231231}
     
    239239{
    240240        THREAD = NULL;
    241        
     241
    242242        atomic_set(&nrdy, 0);
    243243        thread_cache = slab_cache_create("thread_t", sizeof(thread_t), 0,
    244244            thr_constructor, thr_destructor, 0);
    245        
     245
    246246#ifdef CONFIG_FPU
    247247        fpu_context_cache = slab_cache_create("fpu_context_t",
    248248            sizeof(fpu_context_t), FPU_CONTEXT_ALIGN, NULL, NULL, 0);
    249249#endif
    250        
     250
    251251        avltree_create(&threads_tree);
    252252}
     
    282282{
    283283        irq_spinlock_lock(&thread->lock, true);
    284        
     284
    285285        assert(thread->state != Ready);
    286286
    287287        before_thread_is_ready(thread);
    288        
     288
    289289        int i = (thread->priority < RQ_COUNT - 1) ?
    290290            ++thread->priority : thread->priority;
     
    305305                cpu = CPU;
    306306        }
    307        
     307
    308308        thread->state = Ready;
    309        
     309
    310310        irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
    311        
     311
    312312        /*
    313313         * Append thread to respective ready queue
    314314         * on respective processor.
    315315         */
    316        
     316
    317317        list_append(&thread->rq_link, &cpu->rq[i].rq);
    318318        cpu->rq[i].n++;
    319319        irq_spinlock_unlock(&(cpu->rq[i].lock), true);
    320        
     320
    321321        atomic_inc(&nrdy);
    322322        atomic_inc(&cpu->nrdy);
     
    344344        if (!thread)
    345345                return NULL;
    346        
     346
    347347        /* Not needed, but good for debugging */
    348348        memsetb(thread->kstack, STACK_SIZE, 0);
    349        
     349
    350350        irq_spinlock_lock(&tidlock, true);
    351351        thread->tid = ++last_tid;
    352352        irq_spinlock_unlock(&tidlock, true);
    353        
     353
    354354        context_save(&thread->saved_context);
    355355        context_set(&thread->saved_context, FADDR(cushion),
    356356            (uintptr_t) thread->kstack, STACK_SIZE);
    357        
     357
    358358        the_initialize((the_t *) thread->kstack);
    359        
     359
    360360        ipl_t ipl = interrupts_disable();
    361361        thread->saved_context.ipl = interrupts_read();
    362362        interrupts_restore(ipl);
    363        
     363
    364364        str_cpy(thread->name, THREAD_NAME_BUFLEN, name);
    365        
     365
    366366        thread->thread_code = func;
    367367        thread->thread_arg = arg;
     
    377377        thread->uspace =
    378378            ((flags & THREAD_FLAG_USPACE) == THREAD_FLAG_USPACE);
    379        
     379
    380380        thread->nomigrate = 0;
    381381        thread->state = Entering;
    382        
     382
    383383        timeout_initialize(&thread->sleep_timeout);
    384384        thread->sleep_interruptible = false;
    385385        thread->sleep_queue = NULL;
    386386        thread->timeout_pending = false;
    387        
     387
    388388        thread->in_copy_from_uspace = false;
    389389        thread->in_copy_to_uspace = false;
    390        
     390
    391391        thread->interrupted = false;
    392392        thread->detached = false;
    393393        waitq_initialize(&thread->join_wq);
    394        
     394
    395395        thread->task = task;
    396        
     396
    397397        thread->workq = NULL;
    398        
     398
    399399        thread->fpu_context_exists = false;
    400400        thread->fpu_context_engaged = false;
    401        
     401
    402402        avltree_node_initialize(&thread->threads_tree_node);
    403403        thread->threads_tree_node.key = (uintptr_t) thread;
    404        
     404
    405405#ifdef CONFIG_UDEBUG
    406406        /* Initialize debugging stuff */
     
    408408        udebug_thread_initialize(&thread->udebug);
    409409#endif
    410        
     410
    411411        /* Might depend on previous initialization */
    412412        thread_create_arch(thread);
    413        
     413
    414414        rcu_thread_init(thread);
    415        
     415
    416416        if ((flags & THREAD_FLAG_NOATTACH) != THREAD_FLAG_NOATTACH)
    417417                thread_attach(thread, task);
    418        
     418
    419419        return thread;
    420420}
     
    435435        assert(thread->task);
    436436        assert(thread->cpu);
    437        
     437
    438438        irq_spinlock_lock(&thread->cpu->lock, false);
    439439        if (thread->cpu->fpu_owner == thread)
    440440                thread->cpu->fpu_owner = NULL;
    441441        irq_spinlock_unlock(&thread->cpu->lock, false);
    442        
     442
    443443        irq_spinlock_pass(&thread->lock, &threads_lock);
    444        
     444
    445445        avltree_delete(&threads_tree, &thread->threads_tree_node);
    446        
     446
    447447        irq_spinlock_pass(&threads_lock, &thread->task->lock);
    448        
     448
    449449        /*
    450450         * Detach from the containing task.
     
    452452        list_remove(&thread->th_link);
    453453        irq_spinlock_unlock(&thread->task->lock, irq_res);
    454        
     454
    455455        /*
    456456         * Drop the reference to the containing task.
     
    475475         */
    476476        irq_spinlock_lock(&task->lock, true);
    477        
     477
    478478        /* Hold a reference to the task. */
    479479        task_hold(task);
    480        
     480
    481481        /* Must not count kbox thread into lifecount */
    482482        if (thread->uspace)
    483483                atomic_inc(&task->lifecount);
    484        
     484
    485485        list_append(&thread->th_link, &task->threads);
    486        
     486
    487487        irq_spinlock_pass(&task->lock, &threads_lock);
    488        
     488
    489489        /*
    490490         * Register this thread in the system-wide list.
     
    506506                /* Generate udebug THREAD_E event */
    507507                udebug_thread_e_event();
    508                
     508
    509509                /*
    510510                 * This thread will not execute any code or system calls from
     
    527527                }
    528528        }
    529        
     529
    530530restart:
    531531        irq_spinlock_lock(&THREAD->lock, true);
     
    535535                goto restart;
    536536        }
    537        
     537
    538538        THREAD->state = Exiting;
    539539        irq_spinlock_unlock(&THREAD->lock, true);
    540        
     540
    541541        scheduler();
    542        
     542
    543543        /* Not reached */
    544544        while (true);
     
    562562{
    563563        assert(thread != NULL);
    564        
     564
    565565        irq_spinlock_lock(&thread->lock, true);
    566        
     566
    567567        thread->interrupted = true;
    568568        bool sleeping = (thread->state == Sleeping);
    569        
     569
    570570        irq_spinlock_unlock(&thread->lock, true);
    571        
     571
    572572        if (sleeping)
    573573                waitq_interrupt_sleep(thread);
     
    583583{
    584584        assert(thread != NULL);
    585        
     585
    586586        bool interrupted;
    587        
     587
    588588        irq_spinlock_lock(&thread->lock, true);
    589589        interrupted = thread->interrupted;
    590590        irq_spinlock_unlock(&thread->lock, true);
    591        
     591
    592592        return interrupted;
    593593}
     
    597597{
    598598        assert(THREAD);
    599        
     599
    600600        THREAD->nomigrate++;
    601601}
     
    606606        assert(THREAD);
    607607        assert(THREAD->nomigrate > 0);
    608        
     608
    609609        if (THREAD->nomigrate > 0)
    610610                THREAD->nomigrate--;
     
    624624        while (sec > 0) {
    625625                uint32_t period = (sec > 1000) ? 1000 : sec;
    626                
     626
    627627                thread_usleep(period * 1000000);
    628628                sec -= period;
     
    643643        if (thread == THREAD)
    644644                return EINVAL;
    645        
     645
    646646        /*
    647647         * Since thread join can only be called once on an undetached thread,
    648648         * the thread pointer is guaranteed to be still valid.
    649649         */
    650        
     650
    651651        irq_spinlock_lock(&thread->lock, true);
    652652        assert(!thread->detached);
    653653        irq_spinlock_unlock(&thread->lock, true);
    654        
     654
    655655        return waitq_sleep_timeout(&thread->join_wq, usec, flags, NULL);
    656656}
     
    672672        irq_spinlock_lock(&thread->lock, true);
    673673        assert(!thread->detached);
    674        
     674
    675675        if (thread->state == Lingering) {
    676676                /*
     
    683683                thread->detached = true;
    684684        }
    685        
     685
    686686        irq_spinlock_unlock(&thread->lock, true);
    687687}
     
    697697{
    698698        waitq_t wq;
    699        
     699
    700700        waitq_initialize(&wq);
    701        
     701
    702702        (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING, NULL);
    703703}
     
    707707        bool *additional = (bool *) arg;
    708708        thread_t *thread = avltree_get_instance(node, thread_t, threads_tree_node);
    709        
     709
    710710        uint64_t ucycles, kcycles;
    711711        char usuffix, ksuffix;
    712712        order_suffix(thread->ucycles, &ucycles, &usuffix);
    713713        order_suffix(thread->kcycles, &kcycles, &ksuffix);
    714        
     714
    715715        char *name;
    716716        if (str_cmp(thread->name, "uinit") == 0)
     
    718718        else
    719719                name = thread->name;
    720        
     720
    721721#ifdef __32_BITS__
    722722        if (*additional)
     
    729729                    thread->task, thread->task->container);
    730730#endif
    731        
     731
    732732#ifdef __64_BITS__
    733733        if (*additional)
     
    741741                    thread->task, thread->task->container);
    742742#endif
    743        
     743
    744744        if (*additional) {
    745745                if (thread->cpu)
     
    747747                else
    748748                        printf("none ");
    749                
     749
    750750                if (thread->state == Sleeping) {
    751751#ifdef __32_BITS__
    752752                        printf(" %10p", thread->sleep_queue);
    753753#endif
    754                        
     754
    755755#ifdef __64_BITS__
    756756                        printf(" %18p", thread->sleep_queue);
    757757#endif
    758758                }
    759                
     759
    760760                printf("\n");
    761761        }
    762        
     762
    763763        return true;
    764764}
     
    773773        /* Messing with thread structures, avoid deadlock */
    774774        irq_spinlock_lock(&threads_lock, true);
    775        
     775
    776776#ifdef __32_BITS__
    777777        if (additional)
     
    782782                    " [ctn]\n");
    783783#endif
    784        
     784
    785785#ifdef __64_BITS__
    786786        if (additional) {
     
    791791                    " [task            ] [ctn]\n");
    792792#endif
    793        
     793
    794794        avltree_walk(&threads_tree, thread_walker, &additional);
    795        
     795
    796796        irq_spinlock_unlock(&threads_lock, true);
    797797}
     
    814814        avltree_node_t *node =
    815815            avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) thread));
    816        
     816
    817817        return node != NULL;
    818818}
     
    832832        assert(interrupts_disabled());
    833833        assert(irq_spinlock_locked(&THREAD->lock));
    834        
     834
    835835        if (user)
    836836                THREAD->ucycles += time - THREAD->last_cycle;
    837837        else
    838838                THREAD->kcycles += time - THREAD->last_cycle;
    839        
     839
    840840        THREAD->last_cycle = time;
    841841}
     
    846846            (thread_t *) avltree_get_instance(node, thread_t, threads_tree_node);
    847847        thread_iterator_t *iterator = (thread_iterator_t *) arg;
    848        
     848
    849849        if (thread->tid == iterator->thread_id) {
    850850                iterator->thread = thread;
    851851                return false;
    852852        }
    853        
     853
    854854        return true;
    855855}
     
    869869        assert(interrupts_disabled());
    870870        assert(irq_spinlock_locked(&threads_lock));
    871        
     871
    872872        thread_iterator_t iterator;
    873        
     873
    874874        iterator.thread_id = thread_id;
    875875        iterator.thread = NULL;
    876        
     876
    877877        avltree_walk(&threads_tree, thread_search_walker, (void *) &iterator);
    878        
     878
    879879        return iterator.thread;
    880880}
     
    885885{
    886886        irq_spinlock_lock(&threads_lock, true);
    887        
     887
    888888        thread_t *thread = thread_find_by_id(thread_id);
    889889        if (thread == NULL) {
     
    892892                return;
    893893        }
    894        
     894
    895895        irq_spinlock_lock(&thread->lock, false);
    896        
     896
    897897        /*
    898898         * Schedule a stack trace to be printed
     
    906906         * is probably justifiable.
    907907         */
    908        
     908
    909909        bool sleeping = false;
    910910        istate_t *istate = thread->udebug.uspace_state;
     
    916916        } else
    917917                printf("Thread interrupt state not available.\n");
    918        
     918
    919919        irq_spinlock_unlock(&thread->lock, false);
    920        
     920
    921921        if (sleeping)
    922922                waitq_interrupt_sleep(thread);
    923        
     923
    924924        irq_spinlock_unlock(&threads_lock, true);
    925925}
     
    935935        if (name_len > THREAD_NAME_BUFLEN - 1)
    936936                name_len = THREAD_NAME_BUFLEN - 1;
    937        
     937
    938938        char namebuf[THREAD_NAME_BUFLEN];
    939939        errno_t rc = copy_from_uspace(namebuf, uspace_name, name_len);
    940940        if (rc != EOK)
    941941                return (sys_errno_t) rc;
    942        
     942
    943943        namebuf[name_len] = 0;
    944        
     944
    945945        /*
    946946         * In case of failure, kernel_uarg will be deallocated in this function.
     
    949949        uspace_arg_t *kernel_uarg =
    950950            (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
    951        
     951
    952952        rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t));
    953953        if (rc != EOK) {
     
    955955                return (sys_errno_t) rc;
    956956        }
    957        
     957
    958958        thread_t *thread = thread_create(uinit, kernel_uarg, TASK,
    959959            THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf);
     
    968968                                 * creation now.
    969969                                 */
    970                                
     970
    971971                                /*
    972972                                 * The new thread structure is initialized, but
     
    976976                                slab_free(thread_cache, thread);
    977977                                free(kernel_uarg);
    978                                
     978
    979979                                return (sys_errno_t) rc;
    980980                         }
    981981                }
    982                
     982
    983983#ifdef CONFIG_UDEBUG
    984984                /*
     
    994994#endif
    995995                thread_ready(thread);
    996                
     996
    997997                return 0;
    998998        } else
    999999                free(kernel_uarg);
    1000        
     1000
    10011001        return (sys_errno_t) ENOMEM;
    10021002}
Note: See TracChangeset for help on using the changeset viewer.