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

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2