Article View: comp.os.os2.programmer
Article #2520Re: Making a non exclusive semaphore (like on Unix)
From: Gary Kushner
Date: Mon, 08 Apr 1991 02:25
Date: Mon, 08 Apr 1991 02:25
78 lines
1984 bytes
1984 bytes
Your code uses polling which should be avoided if at all possible. Here
is the code I use:
----------
pv.h:
----------
/*------------
Type for PVsems
------------*/
typedef struct _pvsem {
long count;
long more; // sem clear if count > 0
long mutex;
} _far PVSEM;
/*-----------
Protos
-----------*/
PVSEM _far *PVSemCreate(void);
void _far _fastcall P(PVSEM _far *s);
void _far _fastcall V(PVSEM _far *s);
--------
pv.c
---------
#define INCL_DOSSEMAPHORES
#include <os2.h>
#include <malloc.h>
#include "pv.h"
/**************
* PVSEMCREATE - allocate sem
**************/
PVSEM _far *PVSemCreate(void)
{
PVSEM *s;
if ( (s=calloc(1, sizeof(PVSEM))) != NULL)
s->count=0;
return s;
}
/**************
* P -
**************/
void _far _fastcall P(PVSEM _far *s)
{
// any V's pending? if not then wait...
DosSemRequest(&s->more, SEM_INDEFINITE_WAIT);
DosSemRequest(&s->mutex, SEM_INDEFINITE_WAIT);
// if there are still more V's, then leave more clear
if (--(s->count) > 0)
DosSemClear(&s->more);
DosSemClear(&s->mutex);
}
/**************
* V - count them up
**************/
void _far _fastcall V(PVSEM _far *s)
{
DosSemRequest(&s->mutex, SEM_INDEFINITE_WAIT);
s->count++;
DosSemClear(&s->more); // ok guys, come and get it
DosSemClear(&s->mutex);
}
Technical Details
Message-ID:
<11871@dog.ee.lbl.gov>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!hellgate.utah.edu!dog.ee.lbl.gov!ux1.lbl.gov!kushnerReferences:
<1991Mar12.152033.11880@ibmpcug.co.uk>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!hellgate.utah.edu!dog.ee.lbl.gov!ux1.lbl.gov!kushner From: kushner@ux1.lbl.gov (Gary Kushner) Newsgroups: comp.os.os2.programmer,connect.audit Subject: Re: Making a non exclusive semaphore (like on Unix) Keywords: semaphore, counter, exclusive Message-ID: <11871@dog.ee.lbl.gov> Date: 8 Apr 91 02:25:05 GMT References: <1991Mar12.152033.11880@ibmpcug.co.uk> Reply-To: kushner@ux1.lbl.gov (Gary Kushner) Organization: Lawrence Berkeley Laboratory Lines: 78 X-Local-Date: Sun, 7 Apr 91 19:25:05 PDT