ppos

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit e59f162dd22da9d3fcef9c7171f7d6d8e46a5ca1
parent 6a8cf3fb4b35741902eb747bd2d43d5c52a9b3fe
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Mon, 24 Aug 2026 09:49:15 -0300

Dispatcher.

Diffstat:
M.gitignore | 1+
Mkernel/dispatcher.c | 71++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Mkernel/scheduler.c | 8++++++++
Mkernel/task.c | 15++++++++-------
Mkernel/tcb.h | 15+++++++++------
Mlib/queue.c | 19++++++++++++++-----
Mtad-vazio/queue.c | 19++++++++++++++-----
7 files changed, 122 insertions(+), 26 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -2,3 +2,4 @@ /pingpong-task1 /pingpong-task2 /pingpong-task3 +/pingpong-dispatcher diff --git a/kernel/dispatcher.c b/kernel/dispatcher.c @@ -6,21 +6,86 @@ #include "dispatcher.h" +#include "scheduler.h" #include "task.h" +#include <assert.h> +#include <queue.h> +#include <stdio.h> + +struct queue_t* ready; + +extern struct task_t* current; +extern struct task_t kernel_task; + void dispatcher_init() { + ready = queue_create(); } void dispatcher_term() { + queue_destroy(ready); } void user_main(void*); void dispatcher() { - struct task_t* task_user = task_create("user_main", user_main, NULL); - task_switch(task_user); - task_destroy(task_user); + task_create("user_main", user_main, NULL); + + while (queue_size(ready) > 0) { + struct task_t* task = scheduler(ready); + + if (task != NULL) + task_run(task); + + if (task->status == TaskStatusFinished) { + task_destroy(task); + } + } +} + +void task_run(struct task_t* task) +{ + assert(queue_del(ready, task) == NOERROR); + + task->status = TaskStatusRunning; + task_switch(task); +} + +void task_yield() +{ + current->status = TaskStatusReady; + queue_add(ready, current); + task_switch(&kernel_task); +} + +void task_suspend(struct queue_t* queue) +{ + current->status = TaskStatusWaiting; + + if (queue != NULL) { + current->waiting_queue = queue; + queue_add(queue, current); + } + + task_switch(&kernel_task); +} + +void task_awake(struct task_t* task) +{ + if (task->waiting_queue != NULL) { + queue_del(task->waiting_queue, task); + task->waiting_queue = NULL; + } + + task->status = TaskStatusReady; + queue_add(ready, task); +} + +void task_exit(int exit_code) +{ + current->status = TaskStatusFinished; + task_switch(&kernel_task); } diff --git a/kernel/scheduler.c b/kernel/scheduler.c @@ -4,6 +4,10 @@ // Escalonador de tarefas prontas. +#include "tcb.h" + +#include <queue.h> + void sched_init() { } @@ -12,3 +16,7 @@ void sched_term() { } +struct task_t* scheduler(struct queue_t* ready) +{ + return queue_head(ready); +} diff --git a/kernel/task.c b/kernel/task.c @@ -8,12 +8,14 @@ #include <assert.h> #include <queue.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <valgrind/valgrind.h> -struct task_t kernel_task; -struct task_t* current; +struct task_t kernel_task; +struct task_t* current; +extern struct queue_t* ready; int uid; @@ -61,6 +63,8 @@ struct task_t* task_create(char* name, void (*entry)(void*), void* arg) task->name = name; task->status = TaskStatusReady; + queue_add(ready, task); + return task; } @@ -68,9 +72,8 @@ int task_destroy(struct task_t* task) { if (task == NULL) return ERROR; - // TODO. - // if (task->status != TaskStatusFinished) - // return ERROR; + if (task->status != TaskStatusFinished) + return ERROR; if (task->context.stack != NULL) free(task->context.stack); @@ -107,8 +110,6 @@ int task_switch(struct task_t* task) struct task_t* c = current; current = task; - c->status = TaskStatusWaiting; - task->status = TaskStatusRunning; ctx_switch(&c->context, &task->context); return NOERROR; diff --git a/kernel/tcb.h b/kernel/tcb.h @@ -11,6 +11,8 @@ #include "ctx.h" +#include <queue.h> + #define PPOS_STACK_SIZE (4096 * 16) typedef enum { @@ -22,12 +24,13 @@ typedef enum { // Task Control Block (TCB), infos sobre uma tarefa struct task_t { - struct ctx_t context; // contexto da tarefa - char* name; // nome da tarefa - struct task_t* parent; - int id; // identificador da tarefa - int valgrind_id; - TaskStatus status; + struct ctx_t context; // contexto da tarefa + char* name; // nome da tarefa + struct task_t* parent; + struct queue_t* waiting_queue; + int id; // identificador da tarefa + int valgrind_id; + TaskStatus status; }; #define PPOS_CONCURRENT_TASKS (1024) diff --git a/lib/queue.c b/lib/queue.c @@ -69,18 +69,27 @@ int queue_del(struct queue_t* queue, void* item) { if (queue == NULL) return ERROR; - node_t** prev = &queue->head; - node_t* node = queue->head; + // Works because of the layout of the structs. + struct node_t* parent = (struct node_t*) queue; + struct node_t* node = queue->head; while (node != NULL) { if (node->item == item) { - *prev = node->next; + parent->next = node->next; if (queue->it == node) queue->it = node->next; + if (queue->tail == node) { + if (parent == (struct node_t*) queue) { + queue->head = NULL; + queue->tail = NULL; + } else { + queue->tail = parent; + } + } free(node); return NOERROR; } - prev = &node->next; - node = node->next; + parent = node; + node = node->next; } return ERROR; diff --git a/tad-vazio/queue.c b/tad-vazio/queue.c @@ -69,18 +69,27 @@ int queue_del(struct queue_t* queue, void* item) { if (queue == NULL) return ERROR; - node_t** prev = &queue->head; - node_t* node = queue->head; + // Works because of the layout of the structs. + struct node_t* parent = (struct node_t*) queue; + struct node_t* node = queue->head; while (node != NULL) { if (node->item == item) { - *prev = node->next; + parent->next = node->next; if (queue->it == node) queue->it = node->next; + if (queue->tail == node) { + if (parent == (struct node_t*) queue) { + queue->head = NULL; + queue->tail = NULL; + } else { + queue->tail = parent; + } + } free(node); return NOERROR; } - prev = &node->next; - node = node->next; + parent = node; + node = node->next; } return ERROR;