STASIS
A pipeline delivery generator
Loading...
Searching...
No Matches
semaphore.c File Reference
#include <stdio.h>
#include <fcntl.h>
#include "core_message.h"
#include "sem.h"
#include "utils.h"
Include dependency graph for semaphore.c:

Functions

void semaphore_handle_exit ()
 
int semaphore_init (struct Semaphore *s, const char *name, const int value)
 
int semaphore_wait (struct Semaphore *s)
 
int semaphore_post (struct Semaphore *s)
 
void semaphore_destroy (struct Semaphore *s)
 

Variables

struct Semaphoresemaphores [1000] = {0}
 
bool semaphore_handle_exit_ready = false
 

Function Documentation

◆ semaphore_init()

int semaphore_init ( struct Semaphore * s,
const char * name,
int value )

Initialize a cross-platform semaphore (Linux/Darwin)

c
#include "sem.h"
int main(int argc, char *argv[]) {
struct Semaphore s;
if (semaphore_init(&s, "mysem", 1)) {
perror("semaphore_init failed");
exit(1);
}
if (semaphore_wait(&s)) {
perror("semaphore_wait failed");
exit(1);
}
//
// Critical section
// CODE HERE
//
if (semaphore_post(&s)) {
perror("semaphore_post failed");
exit(1);
}
semaphore_destroy(&s);
}
int semaphore_init(struct Semaphore *s, const char *name, int value)
Definition semaphore.c:33
Definition sem.h:15
Parameters
sa pointer to Semaphore
nameof the semaphore
valueinitial value of the semaphore
Returns
-1 on error
0 on success