🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.os2.programmer
Article #1607

Re: Counting semaphores?

#1607
From: Tom Johnson
Date: Wed, 31 Oct 1990 22:46
50 lines
2407 bytes
>>> 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
Technical Details
Message-ID: <1610001@hpcc01.HP.COM>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpcc05!hpcc01!tomj
References: <90Oct26.163804edt.8236@orasis.vis.toronto.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!zaphod.mps.ohio-state.edu!sdd.hp.com!hplabs!hpcc05!hpcc01!tomj
From: tomj@hpcc01.HP.COM (Tom Johnson)
Newsgroups: comp.os.os2.programmer
Subject: Re: Counting semaphores?
Message-ID: <1610001@hpcc01.HP.COM>
Date: 31 Oct 90 22:46:20 GMT
References: <90Oct26.163804edt.8236@orasis.vis.toronto.edu>
Organization: HP Corp Computing & Services
Lines: 50