(file) Return to Mutex.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  1 karl  1.3 //%2006////////////////////////////////////////////////////////////////////////
  2 mike  1.2 //
  3           // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
  4           // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
  5           // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
  6           // IBM Corp.; EMC Corporation, The Open Group.
  7           // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
  8           // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
  9           // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 10           // EMC Corporation; VERITAS Software Corporation; The Open Group.
 11 karl  1.3 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 12           // EMC Corporation; Symantec Corporation; The Open Group.
 13 mike  1.2 //
 14           // Permission is hereby granted, free of charge, to any person obtaining a copy
 15           // of this software and associated documentation files (the "Software"), to
 16           // deal in the Software without restriction, including without limitation the
 17           // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 18           // sell copies of the Software, and to permit persons to whom the Software is
 19           // furnished to do so, subject to the following conditions:
 20 karl  1.3 // 
 21 mike  1.2 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
 22           // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
 23           // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 24           // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
 25           // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 26           // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 27           // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 28           // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 29           //
 30           //==============================================================================
 31           //
 32           //%/////////////////////////////////////////////////////////////////////////////
 33           
 34           #ifndef Pegasus_Mutex_h
 35           #define Pegasus_Mutex_h
 36           
 37           #include <Pegasus/Common/Config.h>
 38           #include <Pegasus/Common/Linkage.h>
 39 mike  1.5 #include <Pegasus/Common/IPCExceptions.h>
 40 mike  1.4 #include <Pegasus/Common/Magic.h>
 41 mike  1.5 #include <Pegasus/Common/Threads.h>
 42 mike  1.2 
 43           PEGASUS_NAMESPACE_BEGIN
 44           
 45 mike  1.5 //==============================================================================
 46           //
 47           // MutexRep
 48           //
 49           //==============================================================================
 50           
 51           #if defined(PEGASUS_HAVE_PTHREADS)
 52           typedef pthread_mutex_t MutexType;
 53           inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
 54           inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
 55           struct MutexRep
 56           {
 57               pthread_mutex_t mutex;
 58               int count;
 59           };
 60           # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
 61           #endif
 62           
 63           #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
 64           typedef HANDLE MutexType;
 65           inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
 66 mike  1.5 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
 67           struct MutexRep
 68           {
 69               MutexType handle;
 70               size_t count;
 71           };
 72           # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
 73           #endif
 74           
 75           //==============================================================================
 76           //
 77           // Mutex
 78           //
 79           //==============================================================================
 80           
 81 mike  1.2 class PEGASUS_COMMON_LINKAGE Mutex
 82           {
 83           public:
 84           
 85 mike  1.6     enum RecursiveTag { RECURSIVE };
 86               enum NonRecursiveTag { NON_RECURSIVE };
 87           
 88               /** Default constructor creates a recursive mutex.
 89               */
 90 mike  1.2     Mutex();
 91           
 92 mike  1.6     /** Call as Mutex(Mutex::RECURSIVE) to create a recursive mutex.
 93               */
 94               Mutex(RecursiveTag);
 95           
 96               /** Call as Mutex(Mutex::NON_RECURSIVE) to create a non-recursive mutex.
 97               */
 98               Mutex(NonRecursiveTag);
 99           
100 mike  1.2     ~Mutex();
101           
102 mike  1.5     void lock();
103           
104               void try_lock();
105           
106               void timed_lock(Uint32 milliseconds);
107 mike  1.2 
108               void unlock();
109           
110 thilo.boehm 1.11 #if defined(PEGASUS_OS_LINUX) ||  \
111                      (defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000)) 
112 kumpf       1.9      /**
113                          This method must only be called after a fork() to reset the mutex
114                          lock status in the new process.  Any other use of this method is
115                          unsafe.
116                      */
117                      void reinitialize();
118                  #endif
119                  
120 mike        1.5  private:
121                      Mutex(const Mutex&);
122                      Mutex& operator=(const Mutex&);
123                  
124                      MutexRep _rep;
125                      Magic<0x57D11485> _magic;
126                  
127                      friend class Condition;
128                  };
129 mike        1.2  
130 mike        1.5  //==============================================================================
131                  //
132                  // AutoMutex
133                  //
134                  //==============================================================================
135                  
136                  class PEGASUS_COMMON_LINKAGE AutoMutex
137                  {
138                  public:
139                  
140                      AutoMutex(Mutex& mutex) : _mutex(mutex)
141 mike        1.2      {
142 mike        1.5          _mutex.lock();
143 mike        1.2      }
144                  
145 mike        1.5      ~AutoMutex()
146                      {
147                          _mutex.unlock();
148                      }
149 mike        1.2  
150 mike        1.5  private:
151                      AutoMutex(); // Unimplemented
152                      AutoMutex(const AutoMutex& x); // Unimplemented
153                      AutoMutex& operator=(const AutoMutex& x); // Unimplemented
154 mike        1.4  
155 mike        1.5      Mutex& _mutex;
156 mike        1.2  };
157                  
158 kumpf       1.9  //==============================================================================
159                  //
160                  // PEGASUS_FORK_SAFE_MUTEX
161                  //
162                  //==============================================================================
163                  
164                  // Use of this macro ensures that a static Mutex is not locked across a fork().
165                  
166                  #if !defined(PEGASUS_HAVE_PTHREADS) || \
167 marek       1.10     (defined(PEGASUS_OS_ZOS) && (__TARGET_LIB__ < 0x41090000)) || \
168 kumpf       1.9      defined(PEGASUS_OS_VMS)
169                  
170                  # define PEGASUS_FORK_SAFE_MUTEX(mutex)
171                  
172 thilo.boehm 1.11 #elif defined(PEGASUS_OS_LINUX) || \
173                        (defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000))
174 kumpf       1.9  
175                  # define PEGASUS_FORK_SAFE_MUTEX(mutex)  \
176                      class ForkSafeMutex ## mutex         \
177                      {                                    \
178                      public:                              \
179                          ForkSafeMutex ## mutex()         \
180                          {                                \
181                              pthread_atfork(              \
182                                  0,                       \
183                                  0,                       \
184                                  _reinitializeMutex);     \
185                          }                                \
186                                                           \
187                      private:                             \
188                          static void _reinitializeMutex() \
189                          {                                \
190                              mutex.reinitialize();        \
191                          }                                \
192                      };                                   \
193                                                           \
194                      static ForkSafeMutex ## mutex __forkSafeMutex ## mutex;
195 kumpf       1.9  
196                  #else
197                  
198                  # define PEGASUS_FORK_SAFE_MUTEX(mutex)  \
199                      class ForkSafeMutex ## mutex         \
200                      {                                    \
201                      public:                              \
202                          ForkSafeMutex ## mutex()         \
203                          {                                \
204                              pthread_atfork(              \
205                                  _lockMutex,              \
206                                  _unlockMutex,            \
207                                  _unlockMutex);           \
208                          }                                \
209                                                           \
210                      private:                             \
211                          static void _lockMutex()         \
212                          {                                \
213                              mutex.lock();                \
214                          }                                \
215                                                           \
216 kumpf       1.9          static void _unlockMutex()       \
217                          {                                \
218                              mutex.unlock();              \
219                          }                                \
220                      };                                   \
221                                                           \
222                      static ForkSafeMutex ## mutex __forkSafeMutex ## mutex;
223                  
224                  #endif
225                  
226 mike        1.2  PEGASUS_NAMESPACE_END
227                  
228                  #endif /* Pegasus_Mutex_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2