- ts.tv_sec = end_time / 1000000;
- ts.tv_nsec = (end_time % 1000000) * 1000;
-
- if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
- return TRUE;
+#ifdef HAVE_PTHREAD_COND_TIMEDWAIT_RELATIVE_NP
+ /* end_time is given relative to the monotonic clock as returned by
+ * g_get_monotonic_time().
+ *
+ * Since this pthreads wants the relative time, convert it back again.
+ */
+ {
+ gint64 now = g_get_monotonic_time ();
+ gint64 relative;
+
+ if (end_time <= now)
+ return FALSE;
+
+ relative = end_time - now;
+
+ ts.tv_sec = relative / 1000000;
+ ts.tv_nsec = (relative % 1000000) * 1000;
+
+ if ((status = pthread_cond_timedwait_relative_np (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
+ return TRUE;
+ }
+#elif defined (HAVE_PTHREAD_CONDATTR_SETCLOCK) && defined (CLOCK_MONOTONIC)
+ /* This is the exact check we used during init to set the clock to
+ * monotonic, so if we're in this branch, timedwait() will already be
+ * expecting a monotonic clock.
+ */
+ {
+ ts.tv_sec = end_time / 1000000;
+ ts.tv_nsec = (end_time % 1000000) * 1000;
+
+ if ((status = pthread_cond_timedwait (g_cond_get_impl (cond), g_mutex_get_impl (mutex), &ts)) == 0)
+ return TRUE;
+ }
+#else
+#error Cannot support GCond on your platform.
+#endif