mirror of
https://github.com/Hizenberg469/MultiThreading_Part_A.git
synced 2026-04-20 02:22:23 +03:00
Semaphore Implementation
This commit is contained in:
70
Semaphore_Implementation/Semaphore_Implementation.c
Normal file
70
Semaphore_Implementation/Semaphore_Implementation.c
Normal file
@@ -0,0 +1,70 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <stdbool.h>
|
||||
#include "Semaphore_Implementation.h"
|
||||
|
||||
sema_t*
|
||||
sema_get_new_semaphore() {
|
||||
|
||||
sema_t* sema = calloc(1, sizeof(sema_t));
|
||||
return sema;
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
sema_init(sema_t* sema, int permit_counter) {
|
||||
|
||||
sema->permit_counter = permit_counter;
|
||||
pthread_cond_init(&sema->cv, NULL);
|
||||
pthread_mutex_init(&sema->mutex, NULL);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
Without using Pending Signals
|
||||
*/
|
||||
|
||||
void
|
||||
sema_wait(sema_t* sema) {
|
||||
|
||||
pthread_mutex_lock(&sema->mutex);
|
||||
sema->permit_counter--;
|
||||
if (sema->permit_counter < 0) {
|
||||
pthread_cond_wait(&sema->cv, &sema->mutex);
|
||||
}
|
||||
pthread_mutex_unlock(&sema->mutex);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
sema_post(sema_t* sema) {
|
||||
|
||||
bool any_thread_waiting;
|
||||
pthread_mutex_lock(&sema->mutex);
|
||||
|
||||
any_thread_waiting = sema->permit_counter < 0 ? true : false;
|
||||
sema->permit_counter++;
|
||||
|
||||
if (any_thread_waiting) {
|
||||
pthread_cond_signal(&sema->cv);
|
||||
}
|
||||
pthread_mutex_unlock(&sema->mutex);
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
sema_destroy(sema_t* sema) {
|
||||
|
||||
sema->permit_counter = 0;
|
||||
pthread_mutex_unlock(&sema->mutex);
|
||||
pthread_cond_destroy(&sema->cv);
|
||||
pthread_mutex_destroy(&sema->mutex);
|
||||
|
||||
}
|
||||
|
||||
int
|
||||
sema_getvalue(sema_t* sema) {
|
||||
|
||||
return sema->permit_counter;
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user