(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 and 1.13

version 1.4, 2006/07/11 18:39:28 version 1.13, 2008/08/15 17:24:52
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  
 //  
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
 #ifndef Pegasus_Mutex_h #ifndef Pegasus_Mutex_h
Line 48 
Line 36 
  
 #include <Pegasus/Common/Config.h> #include <Pegasus/Common/Config.h>
 #include <Pegasus/Common/Linkage.h> #include <Pegasus/Common/Linkage.h>
 #include <Pegasus/Common/IPCTypes.h>  #include <Pegasus/Common/IPCExceptions.h>
 #include <Pegasus/Common/Magic.h> #include <Pegasus/Common/Magic.h>
   #include <Pegasus/Common/Threads.h>
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
   //==============================================================================
   //
   // MutexRep
   //
   //==============================================================================
   
   #if defined(PEGASUS_HAVE_PTHREADS)
   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
   {
       pthread_mutex_t mutex;
       int count;
   };
   # define PEGASUS_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
   #endif
   
   #if defined(PEGASUS_HAVE_WINDOWS_THREADS)
   typedef HANDLE MutexType;
   inline void mutex_lock(MutexType* m) { WaitForSingleObject(*m, INFINITE); }
   inline void mutex_unlock(MutexType* m) { ReleaseMutex(*m); }
   struct MutexRep
   {
       MutexType handle;
       size_t count;
   };
   # define PEGASUS_MUTEX_INITIALIZER (CreateMutex(NULL, FALSE, NULL))
   #endif
   
   //==============================================================================
   //
   // Mutex
   //
   //==============================================================================
   
 class PEGASUS_COMMON_LINKAGE Mutex class PEGASUS_COMMON_LINKAGE Mutex
 { {
 public: public:
  
       enum RecursiveTag { RECURSIVE };
       enum NonRecursiveTag { NON_RECURSIVE };
   
       /** Default constructor creates a recursive mutex.
       */
     Mutex();     Mutex();
     Mutex(int type);  
       /** 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();
  
     // block until gaining the lock - throw a deadlock      void lock();
     // exception if process already holds the lock  
     // @exception Deadlock      /**
     // @exception WaitFailed          Attempts to lock the mutex without blocking.
     void lock(PEGASUS_THREAD_TYPE caller = pegasus_thread_self());          @return A Boolean indicating whether the lock was acquired.
       */
     // try to gain the lock - lock succeeds immediately if the      Boolean try_lock();
     // mutex is not already locked. throws an exception and returns  
     // immediately if the mutex is currently locked.      /**
     // @exception Deadlock          Attempts to lock the mutex within the specified time.
     // @exception AlreadyLocked          @param milliseconds The maximum time to block while attempting to
     // @exception WaitFailed              acquire the lock.
     void try_lock(PEGASUS_THREAD_TYPE caller = pegasus_thread_self());          @return A Boolean indicating whether the lock was acquired.
       */
     // wait for milliseconds and throw an exception then return if the wait      Boolean timed_lock(Uint32 milliseconds);
     // expires without gaining the lock. Otherwise return without throwing an  
     // exception.  
     // @exception Deadlock  
     // @exception TimeOut  
     // @exception WaitFailed  
     void timed_lock(  
         Uint32 milliseconds,  
         PEGASUS_THREAD_TYPE caller = pegasus_thread_self());  
  
     // unlock the semaphore  
     // @exception Permission  
     void unlock();     void unlock();
  
     inline PEGASUS_THREAD_TYPE get_owner() { return(_mutex.owner); }  #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:
     inline void _set_owner(PEGASUS_THREAD_TYPE owner) { _mutex.owner = owner; }      Mutex(const Mutex&);
     PEGASUS_MUTEX_HANDLE _mutex;      Mutex& operator=(const Mutex&);
     PEGASUS_MUTEX_HANDLE & _get_handle()  
       MutexRep _rep;
       Magic<0x57D11485> _magic;
   
       friend class Condition;
   };
   
   //==============================================================================
   //
   // AutoMutex
   //
   //==============================================================================
   
   class PEGASUS_COMMON_LINKAGE AutoMutex
   {
   public:
   
       AutoMutex(Mutex& mutex) : _mutex(mutex)
     {     {
         return _mutex;          _mutex.lock();
     }     }
  
     // Hide the assignment operator to avoid implicit use of the default      ~AutoMutex()
     // assignment operator.  Do not use this method.      {
     Mutex& operator=(const Mutex& original) {return *this;}          _mutex.unlock();
       }
     // Hide the copy constructor to avoid implicit use of the default  
     // copy constructor.  Do not use this method.  
     Mutex(const Mutex& _mutex);  
  
     friend class Condition;  private:
       AutoMutex(); // Unimplemented
       AutoMutex(const AutoMutex& x); // Unimplemented
       AutoMutex& operator=(const AutoMutex& x); // Unimplemented
  
     Magic<0x57D11485> _magic;      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  
changed lines
  Added in v.1.13

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2