glib.MainContext.MainContext Class Reference

List of all members.

Detailed Description

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. 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. 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. GTK+ contains wrappers of some of these functions, e.g. gtk_main(), gtk_main_quit() and gtk_events_pending(). Creating new sources types 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. 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().


Customizing the main loop iteration 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(). The operation of these functions can best be seen in terms of a state diagram, as shown in Figure1, States of a Main Context. Figure1.States of a Main Context


Public Member Functions

GMainContextgetMainContextStruct ()
 this (GMainContext *gMainContext)
 Sets our main struct and passes it to the parent class.
 this ()
 Creates a new GMainContext strcuture Returns: the new GMainContext.
MainContext ref ()
 Increases the reference count on a GMainContext object by one.
void unref ()
 Decreases the reference count on a GMainContext object by one.
int iteration (int mayBlock)
 Runs a single iteration for the given main loop.
int pending ()
 Checks if any sources have pending events for the given context.
Source findSourceById (uint sourceId)
 Finds a GSource given a pair of context and ID.
Source findSourceByUserData (void *userData)
 Finds a source with the given user data for the callback.
Source findSourceByFuncsUserData (GSourceFuncs *funcs, void *userData)
 Finds a source with the given source functions and user data.
void wakeup ()
 If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.
int acquire ()
 Tries to become the owner of the specified context.
void release ()
 Releases ownership of a context previously acquired by this thread with g_main_context_acquire().
int isOwner ()
 Determines whether this thread holds the (recursive) ownership of this GMaincontext.
int wait (Cond cond, Mutex mutex)
 Tries to become the owner of the specified context, as with g_main_context_acquire().
int prepare (int *priority)
 Prepares to poll sources within a main loop.
int query (int maxPriority, int *timeout, GPollFD *fds, int nFds)
 Determines information necessary to poll this main loop.
int check (int maxPriority, GPollFD *fds, int nFds)
 Passes the results of polling back to the main loop.
void dispatch ()
 Dispatches all pending sources.
void setPollFunc (GPollFunc func)
 Sets the function to use to handle polling of file descriptors.
GPollFunc getPollFunc ()
 Gets the poll function set by g_main_context_set_poll_func().
void addPoll (GPollFD *fd, int priority)
 Adds a file descriptor to the set of file descriptors polled for this context.
void removePoll (GPollFD *fd)
 Removes file descriptor from the set of file descriptors to be polled for a particular context.

Static Public Member Functions

static MainContext defaulx ()
 Returns the default main context.

Protected Member Functions

void * getStruct ()
 the main Gtk struct as a void*

Protected Attributes

GMainContextgMainContext
 the main Gtk struct


Constructor & Destructor Documentation

glib.MainContext.MainContext.this ( GMainContext gMainContext  ) 

Sets our main struct and passes it to the parent class.

glib.MainContext.MainContext.this (  ) 

Creates a new GMainContext strcuture Returns: the new GMainContext.


Member Function Documentation

int glib.MainContext.MainContext.acquire (  ) 

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(). 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(). context: a GMainContext Returns: TRUE if the operation succeeded, and this thread is now the owner of context.

void glib.MainContext.MainContext.addPoll ( GPollFD fd,
int  priority 
)

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. context: a GMainContext (or NULL for the default context) fd: a GPollFD structure holding information about a file descriptor to watch. 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.

int glib.MainContext.MainContext.check ( int  maxPriority,
GPollFD fds,
int  nFds 
)

Passes the results of polling back to the main loop.

context: a GMainContext max_priority: the maximum numerical priority of sources to check fds: array of GPollFD's that was passed to the last call to g_main_context_query() n_fds: return value of g_main_context_query() Returns: TRUE if some sources are ready to be dispatched.

static MainContext glib.MainContext.MainContext.defaulx (  )  [static]

Returns the default main context.

This is the main context used for main loop functions when a main loop is not explicitly specified. Returns: the default main context.

void glib.MainContext.MainContext.dispatch (  ) 

Dispatches all pending sources.

context: a GMainContext

Source glib.MainContext.MainContext.findSourceByFuncsUserData ( GSourceFuncs funcs,
void *  userData 
)

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. context: a GMainContext (if NULL, the default context will be used). funcs: the source_funcs passed to g_source_new(). user_data: the user data from the callback. Returns: the source, if one was found, otherwise NULL

Source glib.MainContext.MainContext.findSourceById ( uint  sourceId  ) 

Finds a GSource given a pair of context and ID.

context: a GMainContext (if NULL, the default context will be used) source_id: the source ID, as returned by g_source_get_id(). Returns: the GSource if found, otherwise, NULL

Source glib.MainContext.MainContext.findSourceByUserData ( void *  userData  ) 

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. context: a GMainContext user_data: the user_data for the callback. Returns: the source, if one was found, otherwise NULL

GMainContext* glib.MainContext.MainContext.getMainContextStruct (  ) 

GPollFunc glib.MainContext.MainContext.getPollFunc (  ) 

Gets the poll function set by g_main_context_set_poll_func().

context: a GMainContext Returns: the poll function

void* glib.MainContext.MainContext.getStruct (  )  [protected]

the main Gtk struct as a void*

int glib.MainContext.MainContext.isOwner (  ) 

Determines whether this thread holds the (recursive) ownership of this GMaincontext.

This is useful to know before waiting on another thread that may be blocking to get ownership of context. context: a GMainContext Returns: TRUE if current thread is owner of context. Since 2.10

int glib.MainContext.MainContext.iteration ( int  mayBlock  ) 

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. context: a GMainContext (if NULL, the default context will be used) may_block: whether the call may block. Returns: TRUE if events were dispatched.

int glib.MainContext.MainContext.pending (  ) 

Checks if any sources have pending events for the given context.

context: a GMainContext (if NULL, the default context will be used) Returns: TRUE if events are pending.

int glib.MainContext.MainContext.prepare ( int priority  ) 

Prepares to poll sources within a main loop.

The resulting information for polling is determined by calling g_main_context_query(). context: a GMainContext priority: location to store priority of highest priority source already ready. Returns: TRUE if some source is ready to be dispatched prior to polling.

int glib.MainContext.MainContext.query ( int  maxPriority,
int timeout,
GPollFD fds,
int  nFds 
)

Determines information necessary to poll this main loop.

context: a GMainContext max_priority: maximum priority source to check timeout_: location to store timeout to be used in polling fds: location to store GPollFD records that need to be polled. n_fds: length of fds. 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.

MainContext glib.MainContext.MainContext.ref (  ) 

Increases the reference count on a GMainContext object by one.

context: a GMainContext Returns: the context that was passed in (since 2.6)

void glib.MainContext.MainContext.release (  ) 

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. context: a GMainContext

void glib.MainContext.MainContext.removePoll ( GPollFD fd  ) 

Removes file descriptor from the set of file descriptors to be polled for a particular context.

context: a GMainContext fd: a GPollFD descriptor previously added with g_main_context_add_poll()

void glib.MainContext.MainContext.setPollFunc ( GPollFunc  func  ) 

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). This function could possibly be used to integrate the GLib event loop with an external event loop. context: a GMainContext func: the function to call to poll all file descriptors

void glib.MainContext.MainContext.unref (  ) 

Decreases the reference count on a GMainContext object by one.

If the result is zero, free the context and free all associated memory. context: a GMainContext

int glib.MainContext.MainContext.wait ( Cond  cond,
Mutex  mutex 
)

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. context: a GMainContext cond: a condition variable mutex: a mutex, currently held Returns: TRUE if the operation succeeded, and this thread is now the owner of context.

void glib.MainContext.MainContext.wakeup (  ) 

If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process.

context: a GMainContext


Member Data Documentation

GMainContext* glib.MainContext.MainContext.gMainContext [protected]

the main Gtk struct


SourceForge.net Logo DSource.org Logo digitalmars.com Logo