11 #ifndef MI_BASE_CONDITION_H
12 #define MI_BASE_CONDITION_H
17 #ifndef MI_PLATFORM_WINDOWS
22 #include <mi/base/miwindows.h>
40 #ifndef MI_PLATFORM_WINDOWS
42 pthread_mutex_init( &m_mutex, NULL);
43 pthread_cond_init( &m_condvar, NULL);
45 m_handle = CreateEvent( NULL,
false,
false, NULL);
52 #ifndef MI_PLATFORM_WINDOWS
53 pthread_mutex_destroy( &m_mutex);
54 pthread_cond_destroy( &m_condvar);
56 CloseHandle( m_handle);
65 #ifndef MI_PLATFORM_WINDOWS
66 pthread_mutex_lock( &m_mutex);
68 pthread_cond_wait( &m_condvar, &m_mutex);
70 pthread_mutex_unlock( &m_mutex);
72 WaitForSingleObject( m_handle, INFINITE);
84 #ifndef MI_PLATFORM_WINDOWS
86 gettimeofday( &now, NULL);
87 struct timespec timeout_abs;
88 timeout_abs.tv_sec = now.tv_sec +
static_cast<long>(
floor( timeout));
90 = 1000 * now.tv_usec +
static_cast<long>( 1E9 * ( timeout -
floor( timeout)));
91 if( timeout_abs.tv_nsec > 1000000000) {
92 timeout_abs.tv_sec += 1;
93 timeout_abs.tv_nsec -= 1000000000;
96 bool timed_out =
false;
97 pthread_mutex_lock( &m_mutex);
100 int result = pthread_cond_timedwait( &m_condvar, &m_mutex, &timeout_abs);
101 timed_out = result == ETIMEDOUT;
106 pthread_mutex_unlock( &m_mutex);
109 DWORD timeout_ms =
static_cast<DWORD
>( 1000 * timeout);
110 DWORD result = WaitForSingleObject( m_handle, timeout_ms);
111 return result == WAIT_TIMEOUT;
125 #ifndef MI_PLATFORM_WINDOWS
126 pthread_mutex_lock( &m_mutex);
128 pthread_cond_signal( &m_condvar);
129 pthread_mutex_unlock( &m_mutex);
140 #ifndef MI_PLATFORM_WINDOWS
141 pthread_mutex_lock( &m_mutex);
143 pthread_mutex_unlock( &m_mutex);
145 ResetEvent( m_handle);
150 #ifndef MI_PLATFORM_WINDOWS
151 pthread_mutex_t m_mutex;
154 pthread_cond_t m_condvar;
169 #endif // MI_BASE_CONDITION_H