Semaphores <sema.h>

Top  Previous  Next

This group of functions allows a Diamond program to create and manipulate semaphores, which can be used to synchronize the activity of several concurrently executing threads in a task.

 

The header file sema.h declares a type SEMA, which is used by these functions. Unlike in some operating systems, Diamond semaphores are not specially created by the kernel; they are ordinary variables (structures) that you can create anywhere in your program just like other variables.

 

Dragons003The only constraint on the creation of semaphores is that they must continue to exist as long as there are threads waiting on them. If you create a semaphore as a local variable to a function and that function returns while a thread is waiting on the semaphore, then that thread can never execute again.

 

In this version of Diamond, threads are placed in a queue of waiting processes, so that the first thread to start waiting on a semaphore is the first to be resumed.

 


sema_init

initialize a semaphore


sema_signal

perform the signal operation on a semaphore


sema_signal_n

perform sema_signal n times


sema_test_wait

check whether waiting on a semaphore would block


sema_wait

perform the wait operation on a semaphore


sema_wait_n

perform sema_wait n times


static_sema_init

initialisation value for a semaphore (macro)