home *** CD-ROM | disk | FTP | other *** search
/ Chip 1997 October / Chip_1997-10_cd.bin / tema / sybase / powerj / hpp.z / WSEMAPHR.HPP < prev    next >
C/C++ Source or Header  |  1996-10-18  |  15KB  |  505 lines

  1. /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2.    %     Copyright (C) 1994, by WATCOM International Inc.  All rights    %
  3.    %     reserved.  No part of this software may be reproduced or        %
  4.    %     used in any form or by any means - graphic, electronic or       %
  5.    %     mechanical, including photocopying, recording, taping or        %
  6.    %     information storage and retrieval systems - except with the     %
  7.    %     written permission of WATCOM International Inc.                 %
  8.    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  9. */
  10.  
  11. #ifndef _WSEMAPHR_HPP_INCLUDED
  12. #define _WSEMAPHR_HPP_INCLUDED
  13.  
  14. #define _WSEMAPHORE_HPP_INCLUDED
  15.  
  16. #ifndef _WNO_PRAGMA_PUSH
  17. #pragma pack(push,8);
  18. #pragma enum int;
  19. #endif
  20.  
  21. //
  22. // Notes on semaphore usage:
  23. //
  24. // The easy way to do mutual exclusion inside a function is as follows:
  25. //
  26. //   1) Declare a semaphore of the appropriate type as a static or
  27. //      global.
  28. //   2) Declare a WSemaphoreLock as an auto, pointing it to the
  29. //      semaphore.
  30. //
  31. //    void myclass::myfunc()
  32. //      {
  33. //        static WCriticalSection critSect;
  34. //        WSemaphoreLock lock( &critSect );
  35. //
  36. //        .....
  37. //      }
  38. //
  39. // Then each time you enter the function, the semaphore is locked
  40. // and then automatically unlocked when you leave.
  41. //
  42. // Alternatively, you can do this:
  43. //
  44. //     static WCriticalSection critSect;
  45. //
  46. //     critSect.Enter();
  47. //
  48. //     critSect.Exit();
  49. //
  50. // But always declare your critical sections as static variables!
  51. //
  52.  
  53. #ifndef _WDEF_HPP_INCLUDED
  54. #  include "wdef.hpp"
  55. #endif
  56. #ifndef _WOBJECT_HPP_INCLUDED
  57. #  include "wobject.hpp"
  58. #endif
  59.  
  60. enum WSemaphoreHandle { NULLHSEMAPHORE = 0, LASTHSEMAPHORE = LAST_16TO32BIT };
  61.  
  62. //
  63. // WBaseSemaphore
  64. //
  65. //     The abstract base class for all the semaphores.
  66. //
  67.  
  68. class WCMCLASS WBaseSemaphore : public WObject {
  69.     WDeclareSubclass( WBaseSemaphore, WObject );
  70.  
  71.     friend class WSemaphoreLock;
  72.  
  73.         /***********************************************************
  74.          * Constructors and Destructors
  75.          ***********************************************************/
  76.  
  77.     protected:
  78.  
  79.         WBaseSemaphore( WSemaphoreHandle handle=NULLHSEMAPHORE,
  80.                         WBool canDelete=TRUE )
  81.             { _handle = handle; _canDelete = canDelete; }
  82.  
  83.     public:
  84.  
  85.         virtual ~WBaseSemaphore() {}
  86.  
  87.     protected:
  88.  
  89.         /***********************************************************
  90.          * Properties
  91.          ***********************************************************/
  92.  
  93.     public:
  94.  
  95.         // Handle
  96.         //
  97.         //     Returns the system handle associated with the
  98.         //     semaphore.
  99.  
  100.         WSemaphoreHandle GetHandle() const { return _handle; }
  101.  
  102.         /***********************************************************
  103.          * Others
  104.          ***********************************************************/
  105.  
  106.         // WSemaphoreLock calls these to acquire/release a lock.
  107.  
  108.         // Lock
  109.  
  110.         virtual WBool Lock( WULong timeout=0xFFFFFFFF ) = 0;
  111.  
  112.         // Unlock
  113.  
  114.         virtual WBool Unlock() = 0;
  115.  
  116.         /***********************************************************
  117.          * Data Members
  118.          ***********************************************************/
  119.  
  120.     protected:
  121.  
  122.         WBool            _canDelete;
  123.         WSemaphoreHandle _handle;
  124.  
  125. };
  126.  
  127. //
  128. // WCriticalSection
  129. //
  130. //     Defines a critical section.
  131. //
  132.  
  133. class WCMCLASS WCriticalSection : public WBaseSemaphore {
  134.     WDeclareSubclass( WCriticalSection, WBaseSemaphore );
  135.  
  136.     public:
  137.  
  138.         /***********************************************************
  139.          * Constructors and Destructors
  140.          ***********************************************************/
  141.  
  142.         WCriticalSection( WBool allowTimeouts=FALSE );
  143.  
  144.         virtual ~WCriticalSection();
  145.  
  146.         /***********************************************************
  147.          * Properties
  148.          ***********************************************************/
  149.  
  150.         /***********************************************************
  151.          * Methods
  152.          ***********************************************************/
  153.  
  154.         // Enter
  155.  
  156.         WBool Enter( WULong timeout=0xFFFFFFFF ) { return Lock( timeout ); }
  157.  
  158.         // Exit
  159.  
  160.         WBool Exit() { return Unlock(); }
  161.  
  162.         /***********************************************************
  163.          * Overrides
  164.          ***********************************************************/
  165.  
  166.     protected:
  167.  
  168.         virtual WBool Lock( WULong timeout=0xFFFFFFFF );
  169.  
  170.         virtual WBool Unlock();
  171.  
  172.         /***********************************************************
  173.          * Static
  174.          ***********************************************************/
  175.  
  176.     public:
  177.  
  178.         static WCriticalSection & GetDefaultCriticalSection();
  179.  
  180.         /***********************************************************
  181.          * Data
  182.          ***********************************************************/
  183.  
  184.     private:
  185.  
  186.         WBool _allowTimeouts;
  187. };
  188.  
  189. //
  190. // WMutexSemaphore
  191. //
  192. //     Defines a mutual-exclusion semaphore.
  193. //
  194.  
  195. class WCMCLASS WMutexSemaphore : public WBaseSemaphore {
  196.     WDeclareSubclass( WMutexSemaphore, WBaseSemaphore );
  197.  
  198.     public:
  199.  
  200.         /***********************************************************
  201.          * Constructors and Destructors
  202.          ***********************************************************/
  203.  
  204.         WMutexSemaphore();
  205.  
  206.         WMutexSemaphore( const WChar *name, WBool own=FALSE );
  207.  
  208.         WMutexSemaphore( WSemaphoreHandle handle, WBool canDelete=FALSE );
  209.  
  210.         virtual ~WMutexSemaphore();
  211.  
  212.         /***********************************************************
  213.          * Properties
  214.          ***********************************************************/
  215.  
  216.         /***********************************************************
  217.          * Methods
  218.          ***********************************************************/
  219.  
  220.         // Acquire
  221.         //
  222.         //     Wait for a semaphore to be acquired/signalled, with
  223.         //     a timeout in milliseconds.  By default the timeout
  224.         //     is infinite.
  225.  
  226.         WBool Acquire( WULong timeout=0xFFFFFFFF );
  227.  
  228.         // Close
  229.         //
  230.         //     Closes the semaphore.
  231.  
  232.         WBool Close();
  233.  
  234.         // Create
  235.         //
  236.         //     Creates a new mutex semaphore of the given name.  If
  237.         //     the sem already exists, simply returns a handle to
  238.         //     it and "own" is ignored, otherwise if "own" is true
  239.         //     then the semaphore is initially owned by this thread.
  240.  
  241.         WBool Create( const WChar *name, WBool own=FALSE );
  242.  
  243.         // Open
  244.         //
  245.         //     Open a semaphore that already exists. 
  246.  
  247.         WBool Open( const WChar *name );
  248.  
  249.         // Release
  250.         //
  251.         //     Release a semaphore after acquiring it.
  252.         //
  253.  
  254.         WBool Release();
  255.  
  256.         /***********************************************************
  257.          * Overrides
  258.          ***********************************************************/
  259.  
  260.     protected:
  261.  
  262.         virtual WBool Lock( WULong t=0xFFFFFFFF ) { return Acquire( t ); }
  263.         virtual WBool Unlock() { return Release(); }
  264. };
  265.  
  266. //
  267. // WCountSemaphore
  268. //
  269. //     Defines a counting semaphore.
  270. //
  271.  
  272. class WCMCLASS WCountSemaphore : public WBaseSemaphore {
  273.     WDeclareSubclass( WCountSemaphore, WBaseSemaphore );
  274.  
  275.     public:
  276.  
  277.         /***********************************************************
  278.          * Constructors and Destructors
  279.          ***********************************************************/
  280.  
  281.         WCountSemaphore();
  282.  
  283.         WCountSemaphore( const WChar * name, WLong initialCount=1,
  284.                          WLong maximumCount=1 );
  285.  
  286.         virtual ~WCountSemaphore();
  287.  
  288.         /***********************************************************
  289.          * Properties
  290.          ***********************************************************/
  291.  
  292.         /***********************************************************
  293.          * Methods
  294.          ***********************************************************/
  295.  
  296.         // Acquire
  297.         //
  298.         //     Wait for a semaphore to be acquired/signalled, with
  299.         //     a timeout in milliseconds.  By default the timeout
  300.         //     is infinite.
  301.  
  302.         WBool Acquire( WULong timeout=0xFFFFFFFF );
  303.  
  304.         // Close
  305.         //
  306.         //     Closes the semaphore.
  307.  
  308.         WBool Close();
  309.  
  310.         // Create
  311.         //
  312.         //     Creates a new count semaphore of the given name.  If
  313.         //     the sem already exists, simply returns a handle to
  314.         //     it and "initialCount" and "maximumCount" are ignored,
  315.         //     otherwise "initialCount" specifies an initial count for
  316.         //     the WCountSemaphore object and "maximumCount" specifies
  317.         //     the maximum count for the WCountSemaphore object.
  318.         //     "maximumCount" must be greater than zero and "initialCount"
  319.         //     must be greater than or equal to zero and less than or equal
  320.         //     to "maximumCount".
  321.  
  322.         WBool Create( const WChar * name, WLong initialCount=1,
  323.                       WLong maximumCount=1 );
  324.  
  325.         // Open
  326.         //
  327.         //     Open a semaphore that already exists. 
  328.  
  329.         WBool Open( const WChar *name );
  330.  
  331.         // Release
  332.         //
  333.         //     Release a semaphore after acquiring it.
  334.         //
  335.  
  336.         WBool Release();
  337.  
  338.         // Wait
  339.         //
  340.         //     Wait on an count semaphore.  Can specify a timeout in
  341.         //     milliseconds, which is set to infinite by default.
  342.  
  343.         WBool Wait( WULong timeout=0xFFFFFFFF );
  344.  
  345.         /***********************************************************
  346.          * Overrides
  347.          ***********************************************************/
  348.  
  349.     protected:
  350.  
  351.         virtual WBool Lock( WULong timeout=0xFFFFFFFF );
  352.         virtual WBool Unlock();
  353. };
  354.  
  355. //
  356. // WEventSemaphore
  357. //
  358. //     Defines an event semaphore.
  359. //
  360.  
  361. class WCMCLASS WEventSemaphore : public WBaseSemaphore {
  362.     WDeclareSubclass( WEventSemaphore, WBaseSemaphore );
  363.  
  364.     public:
  365.  
  366.         /***********************************************************
  367.          * Constructors and Destructors
  368.          ***********************************************************/
  369.  
  370.         WEventSemaphore();
  371.  
  372.         WEventSemaphore( const WChar *name, WBool initialReset=TRUE,
  373.                          WBool manualReset=TRUE );
  374.  
  375.         WEventSemaphore( WSemaphoreHandle handle, WBool canDelete=FALSE );
  376.  
  377.         virtual ~WEventSemaphore();
  378.  
  379.         /***********************************************************
  380.          * Properties
  381.          ***********************************************************/
  382.  
  383.         /***********************************************************
  384.          * Methods
  385.          ***********************************************************/
  386.  
  387.         // Close
  388.         //
  389.         //     Closes the semaphore.
  390.  
  391.         WBool Close();
  392.  
  393.         // Create
  394.         //
  395.         //     Creates a new event semaphore of the given name.  If
  396.         //     the sem already exists, simply returns a handle to
  397.         //     it and the other parms are ignored.  The initiallyReset
  398.         //     parm controls the initial state.  If manualReset is
  399.         //     TRUE, then the semaphore must be explicitly reset after
  400.         //     it is triggered, otherwise a single thread is allowed
  401.         //     to proceed before the semaphore resets itself.
  402.  
  403.         WBool Create( const WChar *name, WBool initiallyReset=TRUE,
  404.                       WBool manualReset=TRUE );
  405.  
  406.         // Open
  407.         //
  408.         //     Open a semaphore that already exists. 
  409.  
  410.         WBool Open( const WChar *name );
  411.  
  412.         // Pulse
  413.         //
  414.         //     "Pulses" the semaphore:  sets it to release any
  415.         //     waiting threads (or a single thread if autoreset)
  416.         //     and then resets it.
  417.  
  418.         WBool Pulse();
  419.  
  420.         // Reset
  421.         //
  422.         //     Sets the semaphore to the "reset" state, which means
  423.         //     any thread that waits on it will block.
  424.  
  425.         WBool Reset();
  426.  
  427.         // Set
  428.         //
  429.         //     Sets the semaphore to the "set" state, which releases
  430.         //     any waiting threads.
  431.  
  432.         WBool Set();
  433.  
  434.         // Wait
  435.         //
  436.         //     Wait on an event semaphore.  Can specify a timeout in
  437.         //     milliseconds, which is set to infinite by default.
  438.         //     If blockMessageProcessing is TRUE, then no messages will be
  439.         //     responded to by this thread until the call to Wait() finishes.
  440.  
  441.         WBool Wait( WULong timeout=0xFFFFFFFF,
  442.                     WBool blockMessageProcessing=TRUE );
  443.  
  444.         /***********************************************************
  445.          * Overrides
  446.          ***********************************************************/
  447.  
  448.     protected:
  449.  
  450.         virtual WBool Lock( WULong t=0xFFFFFFFF );
  451.         virtual WBool Unlock();
  452. };
  453.  
  454. //
  455. // WSemaphoreLock
  456. //
  457. //     A convenience class which encapsulates waiting for a
  458. //     semaphore to be acquired and then releasing it on destruction.
  459. //
  460.  
  461. class WCMCLASS WSemaphoreLock : public WObject {
  462.     WDeclareSubclass( WSemaphoreLock, WObject );
  463.  
  464.     public:
  465.  
  466.         /***********************************************************
  467.          * Constructors and Destructors
  468.          ***********************************************************/
  469.  
  470.         WSemaphoreLock( WBaseSemaphore *semaphore );
  471.  
  472.         WSemaphoreLock( WBaseSemaphore & semaphore );
  473.  
  474.         virtual ~WSemaphoreLock();
  475.  
  476.         /***********************************************************
  477.          * Properties
  478.          ***********************************************************/
  479.  
  480.         /***********************************************************
  481.          * Methods
  482.          ***********************************************************/
  483.  
  484.         // Unlock
  485.         //
  486.         //    Prematurely release the semaphore.
  487.  
  488.         WBool Unlock();
  489.  
  490.         /***********************************************************
  491.          * Data Members
  492.          ***********************************************************/
  493.  
  494.     private:
  495.  
  496.         WBaseSemaphore *_semaphore;
  497. };
  498.  
  499. #ifndef _WNO_PRAGMA_PUSH
  500. #pragma enum pop;
  501. #pragma pack(pop);
  502. #endif
  503.  
  504. #endif // _WSEMAPHORE_HPP_INCLUDED
  505.