3333#include < errno.h>
3434#include < pthread.h>
3535#include < sys/time.h>
36- #include < time.h>
3736#include < stdio.h>
38- #ifdef __APPLE__
39- #include < mach/mach_time.h>
40- #endif
4137#define HXCPP_PTHREADS
4238#endif
4339
@@ -219,12 +215,6 @@ struct HxSemaphore
219215
220216#define HX_THREAD_SEMAPHORE_LOCKABLE
221217
222- // Timed waits run on a clock that changing the system time does not move.
223- // Apple has no condition clock, so it waits for the time left instead.
224- #if !defined(__APPLE__) && (!defined(__ANDROID__) || __ANDROID_API__ >= 21)
225- #define HX_THREAD_COND_MONOTONIC
226- #endif
227-
228218struct HxSemaphore
229219{
230220 HxMutex mMutex ;
@@ -236,15 +226,7 @@ struct HxSemaphore
236226 {
237227 mSet = false ;
238228 mCondition = new pthread_cond_t ();
239- #ifdef HX_THREAD_COND_MONOTONIC
240- pthread_condattr_t attr;
241- pthread_condattr_init (&attr);
242- pthread_condattr_setclock (&attr, CLOCK_MONOTONIC );
243- pthread_cond_init (mCondition ,&attr);
244- pthread_condattr_destroy (&attr);
245- #else
246229 pthread_cond_init (mCondition ,0 );
247- #endif
248230 }
249231 ~HxSemaphore ()
250232 {
@@ -286,63 +268,31 @@ struct HxSemaphore
286268 pthread_cond_wait ( mCondition , mMutex .mMutex );
287269 mSet = false ;
288270 }
289- static void Now (timespec &outNow)
290- {
291- #ifdef __APPLE__
292- static mach_timebase_info_data_t timebase;
293- if (!timebase.denom )
294- mach_timebase_info (&timebase);
295- unsigned long long ns = mach_absolute_time () * timebase.numer / timebase.denom ;
296- outNow.tv_sec = ns / 1000000000 ;
297- outNow.tv_nsec = ns % 1000000000 ;
298- #elif defined(HX_THREAD_COND_MONOTONIC)
299- clock_gettime (CLOCK_MONOTONIC , &outNow);
300- #else
301- clock_gettime (CLOCK_REALTIME , &outNow);
302- #endif
303- }
304- int TimedWait (const timespec &inDeadline)
305- {
306- #ifdef __APPLE__
307- timespec now;
308- Now (now);
309- timespec left;
310- left.tv_sec = inDeadline.tv_sec - now.tv_sec ;
311- left.tv_nsec = inDeadline.tv_nsec - now.tv_nsec ;
312- if (left.tv_nsec <0 )
313- {
314- left.tv_nsec += 1000000000 ;
315- left.tv_sec --;
316- }
317- if (left.tv_sec <0 )
318- return ETIMEDOUT ;
319- return pthread_cond_timedwait_relative_np ( mCondition , mMutex .mMutex , &left );
320- #else
321- return pthread_cond_timedwait ( mCondition , mMutex .mMutex , &inDeadline );
322- #endif
323- }
324271 // Returns true if the wait was success, false on timeout.
325272 bool WaitSeconds (double inSeconds)
326273 {
274+ struct timeval tv;
275+ gettimeofday (&tv, 0 );
276+
327277 // double->int is undefined past 2^31
328278 if (inSeconds > 0x7fffffff )
329279 inSeconds = 0x7fffffff ;
330280 int isec = (int )inSeconds;
281+ int usec = (int )((inSeconds-isec)*1000000.0 );
331282 timespec spec;
332- Now (spec);
333- spec.tv_nsec += (long )((inSeconds-isec)*1000000000.0 );
283+ spec.tv_nsec = (tv.tv_usec + usec) * 1000 ;
334284 if (spec.tv_nsec >=1000000000 )
335285 {
336286 spec.tv_nsec -=1000000000 ;
337287 isec++;
338288 }
339- spec.tv_sec += isec;
289+ spec.tv_sec = tv. tv_sec + isec;
340290
341291 AutoLock lock (mMutex );
342292
343293 int result = 0 ;
344294 // Wait for set to be true...
345- while ( !mSet && (result=TimedWait ( spec)) != ETIMEDOUT )
295+ while ( !mSet && (result=pthread_cond_timedwait ( mCondition , mMutex . mMutex , & spec )) != ETIMEDOUT )
346296 {
347297 if (result!=0 )
348298 {
0 commit comments