| iMatix home page
| << | < | > | >>
SMT Logo SMT
Version 2.81

 

semaph_create

#include "smtlib.h"
SEMAPH *
semaph_create (
    const char *semaph_name,            /*  Name of semaph to create         */
    int   value                         /*  Initial semaphore value          */
)

Synopsis

Creates a new semaphore. You must create a semaphore before you can use it. The value argument specifies the number of parties that can access the semaphore (or its related resources) at once. The value must be greated than zero. A 'normal' binary semaphore has an initial value of 1. The semaph name is an arbitrary text, unique within the application. Returns the address of the created SEMAPH block. If there was an error, returns NULL and sets smt_errno to one of these values:
SMT NOTREADY smt_init() was not called, or failed
SMT OUTOFMEMORY Not enough heap memory left
SMT SEMAPHEXISTS A semaphore with this name is already declared

Source Code - (smtlib.c)

{
    SYMBOL  *dict_entry;                /*  Dictionary symbol                */
    SEMAPH  *semaph;                    /*  Agent information block          */
    char    *full_name;                 /*  Full semaph name                 */

#if (defined (SMT_TRACE))
    trace ("semaph_create: semaph=%s", semaph_name);
#endif
    ASSERT (semaph_name);
    ASSERT (value > 0);
    if (!smt_alive)                     /*  If SMT API was not correctly     */
      {                                 /*    initialised, forget it         */
        smt_errno = SMT_NOTREADY;
        return (NULL);
      }

    /*  Check that semaphore is not already declared                         */
    full_name = get_semaph_name (semaph_name);
    if (sym_lookup_symbol (dict, full_name))
      {
        smt_errno = SMT_SEMAPHEXISTS;
        return (NULL);
      }

    /*  Now create entry for the semaphore                                   */
    dict_entry = sym_create_symbol (dict, full_name, NULL);
    if (dict_entry == NULL)
      {
        smt_errno = SMT_OUTOFMEMORY;
        return (NULL);
      }

    /*  Allocate an SEMAPH block and attach it to the semaphore list         */
    semaph = (SEMAPH *) node_create (semaphs.prev, sizeof (SEMAPH));
    if (semaph == NULL)
      {
        sym_delete_symbol (dict, dict_entry);
        smt_errno = SMT_OUTOFMEMORY;
        return (NULL);
      }

    /*  Point the dictionary entry to the semaph information block           */
    dict_entry-> data = semaph;

    /*  Now initialise the semaph - all fields are already set to zero       */
    semaph-> symbol        = dict_entry;
    semaph-> name          = mem_strdup (semaph_name);
    semaph-> threads.left  = &semaph-> threads;
    semaph-> threads.right = &semaph-> threads;
    semaph-> value         = value;

    return (semaph);
}

| << | < | > | >> iMatix Copyright © 1996-99 iMatix Corporation