BEEP-8 SDK 1.0.0
Loading...
Searching...
No Matches
semaphore.h File Reference

POSIX-compliant semaphore definitions for the BEEP-8 system. More...

#include <stdint.h>
#include <time.h>

Go to the source code of this file.

Classes

struct  _sem_t
 

Macros

#define SEM_FAILED   ((sem_t *) 0)
 

Typedefs

typedef struct _sem_t sem_t
 

Functions

int sem_init (sem_t *sem, int pshared, unsigned int value)
 Initialize an unnamed semaphore.
 
int sem_wait (sem_t *sem)
 Wait on an unnamed semaphore.
 
int sem_timedwait (sem_t *sem, const struct timespec *__abstime)
 Wait on an unnamed semaphore with a timeout.
 
int sem_trywait (sem_t *sem)
 Try to wait on an unnamed semaphore without blocking.
 
int sem_post (sem_t *sem)
 Post (signal) an unnamed semaphore.
 
int sem_getvalue (sem_t *sem, int *sval)
 Get the current value of an unnamed semaphore.
 
int sem_destroy (sem_t *sem)
 
sem_tsem_open (const char *name, int __oflag,...)
 
int sem_close (sem_t *sem)
 
int sem_unlink (const char *name)
 

Detailed Description

POSIX-compliant semaphore definitions for the BEEP-8 system.

This file provides POSIX-compliant semaphore definitions and function prototypes for the BEEP-8 system. It includes a basic sem_t structure and functions for initializing, waiting, posting, and destroying unnamed semaphores.

The provided functions include:

  • sem_init: Initialize an unnamed semaphore
  • sem_wait: Wait on an unnamed semaphore
  • sem_timedwait: Wait on an unnamed semaphore with a timeout
  • sem_trywait: Try to wait on an unnamed semaphore without blocking
  • sem_post: Post (signal) an unnamed semaphore
  • sem_getvalue: Get the current value of an unnamed semaphore

Note that named semaphores are not supported in this implementation. Functions related to named semaphores, such as sem_open, sem_close, and sem_unlink, are declared but not implemented and will return errors if used.

Typically, users do not need to use this header directly. It is intended for internal use within the BEEP-8 system to manage synchronization between threads and processes.

For more detailed information, please refer to the BEEP-8 data sheet.

Function Documentation

◆ sem_getvalue()

int sem_getvalue ( sem_t * sem,
int * sval )
extern

Get the current value of an unnamed semaphore.

This function stores the current value of the semaphore pointed to by sem in the location pointed to by sval.

Parameters
semA pointer to the semaphore.
svalA pointer to an integer where the semaphore's value will be stored.
Returns
0 on success; -1 on error.

◆ sem_init()

int sem_init ( sem_t * sem,
int pshared,
unsigned int value )
extern

Initialize an unnamed semaphore.

This function initializes an unnamed semaphore. The semaphore can be shared between processes if the pshared parameter is non-zero; otherwise, it is shared only between threads of the same process.

Parameters
semA pointer to the semaphore to initialize.
psharedA flag indicating whether the semaphore is shared between processes.
valueThe initial value of the semaphore.
Returns
0 on success; -1 on error.

◆ sem_post()

int sem_post ( sem_t * sem)
extern

Post (signal) an unnamed semaphore.

This function increases (unlocks) the semaphore pointed to by sem. If the semaphore's value was zero and there are threads waiting on the semaphore, one of the waiting threads is awakened.

Parameters
semA pointer to the semaphore to post.
Returns
0 on success; -1 on error.

◆ sem_timedwait()

int sem_timedwait ( sem_t * sem,
const struct timespec * __abstime )
extern

Wait on an unnamed semaphore with a timeout.

This function decreases (locks) the semaphore pointed to by sem, but if the decrement cannot be immediately performed, it will block until the timeout specified by __abstime elapses.

Parameters
semA pointer to the semaphore to wait on.
__abstimeA pointer to a timespec structure specifying the timeout.
Returns
0 on success; -1 on error or timeout.

◆ sem_trywait()

int sem_trywait ( sem_t * sem)
extern

Try to wait on an unnamed semaphore without blocking.

This function attempts to decrease (lock) the semaphore pointed to by sem. If the semaphore's value is greater than zero, the decrement proceeds and the function returns immediately. If the semaphore's value is zero, the function returns immediately without decrementing the semaphore.

Parameters
semA pointer to the semaphore to wait on.
Returns
0 on success; -1 if the semaphore could not be locked immediately.

◆ sem_wait()

int sem_wait ( sem_t * sem)
extern

Wait on an unnamed semaphore.

This function decreases (locks) the semaphore pointed to by sem. If the semaphore's value is greater than zero, the decrement proceeds and the function returns immediately. If the semaphore's value is zero, the function blocks until the semaphore's value becomes greater than zero.

Parameters
semA pointer to the semaphore to wait on.
Returns
0 on success; -1 on error.