Top 顶部 |
![]() |
![]() |
![]() |
![]() |
The Main Event Loop 主事件循环The Main Event Loop — manages all available sources of events |
GMainLoop | |
#define | G_PRIORITY_HIGH |
#define | G_PRIORITY_DEFAULT |
#define | G_PRIORITY_HIGH_IDLE |
#define | G_PRIORITY_DEFAULT_IDLE |
#define | G_PRIORITY_LOW |
#define | G_SOURCE_CONTINUE |
#define | G_SOURCE_REMOVE |
GMainContext | |
enum | GMainContextFlags |
typedef | GMainContextPusher |
typedef | GPid |
#define | G_PID_FORMAT |
struct | GPollFD |
#define | G_POLLFD_FORMAT |
struct | GSource |
struct | GSourceFuncs |
struct 结构体 | GSourceCallbackFuncs |
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()
.
主事件循环管理 GLib 和 GTK+ 应用程序的所有可用事件源。这些事件可以来自任意数量的不同类型的源,例如文件描述符(普通文件、管道或套接字)和超时。还可以使用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. All
functions which operate on a GMainContext or a built-in GSource are
thread-safe.
为了允许在不同线程中处理多个独立的源集,每个源都与一个GMainContext关联。 GMainContext只能在单个线程中运行,但可以在其他线程中向其中添加源或从中删除源。所有在GMainContext或内置GSource上操作的函数都是线程安全的。
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: if
several sources are ready to dispatch, the ones with equal-highest priority
will be dispatched on the current GMainContext iteration, and the rest wait
until a subsequent GMainContext iteration when they have the highest
priority of the sources which are ready for dispatch.
每个事件源都被分配一个优先级。默认优先级G_PRIORITY_DEFAULT
为 0。小于 0 的值表示更高的优先级。大于 0 的值表示优先级较低。来自高优先级源的事件始终在来自低优先级源的事件之前处理:如果有多个源准备好分派,则具有相同最高优先级的源将在当前GMainContext迭代中分派,其余的则等到后续GMainContext迭代时再进行处理。具有最高优先级的已准备好调度的源。
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 dispatched.
还可以添加空闲功能,并分配优先级。只要没有更高优先级的事件可供调度,这些事件就会运行。
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.
GMainLoop数据类型表示主事件循环。 GMainLoop 是使用g_main_loop_new()
创建的。添加初始事件源后,调用g_main_loop_run()
。这会持续检查来自每个事件源的新事件并分派它们。最后,对来自其中一个源的事件的处理导致调用g_main_loop_quit()
以退出主循环,并且g_main_loop_run()
返回。
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.
可以递归地创建GMainLoop的新实例。这通常在 GTK+ 应用程序中显示模式对话框时使用。请注意,事件源与特定的GMainContext关联,并且将为与该 GMainContext 关联的所有主循环进行检查和分派。
GTK+ contains wrappers of some of these functions, e.g. gtk_main()
,
gtk_main_quit()
and gtk_events_pending()
.
GTK+ 包含其中一些函数的包装器,例如gtk_main()
、 gtk_main_quit()
和gtk_events_pending()
。
One of the unusual features of the GMainLoop 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 type.
GMainLoop功能的不寻常功能之一是,除了内置类型的事件源之外,还可以创建和使用新类型的事件源。新的事件源类型用于处理 GDK 事件。通过从GSource结构“派生”来创建新的源类型。源的派生类型由一个结构表示,该结构将GSource结构作为第一个元素,以及特定于新源类型的其他元素。要创建新源类型的实例,请调用g_source_new()
并传入派生结构的大小和函数表。这些GSourceFunc确定新源类型的行为。
New source types basically interact 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()
.
新的源类型基本上以两种方式与主上下文交互。 GSourceFuncs中的准备函数可以设置超时,以确定主循环在再次检查源之前休眠的最长时间。此外,源还可以将文件描述符添加到主上下文使用g_source_add_poll()
检查的集合中。
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()
.
GMainContext的单次迭代可以使用g_main_context_iteration()
运行。在某些情况下,需要对主循环的工作细节进行更详细的控制,例如,将GMainLoop与外部主循环集成时。在这种情况下,您可以直接调用g_main_context_iteration()
的组件函数。这些函数是g_main_context_prepare()
、 g_main_context_query()
、 g_main_context_check()
和g_main_context_dispatch()
。
If the event loop thread releases GMainContext ownership until the results
required by g_main_context_check()
are ready you must create a context with
the flag G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING
or else you'll lose
g_source_attach()
notifications. This happens for instance when you integrate
the GLib event loop into implementations that follow the proactor pattern
(i.e. in these contexts the
implementation will reclaim the thread for
other tasks until the results are ready). One example of the proactor pattern
is the Boost.Asio library.poll()
如果事件循环线程释放GMainContext所有权,直到g_main_context_check()
所需的结果准备就绪,则必须使用标志创建一个上下文 G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING
否则您将丢失g_source_attach()
通知。例如,当您将 GLib 事件循环集成到遵循前摄器模式的实现中时,就会发生这种情况(即,在这些上下文中,
实现将回收其他任务的线程,直到结果准备就绪)。前摄器模式的一个示例是 Boost.Asio 库。poll()
The operation of these functions can best be seen in terms
of a state diagram, as shown in this image.
这些函数的操作可以通过状态图来最好地了解,如下图所示。
On UNIX, the GLib mainloop is incompatible with fork()
. Any program
using the mainloop must either exec()
or exit()
from the child
without returning to the mainloop.
在 UNIX 上,GLib 主循环与fork()
不兼容。任何使用主循环的程序都必须从子循环中执行exec()
或exit()
而不返回到主循环。
There are two options for memory management of the user data passed to a
GSource to be passed to its callback on invocation. This data is provided
in calls to g_timeout_add()
, g_timeout_add_full()
, g_idle_add()
, etc. and
more generally, using g_source_set_callback()
. This data is typically an
object which ‘owns’ the timeout or idle callback, such as a widget or a
network protocol implementation. In many cases, it is an error for the
callback to be invoked after this owning object has been destroyed, as that
results in use of freed memory.
对于传递给GSource的用户数据进行内存管理有两个选项,以便在调用时传递给其回调。此数据在对g_timeout_add()
、 g_timeout_add_full()
、 g_idle_add()
等的调用中提供,更一般地,使用g_source_set_callback()
提供。该数据通常是“拥有”超时或空闲回调的对象,例如小部件或网络协议实现。在许多情况下,在拥有的对象被销毁后调用回调是错误的,因为这会导致使用已释放的内存。
The first, and preferred, option is to store the source ID returned by
functions such as g_timeout_add()
or g_source_attach()
, and explicitly
remove that source from the main context using g_source_remove()
when the
owning object is finalized. This ensures that the callback can only be
invoked while the object is still alive.
第一个也是首选选项是存储g_timeout_add()
或g_source_attach()
等函数返回的源 ID,并在所属对象完成时使用g_source_remove()
从主上下文中显式删除该源。这确保了只能在对象仍然存在时调用回调。
The second option is to hold a strong reference to the object in the
callback, and to release it in the callback’s GDestroyNotify. This ensures
that the object is kept alive until after the source is finalized, which is
guaranteed to be after it is invoked for the final time. The GDestroyNotify
is another callback passed to the ‘full’ variants of GSource functions (for
example, g_timeout_add_full()
). It is called when the source is finalized,
and is designed for releasing references like this.
第二个选项是在回调中保留对对象的强引用,并在回调的GDestroyNotify中释放它。这确保了对象在源完成之前一直保持活动状态,这保证是在最后一次调用它之后。 GDestroyNotify是传递给GSource函数的“完整”变体的另一个回调(例如, g_timeout_add_full()
)。它在源代码最终确定时被调用,旨在释放此类引用。
One important caveat of this second approach is that it will keep the object
alive indefinitely if the main loop is stopped before the GSource is
invoked, which may be undesirable.
第二种方法的一个重要警告是,如果在调用GSource之前停止主循环,它将使对象无限期地保持活动状态,这可能是不可取的。
GMainLoop * g_main_loop_new (GMainContext *context
,gboolean is_running
);
Creates a new GMainLoop structure.
创建一个新的GMainLoop结构。
context 语境 |
a GMainContext (if |
[nullable] [可为空] |
is_running 正在运行 |
set to |
GMainLoop *
g_main_loop_ref (GMainLoop *loop
);
Increases the reference count on a GMainLoop object by one.
将GMainLoop对象的引用计数增加一。
void
g_main_loop_unref (GMainLoop *loop
);
Decreases the reference count on a GMainLoop object by one. If
the result is zero, free the loop and free all associated memory.
将GMainLoop对象的引用计数减一。如果结果为零,则释放循环并释放所有关联的内存。
void
g_main_loop_run (GMainLoop *loop
);
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.
运行主循环,直到在循环中调用g_main_loop_quit()
。如果为循环的GMainContext线程调用此方法,它将处理循环中的事件,否则它将简单地等待。
void
g_main_loop_quit (GMainLoop *loop
);
Stops a GMainLoop from running. Any calls to g_main_loop_run()
for the loop will return.
停止GMainLoop的运行。任何对循环的g_main_loop_run()
调用都将返回。
Note that sources that have already been dispatched when
g_main_loop_quit()
is called will still be executed.
请注意,调用g_main_loop_quit()
时已分派的源仍将被执行。
gboolean
g_main_loop_is_running (GMainLoop *loop
);
Checks to see if the main loop is currently being run via g_main_loop_run()
.
检查主循环当前是否正在通过g_main_loop_run()
运行。
GMainContext *
g_main_loop_get_context (GMainLoop *loop
);
Returns the GMainContext of loop
.
返回loop
的GMainContext 。
#define g_main_new(is_running)
g_main_new
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_new
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_loop_new()
instead
使用g_main_loop_new()
代替
Creates a new GMainLoop for th default main context.
为默认主上下文创建一个新的GMainLoop 。
is_running 正在运行 |
set to |
#define g_main_destroy(loop)
g_main_destroy
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_destroy
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_loop_unref()
instead
使用g_main_loop_unref()
代替
Frees the memory allocated for the GMainLoop.
释放为GMainLoop分配的内存。
#define g_main_run(loop)
g_main_run
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_run
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_loop_run()
instead
使用g_main_loop_run()
代替
Runs a main loop until it stops running.
运行主循环直到停止运行。
#define g_main_quit(loop)
g_main_quit
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_quit
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_loop_quit()
instead
使用g_main_loop_quit()
代替
Stops the GMainLoop.
If g_main_run()
was called to run the GMainLoop, it will now return.
停止GMainLoop 。如果调用g_main_run()
来运行GMainLoop ,它现在将返回。
#define g_main_is_running(loop)
g_main_is_running
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_is_running
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_loop_is_running()
instead
使用g_main_loop_is_running()
代替
Checks if the main loop is running.
检查主循环是否正在运行。
GMainContext *
g_main_context_new (void
);
Creates a new GMainContext structure.
创建一个新的GMainContext结构。
GMainContext *
g_main_context_new_with_flags (GMainContextFlags flags
);
Creates a new GMainContext structure.
创建一个新的GMainContext结构。
flags 旗帜 |
a bitwise-OR combination of GMainContextFlags flags that can only be
set at creation time. |
GMainContext *
g_main_context_ref (GMainContext *context
);
Increases the reference count on a GMainContext object by one.
将GMainContext对象的引用计数增加一。
void
g_main_context_unref (GMainContext *context
);
Decreases the reference count on a GMainContext object by one. If
the result is zero, free the context and free all associated memory.
将GMainContext对象的引用计数减一。如果结果为零,则释放上下文并释放所有关联的内存。
GMainContext *
g_main_context_default (void
);
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
g_main_context_get_thread_default()
.
返回全局默认主上下文。这是当未显式指定主循环时用于主循环函数的主要上下文,并且对应于“main”主循环。参见 g_main_context_get_thread_default()
。
gboolean g_main_context_iteration (GMainContext *context
,gboolean 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. Otherwise, if may_block
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.
为给定的主循环运行一次迭代。这涉及检查是否有任何事件源准备好处理,如果没有事件源准备好并且may_block
为TRUE
,则等待源准备好,然后分派准备就绪的最高优先级事件源。否则,如果may_block
为FALSE
则不会等待源准备就绪,只有那些最高优先级的事件源将被调度(如果有),这些事件源在给定时刻已准备好,无需进一步等待。
Note that even when may_block
is TRUE
, it is still possible for
g_main_context_iteration()
to return FALSE
, since the wait may
be interrupted for other reasons than an event source becoming ready.
请注意,即使may_block
为TRUE
, g_main_context_iteration()
仍然有可能返回FALSE
,因为等待可能会因事件源准备就绪以外的其他原因而中断。
context 语境 |
a GMainContext (if |
[nullable] [可为空] |
may_block 可能会阻止 |
whether the call may block. |
#define g_main_iteration(may_block)
g_main_iteration
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_iteration
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_context_iteration()
instead.
使用g_main_context_iteration()
代替。
Runs a single iteration for the default GMainContext.
为默认的GMainContext运行一次迭代。
may_block 可能会阻止 |
set to |
gboolean
g_main_context_pending (GMainContext *context
);
Checks if any sources have pending events for the given context.
检查是否有任何源有给定上下文的待处理事件。
context 语境 |
a GMainContext (if |
[nullable] [可为空] |
#define g_main_pending()
g_main_pending
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_pending
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_context_pending()
instead.
使用g_main_context_pending()
代替。
Checks if any events are pending for the default GMainContext
(i.e. ready to be processed).
检查默认GMainContext是否有任何待处理事件(即准备好处理)。
GSource * g_main_context_find_source_by_id (GMainContext *context
,guint source_id
);
Finds a GSource given a pair of context and ID.
在给定一对上下文和 ID 的情况下查找GSource 。
It is a programmer error to attempt to look up 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.
更具体地说:源 ID 可以在源被销毁后重新发布,因此使用此函数与可能已被删除的源 ID 永远是无效的。一个示例是,当使用g_idle_add()
调度空闲在另一个线程中运行时:在其(现在无效)源 ID 上调用此函数时,空闲可能已经运行并被删除。该源 ID 可能已重新发布,从而导致针对错误的源执行操作。
context 语境 |
a GMainContext (if |
[nullable] [可为空] |
source_id 源ID |
the source ID, as returned by |
GSource * g_main_context_find_source_by_user_data (GMainContext *context
,gpointer user_data
);
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.
查找具有给定用户数据的回调源。如果存在具有相同用户数据的多个源,则将返回找到的第一个源。
GSource * g_main_context_find_source_by_funcs_user_data (GMainContext *context
,GSourceFuncs *funcs
,gpointer user_data
);
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 |
[nullable] [可为空] |
funcs 函数 |
the |
|
user_data 用户数据 |
the user data from the callback. |
void
g_main_context_wakeup (GMainContext *context
);
If context
is currently blocking in g_main_context_iteration()
waiting for a source to become ready, cause it to stop blocking
and return. Otherwise, cause the next invocation of
g_main_context_iteration()
to return without blocking.
如果context
当前在g_main_context_iteration()
中阻塞等待源准备就绪,则使其停止阻塞并返回。否则,导致g_main_context_iteration()
的下一次调用返回而不阻塞。
This API is useful for low-level control over GMainContext; for
example, integrating it with main loop implementations such as
GMainLoop.
此 API 对于GMainContext的低级控制很有用;例如,将其与GMainLoop等主循环实现集成。
Another related use for this function is when implementing a main
loop with a termination condition, computed from multiple threads:
此函数的另一个相关用途是实现具有终止条件的主循环(从多个线程计算):
1 2 3 4 5 6 |
#define NUM_TASKS 10 static gint tasks_remaining = NUM_TASKS; // (atomic) ... while (g_atomic_int_get (&tasks_remaining) != 0) g_main_context_iteration (NULL, TRUE); |
Then in a thread: 然后在一个线程中:
1 2 3 4 |
perform_work(); if (g_atomic_int_dec_and_test (&tasks_remaining)) g_main_context_wakeup (NULL); |
gboolean
g_main_context_acquire (GMainContext *context
);
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()
.
尝试成为指定上下文的所有者。如果某个其他线程是上下文的所有者,则立即返回FALSE
。所有权是正确递归的:所有者可以再次请求所有权,并在调用g_main_context_release()
与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()
.
您必须是上下文的所有者,然后才能调用g_main_context_prepare()
、 g_main_context_query()
、 g_main_context_check()
、 g_main_context_dispatch()
。
void
g_main_context_release (GMainContext *context
);
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.
释放该线程之前使用g_main_context_acquire()
获取的上下文的所有权。如果上下文被多次获取,则只有当g_main_context_release()
被调用的次数与获取的次数相同时,所有权才会被释放。
gboolean
g_main_context_is_owner (GMainContext *context
);
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
.
确定此线程是否拥有此GMainContext的(递归)所有权。在等待可能阻塞的另一个线程获取context
所有权之前了解这一点很有用。
gboolean g_main_context_wait (GMainContext *context
,GCond *cond
,GMutex *mutex
);
g_main_context_wait
has been deprecated since version 2.58 and should not be used in newly-written code.g_main_context_wait
自版本 2.58 起已被弃用,不应在新编写的代码中使用。
Use g_main_context_is_owner()
and separate locking instead.
使用g_main_context_is_owner()
并单独锁定。
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.
尝试成为指定上下文的所有者,如g_main_context_acquire()
。但是,如果另一个线程是所有者,则自动删除mutex
并等待cond
直到该所有者释放所有权或直到cond
发出信号,然后再次尝试(一次)成为所有者。
gboolean g_main_context_prepare (GMainContext *context
,gint *priority
);
Prepares to poll sources within a main loop. The resulting information
for polling is determined by calling g_main_context_query()
.
准备在主循环内轮询源。轮询的结果信息是通过调用g_main_context_query()
确定的。
You must have successfully acquired the context with
g_main_context_acquire()
before you may call this function.
您必须先使用g_main_context_acquire()
成功获取上下文,然后才能调用此函数。
gint g_main_context_query (GMainContext *context
,gint max_priority
,gint *timeout_
,GPollFD *fds
,gint n_fds
);
Determines information necessary to poll this main loop. You should
be careful to pass the resulting fds
array and its length n_fds
as is when calling g_main_context_check()
, as this function relies
on assumptions made when the array is filled.
确定轮询该主循环所需的信息。在调用g_main_context_check()
时,您应该小心地传递生成的fds
数组及其长度n_fds
,因为此函数依赖于数组填充时所做的假设。
You must have successfully acquired the context with
g_main_context_acquire()
before you may call this function.
您必须先使用g_main_context_acquire()
成功获取上下文,然后才能调用此函数。
context 语境 |
||
max_priority 最大优先级 |
maximum priority source to check |
|
timeout_ 暂停_ |
location to store timeout to be used in polling. |
[out] [出去] |
fds |
location to
store GPollFD records that need to be polled. |
[out caller-allocates][array length=n_fds] [调用者分配][数组长度=n_fds] |
n_fds |
length of |
[in] [在] |
gboolean g_main_context_check (GMainContext *context
,gint max_priority
,GPollFD *fds
,gint n_fds
);
Passes the results of polling back to the main loop. You should be
careful to pass fds
and its length n_fds
as received from
g_main_context_query()
, as this functions relies on assumptions
on how fds
is filled.
将轮询结果传递回主循环。您应该小心传递从g_main_context_query()
接收到的fds
及其长度n_fds
,因为此函数依赖于对fds
填充方式的假设。
You must have successfully acquired the context with
g_main_context_acquire()
before you may call this function.
您必须先使用g_main_context_acquire()
成功获取上下文,然后才能调用此函数。
context 语境 |
||
max_priority 最大优先级 |
the maximum numerical priority of sources to check |
|
fds |
array of GPollFD's that was passed to
the last call to |
[array length=n_fds] [数组长度=n_fds] |
n_fds |
return value of |
void
g_main_context_dispatch (GMainContext *context
);
Dispatches all pending sources.
调度所有待处理的源。
You must have successfully acquired the context with
g_main_context_acquire()
before you may call this function.
您必须先使用g_main_context_acquire()
成功获取上下文,然后才能调用此函数。
void g_main_context_set_poll_func (GMainContext *context
,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).
设置用于处理文件描述符轮询的函数。将使用它将代替poll()
系统调用(或 GLib 的替换函数,该函数在poll()
不可用的情况下使用)。
This function could possibly be used to integrate the GLib event
loop with an external event loop.
该函数可用于将 GLib 事件循环与外部事件循环集成。
GPollFunc
g_main_context_get_poll_func (GMainContext *context
);
Gets the poll function set by g_main_context_set_poll_func()
.
获取由以下设置的 poll 函数 g_main_context_set_poll_func()
。
gint (*GPollFunc) (GPollFD *ufds
,guint nfsd
,gint timeout_
);
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.
指定传递给的函数类型 g_main_context_set_poll_func()
。该函数的语义应与poll()
系统调用的语义相匹配。
void g_main_context_add_poll (GMainContext *context
,GPollFD *fd
,gint priority
);
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 g_source_add_unix_fd()
instead.
将文件描述符添加到为此上下文轮询的文件描述符集中。这很少会被直接使用。相反,典型的事件源将使用g_source_add_unix_fd()
代替。
context 语境 |
a GMainContext (or |
[nullable] [可为空] |
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 |
void g_main_context_remove_poll (GMainContext *context
,GPollFD *fd
);
Removes file descriptor from the set of file descriptors to be
polled for a particular context.
从要轮询特定上下文的文件描述符集中删除文件描述符。
context 语境 |
||
fd |
a GPollFD descriptor previously added with |
gint
g_main_depth (void
);
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_iteration()
,
it returns 2. And so forth.
返回当前线程中任何GMainContext上对g_main_context_dispatch()
的调用堆栈的深度。也就是说,当从顶层调用时,它给出 0。当从g_main_context_iteration()
(或g_main_loop_run()
等)的回调中调用时,它返回 1。当从g_main_context_iteration()
递归调用的回调中调用时,它返回 1。 ,它返回 2。依此类推。
This function is useful in a situation like the following:
Imagine an extremely simple "garbage collected" system.
该函数在如下情况下非常有用:想象一个极其简单的“垃圾收集”系统。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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()
这可以在应用程序中工作,但是,如果您想从库中执行相同的操作,则会变得更加困难,因为您不再控制主循环。您可能认为可以简单地使用空闲函数来调用free_allocated_memory()
,但这不起作用,因为可以从递归回调中调用空闲函数。这可以通过使用g_main_depth()
来修复
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
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:
人们倾向于使用g_main_depth()
来解决可重入问题。例如,当等待从网络接收响应于菜单项的数据时,可以再次选择该菜单项。如果g_main_depth()
返回大于 1 的值,似乎可以使菜单项的回调立即返回并且不执行任何操作。但是,应该避免这种情况,因为用户会看到选择菜单项不执行任何操作。此外,您会发现自己在代码中添加了这些检查,因为用户无疑可以做很多很多事情。相反,您可以使用以下技术:
Use gtk_widget_set_sensitive()
or modal dialogs to prevent
the user from interacting with elements while the main
loop is recursing.
使用gtk_widget_set_sensitive()
或模式对话框来防止用户在主循环递归时与元素交互。
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.
在无法处理任意回调的情况下避免主循环递归。相反,构建您的代码,以便您只需返回主循环,然后在有更多工作要做时再次调用。
GSource *
g_main_current_source (void
);
Returns the currently firing source for this thread.
返回该线程当前的触发源。
#define g_main_set_poll_func(func)
g_main_set_poll_func
has been deprecated since version 2.2 and should not be used in newly-written code.g_main_set_poll_func
自 2.2 版本起已被弃用,不应在新编写的代码中使用。
Use g_main_context_set_poll_func()
again 使用 g_main_context_set_poll_func()
再次
Sets the function to use for the handle polling of file descriptors
for the default main context.
设置用于默认主上下文的文件描述符句柄轮询的函数。
void g_main_context_invoke (GMainContext *context
,GSourceFunc function
,gpointer data
);
Invokes a function in such a way that context
is owned during the
invocation of function
.
以在调用function
期间拥有context
的方式调用函数。
If context
is NULL
then the global default main context — as
returned by g_main_context_default()
— is used.
如果context
为NULL
,则使用g_main_context_default()
返回的全局默认主上下文。
If context
is owned by the current thread, function
is called
directly. Otherwise, if context
is the thread-default main context
of the current thread and g_main_context_acquire()
succeeds, then
function
is called and g_main_context_release()
is called
afterwards.
如果context
由当前线程拥有,则直接调用function
。否则,如果context
是当前线程的线程默认主上下文,并且g_main_context_acquire()
成功,则调用function
,然后调用g_main_context_release()
。
In any other case, an idle source is created to call function
and
that source is attached to context
(presumably to be run in another
thread). The idle source is attached with G_PRIORITY_DEFAULT
priority. If you want a different priority, use
g_main_context_invoke_full()
.
在任何其他情况下,都会创建一个空闲源来调用function
,并将该源附加到context
(可能在另一个线程中运行)。空闲源附加G_PRIORITY_DEFAULT
优先级。如果您想要不同的优先级,请使用g_main_context_invoke_full()
。
Note that, as with normal idle functions, function
should probably
return FALSE
. If it returns TRUE
, it will be continuously run in a
loop (and may prevent this call from returning).
请注意,与正常的空闲函数一样, function
可能应该返回FALSE
。如果它返回TRUE
,它将在循环中连续运行(并且可能会阻止此调用返回)。
context 语境 |
a GMainContext, or |
[nullable] [可为空] |
function 功能 |
function to call 要调用的函数 |
|
data 数据 |
data to pass to |
void g_main_context_invoke_full (GMainContext *context
,gint priority
,GSourceFunc function
,gpointer data
,GDestroyNotify notify
);
Invokes a function in such a way that context
is owned during the
invocation of function
.
以在调用function
期间拥有context
的方式调用函数。
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 GDestroyNotify for data
.
此函数与g_main_context_invoke()
相同,只是它允许您指定优先级,以防function
最终被安排为空闲,并且还允许您为data
提供GDestroyNotify 。
notify
should not assume that it is called from any particular
thread or with any particular context acquired.notify
不应假设它是从任何特定线程调用的,或者是在获取任何特定上下文的情况下调用的。
context 语境 |
a GMainContext, or |
[nullable] [可为空] |
priority 优先事项 |
the priority at which to run |
|
function 功能 |
function to call 要调用的函数 |
|
data 数据 |
data to pass to |
|
notify 通知 |
a function to call when |
[nullable] [可为空] |
GMainContextPusher *
g_main_context_pusher_new (GMainContext *main_context
);
Push main_context
as the new thread-default main context for the current
thread, using g_main_context_push_thread_default()
, and return a new
GMainContextPusher. Pop with g_main_context_pusher_free()
. Using
g_main_context_pop_thread_default()
on main_context
while a
GMainContextPusher exists for it can lead to undefined behaviour.
将main_context
推入当前线程的新线程默认主上下文,使用 g_main_context_push_thread_default()
,并返回一个新的GMainContextPusher 。使用g_main_context_pusher_free()
弹出。使用 g_main_context_pop_thread_default()
当GMainContextPusher存在时,在main_context
上可能会导致未定义的行为。
Using two GMainContextPushers in the same scope is not allowed, as it leads
to an undefined pop order.
不允许在同一范围内使用两个GMainContextPushers ,因为这会导致未定义的弹出顺序。
This is intended to be used with g_autoptr()
. Note that g_autoptr()
is only available when using GCC or clang, so the following example
will only work with those compilers:
这旨在与g_autoptr()
一起使用。请注意, g_autoptr()
仅在使用 GCC 或 clang 时可用,因此以下示例仅适用于这些编译器:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
typedef struct { ... GMainContext *context; ... } MyObject; static void my_object_do_stuff (MyObject *self) { g_autoptr(GMainContextPusher) pusher = g_main_context_pusher_new (self->context); // Code with main context as the thread default here if (cond) // No need to pop return; // Optionally early pop g_clear_pointer (&pusher, g_main_context_pusher_free); // Code with main context no longer the thread default here } |
void
g_main_context_pusher_free (GMainContextPusher *pusher
);
Pop pusher
’s main context as the thread default main context.
See g_main_context_pusher_new()
for details.
Pop pusher
的主上下文作为线程默认主上下文。有关详细信息,请参阅g_main_context_pusher_new()
。
This will pop the GMainContext as the current thread-default main context,
but will not call g_main_context_unref()
on it.
这会将GMainContext弹出为当前线程默认主上下文,但不会对其调用g_main_context_unref()
。
GMainContext *
g_main_context_get_thread_default (void
);
Gets the thread-default GMainContext 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
g_main_context_ref_thread_default()
to get a GMainContext 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 NULL
if you are running in the default thread.)
获取此线程的线程默认GMainContext 。希望能够在默认上下文以外的上下文中运行的异步操作应调用此方法或 g_main_context_ref_thread_default()
获取GMainContext以将其GSource添加到其中。 (请注意,即使在单线程程序中,应用程序有时也可能想要临时推送非默认上下文,因此如果您在默认线程中运行,则假设这将始终返回NULL
是不安全的。)
If you need to hold a reference on the context, use
g_main_context_ref_thread_default()
instead.
如果您需要保留上下文的引用,请使用 g_main_context_ref_thread_default()
反而。
the thread-default GMainContext, or
NULL
if the thread-default context is the global default context.
线程默认GMainContext ,如果线程默认上下文是全局默认上下文,则为NULL
。
[transfer none][nullable]
[不传输][可为空]
GMainContext *
g_main_context_ref_thread_default (void
);
Gets the thread-default GMainContext for this thread, as with
g_main_context_get_thread_default()
, but also adds a reference to
it with g_main_context_ref()
. In addition, unlike
g_main_context_get_thread_default()
, if the thread-default context
is the global default context, this will return that GMainContext
(with a ref added to it) rather than returning NULL
.
获取该线程的线程默认GMainContext ,如下所示 g_main_context_get_thread_default()
,而且还使用g_main_context_ref()
添加对它的引用。另外,与 g_main_context_get_thread_default()
,如果线程默认上下文是全局默认上下文,这将返回GMainContext (添加了引用)而不是返回NULL
。
the thread-default GMainContext. Unref
with g_main_context_unref()
when you are done with it.
线程默认的GMainContext 。完成后使用g_main_context_unref()
取消引用。
[transfer full]
[转满]
void
g_main_context_push_thread_default (GMainContext *context
);
Acquires 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 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 g_main_context_get_thread_default()
,
not the one returned by g_main_context_default()
, so it does not affect
the context used by functions like g_idle_add()
.
获取context
并将其设置为当前线程的线程默认上下文。这将导致在此线程中启动的某些异步操作(例如大多数基于 gio 的 I/O)在context
下运行并将其结果传递到其主循环,而不是在主线程中的全局默认上下文下运行。请注意,调用此函数会更改返回的上下文 g_main_context_get_thread_default()
,而不是g_main_context_default()
返回的值,因此它不会影响g_idle_add()
等函数使用的上下文。
Normally you would call this function shortly after creating a new
thread, passing it a GMainContext which will be run by a
GMainLoop 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 g_main_context_pop_thread_default()
, assuming you want the
new GMainContext to be the default for the whole lifecycle of the
thread.
通常,您会在创建新线程后不久调用此函数,并向其传递将由该线程中的GMainLoop运行的GMainContext ,以便为该线程中的所有异步操作设置新的默认上下文。在这种情况下,您可能不需要致电 g_main_context_pop_thread_default()
,假设您希望新的GMainContext成为线程整个生命周期的默认值。
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 GThreadPool), it is always suggested to wrap
the logic that needs to use the new GMainContext inside a
g_main_context_push_thread_default()
/ g_main_context_pop_thread_default()
pair, otherwise threads that are re-used will end up never explicitly
releasing the GMainContext reference they hold.
如果您无法控制新线程的创建方式(例如,新线程不是新创建的,或者线程生命周期由GThreadPool管理),则始终建议包装需要的逻辑在 a 中使用新的GMainContext g_main_context_push_thread_default()
/ g_main_context_pop_thread_default()
配对,否则重用的线程最终将永远不会显式释放它们持有的GMainContext引用。
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
g_main_context_push_thread_default()
/
g_main_context_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.
在某些情况下,您可能希望在非默认上下文中安排单个操作,或者在主线程中临时使用非默认上下文。在这种情况下,您可以将对异步操作的调用包装在 g_main_context_push_thread_default()
/ g_main_context_pop_thread_default()
配对,但您需要确保在非默认上下文处于活动状态时不会意外启动其他异步操作。
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()
.
请注意,早于该函数的库可能无法正确处理具有线程默认上下文的线程的使用。例如,参见 g_file_supports_thread_contexts()
。
context 语境 |
a GMainContext, or |
[nullable] [可为空] |
void
g_main_context_pop_thread_default (GMainContext *context
);
Pops context
off the thread-default context stack (verifying that
it was on the top of the stack).
将context
从线程默认上下文堆栈中弹出(验证它位于堆栈顶部)。
GSource *
g_timeout_source_new (guint interval
);
Creates a new timeout source.
创建新的超时源。
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.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()
添加到 GMainContext 中。
The interval given is in terms of monotonic time, not wall clock
time. See g_get_monotonic_time()
.
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()
。
GSource *
g_timeout_source_new_seconds (guint interval
);
Creates a new timeout source.
创建新的超时源。
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.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()
添加到 GMainContext 中。
The scheduling granularity/accuracy of this timeout source will be
in seconds.
该超时源的调度粒度/精度将以秒为单位。
The interval given is in terms of monotonic time, not wall clock time.
See g_get_monotonic_time()
.
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()
。
guint g_timeout_add (guint interval
,GSourceFunc function
,gpointer data
);
Sets a function to be called at regular intervals, with the default
priority, G_PRIORITY_DEFAULT
.
设置定期调用的函数,使用默认优先级G_PRIORITY_DEFAULT
。
The given function
is called repeatedly until it returns G_SOURCE_REMOVE
or FALSE
, at which point the timeout is automatically destroyed and the
function will not be called again. The first call to the function will be
at the end of the first interval
.
给定的function
会被重复调用,直到返回G_SOURCE_REMOVE
或FALSE
,此时超时会自动销毁,并且该函数不会再次被调用。第一次调用该函数将在第一个interval
结束时进行。
Note that timeout functions may be delayed, due to the processing of other
event sources. Thus they should not be relied on for precise timing.
After each call to the timeout function, the time of the next
timeout is recalculated based on the current time and the given interval
(it does not try to 'catch up' time lost in delays).
请注意,由于其他事件源的处理,超时函数可能会延迟。因此,不应依赖它们来进行精确计时。每次调用超时函数后,都会根据当前时间和给定的时间间隔重新计算下一次超时的时间(它不会尝试“弥补”延迟中损失的时间)。
See memory management of sources for details
on how to handle the return value and memory management of data
.
有关如何处理返回值和data
内存管理的详细信息,请参阅源的内存管理。
If you want to have a timer in the "seconds" range and do not care
about the exact time of the first call of the timer, use the
g_timeout_add_seconds()
function; this function allows for more
optimizations and more efficient system power usage.
如果你想有一个“秒”范围内的计时器,并且不关心第一次调用计时器的确切时间,请使用g_timeout_add_seconds()
函数;此功能可以实现更多优化和更有效的系统电源使用。
This internally creates a main loop source using g_timeout_source_new()
and attaches it to the global GMainContext using g_source_attach()
, so
the callback will be invoked in whichever thread is running that main
context. You can do these steps manually if you need greater control or to
use a custom main context.
这在内部使用g_timeout_source_new()
创建一个主循环源,并使用g_source_attach()
将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。
It is safe to call this function from any thread.
从任何线程调用此函数都是安全的。
The interval given is in terms of monotonic time, not wall clock
time. See g_get_monotonic_time()
.
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()
。
guint g_timeout_add_full (gint priority
,guint interval
,GSourceFunc function
,gpointer data
,GDestroyNotify notify
);
Sets a function to be called at regular intervals, with the given
priority. The function is called repeatedly until it returns
FALSE
, at which point the timeout is automatically destroyed and
the function will not be called again. The notify
function is
called when the timeout is destroyed. The first call to the
function will be at the end of the first interval
.
设置一个以给定优先级定期调用的函数。该函数会被重复调用,直到返回FALSE
,此时超时自动销毁,并且该函数不会再次被调用。当超时被销毁时,会调用notify
函数。第一次调用该函数将在第一个interval
结束时进行。
Note that timeout functions may be delayed, due to the processing of other
event sources. Thus they should not be relied on for precise timing.
After each call to the timeout function, the time of the next
timeout is recalculated based on the current time and the given interval
(it does not try to 'catch up' time lost in delays).
请注意,由于其他事件源的处理,超时函数可能会延迟。因此,不应依赖它们来进行精确计时。每次调用超时函数后,都会根据当前时间和给定的时间间隔重新计算下一次超时的时间(它不会尝试“弥补”延迟中损失的时间)。
See memory management of sources for details
on how to handle the return value and memory management of data
.
有关如何处理返回值和data
内存管理的详细信息,请参阅源的内存管理。
This internally creates a main loop source using g_timeout_source_new()
and attaches it to the global GMainContext using g_source_attach()
, so
the callback will be invoked in whichever thread is running that main
context. You can do these steps manually if you need greater control or to
use a custom main context.
这在内部使用g_timeout_source_new()
创建一个主循环源,并使用g_source_attach()
将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。
The interval given is in terms of monotonic time, not wall clock time.
See g_get_monotonic_time()
.
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()
。
[rename-to g_timeout_add]
[重命名为g_timeout_add]
priority 优先事项 |
the priority of the timeout source. Typically this will be in
the range between |
|
interval 间隔 |
the time between calls to the function, in milliseconds
(1/1000ths of a second) |
|
function 功能 |
function to call 要调用的函数 |
|
data 数据 |
data to pass to |
|
notify 通知 |
function to call when the timeout is removed, or |
[nullable] [可为空] |
guint g_timeout_add_seconds (guint interval
,GSourceFunc function
,gpointer data
);
Sets a function to be called at regular intervals with the default
priority, G_PRIORITY_DEFAULT
.
设置一个以默认优先级G_PRIORITY_DEFAULT
定期调用的函数。
The function is called repeatedly until it returns G_SOURCE_REMOVE
or FALSE
, at which point the timeout is automatically destroyed
and the function will not be called again.
该函数会被重复调用,直到返回G_SOURCE_REMOVE
或FALSE
,此时超时自动销毁,该函数不会再次被调用。
This internally creates a main loop source using
g_timeout_source_new_seconds()
and attaches it to the main loop context
using g_source_attach()
. You can do these steps manually if you need
greater control. Also see g_timeout_add_seconds_full()
.
这在内部创建了一个主循环源,使用 g_timeout_source_new_seconds()
并使用g_source_attach()
将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。另请参阅g_timeout_add_seconds_full()
。
It is safe to call this function from any thread.
从任何线程调用此函数都是安全的。
Note that the first call of the timer may not be precise for timeouts
of one second. If you need finer precision and have such a timeout,
you may want to use g_timeout_add()
instead.
请注意,计时器的第一次调用对于一秒的超时可能不精确。如果您需要更精细的精度并有这样的超时,您可能需要使用g_timeout_add()
代替。
See memory management of sources for details
on how to handle the return value and memory management of data
.
有关如何处理返回值和data
内存管理的详细信息,请参阅源的内存管理。
The interval given is in terms of monotonic time, not wall clock
time. See g_get_monotonic_time()
.
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()
。
guint g_timeout_add_seconds_full (gint priority
,guint interval
,GSourceFunc function
,gpointer data
,GDestroyNotify notify
);
Sets a function to be called at regular intervals, with priority
.
设置一个定期调用的函数, priority
为 。
The function is called repeatedly until it returns G_SOURCE_REMOVE
or FALSE
, at which point the timeout is automatically destroyed and
the function will not be called again.
该函数会被重复调用,直到返回G_SOURCE_REMOVE
或FALSE
,此时超时自动销毁,该函数不会再次被调用。
Unlike g_timeout_add()
, this function operates at whole second granularity.
The initial starting point of the timer is determined by the implementation
and the implementation is expected to group multiple timers together so that
they fire all at the same time. To allow this grouping, the interval
to the
first timer is rounded and can deviate up to one second from the specified
interval. Subsequent timer iterations will generally run at the specified
interval.
与g_timeout_add()
不同,该函数以整个秒粒度运行。计时器的初始起点由实现确定,并且预期实现将多个计时器分组在一起,以便它们同时触发。为了允许这种分组,第一个计时器的interval
被舍入,并且可以与指定的时间间隔最多偏差一秒。后续的计时器迭代通常会按照指定的时间间隔运行。
Note that timeout functions may be delayed, due to the processing of other
event sources. Thus they should not be relied on for precise timing.
After each call to the timeout function, the time of the next
timeout is recalculated based on the current time and the given interval
请注意,由于其他事件源的处理,超时函数可能会延迟。因此,不应依赖它们来进行精确计时。每次调用超时函数后,都会根据当前时间和给定的interval
重新计算下一次超时的时间
See memory management of sources for details
on how to handle the return value and memory management of data
.
有关如何处理返回值和data
内存管理的详细信息,请参阅源的内存管理。
If you want timing more precise than whole seconds, use g_timeout_add()
instead.
如果您希望计时比整秒更精确,请改用g_timeout_add()
。
The grouping of timers to fire at the same time results in a more power
and CPU efficient behavior so if your timer is in multiples of seconds
and you don't require the first timer exactly one second from now, the
use of g_timeout_add_seconds()
is preferred over g_timeout_add()
.
同时触发的计时器分组会导致更省电和更高效的 CPU 行为,因此,如果您的计时器以秒为单位,并且您不需要从现在起正好一秒后使用第一个计时器,则使用g_timeout_add_seconds()
是优于g_timeout_add()
。
This internally creates a main loop source using
g_timeout_source_new_seconds()
and attaches it to the main loop context
using g_source_attach()
. You can do these steps manually if you need
greater control.
这在内部创建了一个主循环源,使用 g_timeout_source_new_seconds()
并使用g_source_attach()
将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。
It is safe to call this function from any thread.
从任何线程调用此函数都是安全的。
The interval given is in terms of monotonic time, not wall clock
time. See g_get_monotonic_time()
.
给出的间隔是单调时间,而不是挂钟时间。请参阅g_get_monotonic_time()
。
[rename-to g_timeout_add_seconds]
[重命名为g_timeout_add_seconds]
priority 优先事项 |
the priority of the timeout source. Typically this will be in
the range between |
|
interval 间隔 |
the time between calls to the function, in seconds |
|
function 功能 |
function to call 要调用的函数 |
|
data 数据 |
data to pass to |
|
notify 通知 |
function to call when the timeout is removed, or |
[nullable] [可为空] |
GSource *
g_idle_source_new (void
);
Creates a new idle source.
创建一个新的空闲源。
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
.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()
添加到 GMainContext 中。请注意,与默认优先级为G_PRIORITY_DEFAULT
的其他源相比,空闲源的默认优先级为G_PRIORITY_DEFAULT_IDLE
。
guint g_idle_add (GSourceFunc function
,gpointer data
);
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.
添加一个函数,只要默认主循环中没有待处理的更高优先级事件,就会调用该函数。该函数被赋予默认的空闲优先级G_PRIORITY_DEFAULT_IDLE
。如果函数返回FALSE
它将自动从事件源列表中删除,并且不会再次调用。
See memory management of sources for details
on how to handle the return value and memory management of data
.
有关如何处理返回值和data
内存管理的详细信息,请参阅源的内存管理。
This internally creates a main loop source using g_idle_source_new()
and attaches it to the global GMainContext using g_source_attach()
, so
the callback will be invoked in whichever thread is running that main
context. You can do these steps manually if you need greater control or to
use a custom main context.
这在内部使用g_idle_source_new()
创建一个主循环源,并使用g_source_attach()
将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。
guint g_idle_add_full (gint priority
,GSourceFunc function
,gpointer data
,GDestroyNotify notify
);
Adds a function to be called whenever there are no higher priority
events pending.
添加一个在没有更高优先级事件待处理时调用的函数。
If the function returns G_SOURCE_REMOVE
or FALSE
it is automatically
removed from the list of event sources and will not be called again.
如果函数返回G_SOURCE_REMOVE
或FALSE
它将自动从事件源列表中删除,并且不会再次调用。
See memory management of sources for details
on how to handle the return value and memory management of data
.
有关如何处理返回值和data
内存管理的详细信息,请参阅源的内存管理。
This internally creates a main loop source using g_idle_source_new()
and attaches it to the global GMainContext using g_source_attach()
, so
the callback will be invoked in whichever thread is running that main
context. You can do these steps manually if you need greater control or to
use a custom main context.
这在内部使用g_idle_source_new()
创建一个主循环源,并使用g_source_attach()
将其附加到全局GMainContext ,因此回调将在运行该主上下文的任何线程中调用。如果您需要更好的控制或使用自定义主上下文,您可以手动执行这些步骤。
[rename-to g_idle_add]
[重命名为g_idle_add]
priority 优先事项 |
the priority of the idle source. Typically this will be in the
range between |
|
function 功能 |
function to call 要调用的函数 |
|
data 数据 |
data to pass to |
|
notify 通知 |
function to call when the idle is removed, or |
[nullable] [可为空] |
gboolean
g_idle_remove_by_data (gpointer data
);
Removes the idle function with the given data.
使用给定数据删除空闲函数。
void (*GChildWatchFunc) (GPid pid
,gint wait_status
,gpointer user_data
);
Prototype of a GChildWatchSource callback, called when a child
process has exited.
GChildWatchSource回调的原型,在子进程退出时调用。
To interpret wait_status
, see the documentation
for g_spawn_check_wait_status()
. In particular,
on Unix platforms, note that it is usually not equal
to the integer passed to
or returned from exit()
.main()
要解释wait_status
,请参阅g_spawn_check_wait_status()
的文档。特别是,在 Unix 平台上,请注意它通常不等于传递给
或从exit()
返回的整数。main()
pid |
the process id of the child process |
|
wait_status 等待状态 |
Status information about the child process, encoded
in a platform-specific manner |
|
user_data 用户数据 |
user data passed to |
GSource *
g_child_watch_source_new (GPid pid
);
Creates a new child_watch source.
创建一个新的 child_watch 源。
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.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()
添加到 GMainContext 中。
Note that child watch sources can only be used in conjunction with
g_spawn...
when the G_SPAWN_DO_NOT_REAP_CHILD
flag is used.
请注意,当使用G_SPAWN_DO_NOT_REAP_CHILD
标志时,子监视源只能与g_spawn...
结合使用。
Note that on platforms where GPid must be explicitly 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.
请注意,在必须显式关闭GPid的平台上(请参阅g_spawn_close_pid()
),当源仍处于活动状态时,不得关闭pid
。通常,您需要在源的回调函数中调用g_spawn_close_pid()
。
On POSIX platforms, the following restrictions apply to this API
due to limitations in POSIX process interfaces:
在 POSIX 平台上,由于 POSIX 进程接口的限制,以下限制适用于此 API:
pid
must be a child of this processpid
必须是该进程的子进程
pid
must be positivepid
必须为正
the application must not call waitpid
with a non-positive
first argument, for instance in another thread
应用程序不得使用非正第一个参数调用waitpid
,例如在另一个线程中
the application must not wait for pid
to exit by any other
mechanism, including waitpid(pid, ...)
or a second child-watch
source for the same pid
应用程序不得通过任何其他机制等待pid
退出,包括waitpid(pid, ...)
或同一pid
的第二个子监视源
the application must not ignore SIGCHLD
应用程序不得忽略SIGCHLD
If any of those conditions are not met, this and related APIs will
not work correctly. This can often be diagnosed via a GLib warning
stating that ECHILD
was received by waitpid
.
如果不满足其中任何条件,此 API 和相关 API 将无法正常工作。这通常可以通过 GLib 警告来诊断,该警告指出ECHILD
已被waitpid
接收。
Calling waitpid
for specific processes other than pid
remains a
valid thing to do.
为除pid
之外的特定进程调用waitpid
仍然是有效的做法。
guint g_child_watch_add (GPid pid
,GChildWatchFunc function
,gpointer data
);
Sets a function to be called when the child indicated by pid
exits, at a default priority, G_PRIORITY_DEFAULT
.
设置一个函数,当pid
指示的子进程退出时,以默认优先级G_PRIORITY_DEFAULT
调用。
If you obtain pid
from g_spawn_async()
or g_spawn_async_with_pipes()
you will need to pass G_SPAWN_DO_NOT_REAP_CHILD
as flag to
the spawn function for the child watching to work.
如果您从g_spawn_async()
或g_spawn_async_with_pipes()
获取pid
,则需要将G_SPAWN_DO_NOT_REAP_CHILD
作为标志传递给 spawn 函数,以便子进程监视工作。
Note that on platforms where GPid must be explicitly 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.
请注意,在必须显式关闭GPid的平台上(请参阅g_spawn_close_pid()
),当源仍处于活动状态时,不得关闭pid
。通常,您需要在源的回调函数中调用g_spawn_close_pid()
。
GLib supports only a single callback per process id.
On POSIX platforms, the same restrictions mentioned for
g_child_watch_source_new()
apply to this function.
GLib 仅支持每个进程 ID 一个回调。在 POSIX 平台上,针对g_child_watch_source_new()
提到的相同限制也适用于该函数。
This internally creates a main loop source using
g_child_watch_source_new()
and attaches it to the main loop context
using g_source_attach()
. You can do these steps manually if you
need greater control.
这在内部使用g_child_watch_source_new()
创建一个主循环源,并使用g_source_attach()
将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。
guint g_child_watch_add_full (gint priority
,GPid pid
,GChildWatchFunc function
,gpointer data
,GDestroyNotify notify
);
Sets a function to be called when the child indicated by pid
exits, at the priority priority
.
设置当pid
指示的子进程退出时要调用的函数, priority
为priority。
If you obtain pid
from g_spawn_async()
or g_spawn_async_with_pipes()
you will need to pass G_SPAWN_DO_NOT_REAP_CHILD
as flag to
the spawn function for the child watching to work.
如果您从g_spawn_async()
或g_spawn_async_with_pipes()
获取pid
,则需要将G_SPAWN_DO_NOT_REAP_CHILD
作为标志传递给 spawn 函数,以便子进程监视工作。
In many programs, you will want to call g_spawn_check_wait_status()
in the callback to determine whether or not the child exited
successfully.
在许多程序中,您需要在回调中调用g_spawn_check_wait_status()
来确定子进程是否成功退出。
Also, note that on platforms where GPid must be explicitly closed
(see g_spawn_close_pid()
) pid
must not be closed while the source
is still active. Typically, you should invoke g_spawn_close_pid()
in the callback function for the source.
另请注意,在必须显式关闭GPid 的平台上(请参阅g_spawn_close_pid()
),当源仍处于活动状态时,不得关闭pid
。通常,您应该在源的回调函数中调用g_spawn_close_pid()
。
GLib supports only a single callback per process id.
On POSIX platforms, the same restrictions mentioned for
g_child_watch_source_new()
apply to this function.
GLib 仅支持每个进程 ID 一个回调。在 POSIX 平台上,针对g_child_watch_source_new()
提到的相同限制也适用于该函数。
This internally creates a main loop source using
g_child_watch_source_new()
and attaches it to the main loop context
using g_source_attach()
. You can do these steps manually if you
need greater control.
这在内部使用g_child_watch_source_new()
创建一个主循环源,并使用g_source_attach()
将其附加到主循环上下文。如果您需要更好的控制,您可以手动执行这些步骤。
[rename-to g_child_watch_add]
[重命名为g_child_watch_add]
priority 优先事项 |
the priority of the idle source. Typically this will be in the
range between |
|
pid |
process to watch. On POSIX the positive pid of a child process. On
Windows a handle for a process (which doesn't have to be a child). |
|
function 功能 |
function to call 要调用的函数 |
|
data 数据 |
data to pass to |
|
notify 通知 |
function to call when the idle is removed, or |
[nullable] [可为空] |
gint g_poll (GPollFD *fds
,guint nfds
,gint timeout
);
Polls fds
, as with the poll()
system call, but portably. (On
systems that don't have poll()
, it is emulated using select()
.)
This is used internally by GMainContext, but it can be called
directly if you need to block until a file descriptor is ready, but
don't want to run the full main loop.
轮询fds
,与poll()
系统调用一样,但可移植。 (在没有poll()
系统上,它是使用select()
模拟的。)这是由GMainContext内部使用的,但如果您需要阻塞直到文件描述符准备好,则可以直接调用它,但不要想要运行完整的主循环。
Each element of fds
is a GPollFD describing a single file
descriptor to poll. The fd
field indicates the file descriptor,
and the events
field indicates the events to poll for. On return,
the revents
fields will be filled with the events that actually
occurred.fds
的每个元素都是一个GPollFD,描述要轮询的单个文件描述符。 fd
字段指示文件描述符, events
字段指示要轮询的事件。返回时, revents
字段将填充实际发生的事件。
On POSIX systems, the file descriptors in fds
can be any sort of
file descriptor, but the situation is much more complicated on
Windows. If you need to use g_poll()
in code that has to run on
Windows, the easiest solution is to construct all of your
GPollFDs with g_io_channel_win32_make_pollfd()
.
在 POSIX 系统上, fds
中的文件描述符可以是任何类型的文件描述符,但在 Windows 上情况要复杂得多。如果您需要在必须在 Windows 上运行的代码中使用g_poll()
,最简单的解决方案是使用以下命令构建所有GPollFD : g_io_channel_win32_make_pollfd()
。
fds |
file descriptors to poll 要轮询的文件描述符 |
|
nfds 国家食品药品监督管理局 |
the number of file descriptors in |
|
timeout 暂停 |
amount of time to wait, in milliseconds, or -1 to wait forever |
void
(*GSourceDummyMarshal) (void
);
This is just a placeholder for GClosureMarshal,
which cannot be used here for dependency reasons.
这只是GCloseMarshal的占位符,由于依赖性原因不能在此处使用。
void
(*GSourceDisposeFunc) (GSource *source
);
Dispose function for source
. See g_source_set_dispose_function()
for
details.source
的处置函数。看 g_source_set_dispose_function()
了解详情。
GSource * g_source_new (GSourceFuncs *source_funcs
,guint struct_size
);
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)
.
创建一个新的GSource结构。指定大小是为了允许创建从包含附加数据的GSource派生的结构。传入的大小必须至少为sizeof (GSource)
。
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.
该源最初不会与任何GMainContext关联,并且必须在执行之前使用g_source_attach()
添加到 GMainContext 中。
GSource *
g_source_ref (GSource *source
);
Increases the reference count on a source by one.
将源的引用计数增加一。
void
g_source_unref (GSource *source
);
Decreases the reference count of a source by one. If the
resulting reference count is zero the source and associated
memory will be destroyed.
将源的引用计数减一。如果生成的引用计数为零,则源和关联的内存将被破坏。
void g_source_set_funcs (GSource *source
,GSourceFuncs *funcs
);
Sets the source functions (can be used to override
default implementations) of an unattached source.
设置未附加源的源函数(可用于覆盖默认实现)。
void g_source_set_dispose_function (GSource *source
,GSourceDisposeFunc dispose
);
Set dispose
as dispose function on source
. dispose
will be called once
the reference count of source
reaches 0 but before any of the state of the
source is freed, especially before the finalize function is called.
将dispose
设置为source
上的 dispose 函数。一旦source
的引用计数达到0,但在释放source的任何状态之前,特别是在调用finalize函数之前,将调用dispose
。
This means that at this point source
is still a valid GSource and it is
allow for the reference count to increase again until dispose
returns.
这意味着此时source
仍然是有效的GSource ,并且允许引用计数再次增加,直到dispose
返回。
The dispose function can be used to clear any "weak" references to the
source
in other data structures in a thread-safe way where it is possible
for another thread to increase the reference count of source
again while
it is being freed.
dispose 函数可用于以线程安全的方式清除其他数据结构中对source
的任何“弱”引用,其中另一个线程可以在释放源时再次增加source
的引用计数。
The finalize function can not be used for this purpose as at that point
source
is already partially freed and not valid anymore.
Finalize 函数不能用于此目的,因为此时source
已经部分释放并且不再有效。
This should only ever be called from GSource implementations.
这只应该从GSource实现中调用。
source 来源 |
A GSource to set the dispose function on |
|
dispose 处置 |
GSourceDisposeFunc to set on the source |
guint g_source_attach (GSource *source
,GMainContext *context
);
Adds a GSource to a context
so that it will be executed within
that context. Remove it by calling g_source_destroy()
.
将GSource添加到context
以便它将在该上下文中执行。通过调用g_source_destroy()
将其删除。
This function is safe to call from any thread, regardless of which thread
the context
is running in.
无论context
在哪个线程中运行,都可以从任何线程安全地调用此函数。
source 来源 |
||
context 语境 |
a GMainContext (if |
[nullable] [可为空] |
the ID (greater than 0) for the source within the
GMainContext.
GMainContext中源的 ID(大于 0)。
void
g_source_destroy (GSource *source
);
Removes a source from its GMainContext, if any, and mark it as
destroyed. The source cannot be subsequently added to another
context. It is safe to call this on sources which have already been
removed from their context.
从其GMainContext中删除源(如果有),并将其标记为已销毁。随后无法将源添加到另一个上下文中。在已经从上下文中删除的源上调用此方法是安全的。
This does not unref the GSource: if you still hold a reference, use
g_source_unref()
to drop it.
这不会取消引用GSource :如果您仍然持有引用,请使用g_source_unref()
删除它。
This function is safe to call from any thread, regardless of which thread
the GMainContext is running in.
无论GMainContext在哪个线程中运行,都可以从任何线程安全地调用此函数。
If the source is currently attached to a GMainContext, destroying it
will effectively unset the callback similar to calling g_source_set_callback()
.
This can mean, that the data's GDestroyNotify gets called right away.
如果源当前附加到GMainContext ,则销毁它将有效地取消设置回调,类似于调用g_source_set_callback()
。这可能意味着数据的GDestroyNotify会立即被调用。
gboolean
g_source_is_destroyed (GSource *source
);
Returns whether source
has been destroyed.
返回source
是否已被销毁。
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.
当您从空闲处理程序中操作对象时,这一点很重要,但可能在调度空闲处理程序之前释放了对象。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
static gboolean idle_callback (gpointer data) { SomeWidget *self = data; g_mutex_lock (&self->idle_id_mutex); // do stuff with self self->idle_id = 0; g_mutex_unlock (&self->idle_id_mutex); return G_SOURCE_REMOVE; } static void some_widget_do_stuff_later (SomeWidget *self) { g_mutex_lock (&self->idle_id_mutex); self->idle_id = g_idle_add (idle_callback, self); g_mutex_unlock (&self->idle_id_mutex); } static void some_widget_init (SomeWidget *self) { g_mutex_init (&self->idle_id_mutex); // ... } static void some_widget_finalize (GObject *object) { SomeWidget *self = SOME_WIDGET (object); if (self->idle_id) g_source_remove (self->idle_id); g_mutex_clear (&self->idle_id_mutex); 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.
如果在空闲处理程序触发之前小部件由于在回调中使用 after free 而被销毁,则在多线程应用程序中这将失败。针对这个特定问题的解决方案是检查源是否已在回调中被销毁。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
static gboolean idle_callback (gpointer data) { SomeWidget *self = data; g_mutex_lock (&self->idle_id_mutex); if (!g_source_is_destroyed (g_main_current_source ())) { // do stuff with self } g_mutex_unlock (&self->idle_id_mutex); return FALSE; } |
Calls to this function from a thread other than the one acquired by the
GMainContext the GSource is attached to are typically redundant, as the
source could be destroyed immediately after this function returns. However,
once a source is destroyed it cannot be un-destroyed, so this function can be
used for opportunistic checks from any thread.
从附加GSource的GMainContext获取的线程以外的线程调用此函数通常是多余的,因为此函数返回后源可能会立即被销毁。然而,一旦源被销毁,它就无法恢复,因此该函数可用于任何线程的机会性检查。
void g_source_set_priority (GSource *source
,gint 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.
设置源的优先级。当主循环运行时,如果源已准备好被调度,并且没有更高(数字较小)优先级的源准备好被调度,则该源将被调度。
A child source always has the same priority as its parent. It is not
permitted to change the priority of a source once it has been added
as a child of another source.
子源始终与其父源具有相同的优先级。一旦将某个源添加为另一个源的子级,就不允许更改该源的优先级。
gint
g_source_get_priority (GSource *source
);
Gets the priority of a source.
获取源的优先级。
void g_source_set_can_recurse (GSource *source
,gboolean can_recurse
);
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.
设置是否可以递归调用源。如果can_recurse
为TRUE
,则在调度源时,该源将被正常处理。否则,该源的所有处理都会被阻止,直到调度函数返回。
gboolean
g_source_get_can_recurse (GSource *source
);
Checks whether a source is allowed to be called recursively.
see g_source_set_can_recurse()
.
检查是否允许递归调用源。请参阅g_source_set_can_recurse()
。
guint
g_source_get_id (GSource *source
);
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()
.
返回特定源的数字 ID。源的 ID 是一个正整数,在特定的主循环上下文中是唯一的。从 ID 到源的反向映射是通过以下方式完成的 g_main_context_find_source_by_id()
。
You can only call this function while the source is associated to a
GMainContext instance; calling this function before g_source_attach()
or after g_source_destroy()
yields undefined behavior. The ID returned
is unique within the GMainContext instance passed to g_source_attach()
.
仅当源与GMainContext实例关联时才能调用此函数;在g_source_attach()
之前或g_source_destroy()
之后调用此函数会产生未定义的行为。返回的 ID 在传递给g_source_attach()
GMainContext实例中是唯一的。
const char *
g_source_get_name (GSource *source
);
Gets a name for the source, used in debugging and profiling. The
name may be NULL if it has never been set with g_source_set_name()
.
获取源的名称,用于调试和分析。如果从未使用g_source_set_name()
设置该名称,则该名称可能为NULL 。
void g_source_set_name (GSource *source
,const char *name
);
Sets a name for the source, used in debugging and profiling.
The name defaults to NULL.
设置源的名称,用于调试和分析。该名称默认为NULL 。
The source name should describe in a human-readable way
what the source does. For example, "X11 event queue"
or "GTK+ repaint idle handler" or whatever it is.
源名称应该以人类可读的方式描述源的用途。例如,“X11 事件队列”或“GTK+ 重绘空闲处理程序”或其他任何内容。
It is permitted to call this function multiple times, but is not
recommended due to the potential performance impact. For example,
one could change the name in the "check" function of a GSourceFuncs
to include details like the event type in the source name.
允许多次调用此函数,但由于潜在的性能影响,不建议这样做。例如,可以更改GSourceFuncs的“检查”函数中的名称,以包含源名称中的事件类型等详细信息。
Use caution if changing the name while another thread may be
accessing it with g_source_get_name()
; that function does not copy
the value, and changing the value will free it while the other thread
may be attempting to use it.
如果在另一个线程可能正在使用g_source_get_name()
访问该名称时更改名称,请务必小心;该函数不会复制该值,并且更改该值将释放该值,而其他线程可能会尝试使用该值。
Also see g_source_set_static_name()
.
另请参阅g_source_set_static_name()
。
void g_source_set_static_name (GSource *source
,const char *name
);
A variant of g_source_set_name()
that does not
duplicate the name
, and can only be used with
string literals.g_source_set_name()
的变体,不重复name
,并且只能与字符串文字一起使用。
void g_source_set_name_by_id (guint tag
,const char *name
);
Sets the name of a source using its ID.
使用源 ID 设置源的名称。
This is a convenience utility to set source names from the return
value of g_idle_add()
, g_timeout_add()
, etc.
这是一个方便的实用程序,用于根据g_idle_add()
、 g_timeout_add()
等的返回值设置源名称。
It is a programmer error to attempt to set the name of 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.
更具体地说:源 ID 可以在源被销毁后重新发布,因此使用此函数与可能已被删除的源 ID 永远是无效的。一个示例是,当使用g_idle_add()
调度空闲在另一个线程中运行时:在其(现在无效)源 ID 上调用此函数时,空闲可能已经运行并被删除。该源 ID 可能已重新发布,从而导致针对错误的源执行操作。
GMainContext *
g_source_get_context (GSource *source
);
Gets the GMainContext with which the source is associated.
获取与源关联的GMainContext 。
You can call this on a source that has been destroyed, provided
that the GMainContext it was attached to still exists (in which
case it will return that GMainContext). In particular, you can
always call this function on the source returned from
g_main_current_source()
. But calling this function on a source
whose GMainContext has been destroyed is an error.
您可以在已销毁的源上调用此方法,前提是它所附加的GMainContext仍然存在(在这种情况下它将返回该GMainContext )。特别是,您始终可以在g_main_current_source()
返回的源上调用此函数。但是在GMainContext已被破坏的源上调用此函数会出错。
the GMainContext with which the
source is associated, or NULL
if the context has not
yet been added to a source.
与源关联的GMainContext ,如果上下文尚未添加到源,则为NULL
。
[transfer none][nullable]
[不传输][可为空]
void g_source_set_callback (GSource *source
,GSourceFunc func
,gpointer data
,GDestroyNotify notify
);
Sets the callback function for a source. The callback for a source is
called from the source's dispatch function.
设置源的回调函数。源的回调是从源的调度函数调用的。
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. Cast func
with G_SOURCE_FUNC()
to avoid warnings about
incompatible function types.func
的确切类型取决于源的类型; IE。您不应该指望以data
作为第一个参数来调用func
。使用G_SOURCE_FUNC()
强制转换func
以避免出现有关不兼容函数类型的警告。
See memory management of sources for details
on how to handle memory management of data
.
有关如何处理data
内存管理的详细信息,请参阅源内存管理。
Typically, you won't use this function. Instead use functions specific
to the type of source you are using, such as g_idle_add()
or g_timeout_add()
.
通常,您不会使用此功能。相反,请使用特定于您所使用的源类型的函数,例如g_idle_add()
或g_timeout_add()
。
It is safe to call this function multiple times on a source which has already
been attached to a context. The changes will take effect for the next time
the source is dispatched after this call returns.
在已附加到上下文的源上多次调用此函数是安全的。更改将在此调用返回后下次分派源时生效。
Note that g_source_destroy()
for a currently attached source has the effect
of also unsetting the callback.
请注意,当前附加源的g_source_destroy()
还具有取消设置回调的效果。
gboolean
(*GSourceFunc) (gpointer user_data
);
Specifies the type of function passed to g_timeout_add()
,
g_timeout_add_full()
, g_idle_add()
, and g_idle_add_full()
.
指定传递给g_timeout_add()
、 g_timeout_add_full()
、 g_idle_add()
和g_idle_add_full()
的函数类型。
When calling g_source_set_callback()
, you may need to cast a function of a
different type to this type. Use G_SOURCE_FUNC()
to avoid warnings about
incompatible function types.
当调用g_source_set_callback()
时,您可能需要将不同类型的函数转换为该类型。使用G_SOURCE_FUNC()
以避免有关不兼容函数类型的警告。
user_data 用户数据 |
data passed to the function, set when the source was
created with one of the above functions |
FALSE
if the source should be removed. G_SOURCE_CONTINUE
and
G_SOURCE_REMOVE
are more memorable names for the return value.
如果应删除源, FALSE
。 G_SOURCE_CONTINUE
和G_SOURCE_REMOVE
是返回值的更容易记住的名称。
#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) GLIB_AVAILABLE_MACRO_IN_2_58
Cast a function pointer to a GSourceFunc, suppressing warnings from GCC 8
onwards with -Wextra
or -Wcast-function-type
enabled about the function
types being incompatible.
将函数指针转换为GSourceFunc ,从 GCC 8 开始,通过启用-Wextra
或-Wcast-function-type
来抑制有关函数类型不兼容的警告。
For example, the correct type of callback for a source created by
g_child_watch_source_new()
is GChildWatchFunc, which accepts more arguments
than GSourceFunc. Casting the function with (GSourceFunc)
to call
g_source_set_callback()
will trigger a warning, even though it will be cast
back to the correct type before it is called by the source.
例如,由g_child_watch_source_new()
创建的源的正确回调类型是GChildWatchFunc ,它比GSourceFunc接受更多的参数。使用(GSourceFunc)
转换函数来调用g_source_set_callback()
将触发警告,即使它在被源调用之前会被转换回正确的类型。
void g_source_set_callback_indirect (GSource *source
,gpointer callback_data
,GSourceCallbackFuncs *callback_funcs
);
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
.
设置将数据存储为引用计数回调“对象”的回调函数。这是内部使用的。请注意,调用 g_source_set_callback_indirect()
假设callback_data
上有一个初始引用计数,因此callback_funcs->unref
最终将比callback_funcs->ref
被调用一次。
It is safe to call this function multiple times on a source which has already
been attached to a context. The changes will take effect for the next time
the source is dispatched after this call returns.
在已附加到上下文的源上多次调用此函数是安全的。更改将在此调用返回后下次分派源时生效。
void g_source_set_ready_time (GSource *source
,gint64 ready_time
);
Sets a GSource to be dispatched when the given monotonic time is
reached (or passed). If the monotonic time is in the past (as it
always will be if ready_time
is 0) then the source will be
dispatched immediately.
设置当达到(或超过)给定的单调时间时要调度的GSource 。如果单调时间已经过去(如果ready_time
为0,则总是如此),则将立即分派源。
If ready_time
is -1 then the source is never woken up on the basis
of the passage of time.
如果ready_time
为-1,则源永远不会根据时间的流逝而被唤醒。
Dispatching the source does not reset the ready time. You should do
so yourself, from the source dispatch function.
调度源不会重置就绪时间。您应该从源调度函数中自己执行此操作。
Note that if you have a pair of sources where the ready time of one
suggests that it will be delivered first but the priority for the
other suggests that it would be delivered first, and the ready time
for both sources is reached during the same main context iteration,
then the order of dispatch is undefined.
请注意,如果您有一对源,其中一个源的就绪时间表明它将首先交付,但另一个源的优先级表明它将首先交付,并且两个源的就绪时间都是在同一主上下文期间达到的迭代,则调度顺序未定义。
It is a no-op to call this function on a GSource which has already been
destroyed with g_source_destroy()
.
在已使用g_source_destroy()
销毁的GSource上调用此函数是无操作的。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
gint64
g_source_get_ready_time (GSource *source
);
Gets the "ready time" of source
, as set by
g_source_set_ready_time()
.
获取source
的“就绪时间”,由g_source_set_ready_time()
设置。
Any time before the current monotonic time (including 0) is an
indication that the source will fire immediately.
当前单调时间之前的任何时间(包括 0)都表示源将立即触发。
gpointer g_source_add_unix_fd (GSource *source
,gint fd
,GIOCondition events
);
Monitors fd
for the IO events in events
.
监视events
中的 IO 事件的fd
。
The tag returned by this function can be used to remove or modify the
monitoring of the fd using g_source_remove_unix_fd()
or
g_source_modify_unix_fd()
.
该函数返回的标签可用于使用g_source_remove_unix_fd()
或g_source_modify_unix_fd()
删除或修改 fd 的监控。
It is not necessary to remove the fd before destroying the source; it
will be cleaned up automatically.
销毁源之前不需要移除fd;它将被自动清理。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。
void g_source_remove_unix_fd (GSource *source
,gpointer tag
);
Reverses the effect of a previous call to g_source_add_unix_fd()
.
反转先前调用g_source_add_unix_fd()
的效果。
You only need to call this if you want to remove an fd from being
watched while keeping the same source around. In the normal case you
will just want to destroy the source.
仅当您想删除某个 fd 并保持相同的源时,才需要调用此函数。在正常情况下,您只想销毁源。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。
source 来源 |
||
tag 标签 |
the tag from |
[not nullable] [不可为空] |
void g_source_modify_unix_fd (GSource *source
,gpointer tag
,GIOCondition new_events
);
Updates the event mask to watch for the fd identified by tag
.
更新事件掩码以监视由tag
标识的 fd 。
tag
is the tag returned from g_source_add_unix_fd()
.tag
是从g_source_add_unix_fd()
返回的标签。
If you want to remove a fd, don't set its event mask to zero.
Instead, call g_source_remove_unix_fd()
.
如果要删除 fd,请勿将其事件掩码设置为零。相反,请调用g_source_remove_unix_fd()
。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。
source 来源 |
||
tag 标签 |
the tag from |
[not nullable] [不可为空] |
new_events 新事件 |
the new event mask to watch |
GIOCondition g_source_query_unix_fd (GSource *source
,gpointer tag
);
Queries the events reported for the fd corresponding to tag
on
source
during the last poll.
查询上次轮询期间source
上tag
对应的 fd 报告的事件。
The return value of this function is only defined when the function
is called from the check or dispatch functions for source
.
仅当从source
检查或调度函数调用该函数时,才定义该函数的返回值。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
As the name suggests, this function is not available on Windows.
顾名思义,该功能在 Windows 上不可用。
source 来源 |
||
tag 标签 |
the tag from |
[not nullable] [不可为空] |
void g_source_add_poll (GSource *source
,GPollFD *fd
);
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.
将文件描述符添加到为此源轮询的文件描述符集中。这通常与g_source_new()
结合使用来添加事件源。事件源的检查函数通常会测试GPollFD结构中的revents
字段,如果需要处理事件则返回TRUE
。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
Using this API forces the linear scanning of event sources on each
main loop iteration. Newly-written event sources should try to use
g_source_add_unix_fd()
instead of this API.
使用此 API 会强制在每次主循环迭代上对事件源进行线性扫描。新编写的事件源应尝试使用g_source_add_unix_fd()
而不是此 API。
void g_source_remove_poll (GSource *source
,GPollFD *fd
);
Removes a file descriptor from the set of file descriptors polled for
this source.
从为此源轮询的文件描述符集中删除文件描述符。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
source 来源 |
||
fd |
a GPollFD structure previously passed to |
void g_source_add_child_source (GSource *source
,GSource *child_source
);
Adds child_source
to source
as a "polled" source; when source
is
added to a GMainContext, child_source
will be automatically added
with the same priority, when child_source
is triggered, it will
cause source
to dispatch (in addition to calling its own
callback), and when source
is destroyed, it will destroy
child_source
as well. (source
will also still be dispatched if
its own prepare/check functions indicate that it is ready.)
将child_source
添加到source
作为“轮询”源;当source
添加到GMainContext时, child_source
会自动以相同的优先级添加,当child_source
被触发时,它会导致source
调度(除了调用它自己的回调),当source
被销毁时,它会销毁child_source
出色地。 (如果源自己的准备/检查功能表明它已准备好,则仍将分派source
。)
If you don't need child_source
to do anything on its own when it
triggers, you can call g_source_set_dummy_callback()
on it to set a
callback that does nothing (except return TRUE
if appropriate).
如果您不需要child_source
在触发时自行执行任何操作,则可以对其调用g_source_set_dummy_callback()
以设置不执行任何操作的回调(如果适用,则返回TRUE
除外)。
source
will hold a reference on child_source
while child_source
is attached to it.
当child_source
附加到它时, source
将保留对child_source
引用。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
void g_source_remove_child_source (GSource *source
,GSource *child_source
);
Detaches child_source
from source
and destroys it.
从source
中分离child_source
并销毁它。
This API is only intended to be used by implementations of GSource.
Do not call this API on a GSource that you did not create.
此 API 仅供GSource的实现使用。请勿在非您创建的GSource上调用此 API。
source 来源 |
||
child_source 子源 |
a GSource previously passed to
|
gint64
g_source_get_time (GSource *source
);
Gets the time to be used when checking this source. The advantage of
calling this function over calling g_get_monotonic_time()
directly is
that when checking multiple sources, GLib can cache a single value
instead of having to repeatedly get the system monotonic time.
获取检查此源时要使用的时间。与直接调用g_get_monotonic_time()
相比,调用此函数的优点是,在检查多个源时,GLib 可以缓存单个值,而不必重复获取系统单调时间。
The time here is the system monotonic time, if available, or some
other reasonable alternative otherwise. See g_get_monotonic_time()
.
这里的时间是系统单调时间(如果可用),或者其他合理的替代时间。请参阅g_get_monotonic_time()
。
void g_source_get_current_time (GSource *source
,GTimeVal *timeval
);
g_source_get_current_time
has been deprecated since version 2.28 and should not be used in newly-written code.g_source_get_current_time
自版本 2.28 起已被弃用,不应在新编写的代码中使用。
use g_source_get_time()
instead
使用g_source_get_time()
代替
This function ignores source
and is otherwise the same as
g_get_current_time()
.
该函数忽略source
,其他方面与g_get_current_time()
相同。
gboolean
g_source_remove (guint tag
);
Removes the source with the given ID from the default main context. You must
use g_source_destroy()
for sources added to a non-default main context.
从默认主上下文中删除具有给定 ID 的源。对于添加到非默认主上下文的源,必须使用g_source_destroy()
。
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()
.
GSource的 ID 由g_source_get_id()
给出,或者由函数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()
返回和g_io_add_watch_full()
。
It is a programmer error to attempt to remove 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.
更具体地说:源 ID 可以在源被销毁后重新发布,因此使用此函数与可能已被删除的源 ID 永远是无效的。一个示例是,当使用g_idle_add()
调度空闲在另一个线程中运行时:在其(现在无效)源 ID 上调用此函数时,空闲可能已经运行并被删除。该源 ID 可能已重新发布,从而导致针对错误的源执行操作。
gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs
,gpointer user_data
);
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.
给定源函数和用户数据,从默认主循环上下文中删除源。如果存在多个具有相同源功能和用户数据的源,则仅销毁一个。
funcs 函数 |
The |
|
user_data 用户数据 |
the user data for the callback |
gboolean
g_source_remove_by_user_data (gpointer user_data
);
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.
给定回调的用户数据,从默认主循环上下文中删除源。如果存在多个具有相同用户数据的源,则只有一个将被销毁。
void
(*GClearHandleFunc) (guint handle_id
);
Specifies the type of function passed to g_clear_handle_id()
.
The implementation is expected to free the resource identified
by handle_id
; for instance, if handle_id
is a GSource ID,
g_source_remove()
can be used.
指定传递给g_clear_handle_id()
的函数类型。该实现预计会释放由handle_id
标识的资源;例如,如果handle_id
是GSource ID,则可以使用g_source_remove()
。
void g_clear_handle_id (guint *tag_ptr
,GClearHandleFunc clear_func
);
Clears a numeric handler, such as a GSource ID.
清除数字处理程序,例如GSource ID。
tag_ptr
must be a valid pointer to the variable holding the handler.tag_ptr
必须是指向保存处理程序的变量的有效指针。
If the ID is zero then this function does nothing.
Otherwise, clear_func()
is called with the ID as a parameter, and the tag is
set to zero.
如果 ID 为零,则此函数不执行任何操作。否则,将使用 ID 作为参数调用clear_func()
,并将标记设置为零。
A macro is also included that allows this function to be used without
pointer casts.
还包含一个宏,允许在不进行指针转换的情况下使用此函数。
[skip]
[跳过]
int
g_steal_fd (int *fd_ptr
);
Sets fd_ptr
to -1
, returning the value that was there before.
将fd_ptr
设置为-1
,返回之前的值。
Conceptually, this transfers the ownership of the file descriptor
from the referenced variable to the caller of the function (i.e.
‘steals’ the reference). This is very similar to g_steal_pointer()
,
but for file descriptors.
从概念上讲,这将文件描述符的所有权从引用的变量转移到函数的调用者(即“窃取”引用)。这与g_steal_pointer()
非常相似,但针对的是文件描述符。
On POSIX platforms, this function is async-signal safe
(see signal(7)
) and
signal-safety(7)
)), making it safe to call from a
signal handler or a GSpawnChildSetupFunc.
在 POSIX 平台上,此函数是异步信号安全的(请参阅signal(7)
)和signal-safety(7)
)),从而可以安全地从信号处理程序或GSpawnChildSetupFunc进行调用。
typedef struct _GMainLoop GMainLoop;
The GMainLoop
struct is an opaque data type
representing the main event loop of a GLib or GTK+ application.GMainLoop
结构是一种不透明的数据类型,表示 GLib 或 GTK+ 应用程序的主事件循环。
#define G_PRIORITY_HIGH -100
Use this for high priority event sources.
将此用于高优先级事件源。
It is not used within GLib or GTK+.
它不在 GLib 或 GTK+ 中使用。
#define G_PRIORITY_DEFAULT 0
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.
在 GLib 中,当使用g_timeout_add()
添加超时函数时使用此优先级。在 GDK 中,此优先级用于来自 X 服务器的事件。
#define G_PRIORITY_HIGH_IDLE 100
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.)
GTK+ 使用G_PRIORITY_HIGH_IDLE
+ 10 进行大小调整操作,使用G_PRIORITY_HIGH_IDLE
+ 20 进行重绘操作。 (这样做是为了确保在任何挂起的重绘之前处理任何挂起的大小调整,以便部件不会不必要地重绘两次。)
#define G_PRIORITY_DEFAULT_IDLE 200
Use this for default priority idle functions.
将此用于默认优先级空闲功能。
In GLib this priority is used when adding idle functions with
g_idle_add()
.
在 GLib 中,当使用g_idle_add()
添加空闲函数时使用此优先级。
#define G_PRIORITY_LOW 300
Use this for very low priority background tasks.
将此用于非常低优先级的后台任务。
It is not used within GLib or GTK+.
它不在 GLib 或 GTK+ 中使用。
#define G_SOURCE_CONTINUE TRUE
Use this macro as the return value of a GSourceFunc to leave
the GSource in the main loop.
使用此宏作为GSourceFunc的返回值,将GSource保留在主循环中。
#define G_SOURCE_REMOVE FALSE
Use this macro as the return value of a GSourceFunc to remove
the GSource from the main loop.
使用此宏作为GSourceFunc的返回值以从主循环中删除GSource 。
typedef struct _GMainContext GMainContext;
The GMainContext
struct is an opaque data
type representing a set of sources to be handled in a main loop.GMainContext
结构是一种不透明的数据类型,表示要在主循环中处理的一组源。
Flags to pass to g_main_context_new_with_flags()
which affect the behaviour
of a GMainContext.
要传递到的标志 g_main_context_new_with_flags()
这会影响GMainContext的行为。
Default behaviour. 默认行为。 |
||
Assume that polling for events will
free the thread to process other jobs. That's useful if you're using
|
typedef void GMainContextPusher GLIB_AVAILABLE_TYPE_IN_2_64;
Opaque type. See g_main_context_pusher_new()
for details.
Since: 2.64
typedef int GPid;
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).
在 UNIX 上,进程由进程 ID(整数)标识,而 Windows 使用进程句柄(指针)。
GPid is used in GLib only for descendant processes spawned with
the g_spawn functions.
GPid 在 GLib 中仅用于使用 g_spawn 函数生成的后代进程。
#define G_PID_FORMAT "i"
A format specifier that can be used in printf()
-style format strings
when printing a GPid.
打印GPid时可在printf()
样式格式字符串中使用的格式说明符。
struct GPollFD { #if defined (G_OS_WIN32) && GLIB_SIZEOF_VOID_P == 8 #endif #else gint fd; #endif gushort events; gushort revents; };
Represents a file descriptor, which events to poll for, and which events
occurred.
表示文件描述符、要轮询哪些事件以及发生了哪些事件。
the file descriptor to poll (or a HANDLE on Win32) |
||
a bitwise combination from GIOCondition, specifying which
events should be polled for. Typically for reading from a file
descriptor you would use |
||
a bitwise combination of flags from GIOCondition, returned
from the |
#define G_POLLFD_FORMAT "%d"
A format specifier that can be used in printf()
-style format strings
when printing the fd
member of a GPollFD.
打印GPollFD的fd
成员时可在printf()
样式格式字符串中使用的格式说明符。
struct GSource { };
The GSource
struct is an opaque data type
representing an event source.GSource
结构是表示事件源的不透明数据类型。
struct GSourceFuncs { gboolean (*prepare) (GSource *source, gint *timeout_);/* Can be NULL */ gboolean (*check) (GSource *source);/* Can be NULL */ gboolean (*dispatch) (GSource *source, GSourceFunc callback, gpointer user_data); void (*finalize) (GSource *source); /* Can be NULL */ };
The GSourceFuncs
struct contains a table of
functions used to handle event sources in a generic manner.GSourceFuncs
结构包含一个用于以通用方式处理事件源的函数表。
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).
对于空闲源,准备和检查函数始终返回TRUE
以指示源始终准备好进行处理。准备函数还返回超时值 0,以确保poll()
调用不会阻塞(因为这会浪费时间,而这些时间本来可以花在运行空闲函数上)。
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.
对于超时源,如果超时间隔已过期,则准备和检查函数均返回TRUE
。准备函数还返回一个超时值,以确保poll()
调用不会阻塞太久而错过下一次超时。
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.
对于文件描述符源,prepare 函数通常返回FALSE
,因为它必须等到调用poll()
后才能知道是否需要处理任何事件。它将返回的超时设置为 -1,表示它不介意poll()
调用阻塞多长时间。在 check 函数中,它测试poll()
调用的结果以查看是否满足所需条件,如果满足则返回TRUE
。
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 |
||
Called after all the file descriptors are polled. The source
should return |
||
Called to dispatch the event source, after it has returned
|
||
Called when the source is finalized. At this point, the source
will have been destroyed, had its callback cleared, and have been removed
from its GMainContext, but it will still have its final reference count,
so methods can be called on it from within this function. |
struct GSourceCallbackFuncs { void (*ref) (gpointer cb_data); void (*unref) (gpointer cb_data); void (*get) (gpointer cb_data, GSource *source, GSourceFunc *func, gpointer *data); };
The GSourceCallbackFuncs
struct contains
functions for managing callback objects.GSourceCallbackFuncs
结构包含用于管理回调对象的函数。