Copyright | Will Thompson Iñaki García Etxebarria and Jonas Platte |
---|---|
License | LGPL-2.1 |
Maintainer | Iñaki García Etxebarria |
Safe Haskell | None |
Language | Haskell2010 |
The GMainContext
struct is an opaque data
type representing a set of sources to be handled in a main loop.
Synopsis
- newtype MainContext = MainContext (ManagedPtr MainContext)
- noMainContext :: Maybe MainContext
- mainContextAcquire :: (HasCallStack, MonadIO m) => MainContext -> m Bool
- mainContextAddPoll :: (HasCallStack, MonadIO m) => MainContext -> PollFD -> Int32 -> m ()
- mainContextCheck :: (HasCallStack, MonadIO m) => MainContext -> Int32 -> [PollFD] -> m Bool
- mainContextDefault :: (HasCallStack, MonadIO m) => m MainContext
- mainContextDispatch :: (HasCallStack, MonadIO m) => MainContext -> m ()
- mainContextFindSourceByFuncsUserData :: (HasCallStack, MonadIO m) => MainContext -> SourceFuncs -> Ptr () -> m Source
- mainContextFindSourceById :: (HasCallStack, MonadIO m) => MainContext -> Word32 -> m Source
- mainContextFindSourceByUserData :: (HasCallStack, MonadIO m) => MainContext -> Ptr () -> m Source
- mainContextGetThreadDefault :: (HasCallStack, MonadIO m) => m MainContext
- mainContextInvokeFull :: (HasCallStack, MonadIO m) => MainContext -> Int32 -> SourceFunc -> m ()
- mainContextIsOwner :: (HasCallStack, MonadIO m) => MainContext -> m Bool
- mainContextIteration :: (HasCallStack, MonadIO m) => MainContext -> Bool -> m Bool
- mainContextNew :: (HasCallStack, MonadIO m) => m MainContext
- mainContextPending :: (HasCallStack, MonadIO m) => MainContext -> m Bool
- mainContextPopThreadDefault :: (HasCallStack, MonadIO m) => MainContext -> m ()
- mainContextPrepare :: (HasCallStack, MonadIO m) => MainContext -> Int32 -> m Bool
- mainContextPushThreadDefault :: (HasCallStack, MonadIO m) => MainContext -> m ()
- mainContextQuery :: (HasCallStack, MonadIO m) => MainContext -> Int32 -> [PollFD] -> m (Int32, Int32, [PollFD])
- mainContextRef :: (HasCallStack, MonadIO m) => MainContext -> m MainContext
- mainContextRefThreadDefault :: (HasCallStack, MonadIO m) => m MainContext
- mainContextRelease :: (HasCallStack, MonadIO m) => MainContext -> m ()
- mainContextRemovePoll :: (HasCallStack, MonadIO m) => MainContext -> PollFD -> m ()
- mainContextUnref :: (HasCallStack, MonadIO m) => MainContext -> m ()
- mainContextWait :: (HasCallStack, MonadIO m) => MainContext -> Cond -> Mutex -> m Bool
- mainContextWakeup :: (HasCallStack, MonadIO m) => MainContext -> m ()
Exported types
newtype MainContext Source #
Memory-managed wrapper type.
Instances
Eq MainContext Source # | |
Defined in GI.GLib.Structs.MainContext (==) :: MainContext -> MainContext -> Bool # (/=) :: MainContext -> MainContext -> Bool # | |
IsGValue MainContext Source # | Convert |
Defined in GI.GLib.Structs.MainContext toGValue :: MainContext -> IO GValue # fromGValue :: GValue -> IO MainContext # | |
BoxedObject MainContext Source # | |
Defined in GI.GLib.Structs.MainContext boxedType :: MainContext -> IO GType # |
noMainContext :: Maybe MainContext Source #
A convenience alias for Nothing
:: Maybe
MainContext
.
Methods
Overloaded methods
acquire
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m Bool | Returns: |
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 mainContextRelease
is called as many times as mainContextAcquire
.
You must be the owner of a context before you
can call mainContextPrepare
, mainContextQuery
,
mainContextCheck
, mainContextDispatch
.
addPoll
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> PollFD |
|
-> Int32 |
|
-> m () |
Adds a file descriptor to the set of file descriptors polled for
this context. This will very seldom be used directly. Instead
a typical event source will use sourceAddUnixFd
instead.
check
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Int32 |
|
-> [PollFD] |
|
-> m Bool | Returns: |
Passes the results of polling back to the main loop.
You must have successfully acquired the context with
mainContextAcquire
before you may call this function.
default
:: (HasCallStack, MonadIO m) | |
=> m MainContext | Returns: the global default main context. |
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. See also
mainContextGetThreadDefault
.
dispatch
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m () |
Dispatches all pending sources.
You must have successfully acquired the context with
mainContextAcquire
before you may call this function.
findSourceByFuncsUserData
mainContextFindSourceByFuncsUserData Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> SourceFuncs |
|
-> Ptr () |
|
-> m Source | Returns: the source, if one was found, otherwise |
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.
findSourceById
mainContextFindSourceById Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Word32 |
|
-> m Source | Returns: the |
Finds a Source
given a pair of context and ID.
It is a programmer error to attempt to lookup a non-existent source.
More specifically: source IDs can be reissued after a source has been
destroyed and therefore it is never valid to use this function with a
source ID which may have already been removed. An example is when
scheduling an idle to run in another thread with g_idle_add()
: the
idle may already have run and been removed by the time this function
is called on its (now invalid) source ID. This source ID may have
been reissued, leading to the operation being performed against the
wrong source.
findSourceByUserData
mainContextFindSourceByUserData Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Ptr () |
|
-> m Source | Returns: the source, if one was found, otherwise |
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.
getThreadDefault
mainContextGetThreadDefault Source #
:: (HasCallStack, MonadIO m) | |
=> m MainContext | Returns: the thread-default |
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 or
mainContextRefThreadDefault
to get a MainContext
to add
their GSources
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 Nothing
if you are running in the default thread.)
If you need to hold a reference on the context, use
mainContextRefThreadDefault
instead.
Since: 2.22
invokeFull
mainContextInvokeFull Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Int32 |
|
-> SourceFunc |
|
-> m () |
Invokes a function in such a way that context
is owned during the
invocation of function
.
This function is the same as g_main_context_invoke()
except that it
lets you specify the priority in case function
ends up being
scheduled as an idle and also lets you give a DestroyNotify
for data
.
notify
should not assume that it is called from any particular
thread or with any particular context acquired.
Since: 2.28
isOwner
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m Bool | Returns: |
Determines whether this thread holds the (recursive)
ownership of this MainContext
. This is useful to
know before waiting on another thread that may be
blocking to get ownership of context
.
Since: 2.10
iteration
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Bool |
|
-> m Bool | Returns: |
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 mayBlock
is True
, waiting
for a source to become ready, then dispatching the highest priority
events sources that are ready. Otherwise, if mayBlock
is False
sources are not waited to become ready, only those highest priority
events sources will be dispatched (if any), that are ready at this
given moment without further waiting.
Note that even when mayBlock
is True
, it is still possible for
mainContextIteration
to return False
, since the wait may
be interrupted for other reasons than an event source becoming ready.
new
:: (HasCallStack, MonadIO m) | |
=> m MainContext | Returns: the new |
Creates a new MainContext
structure.
pending
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m Bool | Returns: |
Checks if any sources have pending events for the given context.
popThreadDefault
mainContextPopThreadDefault Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m () |
Pops context
off the thread-default context stack (verifying that
it was on the top of the stack).
Since: 2.22
prepare
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Int32 |
|
-> m Bool | Returns: |
Prepares to poll sources within a main loop. The resulting information for polling is determined by calling g_main_context_query ().
You must have successfully acquired the context with
mainContextAcquire
before you may call this function.
pushThreadDefault
mainContextPushThreadDefault Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m () |
Acquires context
and sets it as the thread-default context for the
current thread. This will cause certain asynchronous operations
(such as most [gio][gio]-based I/O) which are
started in this thread to run under 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 mainContextGetThreadDefault
,
not the one returned by mainContextDefault
, 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 MainContext
which will be run by a
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 mainContextPopThreadDefault
, assuming you want the
new 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.
in the new thread isn't newly created, or if the thread life
cycle is managed by a ThreadPool
), it is always suggested to wrap
the logic that needs to use the new MainContext
inside a
mainContextPushThreadDefault
/ mainContextPopThreadDefault
pair, otherwise threads that are re-used will end up never explicitly
releasing the 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
mainContextPushThreadDefault
/
mainContextPopThreadDefault
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 g_file_supports_thread_contexts()
.
Since: 2.22
query
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Int32 |
|
-> [PollFD] |
|
-> m (Int32, Int32, [PollFD]) | Returns: the number of records actually stored in |
Determines information necessary to poll this main loop.
You must have successfully acquired the context with
mainContextAcquire
before you may call this function.
ref
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m MainContext | Returns: the |
Increases the reference count on a MainContext
object by one.
refThreadDefault
mainContextRefThreadDefault Source #
:: (HasCallStack, MonadIO m) | |
=> m MainContext | Returns: the thread-default |
Gets the thread-default MainContext
for this thread, as with
mainContextGetThreadDefault
, but also adds a reference to
it with mainContextRef
. In addition, unlike
mainContextGetThreadDefault
, if the thread-default context
is the global default context, this will return that MainContext
(with a ref added to it) rather than returning Nothing
.
Since: 2.32
release
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m () |
Releases ownership of a context previously acquired by this thread
with mainContextAcquire
. If the context was acquired multiple
times, the ownership will be released only when mainContextRelease
is called as many times as it was acquired.
removePoll
mainContextRemovePoll Source #
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> PollFD |
|
-> m () |
Removes file descriptor from the set of file descriptors to be polled for a particular context.
unref
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m () |
Decreases the reference count on a MainContext
object by one. If
the result is zero, free the context and free all associated memory.
wait
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> Cond |
|
-> Mutex |
|
-> m Bool | Returns: |
Deprecated: (Since version 2.58)Use mainContextIsOwner
and separate locking instead.
Tries to become the owner of the specified context,
as with mainContextAcquire
. 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.
wakeup
:: (HasCallStack, MonadIO m) | |
=> MainContext |
|
-> m () |
If context
is currently blocking in mainContextIteration
waiting for a source to become ready, cause it to stop blocking
and return. Otherwise, cause the next invocation of
mainContextIteration
to return without blocking.
This API is useful for low-level control over MainContext
; for
example, integrating it with main loop implementations such as
MainLoop
.
Another related use for this function is when implementing a main loop with a termination condition, computed from multiple threads:
C code
#define NUM_TASKS 10 static volatile gint tasks_remaining = NUM_TASKS; ... while (g_atomic_int_get (&tasks_remaining) != 0) g_main_context_iteration (NULL, TRUE);
Then in a thread:
C code
perform_work(); if (g_atomic_int_dec_and_test (&tasks_remaining)) g_main_context_wakeup (NULL);