added primitive semaphore

This commit is contained in:
2022-08-30 16:26:29 +02:00
parent f5d433f177
commit 4c67d0bba4
2 changed files with 35 additions and 0 deletions

23
semaphore.c Normal file
View File

@@ -0,0 +1,23 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
#include "semaphore.h"
typedef struct {
int value;
} sem_t;
int sem_init(sem_t* sem, int value) {
sem->value = value;
}
int sem_wait(sem_t* sem) {
while(sem->value > 0);
sem->value--;
}
int sem_post(sem_t* sem) {
sem->value++;
}

12
semaphore.h Normal file
View File

@@ -0,0 +1,12 @@
/*
* Copyright 2022 Oleg Borodin <borodin@unix7.org>
*/
typedef struct {
int value;
} sem_t;
int sem_init(sem_t* sem, int value);
int sem_wait(sem_t* sem);
int sem_post(sem_t* sem);