[Contents] [Index] [Help] [Retrace] [Browse <] [Browse >]

The ObtainSemaphore() function can be used to get an exclusive lock on a
semaphore.  If another task currently has an exclusive or shared lock(s)
on the semaphore, your task will be put to sleep until all locks on the
the semaphore are released.

    Semaphore Nesting.
    ------------------
    SignalSemaphores have nesting.  That is, if your task already
    owns the semaphore, it will get a second ownership of that semaphore.
    This simplifies the writing of routines that must own the semaphore
    but do not know if the caller has obtained it yet.

To obtain a semaphore use:

    struct SignalSemaphore *semaphore;
    ObtainSemaphore(semaphore);

To get an exclusive lock on a public semaphore, the following code should
be used:

    UBYTE *name;
    struct SignalSemaphore *semaphore;

    Forbid();     /* Make sure the semaphore will not go away if found. */
    if (semaphore=FindSemaphore(name))
        ObtainSemaphore(semaphore);
    Permit();

The value of semaphore is NULL if the semaphore does not exist.  This is
only needed if the semaphore has a chance of going away at any time (i.e.,
the semaphore is public and  might be removed by some other program).  If
there is a guarantee that the semaphore will not disappear, the semaphore
address could be cached, and all that would be needed is a call to the
ObtainSemaphore() function.