static gboolean g_timeout_prepare (gpointer source_data, GTimeVal *current_time, gint *timeout, gpointer user_data) { glong msec; GTimeoutData *data = source_data; msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 + (data->expiration.tv_usec - current_time->tv_usec) / 1000; if (msec < 0) msec = 0; else if (msec > data->interval) { /* The system time has been set backwards, so we * reset the expiration time to now + data->interval; * this at least avoids hanging for long periods of time. */ g_timeout_set_expiration (data, current_time); msec = data->interval; } *timeout = msec; return (msec == 0); }
static gboolean g_timeout_prepare (GSource *source, gint *timeout) { glong sec; glong msec; GTimeVal current_time; GTimeoutSource *timeout_source = (GTimeoutSource *)source; g_source_get_current_time (source, ¤t_time); sec = timeout_source->expiration.tv_sec - current_time.tv_sec; msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000; /* We do the following in a rather convoluted fashion to deal with * the fact that we don't have an integral type big enough to hold * the difference of two timevals in millseconds. */ if (sec < 0 || (sec == 0 && msec < 0)) msec = 0; else { glong interval_sec = timeout_source->interval / 1000; glong interval_msec = timeout_source->interval % 1000; if (msec < 0) { msec += 1000; sec -= 1; } if (sec > interval_sec || (sec == interval_sec && msec > interval_msec)) { /* The system time has been set backwards, so we * reset the expiration time to now + timeout_source->interval; * this at least avoids hanging for long periods of time. */ g_timeout_set_expiration (timeout_source, ¤t_time); msec = MIN (G_MAXINT, timeout_source->interval); } else { msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec); } } *timeout = (gint)msec; return msec == 0; }
GSource * g_timeout2_source_new (guint interval) { GSource *source = g_source_new (&g_timeout2_funcs, sizeof (GTimeoutSource)); GTimeoutSource *timeout_source = (GTimeoutSource *)source; GTimeVal current_time; timeout_source->interval = interval; g_get_current_time (¤t_time); g_timeout_set_expiration (timeout_source, ¤t_time); return source; }
static gboolean g_timeout_dispatch (GSource *source, GSourceFunc callback, gpointer user_data) { #if 0 // as GLib GTimeoutSource *timeout_source = (GTimeoutSource *)source; if (!callback) { g_warning ("Timeout source dispatched without callback\n" "You must call g_source_set_callback()."); return FALSE; } if (callback (user_data)) { GTimeVal current_time; g_source_get_current_time (source, ¤t_time); g_timeout_set_expiration (timeout_source, ¤t_time); return TRUE; } else return FALSE; #else GTimeoutSource *timeout_source = (GTimeoutSource *)source; GTimeVal current_time; g_source_get_current_time (source, ¤t_time); g_timeout_set_expiration (timeout_source, ¤t_time); return (callback (user_data)); #endif }
static gboolean g_timeout_dispatch (gpointer source_data, GTimeVal *dispatch_time, gpointer user_data) { GTimeoutData *data = source_data; if (data->callback (user_data)) { g_timeout_set_expiration (data, dispatch_time); return TRUE; } else return FALSE; }
guint g_timeout_add_full (gint priority, guint interval, GSourceFunc function, gpointer data, GDestroyNotify notify) { GTimeoutData *timeout_data = g_new (GTimeoutData, 1); GTimeVal current_time; timeout_data->interval = interval; timeout_data->callback = function; g_get_current_time (¤t_time); g_timeout_set_expiration (timeout_data, ¤t_time); return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify); }