Semaphore Package for TCL

Current Version 2.5 zip tar gzip

Author: Evan Rempel erempel@UVic.CA


This package implements a straight forward semaphore signal and wait mechanism. Semaphores can go up or down the interpreter hierarchy. Names within the same interpreter may not contain a "/" character. To specify a semaphore in a child interpreter, use a semaphore name of the form child/name which may be recursive. To specify a semaphore of a parent interpreter, use a semaphore name of the form ../name which may be recursive.

::Semaphore::Signal name

Signal a semaphore returning the semaphore value after it is signaled. It is not possible to determine if a Semaphore::Wait was release as a result of the Signal.

Signalling a semaphore may release one of the pending ::Semaphore::Wait or incrementing its counter so that a successive ::Semaphore::Wait will not block.

Semaphore names in a child interpreter take on the name of {interpreter name}/{semaphore name} in the parent interpreter. Children can use the name ../{semaphore name} to signal parent semaphore. Siblings may signal each other by using semaphores in the parent interpreter.

::Semaphore::Wait {glob name} ...

Wait for a semaphore to be signaled, returning the name of the signaled semaphore. This name may be a semaphore in a parent or a child interpreter. If the semaphore has previously been signaled without a matching wait, then this routine will return immediately, returning the name of the semaphore that was signaled.

The semaphore names are checked in the order that they are specified allowing you to create an effective priority for waiting on semaphores.

Semaphore names in a child interpreter take on the name of {interpreter name}/{semaphore name} in the parent interpreter. Children can use the name ../{semaphore name} to signal parent semaphore. Siblings may signal each other by using semaphores in the parent interpreter.

::Semaphore::Reset name

Reset for a semaphore. Once a semaphore has been reset, a wait will block until a signal is generated. Resetting a semaphore does not signal it, which may result in routines waiting on the semaphore that has already been signaled and reset, which would then block indefinitely.

::Semaphore::Status name

Return the current outstanding signal counts that have not yet been waited on. This can be used to determine if you will block prior to calling Wait. If Status returns zero then the Wait will block.

::Semaphore::Extend interpreter

Extend the current semaphore symantics to the child interpreter. This allows the interpreter to signal and wait on semaphores in the current interpreter by prefixing the semaphore name with ../ as in ../signalName and it allows the current interpreter to signal and wait on semaphores in the child interpreter by prefacing the signal name with interpreter as in interpreter/signalName.

Although the Extend can only extend to an immediate child, the signal, wait and status routines are not restricted in this way. You can use ../../signalName and interp/interp/signalName or further nestings if you desire.

This construct allows code to be written that signals semaphores within its own interpreter without the knowledge that it is running in a child interpreter, even though a parent interpreter is waiting on child/signalName without any worry of signal name conflicts with other packages.