home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 9 / CDACTUAL9.iso / progs / CB / DATA.Z / STDMUTEX.H < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-20  |  6.8 KB  |  239 lines

  1. #ifndef __RWSTD_MUTEX_H__
  2. #define __RWSTD_MUTEX_H__
  3. /* $Revision:   8.3  $ */
  4.  
  5. /*
  6.  * Declarations for class RWSTDMutex and RWSTDGuard.
  7.  *
  8.  * $Id: stdmutex.h,v 1.7 1995/09/08 23:45:09 smithey Exp $
  9.  *
  10.  ***************************************************************************
  11.  *
  12.  * (c) Copyright 1994, 1995 Rogue Wave Software, Inc.
  13.  * ALL RIGHTS RESERVED
  14.  *
  15.  * The software and information contained herein are proprietary to, and
  16.  * comprise valuable trade secrets of, Rogue Wave Software, Inc., which
  17.  * intends to preserve as trade secrets such software and information.
  18.  * This software is furnished pursuant to a written license agreement and
  19.  * may be used, copied, transmitted, and stored only in accordance with
  20.  * the terms of such license and with the inclusion of the above copyright
  21.  * notice.  This software and information or any other copies thereof may
  22.  * not be provided or otherwise made available to any other person.
  23.  *
  24.  * Notwithstanding any other lease or license that may pertain to, or
  25.  * accompany the delivery of, this computer software and information, the
  26.  * rights of the Government regarding its use, reproduction and disclosure
  27.  * are as set forth in Section 52.227-19 of the FARS Computer
  28.  * Software-Restricted Rights clause.
  29.  *
  30.  * Use, duplication, or disclosure by the Government is subject to
  31.  * restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in
  32.  * Technical Data and Computer Software clause at DFARS 252.227-7013.
  33.  * Contractor/Manufacturer is Rogue Wave Software, Inc.,
  34.  * P.O. Box 2328, Corvallis, Oregon 97339.
  35.  *
  36.  * This computer software and information is distributed with "restricted
  37.  * rights."  Use, duplication or disclosure is subject to restrictions as
  38.  * set forth in NASA FAR SUP 18-52.227-79 (April 1985) "Commercial
  39.  * Computer Software-Restricted Rights (April 1985)."  If the Clause at
  40.  * 18-52.227-74 "Rights in Data General" is specified in the contract,
  41.  * then the "Alternate III" clause applies.
  42.  *
  43.  ***************************************************************************
  44.  *
  45.  * This class is a portable implementation of a simple mutex lock
  46.  * to be used for synchronizing multiple threads within a single process.
  47.  * It is not suitable for use among threads of different processes.
  48.  * This code was taken from the tools mutex.h.
  49.  *
  50.  ***************************************************************************/
  51.  
  52. #include <stdcomp.h>
  53.  
  54. #ifndef __RWSTDDEFS_H__
  55. #include <stddefs.h>
  56. #endif
  57.  
  58. #ifdef RWSTD_MULTI_THREAD /* This class only relevant in MT situation */
  59.  
  60. #if defined(sun)          /* assuming Solaris 2.1 or greater */
  61. #include <synch.h>
  62. typedef mutex_t RWSTDMutexType;
  63. #elif defined(RW_POSIX_THREADS)
  64. #include <pthread.h>
  65. typedef pthread_mutex_t RWSTDMutexType;
  66. #define RWSTD_NEEDS_SEM_INIT
  67. #elif defined(__WIN32__)
  68.  
  69. // JLC: Tempory fix for Windows.h vs. Windows.hpp issue
  70. #if defined(WindowsHPP) 
  71. #define JWHANDLE THandle
  72. #elif defined(_WINDOWS_)
  73. #define JWHANDLE HANDLE
  74. #else
  75. #include <windows.h>
  76. #define JWHANDLE HANDLE
  77. #endif
  78.  
  79. typedef HANDLE RWSTDMutexType;
  80.  
  81. #elif defined(__OS2__)
  82. #define INCL_DOSSEMAPHORES
  83. #define RWSTD_NEEDS_SEM_INIT
  84. #include <os2.h>
  85. typedef HMTX RWSTDMutexType;
  86. const char* __rw_mutex_exception = "Thread synchronization exception.";
  87. #else
  88. #error Class RWSTDMutex is not yet supported in this environment
  89. #endif
  90.  
  91. class RWSTDMutex
  92. {
  93.   private:
  94.  
  95.     RWSTDMutexType mutex;
  96. #if defined(RWSTD_NEEDS_SEM_INIT)
  97.     int initFlag;
  98. #endif
  99.  
  100.     void init ();
  101.     //
  102.     // Disallow copying and assignment.
  103.     //
  104.     RWSTDMutex (const RWSTDMutex&);
  105.     RWSTDMutex& operator= (const RWSTDMutex&);
  106.  
  107. public:
  108.  
  109.   enum StaticCtor { staticCtor };
  110.  
  111.   RWSTDMutex ();             // Construct the mutex.
  112.   RWSTDMutex (StaticCtor);   // Some statics need special handling.
  113.   ~RWSTDMutex ();            // Destroy the mutex.
  114.  
  115.   void acquire ();           // Acquire the mutex.
  116.   void release ();           // Release the mutex.
  117. };
  118.  
  119. class RWSTDGuard
  120. {
  121.   private:
  122.  
  123.     RWSTDMutex& rwmutex;
  124.  
  125.   public:
  126.  
  127.     RWSTDGuard  (RWSTDMutex& m);  // Acquire the mutex.
  128.     ~RWSTDGuard ();               // Release the mutex.
  129. };
  130.  
  131. /*
  132. ** For those OSs that require a non-zero mutex, we must treat static
  133. ** mutexes specially; they may not be initialized when we need them.
  134. ** For efficiency, we do conditional compilation in several methods
  135. ** based on that need.
  136. */
  137.  
  138. inline RWSTDMutex::RWSTDMutex (RWSTDMutex::StaticCtor)
  139. {
  140.     //
  141.     // Empty, because acquire() may already have been used.
  142.     //
  143. }
  144.  
  145. inline RWSTDMutex::~RWSTDMutex ()
  146. {
  147. #if defined(RWSTD_NEEDS_SEM_INIT)
  148.     if (0 == initFlag)
  149.         return;
  150.     else
  151.         initFlag = 0;
  152. #endif
  153. #if defined(sun)
  154.     mutex_destroy(&mutex);
  155. #elif defined(RW_POSIX_THREADS)
  156.     pthread_mutex_destroy(&mutex);
  157. #elif defined(__WIN32__)
  158.     CloseHandle((JWHANDLE)mutex);
  159. #elif defined(__OS2__)
  160.     APIRET rv;
  161.     RWSTD_THROW(0 != (rv = DosCloseMutexSem(mutex)),
  162.                 runtime_error,
  163.                 __rw_mutex_exception);
  164. #endif
  165. }
  166.  
  167. void inline RWSTDMutex::init ()
  168. {
  169. #if defined(sun)
  170.     mutex_init(&mutex, USYNC_THREAD, NULL);
  171. #elif defined(RW_POSIX_THREADS)
  172.     pthread_mutex_init(&mutex, pthread_mutexattr_default);
  173. #elif defined(__WIN32__)
  174.     mutex = (HANDLE) CreateMutex(NULL, FALSE, NULL);
  175. #elif defined(__OS2__)
  176.     APIRET rv;
  177.     RWSTD_THROW(0 != (rv = DosCreateMutexSem(0,&mutex,DC_SEM_SHARED,FALSE)),
  178.                 runtime_error,
  179.                 __rw_mutex_exception);
  180. #endif
  181. #if defined(RWSTD_NEEDS_SEM_INIT)
  182.     initFlag = 1;
  183. #endif
  184. }
  185.  
  186. inline RWSTDMutex::RWSTDMutex () { init(); }  // Initialize the mutex.
  187.  
  188. inline void RWSTDMutex::acquire ()
  189. {
  190. #if defined(RWSTD_NEEDS_SEM_INIT)
  191.     if(0 == initFlag)
  192.         init();
  193. #endif
  194. #if defined(sun)
  195.     mutex_lock(&mutex);
  196. #elif defined(RW_POSIX_THREADS)
  197.     pthread_mutex_lock(&mutex);
  198. #elif defined(__WIN32__)
  199.     WaitForSingleObject((JWHANDLE)mutex,INFINITE);
  200. #elif defined(__OS2__)
  201.     APIRET rv;
  202.     RWSTD_THROW(0 != (rv = DosRequestMutexSem(mutex, SEM_INDEFINITE_WAIT)),
  203.                 runtime_error,
  204.                 __rw_mutex_exception);
  205. #endif
  206. }
  207.  
  208. inline void RWSTDMutex::release ()
  209. {
  210. #if defined(sun)
  211.     mutex_unlock(&mutex);
  212. #elif defined(RW_POSIX_THREADS)
  213.     pthread_mutex_unlock(&mutex);
  214. #elif defined(__WIN32__)
  215.     ReleaseMutex((JWHANDLE)mutex);
  216. #elif defined(__OS2__)
  217.     APIRET rv;
  218.     RWSTD_THROW(0 != (rv = DosReleaseMutexSem(mutex)),
  219.                 runtime_error,
  220.                 __rw_mutex_exception);
  221. #endif
  222. }
  223.  
  224. inline RWSTDGuard::RWSTDGuard (RWSTDMutex& m) : rwmutex(m)
  225. {
  226.     rwmutex.acquire();
  227. }
  228.  
  229. inline RWSTDGuard::~RWSTDGuard () { rwmutex.release(); }
  230.  
  231.  
  232. #define STDGUARD(a) RWSTDGuard(a)
  233. #else
  234. #define STDGUARD(a)
  235. #endif  /*RWSTD_MULTI_THREAD*/
  236. #endif  /*__RWSTD_MUTEX_H__*/
  237.  
  238.  
  239.