typedef typedefG_BEGIN_DECLS struct _GMainContext | GMainContext |
typedef struct _GMainLoop | GMainLoop |
typedef struct _GSource | GSource |
typedef struct _GSourceCallbackFuncs | GSourceCallbackFuncs |
typedef struct _GSourceFuncs | GSourceFuncs |
typedef gboolean(* | GSourceFunc |
typedef void(* | GChildWatchFunc |
typedef void(* | GSourceDummyMarshal |
IMPORT_C GMainContext * | g_main_context_new | ( | void | ) |
IMPORT_C GMainContext * | g_main_context_ref | ( | GMainContext * | context | ) |
g_main_context_ref: : a GMainContext
Increases the reference count on a GMainContext object by one.
Returns: the that was passed in (since 2.6)
IMPORT_C void | g_main_context_unref | ( | GMainContext * | context | ) |
g_main_context_unref: : a GMainContext
Decreases the reference count on a GMainContext object by one. If the result is zero, free the context and free all associated memory.
IMPORT_C GMainContext * | g_main_context_default | ( | void | ) |
g_main_context_default:
Returns the default main context. This is the main context used for main loop functions when a main loop is not explicitly specified.
Return value: the default main context.
IMPORT_C gboolean | g_main_context_iteration | ( | GMainContext * | context, |
gboolean | may_block | |||
) |
IMPORT_C gboolean | g_main_context_pending | ( | GMainContext * | context | ) |
IMPORT_C GSource * | g_main_context_find_source_by_id | ( | GMainContext * | context, |
guint | source_id | |||
) |
g_main_context_find_source_by_id: : a GMainContext (if NULL, the default context will be used) : the source ID, as returned by g_source_get_id().
Finds a GSource given a pair of context and ID.
Return value: the GSource if found, otherwise, NULL
IMPORT_C GSource * | g_main_context_find_source_by_user_data | ( | GMainContext * | context, |
gpointer | user_data | |||
) |
g_main_context_find_source_by_user_data: : a GMainContext : the user_data for the callback.
Finds a source with the given user data for the callback. If multiple sources exist with the same user data, the first one found will be returned.
Return value: the source, if one was found, otherwise NULL
IMPORT_C GSource * | g_main_context_find_source_by_funcs_user_data | ( | GMainContext * | context, |
GSourceFuncs * | funcs, | |||
gpointer | user_data | |||
) |
g_main_context_find_source_by_funcs_user_data: : a GMainContext (if NULL, the default context will be used). : the passed to g_source_new(). : the user data from the callback.
Finds a source with the given source functions and user data. If multiple sources exist with the same source function and user data, the first one found will be returned.
Return value: the source, if one was found, otherwise NULL
IMPORT_C void | g_main_context_wakeup | ( | GMainContext * | context | ) |
IMPORT_C gboolean | g_main_context_acquire | ( | GMainContext * | context | ) |
g_main_context_acquire: : a GMainContext
Tries to become the owner of the specified context. If some other thread is the owner of the context, returns FALSE immediately. Ownership is properly recursive: the owner can require ownership again and will release ownership when g_main_context_release() is called as many times as g_main_context_acquire().
You must be the owner of a context before you can call g_main_context_prepare(), g_main_context_query(), g_main_context_check(), g_main_context_dispatch().
Return value: TRUE if the operation succeeded, and this thread is now the owner of .
IMPORT_C void | g_main_context_release | ( | GMainContext * | context | ) |
g_main_context_release: : a GMainContext
Releases ownership of a context previously acquired by this thread with g_main_context_acquire(). If the context was acquired multiple times, the ownership will be released only when g_main_context_release() is called as many times as it was acquired.
IMPORT_C gboolean | g_main_context_is_owner | ( | GMainContext * | context | ) |
IMPORT_C gboolean | g_main_context_wait | ( | GMainContext * | context, |
GCond * | cond, | |||
GMutex * | mutex | |||
) |
IMPORT_C gboolean | g_main_context_prepare | ( | GMainContext * | context, |
gint * | priority | |||
) |
IMPORT_C gint | g_main_context_query | ( | GMainContext * | context, |
gint | max_priority, | |||
gint * | timeout_, | |||
GPollFD * | fds, | |||
gint | n_fds | |||
) |
IMPORT_C gint | g_main_context_check | ( | GMainContext * | context, |
gint | max_priority, | |||
GPollFD * | fds, | |||
gint | n_fds | |||
) |
IMPORT_C void | g_main_context_dispatch | ( | GMainContext * | context | ) |
IMPORT_C void | g_main_context_set_poll_func | ( | GMainContext * | context, |
GPollFunc | func | |||
) |
IMPORT_C GPollFunc | g_main_context_get_poll_func | ( | GMainContext * | context | ) |
IMPORT_C void | g_main_context_add_poll | ( | GMainContext * | context, |
GPollFD * | fd, | |||
gint | priority | |||
) |
IMPORT_C void | g_main_context_remove_poll | ( | GMainContext * | context, |
GPollFD * | fd | |||
) |
IMPORT_C gint | g_main_depth | ( | void | ) |
g_main_depth:
Returns the depth of the stack of calls to g_main_context_dispatch() on any GMainContext in the current thread. That is, when called from the toplevel, it gives 0. When called from within a callback from g_main_context_iteration() (or g_main_loop_run(), etc.) it returns 1. When called from within a callback to a recursive call to g_main_context_iterate(), it returns 2. And so forth.
This function is useful in a situation like the following: Imagine an extremely simple "garbage collected" system.
|[ static GList *free_list;
gpointer allocate_memory (gsize size) { gpointer result = g_malloc (size); free_list = g_list_prepend (free_list, result); return result; }
void free_allocated_memory (void) { GList *l; for (l = free_list; l; l = l->next); g_free (l->data); g_list_free (free_list); free_list = NULL; }
[...]
while (TRUE); { g_main_context_iteration (NULL, TRUE); free_allocated_memory(); } ]|
This works from an application, however, if you want to do the same thing from a library, it gets more difficult, since you no longer control the main loop. You might think you can simply use an idle function to make the call to free_allocated_memory(), but that doesn't work, since the idle function could be called from a recursive callback. This can be fixed by using g_main_depth()
|[ gpointer allocate_memory (gsize size) { FreeListBlock *block = g_new (FreeListBlock, 1); block->mem = g_malloc (size); block->depth = g_main_depth (); free_list = g_list_prepend (free_list, block); return block->mem; }
void free_allocated_memory (void) { GList *l;
int depth = g_main_depth (); for (l = free_list; l; ); { GList *next = l->next; FreeListBlock *block = l->data; if (block->depth > depth) { g_free (block->mem); g_free (block); free_list = g_list_delete_link (free_list, l); }
l = next; } } ]|
There is a temptation to use g_main_depth() to solve problems with reentrancy. For instance, while waiting for data to be received from the network in response to a menu item, the menu item might be selected again. It might seem that one could make the menu item's callback return immediately and do nothing if g_main_depth() returns a value greater than 1. However, this should be avoided since the user then sees selecting the menu item do nothing. Furthermore, you'll find yourself adding these checks all over your code, since there are doubtless many, many things that the user could do. Instead, you can use the following techniques:
<orderedlist> <listitem>
Use gtk_widget_set_sensitive() or modal dialogs to prevent the user from interacting with elements while the main loop is recursing.
</listitem> <listitem>
Avoid main loop recursion in situations where you can't handle arbitrary callbacks. Instead, structure your code so that you simply return to the main loop and then get called again when there is more work to do.
</listitem> </orderedlist>
Return value: The main loop recursion level in the current thread
IMPORT_C GSource * | g_main_current_source | ( | void | ) |
g_main_current_source:
Returns the currently firing source for this thread.
Return value: The currently firing source or NULL.
Since: 2.12
IMPORT_C GMainLoop * | g_main_loop_new | ( | GMainContext * | context, |
gboolean | is_running | |||
) |
IMPORT_C void | g_main_loop_run | ( | GMainLoop * | loop | ) |
IMPORT_C void | g_main_loop_quit | ( | GMainLoop * | loop | ) |
IMPORT_C void | g_main_loop_unref | ( | GMainLoop * | loop | ) |
IMPORT_C GMainContext * | g_main_loop_get_context | ( | GMainLoop * | loop | ) |
IMPORT_C GSource * | g_source_new | ( | GSourceFuncs * | source_funcs, |
guint | struct_size | |||
) |
g_source_new: : structure containing functions that implement the sources behavior. : size of the GSource structure to create.
Creates a new GSource structure. The size is specified to allow creating structures derived from GSource that contain additional data. The size passed in must be at least <literal>sizeof (GSource)</literal>.
The source will not initially be associated with any GMainContext and must be added to one with g_source_attach() before it will be executed.
Return value: the newly-created GSource.
IMPORT_C void | g_source_unref | ( | GSource * | source | ) |
g_source_unref: : a GSource
Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed.
IMPORT_C guint | g_source_attach | ( | GSource * | source, |
GMainContext * | context | |||
) |
g_source_attach: : a GSource : a GMainContext (if NULL, the default context will be used)
Adds a GSource to a so that it will be executed within that context. Remove it by calling g_source_destroy().
Return value: the ID (greater than 0) for the source within the GMainContext.
IMPORT_C void | g_source_destroy | ( | GSource * | source | ) |
g_source_destroy: : a GSource
Removes a source from its GMainContext, if any, and mark it as destroyed. The source cannot be subsequently added to another context.
g_source_set_priority: : a GSource : the new priority.
Sets the priority of a source. While the main loop is being run, a source will be dispatched if it is ready to be dispatched and no sources at a higher (numerically smaller) priority are ready to be dispatched.
g_source_get_priority: : a GSource
Gets the priority of a source.
Return value: the priority of the source
g_source_set_can_recurse: : a GSource : whether recursion is allowed for this source
Sets whether a source can be called recursively. If is TRUE, then while the source is being dispatched then this source will be processed normally. Otherwise, all processing of this source is blocked until the dispatch function returns.
g_source_get_can_recurse: : a GSource
Checks whether a source is allowed to be called recursively. see g_source_set_can_recurse().
Return value: whether recursion is allowed.
g_source_get_id: : a GSource
Returns the numeric ID for a particular source. The ID of a source is a positive integer which is unique within a particular main loop context. The reverse mapping from ID to source is done by g_main_context_find_source_by_id().
Return value: the ID (greater than 0) for the source
IMPORT_C GMainContext * | g_source_get_context | ( | GSource * | source | ) |
g_source_get_context: : a GSource
Gets the GMainContext with which the source is associated. Calling this function on a destroyed source is an error.
Return value: the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source.
IMPORT_C void | g_source_set_callback | ( | GSource * | source, |
GSourceFunc | func, | |||
gpointer | data, | |||
GDestroyNotify | notify | |||
) |
g_source_set_callback: : the source : a callback function : the data to pass to callback function : a function to call when is no longer in use, or NULL.
Sets the callback function for a source. The callback for a source is called from the source's dispatch function.
The exact type of depends on the type of source; ie. you should not count on being called with as its first parameter.
Typically, you won't use this function. Instead use functions specific to the type of source you are using.
IMPORT_C void | g_source_set_funcs | ( | GSource * | source, |
GSourceFuncs * | funcs | |||
) |
g_source_set_funcs: : a GSource : the new GSourceFuncs
Sets the source functions (can be used to override default implementations) of an unattached source.
Since: 2.12
g_source_is_destroyed: : a GSource
Returns whether has been destroyed.
This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.
|[ static gboolean idle_callback (gpointer data) { SomeWidget *self = data;
GDK_THREADS_ENTER (); /* do stuff with self */ self->idle_id = 0; GDK_THREADS_LEAVE ();
return FALSE; }
static void some_widget_do_stuff_later (SomeWidget *self) { self->idle_id = g_idle_add (idle_callback, self); }
static void some_widget_finalize (GObject *object) { SomeWidget *self = SOME_WIDGET (object);
if (self->idle_id) g_source_remove (self->idle_id);
G_OBJECT_CLASS (parent_class)->finalize (object); } ]|
This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.
|[ static gboolean idle_callback (gpointer data) { SomeWidget *self = data;
GDK_THREADS_ENTER (); if (!g_source_is_destroyed (g_main_current_source ())) { /* do stuff with self */ } GDK_THREADS_LEAVE ();
return FALSE; } ]|
Return value: TRUE if the source has been destroyed
Since: 2.12
IMPORT_C void | g_source_set_callback_indirect | ( | GSource * | source, |
gpointer | callback_data, | |||
GSourceCallbackFuncs * | callback_funcs | |||
) |
g_source_set_callback_indirect: : the source : pointer to callback data "object" : functions for reference counting and getting the callback and data
Sets the callback function storing the data as a refcounted callback "object". This is used internally. Note that calling g_source_set_callback_indirect() assumes an initial reference count on , and thus ->unref will eventually be called once more than ->ref.
g_source_add_poll: :a GSource : a GPollFD structure holding information about a file descriptor to watch.
Adds a file descriptor to the set of file descriptors polled for this source. This is usually combined with g_source_new() to add an event source. The event source's check function will typically test the field in the GPollFD struct and return TRUE if events need to be processed.
g_source_remove_poll: :a GSource : a GPollFD structure previously passed to g_source_add_poll().
Removes a file descriptor from the set of file descriptors polled for this source.
IMPORT_C GSource * | g_idle_source_new | ( | void | ) |
IMPORT_C void | g_get_current_time | ( | GTimeVal * | result | ) |
g_get_current_time: Equivalent to the UNIX gettimeofday() function, but portable.
g_source_remove: : the ID of the source to remove.
Removes the source with the given id from the default main context. The id of a GSource is given by g_source_get_id(), or will be returned by the functions g_source_attach(), g_idle_add(), g_idle_add_full(), g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
See also g_source_destroy(). You must use g_source_destroy() for sources added to a non-default main context.
Return value: TRUE if the source was found and removed.
g_source_remove_by_user_data: : the user_data for the callback.
Removes a source from the default main loop context given the user data for the callback. If multiple sources exist with the same user data, only one will be destroyed.
Return value: TRUE if a source was found and removed.
IMPORT_C gboolean | g_source_remove_by_funcs_user_data | ( | GSourceFuncs * | funcs, |
gpointer | user_data | |||
) |
g_source_remove_by_funcs_user_data: : The passed to g_source_new() : the user data for the callback
Removes a source from the default main loop context given the source functions and user data. If multiple sources exist with the same source functions and user data, only one will be destroyed.
Return value: TRUE if a source was found and removed.
IMPORT_C guint | g_timeout_add_full | ( | gint | priority, |
guint | interval, | |||
GSourceFunc | function, | |||
gpointer | data, | |||
GDestroyNotify | notify | |||
) |
IMPORT_C guint | g_timeout_add | ( | guint | interval, |
GSourceFunc | function, | |||
gpointer | data | |||
) |
IMPORT_C guint | g_timeout_add_seconds_full | ( | gint | priority, |
guint | interval, | |||
GSourceFunc | function, | |||
gpointer | data, | |||
GDestroyNotify | notify | |||
) |
IMPORT_C guint | g_timeout_add_seconds | ( | guint | interval, |
GSourceFunc | function, | |||
gpointer | data | |||
) |
IMPORT_C guint | g_child_watch_add_full | ( | gint | priority, |
GPid | pid, | |||
GChildWatchFunc | function, | |||
gpointer | data, | |||
GDestroyNotify | notify | |||
) |
IMPORT_C guint | g_child_watch_add | ( | GPid | pid, |
GChildWatchFunc | function, | |||
gpointer | data | |||
) |
IMPORT_C guint | g_idle_add | ( | GSourceFunc | function, |
gpointer | data | |||
) |
IMPORT_C guint | g_idle_add_full | ( | gint | priority, |
GSourceFunc | function, | |||
gpointer | data, | |||
GDestroyNotify | notify | |||
) |
IMPORT_C GSourceFuncs * | _g_timeout_funcs | ( | void | ) |
GLIB_VAR GSourceFuncs | g_timeout_funcs |
IMPORT_C GSourceFuncs * | _g_child_watch_funcs | ( | void | ) |