svcond - POSIX-like condition variables implemented using SysV semaphores.
#include <mba/svcond.h>
int svcond_create(svcond_t *cond, struct pool *sempool);
int svcond_destroy(svcond_t *cond);
int svcond_wait(svcond_t *cond, svsem_t *lock);
int svcond_signal(svcond_t *cond);
int svcond_broadcast(svcond_t *cond);
Condition variables are similar to semaphores however a lock can be specified
with the wait function that will be unlocked just before blocking. When the
blocked process or thread is subsequently signalled the lock will be
reaquired. In practice this is frequently a superior coordination mechanism to
semaphores alone. The svcond(3m) module provides a POSIX-like condition
variables interface implemented using only System V semaphores.
The svcond(3m) module is not available in the Win32
environment.
- create
- The svcond_create function initializes the condition variable
cond. The sempool parameter is an svsem(3m) pool
created with the svsem_pool_create function as illustrated below.
The value parameter must be 1 and the max_size parameter
must be 3 times the number of condition variables that will be in use at
any moment. If semaphores for the condition variable cannot be aquired
from the pool, errno will be set to EAGAIN and -1 will be
returned.
void
foo(void)
{
struct pool sempool;
svcond_t condvar;
svsem_pool_create(&sempool, 250, 1, 0, NULL); /* create semaphore array */
svcond_create(&condvar, &sempool); /* initialize one condition variable */
svcond_wait(&convar);
...
- destroy
- The svcond_destroy function releases the semaphores used by the
condition variable cond. It is not an error to call this function
with a NULL parameter, on memory that is zero'd or repeatedly on
the same cond object -- it will be ignored or destroyed only
once.
- wait
- The svcond_wait function will unlock the semaphore lock and
then sleep until one of the following occurs;
the thread or process is interrupted by a signal (e.g.
SIGQUIT),
the svcond_broadcast function is called with the condition
variable,
or the svcond_signal function is called with the condition variable
and that process or thread is the next in the wait queue.
If a SIGINT is recieved the function will set
errno to EINTR and return but not before reaquiring
lock.
- signal
- The svcond_signal function wakes up one process or thread blocked
on the condition variable cond and return from the
svsem_wait call but not before reaquiring the lock.
- broadcast
- The svcond_broadcast function wakes up all processes and threads
blocked on the condition variable cond and return from the
svsem_wait call but not before reaquiring the lock.
- create
- The svcond_create function returns 0 if the condition variable was
successfully initialized or -1 if the operation failed in which case
errno will be set to an appropriate value (e.g. EAGAIN if 3
semaphores cannot be obtained from the pool).
- destroy
- If the operation is successful 0 is returned. Otherwise -1 is returned and
errno is set appropriately.
- wait
- If the operation is successful 0 is returned. Otherwise -1 is returned and
errno is set appropriately.
- signal
- If the operation is successful 0 is returned. Otherwise -1 is returned and
errno is set appropriately.
- broadcast
- If the operation is successful 0 is returned. Otherwise -1 is returned and
errno is set appropriately.