[comp.os.os2.programmer] Counting semaphores?

green@vis.toronto.edu (Anthony Thomas Green) (10/27/90)

I have a question regarding the use of semaphores. 

Is it possible to set up a system semaphore that must be cleared as many
times as it has been set in order for a waiting thread to become
runnable?

ATdhvaannkcse

Anthony Green
green@ai.toronto.edu

lsalomo@hubcap.clemson.edu (lsalomo) (10/29/90)

From article <90Oct26.163804edt.8236@orasis.vis.toronto.edu>, by green@vis.toronto.edu (Anthony Thomas Green):
> Is it possible to set up a system semaphore that must be cleared as many
> times as it has been set in order for a waiting thread to become
> runnable?

I always thought that's the way RAM semaphores were implemented?

Cheers,
Q - the "Q"uestor for knowledge (, a degree, etc.)

lsalomo@hubcap.clemson.edu
ibmman@prism.clemson.edu
ibmman@clemson.clemson.edu
=============================================================================
"Gee Wally, I think there's something wrong with the Beaver."
=============================================================================

bobn@mcs213f.cs.umr.edu (Bob Niedergerke) (10/30/90)

In article <11219@hubcap.clemson.edu> lsalomo@hubcap.clemson.edu (lsalomo) writes:
>From article <90Oct26.163804edt.8236@orasis.vis.toronto.edu>, by green@vis.toronto.edu (Anthony Thomas Green):
>> Is it possible to set up a system semaphore that must be cleared as many
>> times as it has been set in order for a waiting thread to become runnable?
>
>I always thought that's the way RAM semaphores were implemented?

It all depends.  If you use DosSemRequest and DosSemClear, the behavior is
as described above.  Using a DosSemWait/DosSemClear scheme, the semaphore
need only be cleared once for a waiting thread to become runnable.

bobn

tomj@hpcc01.HP.COM (Tom Johnson) (11/01/90)

>>> Is it possible to set up a system semaphore that must be cleared as many
>> times as it has been set in order for a waiting thread to become runnable?
>
>>I always thought that's the way RAM semaphores were implemented?

> It all depends.  If you use DosSemRequest and DosSemClear, the behavior is
> as described above.  Using a DosSemWait/DosSemClear scheme, the semaphore
> need only be cleared once for a waiting thread to become runnable.
> ----------

I'm not so sure this is true, but I can't say I've ever tried it.
According to the Programmers Reference, "The DosSemRequest function requests
that the specified semaphore be set AS SOON AS IT IS CLEAR...If the
semaphore has already been set by another thread, the function WAITS until
a thread clears the semaphore... or until a time-out occurs."

It sounds to me like this is not what is desired by the original poster.
What is needed is a way to set a semaphore without waiting, but have the
semaphore not be cleared until all threads that set the semaphore clear
it.  I'm not sure DosSemRequest can do that.

If OS/2 doesn't give you this functionality, it is possible to create
this functionality using 2 normal system semaphores.  The following
algorithm describes how to do it:

var MUTEX, DELAY: SEMAPHORE /* binary semaphores */
    K: INTEGER;   /* counting semaphore value */

    MUTEX = 1; DELAY = 0;
    K = x;      /* initialize it to highest desired value */

    P(K) = P(MUTEX);
           K = K - 1;
           if K < 0 then
               V(MUTEX);
               P(DELAY);
           else
               V(MUTEX);

    V(K) = P(MUTEX);
           K = K + 1;
           if K <= 0
               V(DELAY);
           V(MUTEX);

If this isn't clear, sorry, but I didn't have time to study it.  I
got the algorithm from "Parallel Programming" by R.H. Perrott.  Almost
any decent Operating Systems book should have a similar algorithm.

Tom Johnson

dans@microsoft.UUCP (Dan SPALDING) (11/04/90)

In article <1610001@hpcc01.HP.COM> tomj@hpcc01.HP.COM (Tom Johnson) writes:
>>>> Is it possible to set up a system semaphore that must be cleared as many
>>> times as it has been set in order for a waiting thread to become 
>runnable?
>>
>>>I always thought that's the way RAM semaphores were implemented?
>
What you want to use is the Fast Safe Ram semaphores or named (system)
semaphores.  Both of them will implement counting and won't release some
thread waiting on the semaphore until the count reaches zero.  However,
the same process can request the semaphore if it already owns it (in the same
thread) and it just bumps the count.