home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / baclsema.zip / OS2SEM.C < prev    next >
C/C++ Source or Header  |  1994-06-07  |  6KB  |  263 lines

  1. // ****************************************************************************
  2. //
  3. // Module:  os2sem.C
  4. // Author:  Dick Lam
  5. //
  6. // Purpose: C++ class source file for osSemaphore
  7. //
  8. // Notes:  This is a base class.  It contains general implementation methods
  9. //         for semaphores used in signalling between processes and threads.
  10. //
  11. // ****************************************************************************
  12.  
  13. #define INCL_DOS
  14. #include <os2.h>
  15.  
  16. #include <string.h>
  17. #include "ossem.h"
  18.  
  19. // ****************************************************************************
  20.  
  21. // init operating system-specific variables
  22.  
  23. const char *semPath = "\\SEM32\\";
  24.  
  25. // ****************************************************************************
  26.  
  27. // osSemaphore - constructor
  28.  
  29. osSemaphore::osSemaphore(ipcSemaphore *interface, const char *name,
  30.                          ipcSemaphoreType type, ipcSemaphoreOp operation)
  31.  
  32. {
  33.    // init instance variables
  34.    myInterface = interface;
  35.    myName = 0;
  36.    myID = 0L;
  37.    myType = type;
  38.    if (operation == semcreate)
  39.       isOwner = 1;
  40.    else
  41.       isOwner = 0;
  42.  
  43.    // check that input name is valid
  44.    if (!name || (strlen(name) == 0)) {
  45.       myInterface->myState = ipcSemaphore::badname;
  46.       return;
  47.    }
  48.  
  49.    // allocate space for the name
  50.    myName = new char[strlen(semPath) + strlen(name) + 1];
  51.    if (!myName) {
  52.       myInterface->myState = ipcSemaphore::bad;
  53.       return;
  54.    }
  55.  
  56.    // and form the complete semaphore pathname
  57.    strcpy(myName, semPath);
  58.    strcat(myName, name);
  59.  
  60.    // set the object state and return
  61.    myInterface->myState = ipcSemaphore::good;
  62. }
  63.  
  64. // ----------------------------------------------------------------------------
  65.  
  66. // ~osSemaphore - destructor
  67.  
  68. osSemaphore::~osSemaphore()
  69.  
  70. {
  71.    delete myName;
  72. }
  73.  
  74. // ----------------------------------------------------------------------------
  75.  
  76. // Name - returns the name of the semaphore
  77.  
  78. char *osSemaphore::Name() const
  79.  
  80. {
  81.    return myName;
  82. }
  83.  
  84. // ----------------------------------------------------------------------------
  85.  
  86. // ID - returns the semaphore id
  87.  
  88. unsigned long osSemaphore::ID() const
  89.  
  90. {
  91.    return myID;
  92. }
  93.  
  94. // ----------------------------------------------------------------------------
  95.  
  96. // Type - returns the type of semaphore
  97.  
  98. ipcSemaphoreType osSemaphore::Type() const
  99.  
  100. {
  101.    return myType;
  102. }
  103.  
  104. // ----------------------------------------------------------------------------
  105.  
  106. // Owner - returns 1 if this is the owner (creator), and 0 otherwise
  107.  
  108. int osSemaphore::Owner() const
  109.  
  110. {
  111.    return isOwner;
  112. }
  113.  
  114. // ----------------------------------------------------------------------------
  115.  
  116. // CreateMutex - creates a mutex semaphore
  117.  
  118. void osSemaphore::CreateMutex()
  119.  
  120. {
  121.    if (::DosCreateMutexSem( (PSZ)myName, (PHMTX)&myID, 0L, 0 ) != 0)
  122.       myInterface->myState = ipcSemaphore::bad;
  123. }
  124.  
  125. // ----------------------------------------------------------------------------
  126.  
  127. // OpenMutex - opens a mutex semaphore
  128.  
  129. void osSemaphore::OpenMutex()
  130.  
  131. {
  132.    myID = 0L;
  133.    if (::DosOpenMutexSem( (PSZ)myName, (PHMTX)&myID) != 0)
  134.       myInterface->myState = ipcSemaphore::notfound;
  135. }
  136.  
  137. // ----------------------------------------------------------------------------
  138.  
  139. // RequestMutex - requests a mutex semaphore
  140.  
  141. void osSemaphore::RequestMutex()
  142.  
  143. {
  144.    ::DosRequestMutexSem( (HMTX)myID, SEM_INDEFINITE_WAIT );
  145. }
  146.  
  147. // ----------------------------------------------------------------------------
  148.  
  149. // ReleaseMutex - releases a mutex semaphore
  150.  
  151. void osSemaphore::ReleaseMutex()
  152.  
  153. {
  154.    ::DosReleaseMutexSem( (HMTX)myID );
  155. }
  156.  
  157. // ----------------------------------------------------------------------------
  158.  
  159. // QueryMutex - queries a mutex semaphore
  160.  
  161. unsigned long osSemaphore::QueryMutex()
  162.  
  163. {
  164.    PID pOwner;
  165.    TID tOwner;
  166.    ULONG count;
  167.  
  168.    ::DosQueryMutexSem( (HMTX)myID, &pOwner, &tOwner, &count );
  169.    return count;
  170. }
  171.  
  172. // ----------------------------------------------------------------------------
  173.  
  174. // CloseMutex - closes a mutex semaphore
  175.  
  176. void osSemaphore::CloseMutex()
  177.  
  178. {
  179.    ::DosCloseMutexSem( (HMTX)myID );
  180. }
  181.  
  182. // ----------------------------------------------------------------------------
  183.  
  184. // CreateEvent - creates an event semaphore
  185.  
  186. void osSemaphore::CreateEvent()
  187.  
  188. {
  189.    if (::DosCreateEventSem( (PSZ)myName, (PHEV)&myID, 0L, 0 ) != 0)
  190.       myInterface->myState = ipcSemaphore::bad;
  191. }
  192.  
  193. // ----------------------------------------------------------------------------
  194.  
  195. // OpenEvent - opens an event semaphore
  196.  
  197. void osSemaphore::OpenEvent()
  198.  
  199. {
  200.    myID = 0L;
  201.    if (::DosOpenEventSem( (PSZ)myName, (PHEV)&myID ) != 0)
  202.       myInterface->myState = ipcSemaphore::notfound;
  203. }
  204.  
  205. // ----------------------------------------------------------------------------
  206.  
  207. // PostEvent - posts an event semaphore
  208.  
  209. void osSemaphore::PostEvent()
  210.  
  211. {
  212.    ::DosPostEventSem( (HEV)myID );
  213. }
  214.  
  215. // ----------------------------------------------------------------------------
  216.  
  217. // ResetEvent - resets an event semaphore
  218.  
  219. void osSemaphore::ResetEvent()
  220.  
  221. {
  222.    ULONG count;
  223.  
  224.    ::DosResetEventSem( (HEV)myID, &count );
  225. }
  226.  
  227. // ----------------------------------------------------------------------------
  228.  
  229. // WaitEvent - waits on an event semaphore
  230.  
  231. void osSemaphore::WaitEvent()
  232.  
  233. {
  234.    ::DosWaitEventSem( (HEV)myID, SEM_INDEFINITE_WAIT );
  235. }
  236.  
  237. // ----------------------------------------------------------------------------
  238.  
  239. // QueryEvent - queries an event semaphore
  240.  
  241. unsigned long osSemaphore::QueryEvent()
  242.  
  243. {
  244.    ULONG count;
  245.  
  246.    ::DosQueryEventSem( (HEV)myID, &count );
  247.    return count;
  248. }
  249.  
  250. // ----------------------------------------------------------------------------
  251.  
  252. // CloseEvent - closes an event semaphore
  253.  
  254. void osSemaphore::CloseEvent()
  255.  
  256. {
  257.    ::DosCloseEventSem( (HEV)myID );
  258. }
  259.  
  260. // ****************************************************************************
  261.  
  262. // end of os2sem.C
  263.