ppos

Usermode educational operating system.
Log | Files | Refs

dispatcher.c (4677B)


      1 // PingPongOS - PingPong Operating System
      2 
      3 // Este arquivo PODE/DEVE ser alterado.
      4 
      5 // Dispatcher: gerencia os estados das tarefas.
      6 
      7 #include "dispatcher.h"
      8 #include "scheduler.h"
      9 #include "task.h"
     10 #include "time.h"
     11 
     12 #include <assert.h>
     13 #include <hardware/cpu.h>
     14 #include <pplibc.h>
     15 #include <queue.h>
     16 
     17 struct queue_t* ready;
     18 struct queue_t* sleeping;
     19 
     20 extern struct task_t* current;
     21 extern struct task_t  kernel_task;
     22 void                  lock_kernel();
     23 void                  unlock_kernel();
     24 
     25 extern int living_tasks;
     26 
     27 void dispatcher_init()
     28 {
     29     ready    = queue_create();
     30     sleeping = queue_create();
     31 }
     32 
     33 void dispatcher_term()
     34 {
     35     queue_destroy(ready);
     36     queue_destroy(sleeping);
     37 }
     38 
     39 void user_main(void*);
     40 
     41 void dispatcher()
     42 {
     43     kernel_task.wall_start     = time();
     44     kernel_task.wall_last_grab = kernel_task.wall_start;
     45     kernel_task.activations    = 1;
     46 
     47     struct task_t* user_task = task_create("user_main", user_main, NULL);
     48 
     49     for (;;) {
     50         // wake sleeping.
     51         int t = time();
     52         for (struct task_t* wt = queue_head(sleeping); wt != NULL;
     53              wt                = queue_next(sleeping)) {
     54             if (wt->wake_on <= t)
     55                 task_awake(wt);
     56         }
     57 
     58         struct task_t* task = scheduler(ready);
     59 
     60         if (task != NULL) {
     61             kernel_task.cpu_time += time() - kernel_task.wall_last_grab;
     62             task_run(task);
     63             // Ensure kernel is unlocked before going on: sleeping on a
     64             // semaphore can keep the kernel locked.
     65             unlock_kernel();
     66         } else {
     67             if (living_tasks <= 0)
     68                 break;
     69             current = NULL;
     70             hw_wfi();
     71             continue;
     72         }
     73 
     74         kernel_task.activations += 1;
     75         if (task->status == TaskStatusFinished) {
     76             task->wall_last_grab = time() - task->wall_start;
     77             printk(
     78                 "PPOS: task %d (%s) %d ms run, %d ms cpu, %d acts, exit "
     79                 "code %d\n",
     80                 task->id,
     81                 task->name,
     82                 task->wall_last_grab,
     83                 task->cpu_time,
     84                 task->activations,
     85                 task->exit_code
     86             );
     87             struct queue_t* q = task->waiting_on_queue;
     88             for (struct task_t* wt = queue_head(q); wt != NULL;
     89                  wt                = queue_next(q)) {
     90                 task_awake(wt);
     91             }
     92             living_tasks -= 1;
     93         }
     94     }
     95 
     96     task_destroy(user_task);
     97 
     98     kernel_task.cpu_time += time() - kernel_task.wall_last_grab;
     99     printk(
    100         "PPOS: task %d (%s) %d ms run, %d ms cpu, %d acts, exit "
    101         "code %d\n",
    102         kernel_task.id,
    103         kernel_task.name,
    104         time() - kernel_task.wall_start,
    105         kernel_task.cpu_time,
    106         kernel_task.activations,
    107         0
    108     );
    109 }
    110 
    111 void task_run(struct task_t* task)
    112 {
    113     assert(queue_del(ready, task) == NOERROR);
    114 
    115     task->status  = TaskStatusRunning;
    116     task->quantum = 10;
    117     task->activations += 1;
    118     task->wall_last_grab = time();
    119     if (task->wall_start == 0)
    120         task->wall_start = task->wall_last_grab;
    121     task_switch(task);
    122 }
    123 
    124 void task_yield()
    125 {
    126     current->status = TaskStatusReady;
    127     current->cpu_time += time() - current->wall_last_grab;
    128 
    129     lock_kernel();
    130     queue_add(ready, current);
    131     unlock_kernel();
    132 
    133     kernel_task.wall_last_grab = time();
    134 
    135     task_switch(&kernel_task);
    136 }
    137 
    138 void prepare_suspension(struct queue_t* queue)
    139 {
    140     current->cpu_time += time() - current->wall_last_grab;
    141     if (queue != NULL) {
    142         current->waiting_queue = queue;
    143         queue_add(queue, current);
    144     }
    145 
    146     kernel_task.wall_last_grab = time();
    147 }
    148 
    149 void task_suspend(struct queue_t* queue)
    150 {
    151     lock_kernel();
    152 
    153     prepare_suspension(queue);
    154 
    155     kernel_task.wall_last_grab = time();
    156     task_switch(&kernel_task);
    157 }
    158 
    159 void task_awake(struct task_t* task)
    160 {
    161     if (task->waiting_queue != NULL) {
    162         queue_del(task->waiting_queue, task);
    163         task->waiting_queue = NULL;
    164     }
    165 
    166     task->status = TaskStatusReady;
    167     queue_add(ready, task);
    168 }
    169 
    170 void task_exit(int exit_code)
    171 {
    172     current->status    = TaskStatusFinished;
    173     current->exit_code = exit_code;
    174 
    175     current->cpu_time += time() - current->wall_last_grab;
    176 
    177     kernel_task.wall_last_grab = time();
    178     task_switch(&kernel_task);
    179 }
    180 
    181 int task_wait(struct task_t* task)
    182 {
    183     if (task == NULL)
    184         return ERROR;
    185     if (task->status == TaskStatusFinished)
    186         return task->exit_code;
    187     current->status = TaskStatusWaiting;
    188     task_suspend(task->waiting_on_queue);
    189     return task->exit_code;
    190 }
    191 
    192 void task_sleep(int t)
    193 {
    194     current->wake_on = t + time();
    195     task_suspend(sleeping);
    196 }