Changeset 286da52 in mainline


Ignore:
Timestamp:
2024-01-20T15:56:45Z (3 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
efed95a3
Parents:
6a0e568
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-03-27 17:37:59)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2024-01-20 15:56:45)
Message:

Streamline requeuing threads

Split thread_ready() into different functions for different circumstances,
since they can be simplified after.

Location:
kernel/generic
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/include/proc/thread.h

    r6a0e568 r286da52  
    186186extern void thread_attach(thread_t *, task_t *);
    187187extern void thread_start(thread_t *);
    188 extern void thread_ready(thread_t *);
     188extern void thread_requeue_sleeping(thread_t *);
    189189extern void thread_exit(void) __attribute__((noreturn));
    190190extern void thread_interrupt(thread_t *);
  • kernel/generic/src/proc/scheduler.c

    r6a0e568 r286da52  
    311311
    312312        irq_spinlock_lock(&THREAD->lock, false);
     313        assert(THREAD->cpu == CPU);
     314
    313315        THREAD->state = Running;
    314         THREAD->cpu = CPU;
    315316        THREAD->priority = rq_index;  /* Correct rq index */
    316317
     
    365366}
    366367
     368static void add_to_rq(thread_t *thread, cpu_t *cpu, int i)
     369{
     370        /* Add to the appropriate runqueue. */
     371        runq_t *rq = &cpu->rq[i];
     372
     373        irq_spinlock_lock(&rq->lock, false);
     374        list_append(&thread->rq_link, &rq->rq);
     375        rq->n++;
     376        irq_spinlock_unlock(&rq->lock, false);
     377
     378        atomic_inc(&nrdy);
     379        atomic_inc(&cpu->nrdy);
     380}
     381
     382/** Requeue a thread that was just preempted on this CPU.
     383 */
     384static void thread_requeue_preempted(thread_t *thread)
     385{
     386        irq_spinlock_lock(&thread->lock, false);
     387
     388        assert(thread->state == Running);
     389        assert(thread->cpu == CPU);
     390
     391        int i = (thread->priority < RQ_COUNT - 1) ?
     392            ++thread->priority : thread->priority;
     393
     394        thread->state = Ready;
     395
     396        irq_spinlock_unlock(&thread->lock, false);
     397
     398        add_to_rq(thread, CPU, i);
     399}
     400
     401void thread_requeue_sleeping(thread_t *thread)
     402{
     403        ipl_t ipl = interrupts_disable();
     404
     405        irq_spinlock_lock(&thread->lock, false);
     406
     407        assert(thread->state == Sleeping || thread->state == Entering);
     408
     409        thread->priority = 0;
     410        thread->state = Ready;
     411
     412        /* Prefer the CPU on which the thread ran last */
     413        if (!thread->cpu)
     414                thread->cpu = CPU;
     415
     416        cpu_t *cpu = thread->cpu;
     417
     418        irq_spinlock_unlock(&thread->lock, false);
     419
     420        add_to_rq(thread, cpu, 0);
     421
     422        interrupts_restore(ipl);
     423}
     424
    367425static void cleanup_after_thread(thread_t *thread, state_t out_state)
    368426{
     
    374432        switch (out_state) {
    375433        case Running:
    376                 thread_ready(thread);
     434                thread_requeue_preempted(thread);
    377435                break;
    378436
     
    398456                        assert(expected == SLEEP_WOKE);
    399457                        /* The thread has already been woken up, requeue immediately. */
    400                         thread_ready(thread);
     458                        thread_requeue_sleeping(thread);
    401459                }
    402460                break;
     
    447505         */
    448506        after_thread_ran_arch();
    449 
    450         if (new_state == Sleeping) {
    451                 /* Prefer the thread after it's woken up. */
    452                 THREAD->priority = -1;
    453         }
    454507
    455508        irq_spinlock_unlock(&THREAD->lock, false);
  • kernel/generic/src/proc/thread.c

    r6a0e568 r286da52  
    210210{
    211211        assert(thread->state == Entering);
    212         thread_ready(thread_ref(thread));
    213 }
    214 
    215 /** Make thread ready
    216  *
    217  * Switch thread to the ready state. Consumes reference passed by the caller.
    218  *
    219  * @param thread Thread to make ready.
    220  *
    221  */
    222 void thread_ready(thread_t *thread)
    223 {
    224         // TODO: move this to scheduler.c
    225         irq_spinlock_lock(&thread->lock, true);
    226 
    227         assert(thread->state != Ready);
    228 
    229         int i = (thread->priority < RQ_COUNT - 1) ?
    230             ++thread->priority : thread->priority;
    231 
    232         /* Prefer the CPU on which the thread ran last */
    233         cpu_t *cpu = thread->cpu ? thread->cpu : CPU;
    234 
    235         thread->state = Ready;
    236 
    237         irq_spinlock_pass(&thread->lock, &(cpu->rq[i].lock));
    238 
    239         /*
    240          * Append thread to respective ready queue
    241          * on respective processor.
    242          */
    243 
    244         list_append(&thread->rq_link, &cpu->rq[i].rq);
    245         cpu->rq[i].n++;
    246         irq_spinlock_unlock(&(cpu->rq[i].lock), true);
    247 
    248         atomic_inc(&nrdy);
    249         atomic_inc(&cpu->nrdy);
     212        thread_requeue_sleeping(thread_ref(thread));
    250213}
    251214
     
    613576                 * the waking thread by the sleeper in thread_wait_finish().
    614577                 */
    615                 thread_ready(thread);
     578                thread_requeue_sleeping(thread);
    616579        }
    617580}
     
    10641027                thread_attach(thread, TASK);
    10651028#endif
    1066                 thread_ready(thread);
     1029                thread_start(thread);
     1030                thread_put(thread);
    10671031
    10681032                return 0;
Note: See TracChangeset for help on using the changeset viewer.