5 #ifndef ICE_UTIL_COND_H
6 #define ICE_UTIL_COND_H
12 #if defined(_WIN32) && !defined(ICE_HAS_WIN32_CONDVAR)
15 namespace IceUtilInternal
29 void post(
int = 1)
const;
44 template <
class T>
class Monitor;
84 template <
typename Lock>
inline void
85 wait(
const Lock& lock)
const
91 waitImpl(lock._mutex);
101 template <
typename Lock>
inline bool
108 return timedWaitImpl(lock._mutex, timeout);
119 #if defined(_WIN32) && !defined(ICE_HAS_WIN32_CONDVAR)
121 template <
typename M>
void
122 waitImpl(
const M& mutex)
const
126 typedef typename M::LockState LockState;
142 template <
typename M>
bool
143 timedWaitImpl(
const M& mutex,
const Time& timeout)
const
147 typedef typename M::LockState LockState;
154 bool rc = timedDowait(timeout);
167 template <
typename M>
void waitImpl(
const M&)
const;
168 template <
typename M>
bool timedWaitImpl(
const M&,
const Time&)
const;
173 # ifdef ICE_HAS_WIN32_CONDVAR
174 mutable CONDITION_VARIABLE _cond;
177 void preWait()
const;
178 void postWait(
bool)
const;
179 bool timedDowait(
const Time&)
const;
183 IceUtilInternal::Semaphore _gate;
184 IceUtilInternal::Semaphore _queue;
185 mutable long _blocked;
186 mutable long _unblocked;
193 mutable State _state;
196 mutable pthread_cond_t _cond;
203 # ifdef ICE_HAS_WIN32_CONDVAR
205 template <
typename M>
inline void
206 Cond::waitImpl(
const M& mutex)
const
208 typedef typename M::LockState LockState;
212 BOOL ok = SleepConditionVariableCS(&_cond, state.mutex, INFINITE);
217 throw ThreadSyscallException(__FILE__, __LINE__, GetLastError());
221 template <
typename M>
inline bool
222 Cond::timedWaitImpl(
const M& mutex,
const Time& timeout)
const
225 if(msTimeout < 0 || msTimeout > 0x7FFFFFFF)
230 typedef typename M::LockState LockState;
234 BOOL ok = SleepConditionVariableCS(&_cond, state.mutex,
static_cast<DWORD
>(msTimeout));
239 DWORD err = GetLastError();
241 if(err != ERROR_TIMEOUT)
243 throw ThreadSyscallException(__FILE__, __LINE__, err);
253 template <
typename M>
inline void
254 Cond::waitImpl(
const M& mutex)
const
256 typedef typename M::LockState LockState;
260 int rc = pthread_cond_wait(&_cond, state.mutex);
265 throw ThreadSyscallException(__FILE__, __LINE__, rc);
269 template <
typename M>
inline bool
270 Cond::timedWaitImpl(
const M& mutex,
const Time& timeout)
const
274 throw InvalidTimeoutException(__FILE__, __LINE__, timeout);
277 typedef typename M::LockState LockState;
293 ts.tv_sec = tv.tv_sec;
294 ts.tv_nsec = tv.tv_usec * 1000;
295 int rc = pthread_cond_timedwait(&_cond, state.mutex, &ts);
306 throw ThreadSyscallException(__FILE__, __LINE__, rc);