home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ool_main.zip / ool / source / xesem.cpp < prev    next >
C/C++ Source or Header  |  1998-04-06  |  3KB  |  125 lines

  1. #include "xesem.h"
  2. #include "xsemex.h"
  3.  
  4. /*@
  5. @class XSemaphore
  6. @type overview
  7. @symbol _
  8. */
  9.  
  10. /*@
  11. @class XEventSemaphore
  12. @parent XSemaphore
  13. @type overview
  14. @symbol _
  15. */
  16.  
  17. /*@ XEventSemaphore :: XEventSemaphore(char * name, BOOL shared, BOOL isSet)
  18. @group constructors/destructors
  19. @remarks   Create an event-semaphore
  20. @parameters
  21. <t '°' c=2>
  22. °char * name °the name of the semaphore to create WITHOUT leading '\\SEM32\\'
  23. °BOOL shared °TRUE=shared semaphore<BR>FALSE=no shared semaphore
  24. °BOOL isSet  °TRUE=state is posted<BR>FALSE=state is not posted<BR>(default is FALSE)
  25. </t>
  26. @exceptions   If the method fails an exception of the type XSemaphoreException is thrown
  27. */
  28. XEventSemaphore :: XEventSemaphore(const char * name, const BOOL shared, const BOOL isSet)
  29. {
  30.    XString n = "\\SEM32\\";
  31.    n += name;
  32.    hev = 0;
  33.    ULONG attr = 0;
  34.    if( shared )
  35.       attr = DC_SEM_SHARED;
  36.    ULONG rc = DosCreateEventSem( (PSZ) (char*) n, &hev, attr, isSet);
  37.    if(rc)
  38.       OOLSemThrow( "cannot create XEventSemaphore", rc);
  39. }
  40.  
  41.  
  42. /*@ XEventSemaphore :: Reset()
  43. @group misc
  44. @remarks   resets an event-semaphore
  45. @returns ULONG count of registered clients of the semaphore
  46. @exceptions   If the method fails an exception of the type XSemaphoreException is thrown
  47. */
  48. ULONG XEventSemaphore :: Reset()
  49. {
  50.    ULONG count;
  51.    ULONG rc = DosResetEventSem( hev, &count);
  52.    if(rc)
  53.       OOLSemThrow( "cannot reset XEventSemaphore", rc);
  54.    return count;
  55. }
  56.  
  57. /*@ XEventSemaphore :: Close()
  58. @group misc
  59. @remarks   close an event-semaphore
  60. @exceptions   If the method fails an exception of the type XSemaphoreException is thrown
  61. */
  62. void XEventSemaphore :: Close()
  63. {
  64.    ULONG rc = DosCloseEventSem(hev);
  65.    if(rc)
  66.       OOLSemThrow( "cannot close XEventSemaphore", rc);
  67.    hev = 0;
  68. }
  69.  
  70.  
  71. /*@ XEventSemaphore :: XEventSemaphore(char * name)
  72. @group constructors/destructors
  73. @remarks   open an event-semaphore which was created by another process
  74. @parameters
  75. char * name   the name of the semaphore to open WITHOUT leading '\\SEM32\\'
  76. @exceptions   If the method fails an exception of the type XSemaphoreException is thrown
  77. */
  78. XEventSemaphore :: XEventSemaphore(const char * name)
  79. {
  80.    XString n = "\\SEM32\\";
  81.    n += name;
  82.    hev = 0;
  83.    ULONG rc = DosOpenEventSem( (PSZ) (char*) n, &hev);
  84.    if(rc)
  85.       OOLSemThrow( "cannot open XEventSemaphore", rc);
  86. }
  87.  
  88.  
  89. /*@ XEventSemaphore :: Post()
  90. @group misc
  91. @remarks   Post an event. All semaphores which have opened this semaphore and are waiting gets post means
  92. they are unlocked.
  93. @parameters
  94. @exceptions   If the method fails an exception of the type XSemaphoreException is thrown
  95. */
  96. void XEventSemaphore :: Post()
  97. {
  98.    ULONG rc = DosPostEventSem( hev );
  99.    if(rc)
  100.       OOLSemThrow( "failed to post a XEventSemaphore", rc);
  101. }
  102.  
  103.  
  104. /*@ XEventSemaphore :: Wait( LONG timeOut)
  105. @group misc
  106. @remarks   Wait for a semaphore until it post or until timeout.
  107. @parameters   <t '°' c=2>
  108.    °LONG timeout   °-1=wait endless<BR> 0 = return at once<BR> other=time to wait max. (in milliseconds)
  109.    </t>
  110. @exceptions   If the method fails an exception of the type XSemaphoreException is thrown
  111. */
  112. void XEventSemaphore :: Wait( const LONG timeOut)
  113. {
  114.    ULONG rc = DosWaitEventSem( hev, timeOut);
  115.    if(rc)
  116.       OOLSemThrow( "failed to wait for XEventSemaphore", rc);
  117. }
  118.  
  119.  
  120. XEventSemaphore :: ~XEventSemaphore()
  121. {
  122.    if(hev)
  123.       Close();
  124. }
  125.