This repository has been archived on 2021-08-17. You can view files and clone it, but cannot push or open issues or pull requests.
unix/fb_arc_set/shared/sem.c

112 lines
3.5 KiB
C
Raw Normal View History

#ifndef LIBRARIES
#define LIBRARIES
#define _GNU_SOURCE
#include <fcntl.h> ///< For O_* constants *
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#endif
#include <errno.h>
#include <semaphore.h> ///< For semaphores
#define FREE_SEM_NAME "/11777707_free"
#define USE_SEM_NAME "/11777707_used"
#define MUTEX_NAME "/11777707_mutex"
static sem_t *free_sem;
static sem_t *use_sem;
static sem_t *mutex;
/**
* @brief Function that executes sem_open(3) and does error handling.
* @details The function executes sem_open(3), giving it a semaphor name and size as parameters,
* and handles the errors if any. The same behaviour as sem_open(3) should be expected.
* Returns a new semaphore
* @param sem_name, sem_size
* @return res
**/
sem_t * open_sem(char *sem_name, size_t sem_size) {
sem_t *res = sem_open(sem_name, O_CREAT | O_EXCL, 0600, sem_size);
if(res == SEM_FAILED) {
fprintf(stderr, "ERROR: Failed opening semaphore\n");
exit(EXIT_FAILURE);
}
return res;
}
/**
* @brief Function that executes sem_getvalue(3) and does error handling.
* @details The function executes sem_getvalue(3), giving it a semaphor and size as parameters,
* and handles the errors if any. The same behaviour as sem_getvalue(3) should be expected.
* Writes the value of a semaphore to a chosen pointer
* @param sem, rem
* @return none
**/
void getval_sem(sem_t *sem, int *res) {
if(sem_getvalue(sem, res) == -1) {
fprintf(stderr, "ERROR: Failed getting semaphore value\n");
exit(EXIT_FAILURE);
}
}
/**
* @brief Function that executes sem_wait(3) and does error handling.
* @details The function executes sem_wait(3), giving it a semaphor as a parameter,
* and handles the errors if any. The same behaviour as sem_wait(3) should be expected.
* If the process was terminated, the function exits
* @param sem
* @return none
**/
void wait_sem(sem_t *sem) {
if(sem_wait(sem) == -1) {
if(errno == EINTR) {
return;
}
fprintf(stderr, "ERROR: Failed locking semaphore\n");
exit(EXIT_FAILURE);
}
}
/**
* @brief Function that executes sem_post(3) and does error handling.
* @details The function executes sem_post(3), giving it a semaphor as a parameter,
* and handles the errors if any. The same behaviour as sem_post(3) should be expected.
* @param sem
* @return none
**/
void post_sem(sem_t *sem) {
if(sem_post(sem) == -1) {
fprintf(stderr, "ERROR: Failed unlocking semaphore\n");
exit(EXIT_FAILURE);
}
}
/**
* @brief Function that executes sem_close(3) and does error handling.
* @details The function executes sem_close(3), giving it a semaphor as a parameter,
* and handles the errors if any. The same behaviour as sem_close(3) should be expected.
* @param sem
* @return none
**/
void close_sem(sem_t *sem) {
if(sem_close(sem) < 0) {
fprintf(stderr, "ERROR: Failed closing semaphore\n");
exit(EXIT_FAILURE);
}
}
/**
* @brief Function that executes sem_unlink(3) and does error handling.
* @details The function executes sem_unlink(3), giving it a semaphor name as a parameter,
* and handles the errors if any. The same behaviour as sem_unlink(3) should be expected.
* @param sem_name
* @return none
**/
void destroy_sem(char *sem_name) {
if(sem_unlink(sem_name) < 0) {
fprintf(stderr, "ERROR: Failed closing semaphore\n");
exit(EXIT_FAILURE);
}
}