home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / vsoup128.zip / sema.hh < prev    next >
Text File  |  1997-01-20  |  2KB  |  77 lines

  1. //  $Id: sema.hh 1.10 1997/01/20 16:30:10 hardy Exp $
  2. //
  3. //  This progam/module was written by Hardy Griech based on ideas and
  4. //  pieces of code from Chin Huang (cthuang@io.org).  Bug reports should
  5. //  be submitted to rgriech@ibm.net.
  6. //
  7. //  This file is part of soup++ for OS/2.  Soup++ including this file
  8. //  is freeware.  There is no warranty of any kind implied.  The terms
  9. //  of the GNU Gernal Public Licence are valid for this piece of software.
  10. //
  11.  
  12.  
  13. #ifndef __SEMA_HH__
  14. #define __SEMA_HH__
  15.  
  16.  
  17. #if defined(OS2)  &&  defined(__MT__)
  18.  
  19. #define INCL_DOSSEMAPHORES
  20. #define INCL_DOSPROCESS
  21. #define INCL_DOSERRORS
  22. #include <os2.h>
  23.  
  24. class TSemaphor {
  25. private:
  26.     HMTX Sema;
  27. public:
  28.     TSemaphor( void )    { DosCreateMutexSem( (PSZ)NULL, &Sema, 0, FALSE ); }
  29.     ~TSemaphor()         { DosCloseMutexSem( Sema ); }
  30.     int  Request( long timeMs = SEM_INDEFINITE_WAIT ) { return (DosRequestMutexSem(Sema,timeMs) == NO_ERROR); }
  31.     void Release( void ) { DosReleaseMutexSem( Sema ); }
  32. };
  33.  
  34.  
  35. class TEvSemaphor {
  36. private:
  37.     HEV Sema;
  38. public:
  39.     TEvSemaphor( void )                            { DosCreateEventSem( (PSZ)NULL, &Sema, 0, FALSE ); }
  40.     ~TEvSemaphor()                                 { DosCloseEventSem( Sema ); }
  41.     void Post( void )                              { DosPostEventSem( Sema ); }
  42.     void Wait( long timeMs = SEM_INDEFINITE_WAIT,
  43.            int resetSema = 1 )                 { if (DosWaitEventSem(Sema,timeMs) == 0) { if (resetSema) Reset();} }
  44.     void Reset( void )                             { unsigned long ul;  DosResetEventSem( Sema,&ul ); }
  45. };
  46.  
  47. #define blockThread()    DosEnterCritSec()
  48. #define unblockThread()  DosExitCritSec()
  49.  
  50. #else
  51.  
  52. class TSemaphor {
  53. public:
  54.     TSemaphor( void )    {}
  55.     ~TSemaphor()         {}
  56.     int  Request( long timeMs=0 ) { timeMs = 0; return 1; }
  57.     void Release( void ) {}
  58. };
  59.  
  60.  
  61. class TEvSemaphor {
  62. public:
  63.     TEvSemaphor( void )      {}
  64.     ~TEvSemaphor()           {}
  65.     void Post( void )        {}
  66.     void Wait( long timeMs=0 ) { timeMs = 0; }
  67.     void Reset( void )       {}
  68. };
  69.  
  70. #define blockThread()
  71. #define unblockThread()
  72.  
  73. #endif
  74.  
  75.  
  76. #endif  // __SEMA_HH__
  77.