add HLH_Ref
[HLH_utils.git] / HLH_Cond.cpp
blob4c71e9922d9f1e1a25c30ef7723c17fac7dc5a45
1 /*******************************************************************************
2 * File Name : HLH_Cond.cpp
3 *
4 * Author : Henry He
5 * Created Time : 2009-10-8 11:46:29
6 * Description :
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 /******************************************************************************
56 * Func : HLH_Cond
57 * Desc : Constructor of HLH_Cond
58 * Args : NONE
59 * Outs : NONE
60 ******************************************************************************/
61 HLH_Cond::HLH_Cond ()
63 // Notify that this object not initialized
64 m_bInited = false;
69 /******************************************************************************
70 * Func : ~HLH_Cond
71 * Desc : Deconstructor of HLH_Cond
72 * Args : NONE
73 * Outs : NONE
74 ******************************************************************************/
75 HLH_Cond::~ HLH_Cond ()
77 Destroy ();
83 /******************************************************************************
84 * Desc : Operations
85 ******************************************************************************/
89 /******************************************************************************
90 * Func : Init
91 * Desc : Initialize this condition
92 * Args : NONE
93 * Outs : if success, return 0, otherwise error code will be return
94 ******************************************************************************/
95 int HLH_Cond::Init ()
98 int ret;
100 // Check if already initialized
101 if (m_bInited) {
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);
108 if (ret < 0) {
109 HLH_DEBUG ( HLH_DEBUG_UTILS, ("can't init") );
110 ret = HLH_COND_ERR_CANT_INIT;
111 goto fail;
114 // Notify that this condition is initialized
115 m_bInited = true;
116 return 0;
118 fail:
119 return ret;
125 /******************************************************************************
126 * Func : HLH_Cond::Destory
127 * Desc : Destory this condition
128 * Args : NONE
129 * Outs : NONE
130 ******************************************************************************/
131 void HLH_Cond::Destroy ()
134 if (m_bInited) {
135 // Destory this condition
136 pthread_cond_destroy (&m_cCond);
137 // Notify that this condition not initialized
138 m_bInited = false;
144 /******************************************************************************
145 * Func : Wait
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)
152 int nRetVal;
154 if (!m_bInited)
155 return HLH_COND_ERR_NOT_INIT;
157 // Wait for this condition
158 nRetVal = pthread_cond_wait ( &m_cCond, &zhmMutex.m_mMutex );
159 if (nRetVal < 0) {
160 HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_wait failed") );
161 return HLH_COND_ERR_FAILED;
164 return 0;
168 /******************************************************************************
169 * Func : TimeWait
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)
177 int nRetVal;
178 struct timespec tsWait;
179 HLH_Time zhtAbs;
181 if (!m_bInited) {
182 HLH_DEBUG ( HLH_DEBUG_UTILS, ("not init") );
183 return HLH_COND_ERR_NOT_INIT;
186 // Set wait time
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);
193 if (nRetVal != 0) {
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") );
202 return 0;
206 /******************************************************************************
207 * Func : Signal
208 * Desc : Signal one waiter that the condition has reached
209 * Args : NONE
210 * Outs : If success, return 0, otherwise return error code
211 ******************************************************************************/
212 int HLH_Cond::Signal ()
214 int nRetVal;
216 // Check if initialized
217 if (!m_bInited) {
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);
224 if (nRetVal < 0) {
225 HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_signal failed") );
226 return HLH_COND_ERR_FAILED;
229 return 0;
232 /******************************************************************************
233 * Func : Broadcast
234 * Desc : Broadcast to all waiter that the condition has reached
235 * Args : NONE
236 * Outs : If success, return 0, otherwise return error code
237 ******************************************************************************/
238 int HLH_Cond::Broadcast ()
240 int nRetVal;
242 // Check if initialized
243 if (!m_bInited) {
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);
250 if (nRetVal < 0) {
251 HLH_DEBUG ( HLH_DEBUG_UTILS, ("pthread_cond_broadcast failed") );
252 return HLH_COND_ERR_FAILED;
255 return 0;