00001 // -*- C++ -*- 00002 00003 //========================================================================== 00004 /** 00005 * @file RW_Mutex.h 00006 * 00007 * RW_Mutex.h,v 4.5 2005/10/28 16:14:55 ossama Exp 00008 * 00009 * Moved from Synch.h. 00010 * 00011 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00012 */ 00013 //========================================================================== 00014 00015 #ifndef ACE_RW_MUTEX_H 00016 #define ACE_RW_MUTEX_H 00017 00018 #include /**/ "ace/pre.h" 00019 00020 #include "ace/ACE_export.h" 00021 00022 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00023 # pragma once 00024 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00025 00026 // ACE platform supports some form of threading. 00027 #if defined (ACE_HAS_THREADS) 00028 00029 #include "ace/OS_NS_Thread.h" 00030 00031 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00032 00033 /** 00034 * @class ACE_RW_Mutex 00035 * 00036 * @brief Wrapper for readers/writer locks. 00037 * 00038 * These are most useful for applications that have many more 00039 * parallel readers than writers... 00040 */ 00041 class ACE_Export ACE_RW_Mutex 00042 { 00043 public: 00044 /// Initialize a readers/writer lock. 00045 ACE_RW_Mutex (int type = USYNC_THREAD, 00046 const ACE_TCHAR *name = 0, 00047 void *arg = 0); 00048 00049 /// Implicitly destroy a readers/writer lock 00050 ~ACE_RW_Mutex (void); 00051 00052 /** 00053 * Explicitly destroy a readers/writer lock. Note that only one 00054 * thread should call this method since it doesn't protect against 00055 * race conditions. 00056 */ 00057 int remove (void); 00058 00059 /// Acquire a read lock, but block if a writer hold the lock. 00060 int acquire_read (void); 00061 00062 /// Acquire a write lock, but block if any readers or a 00063 /// writer hold the lock. 00064 int acquire_write (void); 00065 00066 /** 00067 * Conditionally acquire a read lock (i.e., won't block). Returns 00068 * -1 on failure. If we "failed" because someone else already had 00069 * the lock, <errno> is set to <EBUSY>. 00070 */ 00071 int tryacquire_read (void); 00072 00073 /// Conditionally acquire a write lock (i.e., won't block). 00074 int tryacquire_write (void); 00075 00076 /** 00077 * Conditionally upgrade a read lock to a write lock. This only 00078 * works if there are no other readers present, in which case the 00079 * method returns 0. Otherwise, the method returns -1 and sets 00080 * <errno> to <EBUSY>. Note that the caller of this method *must* 00081 * already possess this lock as a read lock (but this condition is 00082 * not checked by the current implementation). 00083 */ 00084 int tryacquire_write_upgrade (void); 00085 00086 /** 00087 * Note, for interface uniformity with other synchronization 00088 * wrappers we include the <acquire> method. This is implemented as 00089 * a write-lock to safe... 00090 */ 00091 int acquire (void); 00092 00093 /** 00094 * Note, for interface uniformity with other synchronization 00095 * wrappers we include the <tryacquire> method. This is implemented 00096 * as a write-lock to be safe... Returns -1 on failure. If we 00097 * "failed" because someone else already had the lock, <errno> is 00098 * set to <EBUSY>. 00099 */ 00100 int tryacquire (void); 00101 00102 /// Unlock a readers/writer lock. 00103 int release (void); 00104 00105 /// Return the underlying lock. 00106 const ACE_rwlock_t &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 protected: 00115 /// Readers/writer lock. 00116 ACE_rwlock_t lock_; 00117 00118 /// Keeps track of whether <remove> has been called yet to avoid 00119 /// multiple <remove> calls, e.g., explicitly and implicitly in the 00120 /// destructor. This flag isn't protected by a lock, so make sure 00121 /// that you don't have multiple threads simultaneously calling 00122 /// <remove> on the same object, which is a bad idea anyway... 00123 int removed_; 00124 00125 private: 00126 // = Prevent assignment and initialization. 00127 void operator= (const ACE_RW_Mutex &); 00128 ACE_RW_Mutex (const ACE_RW_Mutex &); 00129 }; 00130 00131 ACE_END_VERSIONED_NAMESPACE_DECL 00132 00133 #if defined (__ACE_INLINE__) 00134 #include "ace/RW_Mutex.inl" 00135 #endif /* __ACE_INLINE__ */ 00136 00137 #endif /* ACE_HAS_THREADS */ 00138 00139 #include /**/ "ace/post.h" 00140 00141 #endif /* ACE_RW_MUTEX_H */