00001 // -*- C++ -*- 00002 // 00003 //============================================================================= 00004 /** 00005 * @file RW_Process_Mutex.h 00006 * 00007 * $Id: RW_Process_Mutex.h 87487 2009-11-12 07:54:39Z johnnyw $ 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 00026 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00027 00028 /** 00029 * @class ACE_RW_Process_Mutex 00030 * 00031 * @brief Wrapper for readers/writer locks that exist across processes. 00032 * 00033 * @note This class uses an ACE_File_Lock as its implementation. Thus, it 00034 * can only be reliably used between separate processes, rather than 00035 * threads in the same process. This isn't a limitation of ACE, it's simply 00036 * the file lock semantics on UNIX and Win32. 00037 * 00038 * @todo For systems with pthread_rwlockattr_setpshared one 00039 * may consider using them to make the mutex faster. 00040 */ 00041 class ACE_Export ACE_RW_Process_Mutex 00042 { 00043 public: 00044 /// Create a cross-process readers/writer mutex, passing in the optional 00045 /// @a name, @a flags and @a mode \sa ACE_File_Lock. 00046 /// If not specified, a name is generated and flags and mode are set 00047 /// to default platform values. 00048 ACE_RW_Process_Mutex (const ACE_TCHAR *name = 0, 00049 int flags = O_CREAT|O_RDWR, 00050 mode_t mode = ACE_DEFAULT_RW_PROCESS_MUTEX_PERMS); 00051 00052 ~ACE_RW_Process_Mutex (void); 00053 00054 /** 00055 * Explicitly destroy the mutex. Note that only one thread should 00056 * call this method since it doesn't protect against race 00057 * conditions. 00058 */ 00059 int remove (void); 00060 00061 /// Same as acquire_write(). 00062 /// Acquire lock ownership; blocks until the lock is acquired or the 00063 /// operation fails. 00064 int acquire (void); 00065 00066 /** 00067 * Same as tryacquire_write(). 00068 * Try to acquire the lock, but do not block if the lock is not immediately 00069 * acquired. 00070 * 00071 * @retval -1 on failure. If the lock is already held, @c errno is set 00072 * to @c EBUSY. 00073 */ 00074 int tryacquire (void); 00075 00076 /// Release lock. 00077 int release (void); 00078 00079 /// Acquire read lock; blocks until the lock is acquired or the 00080 /// operation fails. 00081 int acquire_read (void); 00082 00083 /// Acquire write lock; blocks until the lock is acquired or the 00084 /// operation fails. 00085 int acquire_write (void); 00086 00087 /** 00088 * Try to acquire the read lock, but do not block if the lock is not 00089 * immediately acquired. 00090 * 00091 * @retval -1 on failure. If the lock is already held, @c errno is set 00092 * to @c EBUSY. 00093 */ 00094 int tryacquire_read (void); 00095 00096 /** 00097 * Try to acquire the write lock, but do not block if the lock is not 00098 * immediately acquired. 00099 * 00100 * @retval -1 on failure. If the lock is already held, @c errno is set 00101 * to @c EBUSY. 00102 */ 00103 int tryacquire_write (void); 00104 00105 /// Attempt to upgrade a read lock to a write lock. Returns 0 on 00106 /// success, -1 on failure. 00107 int tryacquire_write_upgrade (void); 00108 00109 /// Return the underlying lock. 00110 const ACE_File_Lock &lock (void) const; 00111 00112 /// Dump the state of an object. 00113 void dump (void) const; 00114 00115 /// Declare the dynamic allocation hooks. 00116 ACE_ALLOC_HOOK_DECLARE; 00117 00118 private: 00119 /// If the user does not provide a name we generate a unique name in 00120 /// this buffer. 00121 ACE_TCHAR name_[ACE_UNIQUE_NAME_LEN]; 00122 00123 /// Create and return the unique name. 00124 const ACE_TCHAR *unique_name (void); 00125 00126 /// We need this to get the readers/writer semantics... 00127 ACE_File_Lock lock_; 00128 }; 00129 00130 ACE_END_VERSIONED_NAMESPACE_DECL 00131 00132 #if defined (__ACE_INLINE__) 00133 #include "ace/RW_Process_Mutex.inl" 00134 #endif /* __ACE_INLINE__ */ 00135 00136 #include /**/ "ace/post.h" 00137 #endif /* ACE_RW_PROCESS_MUTEX_H */