TIP: Click on subject to list as thread! ANSI
echo: os2prog
to: Darin McBride
from: Doug Nazar
date: 1996-05-23 00:36:56
subject: Semaphores (16 & 32)

Hello Darin!

Tuesday May 21 1996 00:19, Darin McBride wrote to All:

DM>         I'm trying to block Sq386p.exe which uses \SEM\SQUISH\DEFAULT,
DM> however the Semaphores included in the Warp toolkit want me to use \SEM32\
DM> to start.  Can I just use \SEM\ anyway?

The "/sem" prefix is used in 16 bit code as you have noticed.

DM> in the header files (the ones that come with Watcom, nonetheless!), so too
DM> bad.  Ack!  Not in the Warp Toolkit headers either, it seems.  I created

They are in the 16bit headers (\watcom\h\os21x). Also in the 1.3 toolkit
& the 16bit headers on the DDK.

DM> the function prototypes, having noticed that these functions were in
DM> OS2286.LIB, and linked with that lib (in the 386 directory), but the
DM> linker
DM> complains that they are undefined references.  Somehow I'm guessing I need
DM> those, but...

Most likely you have the calling convention wrong. You need to do something like

extern "C" {
   APIRET16 APIENTRY DosCreateSem(...);
}

DM> Can anyone help me?

Now that we've covered all that, here's the easy way to do it .
Scott uses a DLL (SMSERIAL.DLL) to handle his serialization after a bit of
work testing it here is a little util to use it.

-- CUT -------- Begin test.cpp -------- CUT --
#include 

#define INCL_DOS
#include 

void _Far16 _Pascal BbsSemSerialize(char * semaphoreName, char* dontKnow);

int main() {
   cout << "Top" << endl;
   BbsSemSerialize(0,0);

   cout << "Middle" << endl;
   DosSleep(15000);

   cout << "Bottom" << endl;
   return 0;
} -- CUT --------- End test.cpp --------- CUT --

I compilied this with IBM VA. You'll need to create an import library like so

implib smserial.lib smserial.dll

DM> to use the 32-bit semaphores, and, again, I'd like a little direction on
DM> what to use: the Mutex semaphores?  If so, can someone give me really
DM> simple sample code?

Yeap. Mutex is the way to go. Here's a quick sample I whipped up:

-- CUT -------- Begin test.cpp -------- CUT --
#include 

#define INCL_ERRORS
#define INCL_DOSMISC
#define INCL_DOSPROCESS
#define INCL_DOSSEMAPHORES
#include 

class OS2MSG { /*fold00*/
   ULONG errorNumber;
   char msgBuf[2048];
   ULONG msgLen;
public:
   OS2MSG(ULONG e, const char * msgFile = "OSO001.MSG") : errorNumber(e) {
      if (errorNumber) {
         APIRET rc = 0;
         rc = DosGetMessage(0, 0, msgBuf, 2048, errorNumber,
"OSO001.MSG", &msgLen);
         if (rc) {
            msgLen = 0;
            *msgBuf = '\0';
         }
      }
   }
   friend ostream& operator<< (ostream& os, const OS2MSG& os2Msg);
};

ostream& operator<< (ostream& os, const OS2MSG& os2Msg) {
   if (os2Msg.errorNumber) {
      os << "SYS";
      os.width(4);
      os.fill('0');
      os << os2Msg.errorNumber << ": " << os2Msg.msgBuf;
   }
   return os;
}

int main() { /*FOLD00*/
   HMTX mutexSemaphore = 0;
   APIRET rc =0;

   rc = DosOpenMutexSem("/sem32/this is a test semaphore",
&mutexSemaphore);
   if (rc && rc!=ERROR_SEM_NOT_FOUND) {
      cerr << "DosOpenMutexSem: " << endl <<
"  " << OS2MSG(rc) << endl;
      return 1;
   }

   if (rc) {
      // We're the first one's! Create the semaphore
      rc = DosCreateMutexSem("/sem32/this is a test semaphore",
&mutexSemaphore,
                             DC_SEM_SHARED, TRUE);
      if (rc) {
         cerr << "DosCreateMutexSem: " << endl
<< "  " << OS2MSG(rc) << endl;
         return 1;
      }
   }
   else {
      // We're not first, request ownership
      cout << "Waiting for control" << endl;

      rc = DosRequestMutexSem(mutexSemaphore, SEM_INDEFINITE_WAIT);
      if (rc) {
         cerr << "DosRequestMutexSem: " << endl
<< "  " << OS2MSG(rc) << endl;
         return 1;
      }
   }

   cout << "Houston, we have control!" << endl;
   DosSleep(15000);
   cout << "Releasing control" << endl;

   rc = DosReleaseMutexSem(mutexSemaphore);
   if (rc)
      cerr << "DosReleaseMutexSem: " << endl <<
"  " << OS2MSG(rc) << endl;

   rc = DosCloseMutexSem(mutexSemaphore);
   if (rc)
      cerr << "DosCloseMutexSem: " << endl <<
"  " << OS2MSG(rc) << endl;

   return 0;
} -- CUT --------- End test.cpp --------- CUT --

DM> Perhaps after I learn semaphores, I'll go on to multi-threading.  :-)

They're not THAT hard .

Doug

--- GoldED/2 2.50+
* Origin: Realm of the Dragons - `Where thy mind hath no limits' (1:250/516)
SEEN-BY: 50/99 270/101 620/243 711/401 409 410 413 430 808 809 934 955
SEEN-BY: 712/407 515 517 628 713/888 800/1
@PATH: 250/516 101 99 3615/50 396/1 270/101 712/515 711/808 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.