00001 // -*- C++ -*- 00002 // 00003 //============================================================================= 00004 /** 00005 * @file RW_Process_Mutex.h 00006 * 00007 * $Id: RW_Process_Mutex.h 81509 2008-04-28 22:00:49Z shuston $ 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 This class uses an ACE_File_Lock as its implementation. Thus, it 00035 * can only be reliably used between separate processes, rather than 00036 * threads in the same process. This isn't a limitation of ACE, it's simply 00037 * the file lock semantics on UNIX and Win32. 00038 * 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 cross-process readers/writer mutex, passing in the optional 00046 /// @a name, @a flags and @a 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 /// Same as acquire_write(). 00069 /// Acquire lock ownership; blocks until the lock is acquired or the 00070 /// operation fails. 00071 int acquire (void); 00072 00073 /** 00074 * Same as tryacquire_write(). 00075 * Try to acquire the lock, but do not block if the lock is not immediately 00076 * acquired. 00077 * 00078 * @retval -1 on failure. If the lock is already held, @c errno is set 00079 * to @c EBUSY. 00080 */ 00081 int tryacquire (void); 00082 00083 /// Release lock. 00084 int release (void); 00085 00086 /// Acquire read lock; blocks until the lock is acquired or the 00087 /// operation fails. 00088 int acquire_read (void); 00089 00090 /// Acquire write lock; blocks until the lock is acquired or the 00091 /// operation fails. 00092 int acquire_write (void); 00093 00094 /** 00095 * Try to acquire the read lock, but do not block if the lock is not 00096 * immediately acquired. 00097 * 00098 * @retval -1 on failure. If the lock is already held, @c errno is set 00099 * to @c EBUSY. 00100 */ 00101 int tryacquire_read (void); 00102 00103 /** 00104 * Try to acquire the write lock, but do not block if the lock is not 00105 * immediately acquired. 00106 * 00107 * @retval -1 on failure. If the lock is already held, @c errno is set 00108 * to @c EBUSY. 00109 */ 00110 int tryacquire_write (void); 00111 00112 /// Attempt to upgrade a read lock to a write lock. Returns 0 on 00113 /// success, -1 on failure. 00114 int tryacquire_write_upgrade (void); 00115 00116 /// Return the underlying lock. 00117 const ACE_File_Lock &lock (void) const; 00118 00119 /// Dump the state of an object. 00120 void dump (void) const; 00121 00122 /// Declare the dynamic allocation hooks. 00123 ACE_ALLOC_HOOK_DECLARE; 00124 00125 private: 00126 /// If the user does not provide a name we generate a unique name in 00127 /// this buffer. 00128 ACE_TCHAR name_[ACE_UNIQUE_NAME_LEN]; 00129 00130 /// Create and return the unique name. 00131 const ACE_TCHAR *unique_name (void); 00132 00133 /// We need this to get the readers/writer semantics... 00134 ACE_File_Lock lock_; 00135 }; 00136 00137 ACE_END_VERSIONED_NAMESPACE_DECL 00138 00139 #if defined (__ACE_INLINE__) 00140 #include "ace/RW_Process_Mutex.inl" 00141 #endif /* __ACE_INLINE__ */ 00142 00143 #include /**/ "ace/post.h" 00144 #endif /* ACE_RW_PROCESS_MUTEX_H */