Add g_source_get_time()

Cached version of g_get_monotonic_time() that does similar to what
g_source_get_current_time() does for g_get_current_time().
This commit is contained in:
Ryan Lortie 2010-10-22 18:40:08 +02:00
parent bf941f200c
commit b7d8363fbe
4 changed files with 50 additions and 1 deletions

View File

@ -535,6 +535,7 @@ GSourceFunc
g_source_set_callback_indirect g_source_set_callback_indirect
g_source_add_poll g_source_add_poll
g_source_remove_poll g_source_remove_poll
g_source_get_time
g_source_get_current_time g_source_get_current_time
g_source_remove g_source_remove
g_source_remove_by_funcs_user_data g_source_remove_by_funcs_user_data

View File

@ -725,6 +725,7 @@ g_source_attach
g_source_destroy g_source_destroy
g_source_get_can_recurse g_source_get_can_recurse
g_source_get_context g_source_get_context
g_source_get_time
g_source_get_current_time g_source_get_current_time
g_source_get_id g_source_get_id
g_source_get_name g_source_get_name

View File

@ -264,6 +264,8 @@ struct _GMainContext
GPollFunc poll_func; GPollFunc poll_func;
GTimeSpec time;
gboolean time_is_fresh;
GTimeVal current_time; GTimeVal current_time;
gboolean current_time_is_fresh; gboolean current_time_is_fresh;
}; };
@ -608,6 +610,7 @@ g_main_context_new (void)
context->pending_dispatches = g_ptr_array_new (); context->pending_dispatches = g_ptr_array_new ();
context->time_is_fresh = FALSE;
context->current_time_is_fresh = FALSE; context->current_time_is_fresh = FALSE;
#ifdef G_THREADS_ENABLED #ifdef G_THREADS_ENABLED
@ -2474,6 +2477,7 @@ g_main_context_prepare (GMainContext *context,
LOCK_CONTEXT (context); LOCK_CONTEXT (context);
context->time_is_fresh = FALSE;
context->current_time_is_fresh = FALSE; context->current_time_is_fresh = FALSE;
if (context->in_check_or_prepare) if (context->in_check_or_prepare)
@ -2638,7 +2642,10 @@ g_main_context_query (GMainContext *context,
{ {
*timeout = context->timeout; *timeout = context->timeout;
if (*timeout != 0) if (*timeout != 0)
context->current_time_is_fresh = FALSE; {
context->time_is_fresh = FALSE;
context->current_time_is_fresh = FALSE;
}
} }
UNLOCK_CONTEXT (context); UNLOCK_CONTEXT (context);
@ -3385,6 +3392,44 @@ g_source_get_current_time (GSource *source,
UNLOCK_CONTEXT (context); UNLOCK_CONTEXT (context);
} }
/**
* g_source_get_time:
* @source: a #GSource
* @timespec: #GTimeSpec structure in which to store the time
*
* Gets the time to be used when checking this source. The advantage of
* calling this function over calling g_get_monotonic_time() directly is
* that when checking multiple sources, GLib can cache a single value
* instead of having to repeatedly get the system monotonic time.
*
* The time here is the system monotonic time, if available, or some
* other reasonable alternative otherwise. See g_get_monotonic_time().
*
* Since: 2.28
**/
void
g_source_get_time (GSource *source,
GTimeSpec *timespec)
{
GMainContext *context;
g_return_if_fail (source->context != NULL);
context = source->context;
LOCK_CONTEXT (context);
if (!context->time_is_fresh)
{
g_get_monotonic_time (&context->time);
context->time_is_fresh = TRUE;
}
*timespec = context->time;
UNLOCK_CONTEXT (context);
}
/** /**
* g_main_context_set_poll_func: * g_main_context_set_poll_func:
* @context: a #GMainContext * @context: a #GMainContext

View File

@ -365,6 +365,8 @@ void g_source_remove_poll (GSource *source,
void g_source_get_current_time (GSource *source, void g_source_get_current_time (GSource *source,
GTimeVal *timeval); GTimeVal *timeval);
void g_source_get_time (GSource *source,
GTimeSpec *timespec);
/* void g_source_connect_closure (GSource *source, /* void g_source_connect_closure (GSource *source,
GClosure *closure); GClosure *closure);