home *** CD-ROM | disk | FTP | other *** search
/ Power GUI Programming with VisualAge C++ / powergui.iso / powergui / thread / signal / signal.cpp < prev    next >
C/C++ Source or Header  |  1996-10-29  |  8KB  |  218 lines

  1. /***************************************************************
  2. * FILE NAME: signal.cpp                                        *
  3. *                                                              *
  4. * DESCRIPTION:                                                 *
  5. *   This file contains the implementation of                   *
  6. *   classes/functions declared in signal.hpp.                  *
  7. *                                                              *
  8. * COPYRIGHT:                                                   *
  9. *   Licensed Materials - Property of Solution Frameworks       *
  10. *   Copyright (C) 1996, Solution Frameworks                    *
  11. *   All Rights Reserved                                        *
  12. ***************************************************************/
  13. #ifdef __OS2__
  14. #define INCL_DOSSEMAPHORES
  15. #define INCL_DOSERRORS
  16. #include <os2.h>
  17.  
  18. #include <istring.hpp>
  19. #else
  20. #include <windows.h>
  21. #endif
  22.  
  23. #include "signal.hpp"
  24.  
  25. #ifndef _IEXCEPT_
  26.   #include <iexcept.hpp>
  27. #endif
  28.  
  29. /*---------------------- Signal::Signal ------------------------
  30. | The construcstors initialize the handle to zero and invoke   |
  31. | the initialize member function (see below).                  |
  32. --------------------------------------------------------------*/
  33. Signal :: Signal ( Boolean     signalled,
  34.                    const char *name ) 
  35.   : handle( 0 ) {
  36.     initialize( signalled, name );
  37.   }
  38.  
  39. Signal :: Signal ( const char *name, 
  40.                    Boolean     signalled )
  41.   : handle( 0 ) {
  42.     initialize( signalled, name );
  43.   }
  44.  
  45. /*--------------------- Signal::~Signal ------------------------
  46. | Under OS/2:                                                  |
  47. |   Close the event semaphore using DosCloseEventSem.          |
  48. | Under Windows:                                               |
  49. |   Close the event semaphore using CloseHandle.               |
  50. --------------------------------------------------------------*/
  51. Signal :: ~Signal ( ) {
  52.   #ifdef __OS2__
  53.     unsigned long
  54.       rc = DosCloseEventSem( handle );
  55.     if ( rc != 0 )
  56.       ITHROWSYSTEMERROR( rc, 
  57.                          "DosCloseEventSem",
  58.                          IErrorInfo::accessError,
  59.                          IException::recoverable );
  60.   #else
  61.     if ( !CloseHandle( handle ) )
  62.       ITHROWSYSTEMERROR( 1,
  63.                          "CloseHandle",
  64.                          IBaseErrorInfo::accessError,
  65.                          IException::recoverable );
  66.   #endif
  67.   }
  68.  
  69. /*---------------------- Signal::reset -------------------------
  70. | Under OS/2:                                                  |                
  71. |   Reset the event semaphore using DosResetEventSem.          |
  72. | Under Windows:                                               |                
  73. |   Reset the event semaphore using ResetEvent.                |
  74. --------------------------------------------------------------*/
  75. Signal &Signal :: reset ( ) {
  76.   #ifdef __OS2__
  77.     unsigned long
  78.       ct,
  79.       rc = DosResetEventSem( handle, &ct );
  80.     if ( rc != 0 )
  81.       ITHROWSYSTEMERROR( rc,
  82.                          "DosResetEventSem",
  83.                          IErrorInfo::accessError,
  84.                          IException::recoverable );
  85.   #else
  86.     if ( !ResetEvent( handle ) )
  87.       ITHROWSYSTEMERROR( 1,
  88.                          "ResetEvent",
  89.                          IBaseErrorInfo::accessError,
  90.                          IException::recoverable );
  91.   #endif
  92.  
  93.   return *this;
  94. }
  95.  
  96. /*---------------------- Signal::signal ------------------------
  97. | Under OS/2:                                                  |
  98. |   Post the event semaphore via DosPostEventSem.              |
  99. | Under Windows:                                               |
  100. |   Post the event semaphore via SetEvent.                     |
  101. --------------------------------------------------------------*/
  102. Signal &Signal :: signal ( )
  103.   {
  104.   #ifdef __OS2__
  105.     unsigned long
  106.       rc = DosPostEventSem( handle );
  107.     if ( rc != 0 )
  108.       ITHROWSYSTEMERROR( rc, 
  109.                          "DosPostEventSem",
  110.                          IErrorInfo::accessError,
  111.                          IException::recoverable );
  112.   #else
  113.     if ( !SetEvent( handle ) )
  114.       ITHROWSYSTEMERROR( 1,
  115.                          "SetEvent",
  116.                          IBaseErrorInfo::accessError,
  117.                          IException::recoverable );
  118.   #endif
  119.  
  120.   return *this;
  121.   }
  122.  
  123. /*----------------------- Signal::wait -------------------------
  124. | Under OS/2:                                                  |
  125. |   Wait for the event semaphore using DosWaitEventSem.        |
  126. | Under Windows:                                               |
  127. |   Wait for the event semaphore using WaitForSinglObject.     |
  128. --------------------------------------------------------------*/
  129. Signal &Signal :: wait ( unsigned long timeout )
  130.   {
  131.   #ifdef __OS2__
  132.     unsigned long
  133.       rc = DosWaitEventSem( handle, timeout );
  134.     if ( rc != 0 )
  135.       ITHROWSYSTEMERROR( rc, 
  136.                          "DosWaitEventSem",
  137.                          IErrorInfo::accessError,
  138.                          IException::recoverable );
  139.   #else
  140.     int
  141.       rc = WaitForSingleObject( handle, timeout );
  142.     if ( rc != WAIT_OBJECT_0 )
  143.       ITHROWSYSTEMERROR( rc,
  144.                          "WaitForSingleObject",
  145.                          IBaseErrorInfo::accessError,
  146.                          IException::recoverable );
  147.   #endif
  148.  
  149.   return *this;
  150.   }
  151.  
  152. /*-------------------- Signal::initialize ----------------------
  153. | Initialize the event sempaphore object.                      |
  154. |                                                              |
  155. | Under OS/2:                                                  |
  156. |   If a name is provided and it doesn't have the \SEM32\      |
  157. |   prefix, insert the prefix.  Then create the event          |
  158. |   semaphore using DosCreateEventSem.  If the creation        |
  159. |   fails because it already existed, then open it using       |
  160. |   DosOpenEventSem.                                           |
  161. | Under Windows:                                               |
  162. |   Just create the event semaphore using CreateEvent.         |
  163. --------------------------------------------------------------*/
  164. void Signal :: initialize ( Boolean initState, const char *name ) {
  165.   Boolean
  166.     shared = ( name != 0 );
  167.   #ifdef __OS2__
  168.     HEV
  169.       hev = 0;
  170.     IString
  171.       sigName = name;
  172.     if ( name && !IString( "\\SEM32\\" ).isAbbreviationFor( sigName ) ) {
  173.       // Make sure name has proper prefix.
  174.       sigName.insert( "\\SEM32\\", 0 );
  175.     }
  176.     unsigned long
  177.       rc = DosCreateEventSem( name ? (const char*)sigName 
  178.                                    : ( const char*)0,
  179.                               &hev,
  180.                               shared ? DC_SEM_SHARED : 0,
  181.                               initState ? 1 : 0 );
  182.     if ( rc == NO_ERROR ) {
  183.       handle = hev;
  184.     } else {
  185.       if ( rc == ERROR_DUPLICATE_NAME ) {
  186.         // Try to open it, then.
  187.         rc = DosOpenEventSem( sigName, &hev );
  188.  
  189.         if ( rc == NO_ERROR ) {
  190.           handle = hev;
  191.         } else {
  192.           ITHROWSYSTEMERROR( rc,
  193.                              "DosOpenEventSem",
  194.                              IErrorInfo::accessError,
  195.                              IException::recoverable );
  196.         }
  197.       } else {
  198.         ITHROWSYSTEMERROR( rc,
  199.                            "DosCreateEventSem",
  200.                            IErrorInfo::accessError,
  201.                            IException::recoverable );
  202.       }
  203.     }
  204.   #else
  205.     HANDLE
  206.       hev = 0;
  207.     hev = CreateEvent( 0, TRUE, FALSE, name );
  208.     if ( hev ) {
  209.       handle = hev;
  210.     } else {
  211.       ITHROWSYSTEMERROR( 0,
  212.                          "CreateEvent",
  213.                          IBaseErrorInfo::accessError,
  214.                          IException::recoverable );
  215.     }
  216.   #endif
  217. }
  218.