RW_Process_Mutex.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 //=============================================================================
00004 /**
00005  *  @file    RW_Process_Mutex.h
00006  *
00007  *  RW_Process_Mutex.h,v 4.12 2005/11/26 03:13:13 ossama Exp
00008  *
00009  *  @author Douglas C. Schmidt <schmidt@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_RW_PROCESS_MUTEX_H
00014 #define ACE_RW_PROCESS_MUTEX_H
00015 
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "ace/File_Lock.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Default_Constants.h"
00025 #include "ace/OS_NS_fcntl.h"
00026 
00027 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00028 
00029 /**
00030  * @class ACE_RW_Process_Mutex
00031  *
00032  * @brief Wrapper for readers/writer locks that exist across processes.
00033  *
00034  * Note that because this class uses the
00035  * <ACE_File_Lock> as its implementation it only can be reliably
00036  * used between separate processes, rather than threads in the
00037  * same process.  This isn't a limitation of ACE, it's simply
00038  * the file lock semantics on UNIX and Win32.
00039  * \todo For systems with pthread_rwlockattr_setpshared one
00040  * may consider using them to make the mutex faster.
00041  */
00042 class ACE_Export ACE_RW_Process_Mutex
00043 {
00044 public:
00045   /// Create a readers/writer <Process_Mutex>, passing in the optional
00046   /// <name>, <flags> and <mode> \sa ACE_File_Lock.
00047   ///  If not specified, a name is generated and flags and mode are set
00048   ///  to default platform values.
00049 #if defined (ACE_WIN32)
00050   ACE_RW_Process_Mutex (const ACE_TCHAR *name = 0,
00051                         int flags = O_CREAT|O_RDWR,
00052                         mode_t mode =  ACE_DEFAULT_OPEN_PERMS);
00053 #else
00054   ACE_RW_Process_Mutex (const ACE_TCHAR *name = 0,
00055                         int flags = O_CREAT|O_RDWR,
00056                             mode_t mode = S_IRUSR | S_IWUSR );
00057 #endif /* ACE_WIN32 */
00058 
00059   ~ACE_RW_Process_Mutex (void);
00060 
00061   /**
00062    * Explicitly destroy the mutex.  Note that only one thread should
00063    * call this method since it doesn't protect against race
00064    * conditions.
00065    */
00066   int remove (void);
00067 
00068   /// Acquire lock ownership (wait on queue if necessary).
00069   int acquire (void);
00070 
00071   /**
00072    * Conditionally acquire lock (i.e., don't wait on queue).  Returns
00073    * -1 on failure.  If we "failed" because someone else already had
00074    * the lock, <errno> is set to <EBUSY>.
00075    */
00076   int tryacquire (void);
00077 
00078   /// Release lock and unblock a thread at head of queue.
00079   int release (void);
00080 
00081   /// Acquire lock ownership (wait on queue if necessary).
00082   int acquire_read (void);
00083 
00084   /// Acquire lock ownership (wait on queue if necessary).
00085   int acquire_write (void);
00086 
00087   /**
00088    * Conditionally acquire a lock (i.e., won't block).  Returns -1 on
00089    * failure.  If we "failed" because someone else already had the
00090    * lock, <errno> is set to <EBUSY>.
00091    */
00092   int tryacquire_read (void);
00093 
00094   /**
00095    * Conditionally acquire a lock (i.e., won't block).  Returns -1 on
00096    * failure.  If we "failed" because someone else already had the
00097    * lock, <errno> is set to <EBUSY>.
00098    */
00099   int tryacquire_write (void);
00100 
00101   /// Attempt to upgrade a read lock to a write lock. Returns 0 on
00102   /// success, -1 on failure.
00103   int tryacquire_write_upgrade (void);
00104 
00105   /// Return the underlying lock.
00106   const ACE_File_Lock &lock (void) const;
00107 
00108   /// Dump the state of an object.
00109   void dump (void) const;
00110 
00111   /// Declare the dynamic allocation hooks.
00112   ACE_ALLOC_HOOK_DECLARE;
00113 
00114 private:
00115   /// If the user does not provide a name we generate a unique name in
00116   /// this buffer.
00117   ACE_TCHAR name_[ACE_UNIQUE_NAME_LEN];
00118 
00119   /// Create and return the unique name.
00120   const ACE_TCHAR *unique_name (void);
00121 
00122   /// We need this to get the readers/writer semantics...
00123   ACE_File_Lock lock_;
00124 };
00125 
00126 ACE_END_VERSIONED_NAMESPACE_DECL
00127 
00128 #if defined (__ACE_INLINE__)
00129 #include "ace/RW_Process_Mutex.inl"
00130 #endif /* __ACE_INLINE__ */
00131 
00132 #include /**/ "ace/post.h"
00133 #endif /* ACE_RW_PROCESS_MUTEX_H */

Generated on Thu Nov 9 09:42:02 2006 for ACE by doxygen 1.3.6