Search code examples
glibvala

About GLib.Idle on Vala


Valadoc is not very well documented in some parts, the namespace Idle in GLib has no description whats they do, there are only a few functions to define a priory level for idle event!

Someone know whats this does?

Functions:

public uint add (owned SourceFunc function, int priority = DEFAULT_IDLE)
public uint add_full (int priority, owned SourceFunc function)
public bool remove_by_data (void* data)

Solution

  • When in doubt refer to the C documentation:

    https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#g-idle-add

    Adds a function to be called whenever there are no higher priority events pending to the default main loop. The function is given the default idle priority, G_PRIORITY_DEFAULT_IDLE. If the function returns FALSE it is automatically removed from the list of event sources and will not be called again.

    See memory management of sources for details on how to handle the return value and memory management of data .

    This internally creates a main loop source using g_idle_source_new() and attaches it to the global GMainContext using g_source_attach(), so the callback will be invoked in whichever thread is running that main context. You can do these steps manually if you need greater control or to use a custom main context.

    In general you may want to read about the main loop:

    https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html#glib-The-Main-Event-Loop.description

    The main event loop manages all the available sources of events for GLib and GTK+ applications. These events can come from any number of different types of sources such as file descriptors (plain files, pipes or sockets) and timeouts. New types of event sources can also be added using g_source_attach().

    To allow multiple independent sets of sources to be handled in different threads, each source is associated with a GMainContext. A GMainContext can only be running in a single thread, but sources can be added to it and removed from it from other threads.

    Each event source is assigned a priority. The default priority, G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities. Values greater than 0 denote lower priorities. Events from high priority sources are always processed before events from lower priority sources.

    Idle functions can also be added, and assigned a priority. These will be run whenever no events with a higher priority are ready to be processed.

    [...]