1 /*******************************************************************************
2 * File Name : HLH_Cond.cpp
5 * Created Time : 2009-10-8 11:46:29
7 ******************************************************************************/
10 /*******************************************************************************
11 * Desc : Includes Files
12 ******************************************************************************/
13 #include "HLH_utils/HLH_Cond.h"
16 /*******************************************************************************
17 * Desc : Macro Definations
18 ******************************************************************************/
21 /*******************************************************************************
22 * Desc : Type Definations
23 ******************************************************************************/
26 /*******************************************************************************
27 * Desc : Global Variables
28 ******************************************************************************/
31 /*******************************************************************************
32 * Desc : File Variables
33 ******************************************************************************/
41 /******************************************************************************
42 * Desc : Member Functions
43 ******************************************************************************/
48 /******************************************************************************
49 * Desc : Constructor / Deconstructor
50 ******************************************************************************/
55 /******************************************************************************
57 * Desc : Constructor of HLH_Cond
60 ******************************************************************************/
63 // Notify that this object not initialized
69 /******************************************************************************
71 * Desc : Deconstructor of HLH_Cond
74 ******************************************************************************/
75 HLH_Cond::~ HLH_Cond ()
83 /******************************************************************************
85 ******************************************************************************/
89 /******************************************************************************
91 * Desc : Initialize this condition
93 * Outs : if success, return 0, otherwise error code will be return
94 ******************************************************************************/
100 // Check if already initialized
102 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("already inited") );
103 return HLH_COND_ERR_ALREADY_INITED
;
106 // Initialize this condition
107 ret
= pthread_cond_init (&m_cCond
, NULL
);
109 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("can't init") );
110 ret
= HLH_COND_ERR_CANT_INIT
;
114 // Notify that this condition is initialized
125 /******************************************************************************
126 * Func : HLH_Cond::Destory
127 * Desc : Destory this condition
130 ******************************************************************************/
131 void HLH_Cond::Destroy ()
135 // Destory this condition
136 pthread_cond_destroy (&m_cCond
);
137 // Notify that this condition not initialized
144 /******************************************************************************
146 * Desc : Wait for this condition, called with zhmMutex locked
147 * Args : zhmMutex Mutex for this condition
148 * Outs : If success, return 0, otherwise return error code
149 ******************************************************************************/
150 int HLH_Cond::Wait (HLH_Mutex
&zhmMutex
)
155 return HLH_COND_ERR_NOT_INIT
;
157 // Wait for this condition
158 nRetVal
= pthread_cond_wait ( &m_cCond
, &zhmMutex
.m_mMutex
);
160 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("pthread_cond_wait failed") );
161 return HLH_COND_ERR_FAILED
;
168 /******************************************************************************
170 * Desc : Wait for condition until time reach, called with zhmMutex locked
171 * Args : zhmMutex Mutex for this condition
172 * zhtTime Max time will be waited
173 * Outs : If success, return 0; otherwise return error code
174 ******************************************************************************/
175 int HLH_Cond::TimeWait (HLH_Mutex
&zhmMutex
, const HLH_Time
&zhtTime
)
178 struct timespec tsWait
;
182 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("not init") );
183 return HLH_COND_ERR_NOT_INIT
;
187 zhtAbs
= HLH_Time::GetCurrentTime () + zhtTime
;
188 tsWait
.tv_sec
= (time_t) zhtAbs
.GetSeconds ();
189 tsWait
.tv_nsec
= (time_t) (zhtAbs
.GetMicroSeconds () * 1000);
191 // Wait until timeout
192 nRetVal
= pthread_cond_timedwait (&m_cCond
, &zhmMutex
.m_mMutex
, &tsWait
);
194 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("pthread_cond_timedwait failed") );
195 return HLH_COND_ERR_FAILED
;
198 if (tsWait
.tv_sec
== 0 && tsWait
.tv_nsec
== 0) {
199 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("timeout") );
206 /******************************************************************************
208 * Desc : Signal one waiter that the condition has reached
210 * Outs : If success, return 0, otherwise return error code
211 ******************************************************************************/
212 int HLH_Cond::Signal ()
216 // Check if initialized
218 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("not init") );
219 return HLH_COND_ERR_NOT_INIT
;
222 // Signal this condition
223 nRetVal
= pthread_cond_signal (&m_cCond
);
225 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("pthread_cond_signal failed") );
226 return HLH_COND_ERR_FAILED
;
232 /******************************************************************************
234 * Desc : Broadcast to all waiter that the condition has reached
236 * Outs : If success, return 0, otherwise return error code
237 ******************************************************************************/
238 int HLH_Cond::Broadcast ()
242 // Check if initialized
244 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("not init") );
245 return HLH_COND_ERR_NOT_INIT
;
248 // Broadcast this condition
249 nRetVal
= pthread_cond_broadcast (&m_cCond
);
251 HLH_DEBUG ( HLH_DEBUG_UTILS
, ("pthread_cond_broadcast failed") );
252 return HLH_COND_ERR_FAILED
;