Skip to content
Snippets Groups Projects
Commit 5acd350c authored by Thierno souleymane Bah's avatar Thierno souleymane Bah
Browse files

feat(semanphore implementation done)

parent d1532c84
No related branches found
No related tags found
No related merge requests found
#if !defined(SEM_H)
#define SEM_H
#include "context.h"
#define SEM_MAGIC 0xCAFEBABE
typedef struct sem_s
{
int cpt;
ctx_t *first_ctx;
int magic;
} sem_t;
int sem_init(sem_t *sem, int val);
void sem_up(sem_t *sem);
void sem_down(sem_t *sem);
#endif
#include "sem.h"
#include "context.h"
#include "utils.h"
extern ctx_t *current_ctx;
int sem_init(sem_t *sem, int val)
{
assert(sem->magic != SEM_MAGIC);
sem->magic = SEM_MAGIC;
sem->cpt = val;
sem->first_ctx = NULL;
return 0;
}
void sem_up(sem_t *sem)
{
assert(sem->magic == SEM_MAGIC);
irq_disable();
if ((++sem->cpt) <= 0)
{
sem->first_ctx->status = CTX_EXEC;
sem->first_ctx = sem->first_ctx->sem_next_ctx;
}
irq_enable();
}
void sem_down(sem_t *sem)
{
assert(sem->magic == SEM_MAGIC);
irq_disable();
if ((++sem->cpt) < 0)
{
current_ctx->status = CTX_WAIT;
current_ctx->sem_next_ctx = sem->first_ctx;
sem->first_ctx = current_ctx;
yield();
}
irq_enable();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment