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

Diff for /pegasus/src/Pegasus/Common/Mutex.h between version 1.4.2.5 and 1.12.2.1

version 1.4.2.5, 2006/07/30 22:51:02 version 1.12.2.1, 2008/08/20 23:05:50
Line 29 
Line 29 
 // //
 //============================================================================== //==============================================================================
 // //
 // Author: Mike Brasher (m.brasher@inovadevelopment.com)  
 //  
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #ifndef Pegasus_Mutex_h #ifndef Pegasus_Mutex_h
Line 52 
Line 50 
  
 #if defined(PEGASUS_HAVE_PTHREADS) #if defined(PEGASUS_HAVE_PTHREADS)
 typedef pthread_mutex_t MutexType; typedef pthread_mutex_t MutexType;
   inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }
   inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }
 struct MutexRep struct MutexRep
 { {
     MutexType mutex;      pthread_mutex_t mutex;
       int count;
 }; };
 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }  
 inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(mutex); }  
 inline void mutex_destroy(MutexType* mutex) { pthread_mutex_destroy(mutex); }  
 # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
 #endif #endif
  
 #if defined(PEGASUS_HAVE_WINDOWS_THREADS) #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
 typedef HANDLE MutexType; typedef HANDLE MutexType;
   inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
   inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
 struct MutexRep struct MutexRep
 { {
     MutexType handle;     MutexType handle;
     size_t count;     size_t count;
 }; };
 inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }  
 inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }  
 # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL)) # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
 #endif #endif
  
Line 84 
Line 82 
 { {
 public: public:
  
     Mutex();      enum RecursiveTag { RECURSIVE };
       enum NonRecursiveTag { NON_RECURSIVE };
     ~Mutex()  
     {  
         mutex_destroy(&_rep.mutex);  
     }  
   
     void lock()  
     {  
         mutex_lock(&_rep.mutex);  
     }  
   
     void try_lock();  
  
     void timed_lock(Uint32 milliseconds);      /** Default constructor creates a recursive mutex.
       */
       Mutex();
  
     void unlock()      /** Call as Mutex(Mutex::RECURSIVE) to create a recursive mutex.
     {      */
         mutex_unlock(&_rep.mutex);      Mutex(RecursiveTag);
     }  
       /** Call as Mutex(Mutex::NON_RECURSIVE) to create a non-recursive mutex.
       */
       Mutex(NonRecursiveTag);
   
       ~Mutex();
   
       void lock();
   
       /**
           Attempts to lock the mutex without blocking.
           @return A Boolean indicating whether the lock was acquired.
       */
       Boolean try_lock();
   
       /**
           Attempts to lock the mutex within the specified time.
           @param milliseconds The maximum time to block while attempting to
               acquire the lock.
           @return A Boolean indicating whether the lock was acquired.
       */
       Boolean timed_lock(Uint32 milliseconds);
   
       void unlock();
   
   #if defined(PEGASUS_OS_LINUX) ||  \
       (defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000))
       /**
           This method must only be called after a fork() to reset the mutex
           lock status in the new process.  Any other use of this method is
           unsafe.
       */
       void reinitialize();
   #endif
  
 private: private:
     Mutex(const Mutex&);     Mutex(const Mutex&);
     Mutex& operator=(const Mutex&);     Mutex& operator=(const Mutex&);
  
   
     MutexRep _rep;     MutexRep _rep;
     Magic<0x57D11485> _magic;     Magic<0x57D11485> _magic;
  
Line 136 
Line 157 
         _mutex.unlock();         _mutex.unlock();
     }     }
  
     void lock()  
     {  
         _mutex.lock();  
     }  
   
     void unlock()  
     {  
         _mutex.unlock();  
     }  
   
 private: private:
     AutoMutex();      AutoMutex(); // Unimplemented
     AutoMutex(const AutoMutex& x);      AutoMutex(const AutoMutex& x); // Unimplemented
     AutoMutex& operator=(const AutoMutex& x);      AutoMutex& operator=(const AutoMutex& x); // Unimplemented
  
     Mutex& _mutex;     Mutex& _mutex;
 }; };
  
   //==============================================================================
   //
   // PEGASUS_FORK_SAFE_MUTEX
   //
   //==============================================================================
   
   // Use of this macro ensures that a static Mutex is not locked across a fork().
   
   #if !defined(PEGASUS_HAVE_PTHREADS) || \
       (defined(PEGASUS_OS_ZOS) && (__TARGET_LIB__ < 0x41090000)) || \
       defined(PEGASUS_OS_VMS)
   
   # define PEGASUS_FORK_SAFE_MUTEX(mutex)
   
   #elif defined(PEGASUS_OS_LINUX) || \
         (defined(PEGASUS_OS_ZOS) && !(__TARGET_LIB__ < 0x41090000))
   
   # define PEGASUS_FORK_SAFE_MUTEX(mutex)  \
       class ForkSafeMutex ## mutex         \
       {                                    \
       public:                              \
           ForkSafeMutex ## mutex()         \
           {                                \
               pthread_atfork(              \
                   0,                       \
                   0,                       \
                   _reinitializeMutex);     \
           }                                \
                                            \
       private:                             \
           static void _reinitializeMutex() \
           {                                \
               mutex.reinitialize();        \
           }                                \
       };                                   \
                                            \
       static ForkSafeMutex ## mutex __forkSafeMutex ## mutex;
   
   #else
   
   # define PEGASUS_FORK_SAFE_MUTEX(mutex)  \
       class ForkSafeMutex ## mutex         \
       {                                    \
       public:                              \
           ForkSafeMutex ## mutex()         \
           {                                \
               pthread_atfork(              \
                   _lockMutex,              \
                   _unlockMutex,            \
                   _unlockMutex);           \
           }                                \
                                            \
       private:                             \
           static void _lockMutex()         \
           {                                \
               mutex.lock();                \
           }                                \
                                            \
           static void _unlockMutex()       \
           {                                \
               mutex.unlock();              \
           }                                \
       };                                   \
                                            \
       static ForkSafeMutex ## mutex __forkSafeMutex ## mutex;
   
   #endif
   
 PEGASUS_NAMESPACE_END PEGASUS_NAMESPACE_END
  
 #endif /* Pegasus_Mutex_h */ #endif /* Pegasus_Mutex_h */


Legend:
Removed from v.1.4.2.5  
changed lines
  Added in v.1.12.2.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2