/src/wrappers/glib/library/core/g_main_loop.e
Specman e | 974 lines | 18 code | 286 blank | 670 comment | 0 complexity | 4f2f6eb66542df36b9c9294c2c2e8a43 MD5 | raw file
Large files files are truncated, but you can click here to view the full file
1indexing 2 copyright: "(C) 2005 Paolo Redaelli " 3 license: "LGPL v2 or later" 4 date: "$Date:$" 5 revision: "$REvision:$" 6 7class G_MAIN_LOOP 8-- Prev Up Home GLib Reference Manual Next 9-- Top | Description 10-- The Main Event Loop 11 12-- The Main Event Loop %G —%@ manages all available sources of events. 13 14-- Synopsis 15 16-- #include <glib.h> 17 18 19-- GMainLoop; 20-- GMainLoop* g_main_loop_new (GMainContext *context, 21-- gboolean is_running); 22-- GMainLoop* g_main_loop_ref (GMainLoop *loop); 23-- void g_main_loop_unref (GMainLoop *loop); 24-- void g_main_loop_run (GMainLoop *loop); 25-- void g_main_loop_quit (GMainLoop *loop); 26-- gboolean g_main_loop_is_running (GMainLoop *loop); 27-- GMainContext* g_main_loop_get_context (GMainLoop *loop); 28-- #define g_main_new (is_running) 29-- #define g_main_destroy (loop) 30-- #define g_main_run (loop) 31-- #define g_main_quit (loop) 32-- #define g_main_is_running (loop) 33 34-- #define G_PRIORITY_HIGH 35-- #define G_PRIORITY_DEFAULT 36-- #define G_PRIORITY_HIGH_IDLE 37-- #define G_PRIORITY_DEFAULT_IDLE 38-- #define G_PRIORITY_LOW 39 40-- GMainContext; 41-- GMainContext* g_main_context_new (void); 42-- GMainContext* g_main_context_ref (GMainContext *context); 43-- void g_main_context_unref (GMainContext *context); 44-- GMainContext* g_main_context_default (void); 45-- gboolean g_main_context_iteration (GMainContext *context, 46-- gboolean may_block); 47-- #define g_main_iteration (may_block) 48-- gboolean g_main_context_pending (GMainContext *context); 49-- #define g_main_pending () 50-- GSource* g_main_context_find_source_by_id 51-- (GMainContext *context, 52-- guint source_id); 53-- GSource* g_main_context_find_source_by_user_data 54-- (GMainContext *context, 55-- gpointer user_data); 56-- GSource* g_main_context_find_source_by_funcs_user_data 57-- (GMainContext *context, 58-- GSourceFuncs *funcs, 59-- gpointer user_data); 60-- void g_main_context_wakeup (GMainContext *context); 61-- gboolean g_main_context_acquire (GMainContext *context); 62-- void g_main_context_release (GMainContext *context); 63-- gboolean g_main_context_wait (GMainContext *context, 64-- GCond *cond, 65-- GMutex *mutex); 66-- gboolean g_main_context_prepare (GMainContext *context, 67-- gint *priority); 68-- gint g_main_context_query (GMainContext *context, 69-- gint max_priority, 70-- gint *timeout_, 71-- GPollFD *fds, 72-- gint n_fds); 73-- gint g_main_context_check (GMainContext *context, 74-- gint max_priority, 75-- GPollFD *fds, 76-- gint n_fds); 77-- void g_main_context_dispatch (GMainContext *context); 78-- void g_main_context_set_poll_func (GMainContext *context, 79-- GPollFunc func); 80-- GPollFunc g_main_context_get_poll_func (GMainContext *context); 81-- gint (*GPollFunc) (GPollFD *ufds, 82-- guint nfsd, 83-- gint timeout_); 84-- void g_main_context_add_poll (GMainContext *context, 85-- GPollFD *fd, 86-- gint priority); 87-- void g_main_context_remove_poll (GMainContext *context, 88-- GPollFD *fd); 89-- int g_main_depth (void); 90-- #define g_main_set_poll_func (func) 91 92-- GSource* g_idle_source_new (void); 93-- guint g_idle_add (GSourceFunc function, 94-- gpointer data); 95-- guint g_idle_add_full (gint priority, 96-- GSourceFunc function, 97-- gpointer data, 98-- GDestroyNotify notify); 99-- gboolean g_idle_remove_by_data (gpointer data); 100 101-- typedef GPid; 102-- void (*GChildWatchFunc) (GPid pid, 103-- gint status, 104-- gpointer data); 105-- GSource* g_child_watch_source_new (GPid pid); 106-- guint g_child_watch_add (GPid pid, 107-- GChildWatchFunc function, 108-- gpointer data); 109-- guint g_child_watch_add_full (gint priority, 110-- GPid pid, 111-- GChildWatchFunc function, 112-- gpointer data, 113-- GDestroyNotify notify); 114 115-- GPollFD; 116 117-- GSource; 118-- void (*GSourceDummyMarshal) (void); 119-- GSourceFuncs; 120-- GSourceCallbackFuncs; 121-- GSource* g_source_new (GSourceFuncs *source_funcs, 122-- guint struct_size); 123-- GSource* g_source_ref (GSource *source); 124-- void g_source_unref (GSource *source); 125-- guint g_source_attach (GSource *source, 126-- GMainContext *context); 127-- void g_source_destroy (GSource *source); 128-- void g_source_set_priority (GSource *source, 129-- gint priority); 130-- gint g_source_get_priority (GSource *source); 131-- void g_source_set_can_recurse (GSource *source, 132-- gboolean can_recurse); 133-- gboolean g_source_get_can_recurse (GSource *source); 134-- guint g_source_get_id (GSource *source); 135-- GMainContext* g_source_get_context (GSource *source); 136-- void g_source_set_callback (GSource *source, 137-- GSourceFunc func, 138-- gpointer data, 139-- GDestroyNotify notify); 140-- gboolean (*GSourceFunc) (gpointer data); 141-- void g_source_set_callback_indirect (GSource *source, 142-- gpointer callback_data, 143-- GSourceCallbackFuncs *callback_funcs); 144-- void g_source_add_poll (GSource *source, 145-- GPollFD *fd); 146-- void g_source_remove_poll (GSource *source, 147-- GPollFD *fd); 148-- void g_source_get_current_time (GSource *source, 149-- GTimeVal *timeval); 150-- gboolean g_source_remove (guint tag); 151-- gboolean g_source_remove_by_funcs_user_data 152-- (GSourceFuncs *funcs, 153-- gpointer user_data); 154-- gboolean g_source_remove_by_user_data (gpointer user_data); 155 156 157-- Description 158 159-- 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(). 160 161-- 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. 162 163-- 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. 164 165-- 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. 166 167-- The GMainLoop data type represents a main event loop. A GMainLoop is created with g_main_loop_new(). After adding the initial event sources, g_main_loop_run() is called. This continuously checks for new events from each of the event sources and dispatches them. Finally, the processing of an event from one of the sources leads to a call to g_main_loop_quit() to exit the main loop, and g_main_loop_run() returns. 168 169-- It is possible to create new instances of GMainLoop recursively. This is often used in GTK+ applications when showing modal dialog boxes. Note that event sources are associated with a particular GMainContext, and will be checked and dispatched for all main loops associated with that GMainContext. 170 171-- GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending(). 172-- Creating new sources types 173 174-- One of the unusual features of the GTK+ main loop functionality is that new types of event source can be created and used in addition to the builtin type of event source. A new event source type is used for handling GDK events. A new source type is created by deriving from the GSource structure. The derived type of source is represented by a structure that has the GSource structure as a first element, and other elements specific to the new source type. To create an instance of the new source type, call g_source_new() passing in the size of the derived structure and a table of functions. These GSourceFuncs determine the behavior of the new source types. 175 176-- New source types basically interact with with the main context in two ways. Their prepare function in GSourceFuncs can set a timeout to determine the maximum amount of time that the main loop will sleep before checking the source again. In addition, or as well, the source can add file descriptors to the set that the main context checks using g_source_add_poll(). 177-- Customizing the main loop iteration 178 179-- Single iterations of a GMainContext can be run with g_main_context_iteration(). In some cases, more detailed control of exactly how the details of the main loop work is desired, for instance, when integrating the GMainLoop with an external main loop. In such cases, you can call the component functions of g_main_context_iteration() directly. These functions are g_main_context_prepare(), g_main_context_query(), g_main_context_check() and g_main_context_dispatch(). 180 181-- The operation of these functions can best be seen in terms of a state diagram, as shown in Figure 1, ’??States of a Main Context’?É. 182 183-- Figure 1. States of a Main Context 184-- States of a Main Context 185-- Details 186-- GMainLoop 187 188-- typedef struct _GMainLoop GMainLoop; 189 190-- The GMainLoop struct is an opaque data type representing the main event loop of a GLib or GTK+ application. 191-- g_main_loop_new () 192 193-- GMainLoop* g_main_loop_new (GMainContext *context, 194-- gboolean is_running); 195 196-- Creates a new GMainLoop structure. 197 198-- context : a GMainContext (if NULL, the default context will be used). 199-- is_running : set to TRUE to indicate that the loop is running. This is not very important since calling g_main_loop_run() will set this to TRUE anyway. 200-- Returns : a new GMainLoop. 201-- g_main_loop_ref () 202 203-- GMainLoop* g_main_loop_ref (GMainLoop *loop); 204 205-- Increases the reference count on a GMainLoop object by one. 206 207-- loop : a GMainLoop 208-- Returns : loop 209-- g_main_loop_unref () 210 211-- void g_main_loop_unref (GMainLoop *loop); 212 213-- Decreases the reference count on a GMainLoop object by one. If the result is zero, free the loop and free all associated memory. 214 215-- loop : a GMainLoop 216-- g_main_loop_run () 217 218-- void g_main_loop_run (GMainLoop *loop); 219 220-- Runs a main loop until g_main_loop_quit() is called on the loop. If this is called for the thread of the loop's GMainContext, it will process events from the loop, otherwise it will simply wait. 221 222-- loop : a GMainLoop 223-- g_main_loop_quit () 224 225-- void g_main_loop_quit (GMainLoop *loop); 226 227-- Stops a GMainLoop from running. Any calls to g_main_loop_run() for the loop will return. 228 229-- loop : a GMainLoop 230-- g_main_loop_is_running () 231 232-- gboolean g_main_loop_is_running (GMainLoop *loop); 233 234-- Checks to see if the main loop is currently being run via g_main_loop_run(). 235 236-- loop : a GMainLoop. 237-- Returns : TRUE if the mainloop is currently being run. 238-- g_main_loop_get_context () 239 240-- GMainContext* g_main_loop_get_context (GMainLoop *loop); 241 242-- Returns the GMainContext of loop. 243 244-- loop : a GMainLoop. 245-- Returns : the GMainContext of loop 246-- g_main_new() 247 248-- #define g_main_new(is_running) 249 250-- Warning 251 252-- g_main_new is deprecated and should not be used in newly-written code. Use g_main_loop_new() instead. 253 254-- Creates a new GMainLoop for the default main loop. 255-- is_running : set to TRUE to indicate that the loop is running. This is not very important since calling g_main_run() will set this to TRUE anyway. 256-- Returns : a new GMainLoop. 257-- g_main_destroy() 258 259-- #define g_main_destroy(loop) 260 261-- Warning 262 263-- g_main_destroy is deprecated and should not be used in newly-written code. Use g_main_loop_unref() instead. 264 265-- Frees the memory allocated for the GMainLoop. 266-- loop : a GMainLoop. 267-- g_main_run() 268 269-- #define g_main_run(loop) 270 271-- Warning 272 273-- g_main_run is deprecated and should not be used in newly-written code. Use g_main_loop_run() instead. 274 275-- Runs a main loop until it stops running. 276-- loop : a GMainLoop. 277-- g_main_quit() 278 279-- #define g_main_quit(loop) 280 281-- Warning 282 283-- g_main_quit is deprecated and should not be used in newly-written code. Use g_main_loop_quit() instead. 284 285-- Stops the GMainLoop. If g_main_run() was called to run the GMainLoop, it will now return. 286-- loop : a GMainLoop. 287-- g_main_is_running() 288 289-- #define g_main_is_running(loop) 290 291-- Warning 292 293-- g_main_is_running is deprecated and should not be used in newly-written code. USe g_main_loop_is_running() instead. 294 295-- Checks if the main loop is running. 296-- loop : a GMainLoop. 297-- Returns : TRUE if the main loop is running. 298-- G_PRIORITY_HIGH 299 300-- #define G_PRIORITY_HIGH -100 301 302-- Use this for high priority event sources. It is not used within GLib or GTK+. 303-- G_PRIORITY_DEFAULT 304 305-- #define G_PRIORITY_DEFAULT 0 306 307-- Use this for default priority event sources. In GLib this priority is used when adding timeout functions with g_timeout_add(). In GDK this priority is used for events from the X server. 308-- G_PRIORITY_HIGH_IDLE 309 310-- #define G_PRIORITY_HIGH_IDLE 100 311 312-- Use this for high priority idle functions. GTK+ uses G_PRIORITY_HIGH_IDLE + 10 for resizing operations, and G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is done to ensure that any pending resizes are processed before any pending redraws, so that widgets are not redrawn twice unnecessarily.) 313-- G_PRIORITY_DEFAULT_IDLE 314 315-- #define G_PRIORITY_DEFAULT_IDLE 200 316 317-- Use this for default priority idle functions. In GLib this priority is used when adding idle functions with g_idle_add(). 318-- G_PRIORITY_LOW 319 320-- #define G_PRIORITY_LOW 300 321 322-- Use this for very low priority background tasks. It is not used within GLib or GTK+. 323-- GMainContext 324 325-- typedef struct _GMainContext GMainContext; 326 327-- The GMainContext struct is an opaque data type representing a set of sources to be handled in a main loop. 328-- g_main_context_new () 329 330-- GMainContext* g_main_context_new (void); 331 332-- Creates a new GMainContext strcuture 333 334-- Returns : the new GMainContext 335-- g_main_context_ref () 336 337-- GMainContext* g_main_context_ref (GMainContext *context); 338 339-- Increases the reference count on a GMainContext object by one. 340 341-- context : a GMainContext 342-- Returns : the context that was passed in (since 2.6) 343-- g_main_context_unref () 344 345-- void g_main_context_unref (GMainContext *context); 346 347-- Decreases the reference count on a GMainContext object by one. If the result is zero, free the context and free all associated memory. 348 349-- context : a GMainContext 350-- g_main_context_default () 351 352-- GMainContext* g_main_context_default (void); 353 354-- Returns the default main context. This is the main context used for main loop functions when a main loop is not explicitly specified. 355 356-- Returns : the default main context. 357-- g_main_context_iteration () 358 359-- gboolean g_main_context_iteration (GMainContext *context, 360-- gboolean may_block); 361 362-- Runs a single iteration for the given main loop. This involves checking to see if any event sources are ready to be processed, then if no events sources are ready and may_block is TRUE, waiting for a source to become ready, then dispatching the highest priority events sources that are ready. Note that even when may_block is TRUE, it is still possible for g_main_context_iteration() to return FALSE, since the the wait may be interrupted for other reasons than an event source becoming ready. 363 364-- context : a GMainContext (if NULL, the default context will be used) 365-- may_block : whether the call may block. 366-- Returns : TRUE if events were dispatched. 367-- g_main_iteration() 368 369-- #define g_main_iteration(may_block) 370 371-- Warning 372 373-- g_main_iteration is deprecated and should not be used in newly-written code. Use g_main_context_iteration() instead. 374 375-- Runs a single iteration for the default GMainContext. 376-- may_block : set to TRUE if it should block (i.e. wait) until an event source becomes ready. It will return after an event source has been processed. If set to FALSE it will return immediately if no event source is ready to be processed. 377-- Returns : TRUE if more events are pending. 378-- g_main_context_pending () 379 380-- gboolean g_main_context_pending (GMainContext *context); 381 382-- Checks if any sources have pending events for the given context. 383 384-- context : a GMainContext (if NULL, the default context will be used) 385-- Returns : TRUE if events are pending. 386-- g_main_pending() 387 388-- #define g_main_pending() 389 390-- Warning 391 392-- g_main_pending is deprecated and should not be used in newly-written code. Use g_main_context_pending() instead. 393 394-- Checks if any events are pending for the default GMainContext (i.e. ready to be processed). 395-- Returns : TRUE if any events are pending. 396-- g_main_context_find_source_by_id () 397 398-- GSource* g_main_context_find_source_by_id 399-- (GMainContext *context, 400-- guint source_id); 401 402-- Finds a GSource given a pair of context and ID 403 404-- context : a GMainContext (if NULL, the default context will be used) 405-- source_id : the source ID, as returned by g_source_get_id() 406-- Returns : the GSource if found, otherwise, NULL 407-- g_main_context_find_source_by_user_data () 408 409-- GSource* g_main_context_find_source_by_user_data 410-- (GMainContext *context, 411-- gpointer user_data); 412 413-- 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. 414 415-- context : a GMainContext 416-- user_data : the user_data for the callback. 417-- Returns : the source, if one was found, otherwise NULL 418-- g_main_context_find_source_by_funcs_user_data () 419 420-- GSource* g_main_context_find_source_by_funcs_user_data 421-- (GMainContext *context, 422-- GSourceFuncs *funcs, 423-- gpointer user_data); 424 425-- 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. 426 427-- context : a GMainContext (if NULL, the default context will be used). 428-- funcs : the source_funcs passed to g_source_new(). 429-- user_data : the user data from the callback. 430-- Returns : the source, if one was found, otherwise NULL 431-- g_main_context_wakeup () 432 433-- void g_main_context_wakeup (GMainContext *context); 434 435-- If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process. 436 437-- context : a GMainContext 438-- g_main_context_acquire () 439 440-- gboolean g_main_context_acquire (GMainContext *context); 441 442-- Tries to become the owner of the specified context. If some other context 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(). 443 444-- 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(). 445 446-- context : a GMainContext 447-- Returns : TRUE if the operation succeeded, and this thread is now the owner of context. 448-- g_main_context_release () 449 450-- void g_main_context_release (GMainContext *context); 451 452-- Releases ownership of a context previously acquired by this thread with g_main_context_acquire(). If the context was acquired multiple times, the only release ownership when g_main_context_release() is called as many times as it was acquired. 453 454-- context : a GMainContext 455-- g_main_context_wait () 456 457-- gboolean g_main_context_wait (GMainContext *context, 458-- GCond *cond, 459-- GMutex *mutex); 460 461-- Tries to become the owner of the specified context, as with g_main_context_acquire(). But if another thread is the owner, atomically drop mutex and wait on cond until that owner releases ownership or until cond is signaled, then try again (once) to become the owner. 462 463-- context : a GMainContext 464-- cond : a condition variable 465-- mutex : a mutex, currently held 466-- Returns : TRUE if the operation succeeded, and this thread is now the owner of context. 467-- g_main_context_prepare () 468 469-- gboolean g_main_context_prepare (GMainContext *context, 470-- gint *priority); 471 472-- Prepares to poll sources within a main loop. The resulting information for polling is determined by calling g_main_context_query(). 473 474-- context : a GMainContext 475-- priority : location to store priority of highest priority source already ready. 476-- Returns : TRUE if some source is ready to be dispatched prior to polling. 477-- g_main_context_query () 478 479-- gint g_main_context_query (GMainContext *context, 480-- gint max_priority, 481-- gint *timeout_, 482-- GPollFD *fds, 483-- gint n_fds); 484 485-- Determines information necessary to poll this main loop. 486 487-- context : a GMainContext 488-- max_priority : maximum priority source to check 489-- timeout_ : location to store timeout to be used in polling 490-- fds : location to store GPollFD records that need to be polled. 491-- n_fds : length of fds. 492-- Returns : the number of records actually stored in fds, or, if more than n_fds records need to be stored, the number of records that need to be stored. 493-- g_main_context_check () 494 495-- gint g_main_context_check (GMainContext *context, 496-- gint max_priority, 497-- GPollFD *fds, 498-- gint n_fds); 499 500-- Passes the results of polling back to the main loop. 501 502-- context : a GMainContext 503-- max_priority : the maximum numerical priority of sources to check 504-- fds : array of GPollFD's that was passed to the last call to g_main_context_query() 505-- n_fds : return value of g_main_context_query() 506-- Returns : TRUE if some sources are ready to be dispatched. 507-- g_main_context_dispatch () 508 509-- void g_main_context_dispatch (GMainContext *context); 510 511-- Dispatches all pending sources. 512 513-- context : a GMainContext 514-- g_main_context_set_poll_func () 515 516-- void g_main_context_set_poll_func (GMainContext *context, 517-- GPollFunc func); 518 519-- Sets the function to use to handle polling of file descriptors. It will be used instead of the poll() system call (or GLib's replacement function, which is used where poll() isn't available). 520 521-- This function could possibly be used to integrate the GLib event loop with an external event loop. 522 523-- context : a GMainContext 524-- func : the function to call to poll all file descriptors 525-- g_main_context_get_poll_func () 526 527-- GPollFunc g_main_context_get_poll_func (GMainContext *context); 528 529-- Gets the poll function set by g_main_context_set_poll_func(). 530 531-- context : a GMainContext 532-- Returns : the poll function 533-- GPollFunc () 534 535-- gint (*GPollFunc) (GPollFD *ufds, 536-- guint nfsd, 537-- gint timeout_); 538 539-- Specifies the type of function passed to g_main_context_set_poll_func(). The semantics of the function should match those of the poll() system call. 540-- ufds : an array of GPollFD elements. 541-- nfsd : the number of elements in ufds. 542-- timeout_ : the maximum time to wait for an event of the file descriptors. A negative value indicates an infinite timeout. 543-- Returns : the number of GPollFD elements which have events or errors reported, or -1 if an error occurred. 544-- g_main_context_add_poll () 545 546-- void g_main_context_add_poll (GMainContext *context, 547-- GPollFD *fd, 548-- gint priority); 549 550-- Adds a file descriptor to the set of file descriptors polled for this context. This will very seldomly be used directly. Instead a typical event source will use g_source_add_poll() instead. 551 552-- context : a GMainContext (or NULL for the default context) 553-- fd : a GPollFD structure holding information about a file descriptor to watch. 554-- priority : the priority for this file descriptor which should be the same as the priority used for g_source_attach() to ensure that the file descriptor is polled whenever the results may be needed. 555-- g_main_context_remove_poll () 556 557-- void g_main_context_remove_poll (GMainContext *context, 558-- GPollFD *fd); 559 560-- Removes file descriptor from the set of file descriptors to be polled for a particular context. 561 562-- context : a GMainContext 563-- fd : a GPollFD descriptor previously added with g_main_context_add_poll() 564-- g_main_depth () 565 566-- int g_main_depth (void); 567 568-- Return value: The main loop recursion level in the current thread 569 570-- 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. 571 572-- Example 1. 573-- 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(); } 574-- 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() 575 576-- Example 2. 577-- 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; } } 578-- 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: 579 580-- 1. 581 582-- Use gtk_widget_set_sensitive() or modal dialogs to prevent the user from interacting with elements while the main loop is recursing. 583-- 2. 584 585-- 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. 586 587-- g_main_set_poll_func() 588 589-- #define g_main_set_poll_func(func) 590 591-- Warning 592 593-- g_main_set_poll_func is deprecated and should not be used in newly-written code. Use g_main_context_set_poll_func() instead. 594 595-- Sets the function to use for the handle polling of file descriptors for the default main context. 596-- func : the function to call to poll all file descriptors. 597-- g_idle_source_new () 598 599-- GSource* g_idle_source_new (void); 600 601-- Creates a new idle source. 602 603-- 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. Note that the default priority for idle sources is G_PRIORITY_DEFAULT_IDLE, as compared to other sources which have a default priority of G_PRIORITY_DEFAULT. 604 605-- Returns : the newly-created idle source 606-- g_idle_add () 607 608-- guint g_idle_add (GSourceFunc function, 609-- gpointer data); 610 611-- 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. 612 613-- function : function to call 614-- data : data to pass to function. 615-- Returns : the id of the event source. 616-- g_idle_add_full () 617 618-- guint g_idle_add_full (gint priority, 619-- GSourceFunc function, 620-- gpointer data, 621-- GDestroyNotify notify); 622 623-- Adds a function to be called whenever there are no higher priority events pending. If the function returns FALSE it is automatically removed from the list of event sources and will not be called again. 624 625-- priority : the priority of the idle source. Typically this will be in the range btweeen G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE. 626-- function : function to call 627-- data : data to pass to function 628-- notify : function to call when the idle is removed, or NULL 629-- Returns : the id of the event source. 630-- g_idle_remove_by_data () 631 632-- gboolean g_idle_remove_by_data (gpointer data); 633 634-- Removes the idle function with the given data. 635 636-- data : the data for the idle source's callback. 637-- Returns : TRUE if an idle source was found and removed. 638-- GPid 639 640-- typedef int GPid; 641 642-- A type which is used to hold a process identification. On Unix, processes are identified by a process id (an integer), while Windows uses process handles (which are pointers). 643-- GChildWatchFunc () 644 645-- void (*GChildWatchFunc) (GPid pid, 646-- gint status, 647-- gpointer data); 648 649-- The type of functions to be called when a child exists. 650-- pid : the process id of the child process 651-- status : Status information about the child process, see waitpid(2) for more information about this field 652-- data : user data passed to g_child_watch_add() 653-- g_child_watch_source_new () 654 655-- GSource* g_child_watch_source_new (GPid pid); 656 657-- Creates a new child_watch source. 658 659-- 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. 660 661-- Note that on platforms where GPid must be explicitely closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source. 662 663-- Note further that using g_child_watch_source_new() is not compatible with calling waitpid(-1) in the application. Calling waitpid() for individual pids will still work fine. 664 665-- pid : process id of a child process to watch. On Windows, a HANDLE for the process to watch (which actually doesn't have to be a child). 666-- Returns : the newly-created child watch source 667 668-- Since 2.4 669-- g_child_watch_add () 670 671-- guint g_child_watch_add (GPid pid, 672-- GChildWatchFunc function, 673-- gpointer data); 674 675-- Sets a function to be called when the child indicated by pid exits, at a default priority, G_PRIORITY_DEFAULT. 676 677-- Note that on platforms where GPid must be explicitely closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source. 678 679-- GLib supports only a single callback per process id. 680 681-- pid : process id of a child process to watch 682-- function : function to call 683-- data : data to pass to function 684-- Returns : the id of event source. 685 686-- Since 2.4 687-- g_child_watch_add_full () 688 689-- guint g_child_watch_add_full (gint priority, 690-- GPid pid, 691-- GChildWatchFunc function, 692-- gpointer data, 693-- GDestroyNotify notify); 694 695-- Sets a function to be called when the child indicated by pid exits, at a default priority, G_PRIORITY_DEFAULT. 696 697-- Note that on platforms where GPid must be explicitely closed (see g_spawn_close_pid()) pid must not be closed while the source is still active. Typically, you will want to call g_spawn_close_pid() in the callback function for the source. 698 699-- GLib supports only a single callback per process id. 700 701-- priority : the priority of the idle source. Typically this will be in the range between G_PRIORITY_DEFAULT_IDLE and G_PRIORITY_HIGH_IDLE. 702-- pid : process id of a child process to watch 703-- function : function to call 704-- data : data to pass to function 705-- notify : function to call when the idle is removed, or NULL 706-- Returns : the id of event source. 707 708-- Since 2.4 709-- GPollFD 710 711-- typedef struct { 712-- gint fd; 713-- gushort events; 714-- gushort revents; 715-- } GPollFD; 716 717-- gint fd; the file descriptor to poll (or a HANDLE on Win32 platforms). 718-- gushort events; a bitwise combination of flags from GIOCondition, specifying which events should be polled for. Typically for reading from a file descriptor you would use G_IO_IN | G_IO_HUP | G_IO_ERR, and for writing you would use G_IO_OUT | G_IO_ERR. 719-- gushort revents; a bitwise combination of flags from GIOCondition, returned from the poll() function to indicate which events occurred. 720 721-- GSource 722 723-- typedef struct { 724-- } GSource; 725 726-- The GSource struct is an opaque data type representing an event source. 727-- GSourceDummyMarshal () 728 729-- void (*GSourceDummyMarshal) (void); 730 731-- This is just a placeholder for GClosureMarshal, which cannot be used here for dependency reasons. 732-- GSourceFuncs 733 734-- typedef struct { 735-- gboolean (*prepare) (GSource *source, 736-- gint *timeout_); 737-- gboolean (*check) (GSource *source); 738-- gboolean (*dispatch) (GSource *source, 739-- GSourceFunc callback, 740-- gpointer user_data); 741-- void (*finalize) (GSource *source); /* Can be NULL */ 742 743-- /* For use by g_source_set_closure */ 744-- GSourceFunc closure_callback; 745-- GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */ 746-- } GSourceFuncs; 747 748-- The GSourceFuncs struct contains a table of functions used to handle event sources in a generic manner. 749-- prepare Called before all the file descriptors are polled. If the source can determine that it is ready here (without waiting for the results of the poll() call) it should return TRUE. It can also return a timeout_ value which should be the maximum timeout (in milliseconds) which should be passed to the poll() call. The actual timeout used will be -1 if all sources returned -1, or it will be the minimum of all the timeout_ values returned which were >= 0. 750-- check Called after all the file descriptors are polled. The source should return TRUE if it is ready to be dispatched. Note that some time may have passed since the previous prepare function was called, so the source should be checked again here. 751-- dispatch Called to dispatch the event source, after it has returned TRUE in either its prepare or its check function. The dispatch function is passed in a callback function and data. The callback function may be NULL if the source was never connected to a callback using g_source_set_callback(). The dispatch function should call the callback function with user_data and whatever additional parameters are needed for this type of event source. 752-- finalize Called when the source is finalized. 753 754-- For idle sources, the prepare and check functions always return TRUE to indicate that the source is always ready to be processed. The prepare function also returns a timeout value of 0 to ensure that the poll() call doesn't block (since that would be time wasted which could have been spent running the idle function). 755 756-- For timeout sources, the prepare and check functions both return TRUE if the timeout interval has expired. The prepare function also returns a timeout value to ensure that the poll() call doesn't block too long and miss the next timeout. 757 758-- For file descriptor sources, the prepare function typically returns FALSE, since it must wait until poll() has been called before it knows whether any events need to be processed. It sets the returned timeout to -1 to indicate that it doesn't mind how long the poll() call blocks. In the check function, it tests the results of the poll() call to see if the required condition has been met, and returns TRUE if so. 759-- GSourceCallbackFuncs 760 761-- typedef struct { 762-- void (*ref) (gpointer cb_data); 763-- void (*unref) (gpointer cb_data); 764-- void (*get) (gpointer cb_data, 765-- GSource *source, 766-- GSourceFunc *func, 767-- gpointer *data); 768-- } GSourceCallbackFuncs; 769 770-- The GSourceCallbackFuncs struct contains functions for managing callback objects. 771-- ref () Called when a reference is added to the callback object. 772-- unref () Called when a reference to the callback object is dropped. 773-- get () Called to extract the callback function and data from the callback object. 774-- g_source_new () 775 776-- GSource* g_source_new (GSourceFuncs *source_funcs, 777-- guint struct_size); 778 779-- 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 sizeof (GSource). 780 781-- 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. 782 783-- source_funcs : structure containing functions that implement the sources behavior. 784-- struct_size : size of the GSource structure to create. 785-- Returns : the newly-created GSource. 786-- g_source_ref () 787 788-- GSource* g_source_ref (GSource *source); 789 790-- Increases the reference count on a source by one. 791 792-- source : a GSource 793-- Returns : source 794-- g_source_unref () 795 796-- void g_source_unref (GSource *source); 797 798-- Decreases the reference count of a source by one. If the resulting reference count is zero the source and associated memory will be destroyed. 799 800-- source : a GSource 801-- g_source_attach () 802 803-- guint g_source_attach (GSource *source, 804-- GMainContext *context); 805 806-- Adds a GSource to a context so that it will be executed within that context. 807 808-- source : a GSource 809-- context : a GMainContext (if NULL, the default context will be used) 810-- Returns : the ID for the source within the GMainContext 811-- g_source_destroy () 812 813-- void g_source_destroy (GSource *source); 814 815-- Removes a source from its GMainContext, if any, and mark it as destroyed. The source cannot be subsequently added to another context. 816 817-- source : a GSource 818-- g_source_set_priority () 819 820-- void g_source_set_priority (GSource *source, 821-- gint priority); 822 823-- 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. 824 825-- source : a GSource 826-- priority : the new priority. 827-- g_source_get_priority () 828 829-- gint g_source_get_priority (GSource *source); 830 831-- Gets the priority of a source. 832 833-- source : a GSource 834-- Returns : the priority of the source 835-- g_source_set_can_recurse () 836 837-- void g_source_set_can_recurse (GSource *source, 838-- gboolean can_recurse); 839 840-- Sets whether a source can be called recursively. If can_recurse 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. 841 842-- source : a GSource 843-- can_recurse : whether recursion is allowed for this source 844-- g_source_get_can_recurse () 845 846-- gboolean g_source_get_can_recurse (GSource *source); 847 848-- Checks whether a source is allowed to be called recursively. see g_source_set_can_recurse(). 849 850-- source : a GSource 851-- Returns : whether recursion is allowed. 852-- g_source_get_id () 853 854-- guint g_source_get_id (GSource *source); 855 856-- Returns the numeric ID for a particular source. The ID of a source 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(). 857 858-- source : a GSource 859-- Returns : the ID for the source 860-- g_source_get_context () 861 862-- GMainContext* g_source_get_context (GSource *source); 863 864-- Gets the GMainContext with which the source is associated. Calling this function on a destroyed source is an error. 865 866-- source : a GSource 867-- Returns : the GMainContext with which the source is associated, or NULL if the context has not yet been added to a source. 868-- g_source_set_callback () 869 870-- void g_source_set_callback (GSource *source, 871-- GSourceFunc func, 872-- gpointer data, 873-- GDestroyNotify notify); 874 875-- Sets the callback function for a source. The callback for a source is called from the source's dispatch function. 876 877-- The exact type of func depends on the type of source; ie. you should not count on func being called with data as its first parameter. 878 879-- Typically, you won't use this function. Instead use functions specific to the type of source you are using. 880 881-- source : the source 882-- func : a callback function 883-- data : the data to pass to callback function 884-- notify : a function to call when data is no longer in use, or NULL. 885-- GSourceFunc () 886 887-- gboolean (*GSourceFunc) (gpointer data); 888 889-- Specifies the type of function passed to g_timeout_add(), g_timeout_add_full(), g_idle_add(), and g_idle_add_full(). 890-- data : data passed to the function, set when the source was created with one of the above functions. 891-- Returns : it should return FALSE if the source should be removed. 892-- g_source_set_callback_indirect () 893 894-- void g_source_set_callback_indirect (GSource *source, 895-- gpointer callback_data, 896-- GSourceCallbackFuncs *callback_funcs); 897 898-- 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 callback_data, and thus callback_funcs->unref will eventually be called once more than callback_funcs->ref. 899 900-- source : the source 901-- callback_data : pointer to callback data "object" 902-- callback_funcs : functions for reference counting callback_data and getting the callback and data 903-- g_source_add_poll () 904 905-- void g_source_add_poll (GSource *source, 906-- GPollFD *fd); 907 908-- 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 revents field in the GPollFD struct and return TRUE if events need to be processed. 909 910-- source : a GSource 911-- fd : a GPollFD structure holding information about a file descriptor to watch. 912-- g_source_remove_poll () 913 914-- void g_source_remove_poll (GSource *source, 915-- GPollFD *fd); 916 917-- Removes a file descriptor from the set of file descriptors polled for this source. 918 919-- source : a GSource 920-- fd : a GPollFD structure previously passed to g_source_add_poll(). 921-- g_source_get_current_time () 922 923-- void g_source_get_current_time (GSource *source, 924-- GTimeVal *timeval); 925 926-- Gets the "current time" to be used when checking this source. The advantage of calling this function over calling g_get_current_time() directly is that when checking multiple sources, GLib can cache a single value instead of having to repeatedly get the system time. 927 928-- source : a GSource 929-- timeval : GTimeVal structure in which to store current time. 930 931 932feature -- Operations 933 934 source_remove (tag: INTEGER) is 935 -- Removes the source with the given id `tag' from the default 936 -- main context. The id of a GSource is given by 937 -- g_source_get_id(), or will be returned by the functions 938 -- g_source_attach(), g_idle_add(), g_idle_add_full(), 939 -- g_timeout_add(), g_timeout_add_full(), g_child_watch_add(), 940 -- g_child_watch_add_full(), g_io_add_watch(), 941 -- and g_io_add_watch_full(). 942 do 943 source_removed := (g_source_remove (tag) /= 0) 944 end 945 946 source_removed: BOOLEAN 947 948feature {} -- Externals 949 950 g_source_remove (tag: INTEGER): INTEGER is 951 external "C use <glib.h>" 952 alias "g_source_remove" 953 end 954 955-- g_source_remove_by_funcs_user_data () 956 957-- gboolean g_source_remove_by_funcs_user_data 958-- (GSourceFuncs *funcs, 959-- gpointer user_data); 960 961-- 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, …
Large files files are truncated, but you can click here to view the full file