home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / baclsema.zip / WINSEM.C < prev   
Text File  |  1994-06-10  |  7KB  |  299 lines

  1. // ****************************************************************************
  2. //
  3. // Module:  winsem.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 STRICT
  14. #include <windows.h>
  15.  
  16. // to avoid conflicts, the following 4 macros (defined in winbase.h) must
  17. // be undefined
  18. #ifdef CreateMutex
  19. #undef CreateMutex
  20. #endif
  21.  
  22. #ifdef OpenMutex
  23. #undef OpenMutex
  24. #endif
  25.  
  26. #ifdef CreateEvent
  27. #undef CreateEvent
  28. #endif
  29.  
  30. #ifdef OpenEvent
  31. #undef OpenEvent
  32. #endif
  33.  
  34. #include <string.h>
  35. #include "ossem.h"
  36.  
  37.  
  38. // ****************************************************************************
  39.  
  40. // osSemaphore - constructor
  41.  
  42. osSemaphore::osSemaphore(ipcSemaphore *interface, const char *name,
  43.                          ipcSemaphoreType type, ipcSemaphoreOp operation)
  44.  
  45. {
  46.    // init instance variables
  47.    myInterface = interface;
  48.    myName = 0;
  49.    myID = 0L;
  50.    myType = type;
  51.    if (operation == semcreate)
  52.       isOwner = 1;
  53.    else
  54.       isOwner = 0;
  55.  
  56.    // check that input name is valid
  57.    if (!name || (strlen(name) == 0)) {
  58.       myInterface->myState = ipcSemaphore::badname;
  59.       return;
  60.    }
  61.  
  62.    // allocate space for the name
  63.    myName = new char[strlen(name) + 1];
  64.    if (!myName) {
  65.       myInterface->myState = ipcSemaphore::bad;
  66.       return;
  67.    }
  68.  
  69.    // and form the complete semaphore pathname
  70.    strcpy(myName, name);
  71.  
  72.    // set the object state and return
  73.    myInterface->myState = ipcSemaphore::good;
  74. }
  75.  
  76. // ----------------------------------------------------------------------------
  77.  
  78. // ~osSemaphore - destructor
  79.  
  80. osSemaphore::~osSemaphore()
  81.  
  82. {
  83.    delete myName;
  84. }
  85.  
  86. // ----------------------------------------------------------------------------
  87.  
  88. // Name - returns the name of the semaphore
  89.  
  90. char *osSemaphore::Name() const
  91.  
  92. {
  93.    return myName;
  94. }
  95.  
  96. // ----------------------------------------------------------------------------
  97.  
  98. // ID - returns the semaphore id
  99.  
  100. unsigned long osSemaphore::ID() const
  101.  
  102. {
  103.    return myID;
  104. }
  105.  
  106. // ----------------------------------------------------------------------------
  107.  
  108. // Type - returns the type of semaphore
  109.  
  110. ipcSemaphoreType osSemaphore::Type() const
  111.  
  112. {
  113.    return myType;
  114. }
  115.  
  116. // ----------------------------------------------------------------------------
  117.  
  118. // Owner - returns 1 if this is the owner (creator), and 0 otherwise
  119.  
  120. int osSemaphore::Owner() const
  121.  
  122. {
  123.    return isOwner;
  124. }
  125.  
  126. // ----------------------------------------------------------------------------
  127.  
  128. // CreateMutex - creates a mutex semaphore
  129.  
  130. void osSemaphore::CreateMutex()
  131.  
  132. {
  133.    HANDLE hsem = ::CreateMutexA(0, FALSE, (LPCTSTR)myName);
  134.    if (!hsem)
  135.       myInterface->myState = ipcSemaphore::bad;
  136.    else
  137.       myID = (unsigned long)hsem;
  138. }
  139.  
  140. // ----------------------------------------------------------------------------
  141.  
  142. // OpenMutex - opens a mutex semaphore
  143.  
  144. void osSemaphore::OpenMutex()
  145.  
  146. {
  147.    HANDLE hsem = ::OpenMutexA(MUTEX_ALL_ACCESS, FALSE, (LPCTSTR)myName);
  148.    if (!hsem)
  149.       myInterface->myState = ipcSemaphore::notfound;
  150.    else
  151.       myID = (unsigned long)hsem;
  152. }
  153.  
  154. // ----------------------------------------------------------------------------
  155.  
  156. // RequestMutex - requests a mutex semaphore
  157.  
  158. void osSemaphore::RequestMutex()
  159.  
  160. {
  161.    ::WaitForSingleObject( (HANDLE)myID, INFINITE );
  162. }
  163.  
  164. // ----------------------------------------------------------------------------
  165.  
  166. // ReleaseMutex - releases a mutex semaphore
  167.  
  168. void osSemaphore::ReleaseMutex()
  169.  
  170. {
  171.    ::ReleaseMutex( (HANDLE)myID );
  172. }
  173.  
  174. // ----------------------------------------------------------------------------
  175.  
  176. // QueryMutex - queries a mutex semaphore
  177.  
  178. unsigned long osSemaphore::QueryMutex()
  179.  
  180. {
  181.    switch (::WaitForSingleObject( (HANDLE)myID, 0 )) {
  182.       case WAIT_OBJECT_0:
  183.          return 1L;
  184.  
  185.       case WAIT_ABANDONED:
  186.          return 1L;
  187.  
  188.       case WAIT_TIMEOUT:
  189.          return 0L;
  190.  
  191.       default:
  192.          return 0L;
  193.    }
  194. }
  195.  
  196. // ----------------------------------------------------------------------------
  197.  
  198. // CloseMutex - closes a mutex semaphore
  199.  
  200. void osSemaphore::CloseMutex()
  201.  
  202. {
  203.    ::CloseHandle( (HANDLE)myID );
  204. }
  205.  
  206. // ----------------------------------------------------------------------------
  207.  
  208. // CreateEvent - creates an event semaphore
  209.  
  210. void osSemaphore::CreateEvent()
  211.  
  212. {
  213.    HANDLE hsem = ::CreateEventA(0, FALSE, FALSE, (LPCTSTR)myName);
  214.    if (!hsem)
  215.       myInterface->myState = ipcSemaphore::bad;
  216.    else
  217.       myID = (unsigned long)hsem;
  218. }
  219.  
  220. // ----------------------------------------------------------------------------
  221.  
  222. // OpenEvent - opens an event semaphore
  223.  
  224. void osSemaphore::OpenEvent()
  225.  
  226. {
  227.    HANDLE hsem = ::OpenEventA(EVENT_ALL_ACCESS, FALSE, (LPCTSTR)myName);
  228.    if (!hsem)
  229.       myInterface->myState = ipcSemaphore::notfound;
  230.    else
  231.       myID = (unsigned long)hsem;
  232. }
  233.  
  234. // ----------------------------------------------------------------------------
  235.  
  236. // PostEvent - posts an event semaphore
  237.  
  238. void osSemaphore::PostEvent()
  239.  
  240. {
  241.    ::SetEvent( (HANDLE)myID );
  242. }
  243.  
  244. // ----------------------------------------------------------------------------
  245.  
  246. // ResetEvent - resets an event semaphore
  247.  
  248. void osSemaphore::ResetEvent()
  249.  
  250. {
  251.    ::ResetEvent( (HANDLE)myID );
  252. }
  253.  
  254. // ----------------------------------------------------------------------------
  255.  
  256. // WaitEvent - waits on an event semaphore
  257.  
  258. void osSemaphore::WaitEvent()
  259.  
  260. {
  261.    ::WaitForSingleObject( (HANDLE)myID, INFINITE );
  262. }
  263.  
  264. // ----------------------------------------------------------------------------
  265.  
  266. // QueryEvent - queries an event semaphore
  267.  
  268. unsigned long osSemaphore::QueryEvent()
  269.  
  270. {
  271.    switch (::WaitForSingleObject( (HANDLE)myID, 0 )) {
  272.       case WAIT_OBJECT_0:
  273.          return 1L;
  274.  
  275.       case WAIT_ABANDONED:
  276.          return 1L;
  277.  
  278.       case WAIT_TIMEOUT:
  279.          return 0L;
  280.  
  281.       default:
  282.          return 0L;
  283.    }
  284. }
  285.  
  286. // ----------------------------------------------------------------------------
  287.  
  288. // CloseEvent - closes an event semaphore
  289.  
  290. void osSemaphore::CloseEvent()
  291.  
  292. {
  293.    ::CloseHandle( (HANDLE)myID );
  294. }
  295.  
  296. // ****************************************************************************
  297.  
  298. // end of os2sem.C
  299.