ppos

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

commit 484158d3fc2945c935294281d55fb01e0037e84c
parent ac2702225689f62e4e6767bf4ac07e848aa00d4b
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Sun, 20 Sep 2026 01:41:53 -0300

Semáforos.

Diffstat:
Mkernel/semaphore.c | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 109 insertions(+), 0 deletions(-)

diff --git a/kernel/semaphore.c b/kernel/semaphore.c @@ -4,11 +4,120 @@ // Semáforos e spinlocks +#include "dispatcher.h" +#include "tcb.h" + +#include <map.h> +#include <queue.h> +#include <stdlib.h> + +extern struct task_t* current; + +struct map_t* semaphores; + +struct semaphore_t { + int lock; + int value; + struct queue_t* awaiting; +}; + +void spin_lock(volatile int* lock) +{ + while (__sync_fetch_and_or(lock, 1)) + ; +} + +void spin_unlock(volatile int* lock) +{ + *lock = 0; +} + void sem_init() { + semaphores = map_create(1024); } void sem_term() { + map_destroy(semaphores); +} + +int sem_create(int value) +{ + struct semaphore_t* semaphore = calloc(1, sizeof(*semaphore)); + if (semaphore == NULL) + return -1; + + semaphore->awaiting = queue_create(); + if (semaphore->awaiting == NULL) { + free(semaphore); + return -1; + } + + semaphore->value = value; + + int id = map_put(semaphores, semaphore); + if (id == -1) + return -1; + return id; +} + +int sem_destroy(int id) +{ + struct semaphore_t* semaphore = map_get(semaphores, id); + if (semaphore == NULL) + return -1; + + queue_destroy(semaphore->awaiting); + free(semaphore); + map_del(semaphores, id); + return 0; +} + +int sem_down(int id) +{ + volatile struct semaphore_t* semaphore = + (volatile struct semaphore_t*) map_get(semaphores, id); + if (semaphore == NULL) + return -1; + + spin_lock(&semaphore->lock); + + int await = 0; + semaphore->value -= 1; + if (semaphore->value < 0) + await = 1; + + spin_unlock(&semaphore->lock); + + if (await) { + current->status = TaskStatusWaiting; + task_suspend(semaphore->awaiting); + } + + if (map_get(semaphores, id) == NULL) + return -1; + + return 0; } +int sem_up(int id) +{ + volatile struct semaphore_t* semaphore = + (volatile struct semaphore_t*) map_get(semaphores, id); + if (semaphore == NULL) + return -1; + + spin_lock(&semaphore->lock); + + semaphore->value += 1; + if (semaphore->value <= 0) { + struct task_t* next = queue_head(semaphore->awaiting); + if (next != NULL) + task_awake(next); + } + + spin_unlock(&semaphore->lock); + + return 0; +}