00001 // -*- C++ -*- 00002 00003 //========================================================================== 00004 /** 00005 * @file Condition_Thread_Mutex.h 00006 * 00007 * Condition_Thread_Mutex.h,v 4.3 2005/10/28 16:14:51 ossama Exp 00008 * 00009 * Moved from Synch.h. 00010 * 00011 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00012 */ 00013 //========================================================================== 00014 00015 #ifndef ACE_CONDITION_THREAD_MUTEX_H 00016 #define ACE_CONDITION_THREAD_MUTEX_H 00017 #include /**/ "ace/pre.h" 00018 00019 #include "ace/ACE_export.h" 00020 00021 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00022 # pragma once 00023 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00024 00025 #if !defined (ACE_HAS_THREADS) 00026 # include "ace/Null_Condition.h" 00027 #else /* ACE_HAS_THREADS */ 00028 // ACE platform supports some form of threading. 00029 00030 #include "ace/Thread_Mutex.h" 00031 00032 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00033 00034 class ACE_Time_Value; 00035 00036 class ACE_Export ACE_Condition_Attributes 00037 { 00038 public: 00039 /// Constructor 00040 ACE_Condition_Attributes (int type = ACE_DEFAULT_SYNCH_TYPE); 00041 00042 /// Destructor 00043 ~ACE_Condition_Attributes (void); 00044 00045 private: 00046 friend class ACE_Condition_Thread_Mutex; 00047 00048 /// The attributes 00049 ACE_condattr_t attributes_; 00050 00051 private: 00052 // = Prevent assignment and initialization. 00053 void operator= (const ACE_Condition_Attributes &); 00054 ACE_Condition_Attributes (const ACE_Condition_Attributes &); 00055 }; 00056 00057 /** 00058 * @class ACE_Condition_Thread_Mutex 00059 * 00060 * @brief ACE_Condition variable wrapper written using ACE_Mutexes This 00061 * allows threads to block until shared data changes state. 00062 * A condition variable enables threads to atomically block and 00063 * test the condition under the protection of a mutual exclu- 00064 * sion lock (mutex) until the condition is satisfied. That is, 00065 * the mutex must have been held by the thread before calling 00066 * wait or signal on the condition. If the condition is false, 00067 * a thread blocks on a condition variable and atomically 00068 * releases the mutex that is waiting for the condition to 00069 * change. If another thread changes the condition, it may wake 00070 * up waiting threads by signaling the associated condition 00071 * variable. The waiting threads, upon awakening, reacquire the 00072 * mutex and re-evaluate the condition. 00073 * 00074 * This should be an instantiation of ACE_Condition but problems 00075 * with compilers precludes this... 00076 */ 00077 class ACE_Export ACE_Condition_Thread_Mutex 00078 { 00079 public: 00080 /// Initialize the condition variable. 00081 ACE_Condition_Thread_Mutex (const ACE_Thread_Mutex &m, 00082 const ACE_TCHAR *name = 0, 00083 void *arg = 0); 00084 00085 /// Initialize the condition variable. 00086 ACE_Condition_Thread_Mutex (const ACE_Thread_Mutex &m, 00087 ACE_Condition_Attributes &attributes, 00088 const ACE_TCHAR *name = 0, 00089 void *arg = 0); 00090 00091 /// Implicitly destroy the condition variable. 00092 ~ACE_Condition_Thread_Mutex (void); 00093 00094 /** 00095 * Explicitly destroy the condition variable. Note that only one 00096 * thread should call this method since it doesn't protect against 00097 * race conditions. 00098 */ 00099 int remove (void); 00100 00101 /** 00102 * Block on condition, or until absolute time-of-day has passed. If 00103 * abstime == 0 use "blocking" <wait> semantics. Else, if <abstime> 00104 * != 0 and the call times out before the condition is signaled 00105 * <wait> returns -1 and sets errno to ETIME. 00106 */ 00107 int wait (const ACE_Time_Value *abstime); 00108 00109 /// Block on condition. 00110 int wait (void); 00111 00112 /** 00113 * Block on condition or until absolute time-of-day has passed. If 00114 * abstime == 0 use "blocking" wait() semantics on the <mutex> 00115 * passed as a parameter (this is useful if you need to store the 00116 * <Condition> in shared memory). Else, if <abstime> != 0 and the 00117 * call times out before the condition is signaled <wait> returns -1 00118 * and sets errno to ETIME. 00119 */ 00120 int wait (ACE_Thread_Mutex &mutex, const ACE_Time_Value *abstime = 0); 00121 00122 /// Signal one waiting thread. 00123 int signal (void); 00124 00125 /// Signal *all* waiting threads. 00126 int broadcast (void); 00127 00128 /// Returns a reference to the underlying mutex; 00129 ACE_Thread_Mutex &mutex (void); 00130 00131 /// Dump the state of an object. 00132 void dump (void) const; 00133 00134 /// Declare the dynamic allocation hooks. 00135 ACE_ALLOC_HOOK_DECLARE; 00136 00137 protected: 00138 /// Condition variable. 00139 ACE_cond_t cond_; 00140 00141 /// Reference to mutex lock. 00142 ACE_Thread_Mutex &mutex_; 00143 00144 /// Keeps track of whether <remove> has been called yet to avoid 00145 /// multiple <remove> calls, e.g., explicitly and implicitly in the 00146 /// destructor. This flag isn't protected by a lock, so make sure 00147 /// that you don't have multiple threads simultaneously calling 00148 /// <remove> on the same object, which is a bad idea anyway... 00149 int removed_; 00150 00151 private: 00152 // = Prevent assignment and initialization. 00153 void operator= (const ACE_Condition_Thread_Mutex &); 00154 ACE_Condition_Thread_Mutex (const ACE_Condition_Thread_Mutex &); 00155 }; 00156 00157 #if 0 00158 // The following class is commented out since there doesn't 00159 // appear to be a portable and robust means of implementing this 00160 // functionality across platforms. If you know of a portable and 00161 // robust way to implement this functionality please let us know. 00162 00163 /** 00164 * @class ACE_Process_Condition 00165 * 00166 * @brief ACE_Condition variable wrapper that works across processes. 00167 */ 00168 class ACE_Export ACE_Process_Condition 00169 { 00170 public: 00171 ACE_Process_Condition (MUTEX &m, const ACE_TCHAR *name = 0, void *arg = 0); 00172 00173 /// Dump the state of an object. 00174 void dump (void) const; 00175 00176 // ACE_ALLOC_HOOK_DECLARE; 00177 // Declare the dynamic allocation hooks. 00178 }; 00179 #endif /* 0 */ 00180 00181 ACE_END_VERSIONED_NAMESPACE_DECL 00182 00183 #if defined (__ACE_INLINE__) 00184 #include "ace/Condition_Thread_Mutex.inl" 00185 #endif /* __ACE_INLINE__ */ 00186 00187 #endif /* !ACE_HAS_THREADS */ 00188 00189 #include /**/ "ace/post.h" 00190 #endif /* ACE_CONDITION_THREAD_MUTEX_H */