16. Svcond

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.

16.1. Memry management functions

These functions should be used to create and destroy svcond condition variables.

The svcond_create function
Synopsis

#include <mba/svcond.h> int svcond_create(svcond_t *cond, struct pool *sempool);
Description
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);
      ...
  
Returns
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).

The svcond_destroy function
Synopsis

#include <mba/svcond.h> int svcond_destroy(svcond_t *cond);
Description
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.
Returns
If the operation is successful 0 is returned. Otherwise -1 is returned and errno is set appropriately.

16.2. Svcond functions

These functions should be used to manipulate svcond condition variables.

The svcond_wait function
Synopsis

#include <mba/svcond.h> int svcond_wait(svcond_t *cond, svsem_t *lock);
Description
The svcond_wait function will unlock the semaphore lock and then sleep until one of the following occurs; If a SIGINT is recieved the function will set errno to EINTR and return but not before reaquiring lock.
Returns
If the operation is successful 0 is returned. Otherwise -1 is returned and errno is set appropriately.

The svcond_signal function
Synopsis

#include <mba/svcond.h> int svcond_signal(svcond_t *cond);
Description
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.
Returns
If the operation is successful 0 is returned. Otherwise -1 is returned and errno is set appropriately.

The svcond_broadcast function
Synopsis

#include <mba/svcond.h> int svcond_broadcast(svcond_t *cond);
Description
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.
Returns
If the operation is successful 0 is returned. Otherwise -1 is returned and errno is set appropriately.


Copyright 2004 Michael B. Allen <mba2000 ioplex.com>