00001 // -*- C++ -*- 00002 00003 //========================================================================== 00004 /** 00005 * @file Condition_T.h 00006 * 00007 * Condition_T.h,v 4.4 2006/05/09 07:19:10 jwillemsen Exp 00008 * 00009 * Moved from Synch.h. 00010 * 00011 * @author Douglas C. Schmidt <schmidt@cs.wustl.edu> 00012 */ 00013 //========================================================================== 00014 00015 #ifndef ACE_CONDITION_T_H 00016 #define ACE_CONDITION_T_H 00017 00018 #include /**/ "ace/pre.h" 00019 00020 #include "ace/OS_NS_Thread.h" 00021 #include "ace/Lock.h" 00022 00023 #if !defined (ACE_LACKS_PRAGMA_ONCE) 00024 # pragma once 00025 #endif /* ACE_LACKS_PRAGMA_ONCE */ 00026 00027 #if defined (ACE_HAS_THREADS) /* ACE platform supports some form of threading. */ 00028 00029 ACE_BEGIN_VERSIONED_NAMESPACE_DECL 00030 00031 class ACE_Time_Value; 00032 00033 /** 00034 * @class ACE_Condition 00035 * 00036 * @brief ACE_Condition variable wrapper, which allows threads to block 00037 * until shared data changes state. 00038 * 00039 * A condition variable enables threads to atomically block and 00040 * test the condition under the protection of a mutual exclu- 00041 * sion lock (mutex) until the condition is satisfied. That is, 00042 * the mutex must have been held by the thread before calling 00043 * wait or signal on the condition. If the condition is false, 00044 * a thread blocks on a condition variable and atomically 00045 * releases the mutex that is waiting for the condition to 00046 * change. If another thread changes the condition, it may wake 00047 * up waiting threads by signaling the associated condition 00048 * variable. The waiting threads, upon awakening, reacquire the 00049 * mutex and re-evaluate the condition. 00050 * Note, you can only parameterize <ACE_Condition> with 00051 * @a ACE_Thread_Mutex, @a ACE_Recursive_Thread_Mutex, or @a ACE_Null_Mutex. 00052 */ 00053 template <class MUTEX> 00054 class ACE_Condition 00055 { 00056 public: 00057 // = Initialiation and termination methods. 00058 /// Initialize the condition variable. 00059 ACE_Condition (MUTEX &m, int type = USYNC_THREAD, 00060 const ACE_TCHAR *name = 0, void *arg = 0); 00061 00062 /// Implicitly destroy the condition variable. 00063 ~ACE_Condition (void); 00064 00065 // = Lock accessors. 00066 /** 00067 * Block on condition, or until absolute time-of-day has passed. If 00068 * abstime == 0 use "blocking" <wait> semantics. Else, if <abstime> 00069 * != 0 and the call times out before the condition is signaled 00070 * <wait> returns -1 and sets errno to ETIME. 00071 */ 00072 int wait (const ACE_Time_Value *abstime); 00073 00074 /// Block on condition. 00075 int wait (void); 00076 00077 /** 00078 * Block on condition or until absolute time-of-day has passed. If 00079 * abstime == 0 use "blocking" wait() semantics on the <mutex> 00080 * passed as a parameter (this is useful if you need to store the 00081 * <Condition> in shared memory). Else, if <abstime> != 0 and the 00082 * call times out before the condition is signaled <wait> returns -1 00083 * and sets errno to ETIME. 00084 */ 00085 int wait (MUTEX &mutex, const ACE_Time_Value *abstime = 0); 00086 00087 /// Signal one waiting thread. 00088 int signal (void); 00089 00090 /// Signal *all* waiting threads. 00091 int broadcast (void); 00092 00093 // = Utility methods. 00094 /// Explicitly destroy the condition variable. 00095 int remove (void); 00096 00097 /// Returns a reference to the underlying mutex_; 00098 MUTEX &mutex (void); 00099 00100 /// Dump the state of an object. 00101 void dump (void) const; 00102 00103 // ACE_ALLOC_HOOK_DECLARE; 00104 // Declare the dynamic allocation hooks. 00105 00106 protected: 00107 /// Condition variable. 00108 ACE_cond_t cond_; 00109 00110 /// Reference to mutex lock. 00111 MUTEX &mutex_; 00112 00113 private: 00114 // = Prevent assignment and initialization. 00115 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Condition<MUTEX> &)) 00116 ACE_UNIMPLEMENTED_FUNC (ACE_Condition (const ACE_Condition<MUTEX> &)) 00117 }; 00118 00119 /** 00120 * @class ACE_Thread_Condition 00121 * 00122 * @brief ACE_Condition variable wrapper that works within processes. 00123 * 00124 * A condition variable enables threads to atomically block and 00125 * test the condition under the protection of a mutual exclu- 00126 * sion lock (mutex) until the condition is satisfied. That is, 00127 * the mutex must have been held by the thread before calling 00128 * wait or signal on the condition. If the condition is false, 00129 * a thread blocks on a condition variable and atomically 00130 * releases the mutex that is waiting for the condition to 00131 * change. If another thread changes the condition, it may wake 00132 * up waiting threads by signaling the associated condition 00133 * variable. The waiting threads, upon awakening, reacquire the 00134 * mutex and re-evaluate the condition. 00135 */ 00136 template <class MUTEX> 00137 class ACE_Thread_Condition : public ACE_Condition<MUTEX> 00138 { 00139 public: 00140 // = Initialization method. 00141 ACE_Thread_Condition (MUTEX &m, const ACE_TCHAR *name = 0, void *arg = 0); 00142 00143 /// Dump the state of an object. 00144 void dump (void) const; 00145 00146 // ACE_ALLOC_HOOK_DECLARE; 00147 // Declare the dynamic allocation hooks. 00148 }; 00149 00150 ACE_END_VERSIONED_NAMESPACE_DECL 00151 00152 #if defined (__ACE_INLINE__) 00153 #include "ace/Condition_T.inl" 00154 #endif /* __ACE_INLINE__ */ 00155 00156 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE) 00157 #include "ace/Condition_T.cpp" 00158 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */ 00159 00160 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA) 00161 #pragma implementation ("Condition_T.cpp") 00162 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */ 00163 00164 #endif /* ACE_HAS_THREADS */ 00165 00166 #include /**/ "ace/post.h" 00167 #endif /* ACE_CONDITION_T_H */