glibmm 2.66.5
Public Types | Public Member Functions | Static Public Member Functions | Related Functions | List of all members
Glib::MainContext Class Reference

Main context. More...

#include <glibmm/main.h>

Public Types

using CppObjectType = Glib::MainContext
 
using BaseObjectType = GMainContext
 

Public Member Functions

 MainContext (const MainContext & other)=delete
 
MainContextoperator= (const MainContext & other)=delete
 
bool iteration (bool may_block)
 Runs a single iteration for the given main loop. More...
 
bool pending ()
 Checks if any sources have pending events for the given context. More...
 
void wakeup ()
 If context is currently waiting in a poll(), interrupt the poll(), and continue the iteration process. More...
 
bool acquire ()
 Tries to become the owner of the specified context. More...
 
bool wait (Glib::Cond & cond, Glib::Mutex & mutex)
 Tries to become the owner of the specified context, as with acquire(). More...
 
bool wait (Glib::Threads::Cond & cond, Glib::Threads::Mutex & mutex)
 Tries to become the owner of the specified context, as with acquire(). More...
 
void release ()
 Releases ownership of a context previously acquired by this thread with acquire(). More...
 
bool prepare (int & priority)
 Prepares to poll sources within a main loop. More...
 
bool prepare ()
 Prepares to poll sources within a main loop. More...
 
void query (int max_priority, int & timeout, std::vector< PollFD > & fds)
 Determines information necessary to poll this main loop. More...
 
bool check (int max_priority, std::vector< PollFD > & fds)
 Passes the results of polling back to the main loop. More...
 
void dispatch ()
 Dispatches all pending sources. More...
 
void set_poll_func (GPollFunc poll_func)
 Sets the function to use to handle polling of file descriptors. More...
 
GPollFunc get_poll_func ()
 Gets the poll function set by g_main_context_set_poll_func(). More...
 
void add_poll (PollFD & fd, int priority)
 Adds a file descriptor to the set of file descriptors polled for this context. More...
 
void remove_poll (PollFD & fd)
 Removes file descriptor from the set of file descriptors to be polled for a particular context. More...
 
void push_thread_default ()
 Acquires the context and sets it as the thread-default context for the current thread. More...
 
void pop_thread_default ()
 Pops the context off the thread-default context stack (verifying that it was on the top of the stack). More...
 
void invoke (const sigc::slot< bool > & slot, int priority=PRIORITY_DEFAULT)
 Invokes a function in such a way that this MainContext is owned during the invocation of slot. More...
 
SignalTimeout signal_timeout ()
 Timeout signal, attached to this MainContext. More...
 
SignalIdle signal_idle ()
 Idle signal, attached to this MainContext. More...
 
SignalIO signal_io ()
 I/O signal, attached to this MainContext. More...
 
SignalChildWatch signal_child_watch ()
 child watch signal, attached to this MainContext. More...
 
void reference () const
 
void unreference () const
 
GMainContext * gobj ()
 
const GMainContext * gobj () const
 
GMainContext * gobj_copy () const
 

Static Public Member Functions

static Glib::RefPtr< MainContextcreate ()
 Creates a new MainContext. More...
 
static Glib::RefPtr< MainContextget_default ()
 Returns the global default main context. More...
 
static Glib::RefPtr< MainContextget_thread_default ()
 Gets the thread-default MainContext for this thread. More...
 

Related Functions

(Note that these are not member functions.)

Glib::RefPtr< MainContextwrap (GMainContext * gobject, bool take_copy=false)
 

Detailed Description

Main context.

Member Typedef Documentation

◆ BaseObjectType

using Glib::MainContext::BaseObjectType = GMainContext

◆ CppObjectType

Constructor & Destructor Documentation

◆ MainContext()

Glib::MainContext::MainContext ( const MainContext other)
delete

Member Function Documentation

◆ acquire()

bool Glib::MainContext::acquire ( )

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 release() is called as many times as acquire(). You must be the owner of a context before you can call prepare(), query(), check(), dispatch().

Returns
true if the operation succeeded, and this thread is now the owner of context.

◆ add_poll()

void Glib::MainContext::add_poll ( PollFD 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 Glib::Source::add_poll() instead.

Parameters
fdA PollFD structure holding information about a file descriptor to watch.
priorityThe priority for this file descriptor which should be the same as the priority used for Glib::Source::attach() to ensure that the file descriptor is polled whenever the results may be needed.

◆ check()

bool Glib::MainContext::check ( int  max_priority,
std::vector< PollFD > &  fds 
)

Passes the results of polling back to the main loop.

Parameters
max_priorityMaximum numerical priority of sources to check.
fdsVector of Glib::PollFD's that was passed to the last call to query()
Returns
true if some sources are ready to be dispatched.

◆ create()

static Glib::RefPtr< MainContext > Glib::MainContext::create ( )
static

Creates a new MainContext.

Returns
The new MainContext.

◆ dispatch()

void Glib::MainContext::dispatch ( )

Dispatches all pending sources.

◆ get_default()

static Glib::RefPtr< MainContext > Glib::MainContext::get_default ( )
static

Returns the global default main context.

This is the main context used for main loop functions when a main loop is not explicitly specified, and corresponds to the "main" main loop.

Returns
The global default main context.
See also
get_thread_default()

◆ get_poll_func()

GPollFunc Glib::MainContext::get_poll_func ( )

Gets the poll function set by g_main_context_set_poll_func().

Returns
The poll function

◆ get_thread_default()

static Glib::RefPtr< MainContext > Glib::MainContext::get_thread_default ( )
static

Gets the thread-default MainContext for this thread.

Asynchronous operations that want to be able to be run in contexts other than the default one should call this method to get a MainContext to add their Glib::Sources to. (Note that even in single-threaded programs applications may sometimes want to temporarily push a non-default context, so it is not safe to assume that this will always return the global default context if you are running in the default thread.)

This method wraps g_main_context_ref_thread_default(), and not g_main_context_get_thread_default().

Returns
The thread-default MainContext.
Since glibmm 2.64:

◆ gobj() [1/2]

GMainContext * Glib::MainContext::gobj ( )

◆ gobj() [2/2]

const GMainContext * Glib::MainContext::gobj ( ) const

◆ gobj_copy()

GMainContext * Glib::MainContext::gobj_copy ( ) const

◆ invoke()

void Glib::MainContext::invoke ( const sigc::slot< bool > &  slot,
int  priority = PRIORITY_DEFAULT 
)

Invokes a function in such a way that this MainContext is owned during the invocation of slot.

If the context is owned by the current thread, slot is called directly. Otherwise, if the context is the thread-default main context of the current thread and acquire() succeeds, then slot is called and release() is called afterwards.

In any other case, an idle source is created to call slot and that source is attached to the context (presumably to be run in another thread).

Note that, as with normal idle functions, slot should probably return false. If it returns true, it will be continuously run in a loop (and may prevent this call from returning).

If an idle source is created to call slot, invoke() may return before slot is called.

Because sigc::trackable is not thread-safe, if the slot represents a non-static method of a class deriving from sigc::trackable, and the slot is created by sigc::mem_fun(), invoke() should only be called from the thread where the context runs. You can use, say, boost::bind() or, in C++11, std::bind() or a C++11 lambda expression instead of sigc::mem_fun().

Parameters
slotA slot to call.
priorityThe priority of the idle source, if one is created.
Since glibmm 2.38:

◆ iteration()

bool Glib::MainContext::iteration ( bool  may_block)

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 iteration() to return false, since the the wait may be interrupted for other reasons than an event source becoming ready.

Parameters
may_blockWhether the call may block.
Returns
true if events were dispatched.

◆ operator=()

MainContext & Glib::MainContext::operator= ( const MainContext other)
delete

◆ pending()

bool Glib::MainContext::pending ( )

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

Returns
true if events are pending.

◆ pop_thread_default()

void Glib::MainContext::pop_thread_default ( )

Pops the context off the thread-default context stack (verifying that it was on the top of the stack).

Since glibmm 2.64:

◆ prepare() [1/2]

bool Glib::MainContext::prepare ( )

Prepares to poll sources within a main loop.

The resulting information for polling is determined by calling query().

Returns
true if some source is ready to be dispatched prior to polling.

◆ prepare() [2/2]

bool Glib::MainContext::prepare ( int &  priority)

Prepares to poll sources within a main loop.

The resulting information for polling is determined by calling query().

Parameters
priorityLocation to store priority of highest priority source already ready.
Returns
true if some source is ready to be dispatched prior to polling.

◆ push_thread_default()

void Glib::MainContext::push_thread_default ( )

Acquires the context and sets it as the thread-default context for the current thread.

This will cause certain asynchronous operations (such as most gio-based I/O) which are started in this thread to run under this context and deliver their results to its main loop, rather than running under the global default context in the main thread. Note that calling this function changes the context returned by get_thread_default(), not the one returned by get_default(), so it does not affect the context used by functions like g_idle_add().

Normally you would call this function shortly after creating a new thread, passing it a Glib::MainContext which will be run by a Glib::MainLoop in that thread, to set a new default context for all async operations in that thread. In this case you may not need to ever call pop_thread_default(), assuming you want the new Glib::MainContext to be the default for the whole lifecycle of the thread.

If you don't have control over how the new thread was created (e.g. if the new thread isn't newly created, or if the thread life cycle is managed by a GThreadPool), it is always suggested to wrap the logic that needs to use the new Glib::MainContext inside a push_thread_default() / pop_thread_default() pair, otherwise threads that are re-used will end up never explicitly releasing the Glib::MainContext reference they hold.

In some cases you may want to schedule a single operation in a non-default context, or temporarily use a non-default context in the main thread. In that case, you can wrap the call to the asynchronous operation inside a push_thread_default() / pop_thread_default() pair, but it is up to you to ensure that no other asynchronous operations accidentally get started while the non-default context is active.

Beware that libraries that predate this function may not correctly handle being used from a thread with a thread-default context. Eg, see Gio::File::supports_thread_contexts().

Since glibmm 2.64:

◆ query()

void Glib::MainContext::query ( int  max_priority,
int &  timeout,
std::vector< PollFD > &  fds 
)

Determines information necessary to poll this main loop.

Parameters
max_priorityMaximum priority source to check.
timeoutLocation to store timeout to be used in polling.
fdsLocation to store Glib::PollFD records that need to be polled.
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.

◆ reference()

void Glib::MainContext::reference ( ) const

◆ release()

void Glib::MainContext::release ( )

Releases ownership of a context previously acquired by this thread with acquire().

If the context was acquired multiple times, the only release ownership when release() is called as many times as it was acquired.

◆ remove_poll()

void Glib::MainContext::remove_poll ( PollFD fd)

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

Parameters
fdA PollFD structure holding information about a file descriptor.

◆ set_poll_func()

void Glib::MainContext::set_poll_func ( GPollFunc  poll_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.

Parameters
poll_funcThe function to call to poll all file descriptors.

◆ signal_child_watch()

SignalChildWatch Glib::MainContext::signal_child_watch ( )

child watch signal, attached to this MainContext.

Returns
A signal proxy; you want to use SignalChildWatch::connect().

◆ signal_idle()

SignalIdle Glib::MainContext::signal_idle ( )

Idle signal, attached to this MainContext.

Returns
A signal proxy; you want to use SignalIdle::connect().

◆ signal_io()

SignalIO Glib::MainContext::signal_io ( )

I/O signal, attached to this MainContext.

Returns
A signal proxy; you want to use SignalIO::connect().

◆ signal_timeout()

SignalTimeout Glib::MainContext::signal_timeout ( )

Timeout signal, attached to this MainContext.

Returns
A signal proxy; you want to use SignalTimeout::connect().

◆ unreference()

void Glib::MainContext::unreference ( ) const

◆ wait() [1/2]

bool Glib::MainContext::wait ( Glib::Cond cond,
Glib::Mutex mutex 
)

Tries to become the owner of the specified context, as with 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.

Parameters
condA condition variable.
mutexA mutex, currently held.
Returns
true if the operation succeeded, and this thread is now the owner of context.
Deprecated:
Use the underlying g_main_context_is_owner() function and separate locking, if you really need this functionality.

◆ wait() [2/2]

bool Glib::MainContext::wait ( Glib::Threads::Cond cond,
Glib::Threads::Mutex mutex 
)

Tries to become the owner of the specified context, as with 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.

Parameters
condA condition variable.
mutexA mutex, currently held.
Returns
true if the operation succeeded, and this thread is now the owner of context.
Deprecated:
Use the underlying g_main_context_is_owner() function and separate locking, if you really need this functionality.

◆ wakeup()

void Glib::MainContext::wakeup ( )

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