My Project
Typedefs | Functions
sem.h File Reference

Semaphore implementation for the synchronization of POSIX threads. More...

Go to the source code of this file.

Typedefs

typedef struct SEM SEM
 

Functions

SEMsemCreate (int initVal)
 Creates a new semaphore. More...
 
void semDestroy (SEM *sem)
 Destroys a semaphore and frees all associated resources. More...
 
void P (SEM *sem)
 P-operation. More...
 
void V (SEM *sem)
 V-operation. More...
 

Detailed Description

Semaphore implementation for the synchronization of POSIX threads.

This module implements counting P/V semaphores suitable for the synchronization of POSIX threads. POSIX mutexes and condition variables are utilized to implement the semaphor operations.

Typedef Documentation

◆ SEM

typedef struct SEM SEM

Opaque type of a semaphore.

Function Documentation

◆ P()

void P ( SEM sem)

P-operation.

Attempts to decrement the semaphore value by 1. If the semaphore value is not a positive number, the operation blocks until a V-operation increments the value and the P-operation succeeds.

Parameters
semHandle of the semaphore to decrement.

◆ semCreate()

SEM* semCreate ( int  initVal)

Creates a new semaphore.

This function creates a new semaphore. If an error occurs during the initialization, the implementation frees all resources already allocated by then and sets errno to an appropriate value.

It is legal to initialize the semaphore with a negative value. If this is the case, in order to reset the semaphore counter to zero, the V-operation must be performed (-initVal) times.

Parameters
initValThe initial value of the semaphore.
Returns
Handle for the created semaphore, or NULL if an error occurred.

◆ semDestroy()

void semDestroy ( SEM sem)

Destroys a semaphore and frees all associated resources.

Parameters
semHandle of the semaphore to destroy. If a NULL pointer is passed, the implementation does nothing.

◆ V()

void V ( SEM sem)

V-operation.

Increments the semaphore value by 1 and notifies P-operations that are blocked on the semaphore of the change.

Parameters
semHandle of the semaphore to increment.