home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional Developers Kit 1992 November / Disc01 / Disc01.mdf / cppbeta / ibmcli / ireslock.hp_ / IRESLOCK.HPP
Encoding:
C/C++ Source or Header  |  1992-10-26  |  8.4 KB  |  202 lines

  1. #ifndef _IRESLOCK_
  2.   #define _IRESLOCK_
  3. /*******************************************************************************
  4. * FILE NAME: ireslock.hpp                                                      *
  5. *                                                                              *
  6. * DESCRIPTION:                                                                 *
  7. *   Declaration of the class(es):                                              *
  8. *     IResource        - The base resource definition class.                   *
  9. *     IPrivateResource - Private non-shared resource class.                    *
  10. *     ISharedResource  - Resource capable of being shared across processes.    *
  11. *     IResourceLock    -                                                       *
  12. *                                                                              *
  13. * COPYRIGHT:                                                                   *
  14. *   (C) Copyright IBM Corporation 1992                                         *
  15. *   All Rights Reserved                                                        *
  16. *   Licensed Materials * Property of IBM                                       *
  17. *                                                                              *
  18. * HISTORY:                                                                     *
  19. * $Log:   R:/ibmclass/ibase/vcs/ireslock.hpv  $
  20. //
  21. //   Rev 1.1   26 Oct 1992 18:23:44   BOBLOVE
  22. //Update for External Beta
  23. *                                                                              *
  24. *******************************************************************************/
  25. #ifndef _IVBASE_
  26.   #include <ivbase.hpp>
  27. #endif
  28.  
  29. #ifndef _ISTRING_
  30.   #include <istring.hpp>
  31. #endif
  32.  
  33.  
  34. class IResource : public IVBase
  35. {
  36. /*******************************************************************************
  37. *  This class is the base resource class.                                      *
  38. *                                                                              *
  39. *******************************************************************************/
  40. friend class IResourceLock;
  41. public:
  42. /*------------------------ CONSTRUCTORS/DESTRUCTORS ----------------------------
  43. | Instances of this class cannot be created.                                   |
  44. ------------------------------------------------------------------------------*/
  45.   IResource();
  46.   ~IResource();
  47. /*------------------------ RESOURCE LOCKING ------------------------------------
  48. | lock    -  Acquire a semaphore for this resource.                            |
  49. | unlock  -  Release the semaphore for this resource.                          |
  50. ------------------------------------------------------------------------------*/
  51. IResource
  52.  &lock(long lTimeOut = -1),
  53.  &unlock();
  54.  
  55.  
  56. protected:
  57. /*------------------------ IMPLEMENTATION --------------------------------------
  58. | handle  -  Return the handle for the semaphore.                              |
  59. ------------------------------------------------------------------------------*/
  60. virtual unsigned long
  61.  &handle()  = 0;
  62.  
  63.  
  64. long 
  65.   lClReferenceCount;
  66.  
  67. IResourceLock 
  68.  *reslockGate;
  69.  
  70. };
  71.  
  72. class IPrivateResource : public IResource
  73. {
  74. /*******************************************************************************
  75. *  This class is used to define a resource that is used within a single        *
  76. *  process.                                                                    *
  77. *                                                                              *
  78. *******************************************************************************/
  79. friend class IResourceLock;
  80. public:
  81. /*------------------------ CONSTRUCTORS/DESTRUCTORS ----------------------------
  82. | There is one way to create instances of this class:                          |
  83. |   1. The default with no parameters.                                         |
  84. ------------------------------------------------------------------------------*/
  85.   IPrivateResource();
  86. virtual 
  87.   ~IPrivateResource();
  88.  
  89. protected:
  90. /*------------------------ IMPLEMENTATION --------------------------------------
  91. | handle  -  Return the handle for the semaphore.                              |
  92. ------------------------------------------------------------------------------*/
  93. unsigned long
  94.  &handle();
  95.  
  96. private:
  97.  
  98. unsigned long 
  99.   ulClHandle;
  100.  
  101. };
  102.  
  103.  
  104. class ISharedResource : public IResource
  105. {
  106. /*******************************************************************************
  107. *  This class is used to define a resource that can be shared across multiple  *
  108. *  processes using a name provide on construction.                             *
  109. *                                                                              *
  110. *******************************************************************************/
  111. friend class IResourceLock;
  112.  
  113. public:
  114. /*------------------------ CONSTRUCTORS/DESTRUCTORS ----------------------------
  115. | There is one way to create instances of this class:                          |
  116. |   1. By proving an a character string identifying the resource to be shared. |
  117. ------------------------------------------------------------------------------*/
  118.   ISharedResource(const char* pszKeyname);
  119. virtual 
  120.   ~ISharedResource();
  121.  
  122. IString 
  123.   keyName();
  124.  
  125. protected:
  126.  
  127. unsigned long
  128.  &handle();
  129.  
  130. private:
  131.  
  132. unsigned long 
  133.   ulClHandle;
  134. IString 
  135.   strClKeyName;
  136. };
  137.  
  138. class IResourceLock : public IBase
  139. {
  140. /*******************************************************************************
  141. *  This class locks a resource for a specified timeout.  The easiest use of    *
  142. *  the class is to declare a stack instance that is in scope for the period    *
  143. *  of time that the resource needs to be locked.  If the timeout is reached    *
  144. *  before the smeaphore can be acquired, an IResourceExhausted exception       *
  145. *  is thrown.                                                                  *
  146. *                                                                              *
  147. *  EXAMPLE:                                                                    *
  148. *                                                                              *
  149. *   static IPrivateResource collectionResource;                                *
  150. *                                                                              *
  151. *   addElement()                                                               *
  152. *   {                                                                          *
  153. *      //  Lock the resource                                                   *
  154. *      IResourceLock collectionLock(collectionResource);                       *
  155. *                                                                              *
  156. *      // Lock is removed on function exit                                     *
  157. *   }                                                                          *
  158. *                                                                              *
  159. *******************************************************************************/
  160. friend  class IResource;
  161. public:
  162.  
  163. /*------------------------ CONSTRUCTORS/DESTRUCTORS ----------------------------
  164. | There is one way to create instances of this class:                          |
  165. |   1. By proving an IResource reference and an optional timeout value. The    |
  166. |      timeout value is specified in milliseconds with -1 indicating an        |
  167. |      indefinate wait and zero requesting an immediate return.                |
  168. ------------------------------------------------------------------------------*/
  169.   IResourceLock(IResource& res, long lTimeOut = -1);
  170.  
  171.   ~IResourceLock();
  172.  
  173. protected:
  174. /*----------------------------- IMPLEMENTATION ---------------------------------
  175. | The following functions are used to implement the class.                     |
  176. |  setLock   - Acquires the semaphore.                                         |
  177. |  clearLock - Releases the semaphore.                                         |
  178. ------------------------------------------------------------------------------*/
  179. IResourceLock
  180.  &setLock(IResource& res, long lTimeOut = -1),
  181.  &clearLock(IResource& res);
  182.  
  183. private:
  184.  IResource* presCl;
  185.  
  186.  
  187. };
  188.  
  189.  
  190. /* INLINES */
  191. inline IResource ::  IResource()
  192.   {lClReferenceCount=0; reslockGate=0;} // rsr was -1
  193.  
  194.  
  195. inline unsigned long& IPrivateResource :: handle()
  196.   { return ulClHandle;   }
  197.  
  198. inline unsigned long& ISharedResource :: handle()
  199.   { return ulClHandle;   }
  200.  
  201. #endif
  202.