(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.2 and 1.10

version 1.4.2.2, 2006/07/28 17:41:27 version 1.10, 2007/08/02 08:06:53
Line 29 
Line 29 
 // //
 //============================================================================== //==============================================================================
 // //
 // Author: Mike Day (mdday@us.ibm.com)  
 //  
 // Modified By: Markus Mueller  
 //              Ramnath Ravindran (Ramnath.Ravindran@compaq.com)  
 //              David Eger (dteger@us.ibm.com)  
 //              Amit K Arora, IBM (amita@in.ibm.com) for PEP#101  
 //              Sean Keenan, Hewlett-Packard Company (sean.keenan@hp.com)  
 //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)  
 //              David Dillard, VERITAS Software Corp.  
 //                  (david.dillard@veritas.com)  
 //              Aruran, IBM (ashanmug@in.ibm.com) for BUG# 3518  
 //  
 // Reworked By: Mike Brasher (m.brasher@inovadevelopment.com)  
 //  
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #ifndef Pegasus_Mutex_h #ifndef Pegasus_Mutex_h
Line 64 
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
 { {
     pthread_mutex_t mutex;     pthread_mutex_t mutex;
     pthread_mutexattr_t attr;      int count;
     ThreadType owner;  
 }; };
 inline void mutex_lock(MutexType* mutex) { pthread_mutex_lock(mutex); }  
 inline void mutex_unlock(MutexType* mutex) { pthread_mutex_unlock(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
 { {
     HANDLE handle;      MutexType handle;
     HANDLE owner;  
     size_t count;     size_t count;
 }; };
   # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
 #endif #endif
  
 //============================================================================== //==============================================================================
Line 95 
Line 82 
 { {
 public: public:
  
       enum RecursiveTag { RECURSIVE };
       enum NonRecursiveTag { NON_RECURSIVE };
   
       /** Default constructor creates a recursive mutex.
       */
     Mutex();     Mutex();
  
       /** Call as Mutex(Mutex::RECURSIVE) to create a recursive mutex.
       */
       Mutex(RecursiveTag);
   
       /** Call as Mutex(Mutex::NON_RECURSIVE) to create a non-recursive mutex.
       */
       Mutex(NonRecursiveTag);
   
     ~Mutex();     ~Mutex();
  
     void lock(ThreadType caller = Threads::self());      void lock();
  
     void try_lock(ThreadType caller = Threads::self());      void try_lock();
  
     void timed_lock(Uint32 milliseconds, ThreadType caller = Threads::self());      void timed_lock(Uint32 milliseconds);
  
     void unlock();     void unlock();
  
     ThreadType get_owner() { return _rep.owner; }  #if defined(PEGASUS_OS_LINUX)
       /**
     void set_owner(ThreadType caller) { _rep.owner = caller; }          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 132 
Line 136 
 { {
 public: public:
  
     AutoMutex(Mutex& mutex, Boolean autoLock = true) :      AutoMutex(Mutex& mutex) : _mutex(mutex)
         _mutex(mutex), _locked(autoLock)  
     {     {
         if (autoLock)  
             _mutex.lock();             _mutex.lock();
     }     }
  
     ~AutoMutex()     ~AutoMutex()
     {     {
         try  
         {  
             if (_locked)  
                 unlock();  
         }  
         catch (...)  
         {  
             // Do not propagate exception from destructor  
         }  
     }  
   
     void lock()  
     {  
         if (_locked)  
             throw AlreadyLocked(Threads::self());  
   
         _mutex.lock();  
         _locked = true;  
     }  
   
     void unlock()  
     {  
         if (!_locked)  
             throw Permission(Threads::self());  
   
         _mutex.unlock();         _mutex.unlock();
         _locked = false;  
     }  
   
     Boolean isLocked() const  
     {  
         return _locked;  
     }     }
  
 private: private:
Line 181 
Line 152 
     AutoMutex& operator=(const AutoMutex& x); // Unimplemented     AutoMutex& operator=(const AutoMutex& x); // Unimplemented
  
     Mutex& _mutex;     Mutex& _mutex;
     Boolean _locked;  
 }; };
  
   //==============================================================================
   //
   // 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)
   
   # 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.2  
changed lines
  Added in v.1.10

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2