1/* gmain.h - the GLib Main loop
2 * Copyright (C) 1998-2000 Red Hat, Inc.
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef __G_MAIN_H__
21#define __G_MAIN_H__
22
23#if !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
24#error "Only <glib.h> can be included directly."
25#endif
26
27#include <glib/gpoll.h>
28#include <glib/gslist.h>
29#include <glib/gthread.h>
30
31G_BEGIN_DECLS
32
33typedef enum /*< flags >*/
34{
35 G_IO_IN GLIB_SYSDEF_POLLIN,
36 G_IO_OUT GLIB_SYSDEF_POLLOUT,
37 G_IO_PRI GLIB_SYSDEF_POLLPRI,
38 G_IO_ERR GLIB_SYSDEF_POLLERR,
39 G_IO_HUP GLIB_SYSDEF_POLLHUP,
40 G_IO_NVAL GLIB_SYSDEF_POLLNVAL
41} GIOCondition;
42
43/**
44 * GMainContextFlags:
45 * @G_MAIN_CONTEXT_FLAGS_NONE: Default behaviour.
46 * @G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING: Assume that polling for events will
47 * free the thread to process other jobs. That's useful if you're using
48 * `g_main_context_{prepare,query,check,dispatch}` to integrate GMainContext in
49 * other event loops.
50 *
51 * Flags to pass to [ctor@GLib.MainContext.new_with_flags] which affect the
52 * behaviour of a [struct@GLib.MainContext].
53 *
54 * Since: 2.72
55 */
56GLIB_AVAILABLE_TYPE_IN_2_72
57typedef enum /*< flags >*/
58{
59 G_MAIN_CONTEXT_FLAGS_NONE = 0,
60 G_MAIN_CONTEXT_FLAGS_OWNERLESS_POLLING = 1
61} GMainContextFlags;
62
63
64/**
65 * GMainContext:
66 *
67 * The `GMainContext` struct is an opaque data
68 * type representing a set of sources to be handled in a main loop.
69 */
70typedef struct _GMainContext GMainContext;
71
72/**
73 * GMainLoop:
74 *
75 * The `GMainLoop` struct is an opaque data type
76 * representing the main event loop of a GLib or GTK application.
77 */
78typedef struct _GMainLoop GMainLoop;
79
80/**
81 * GSource:
82 *
83 * The `GSource` struct is an opaque data type
84 * representing an event source.
85 */
86typedef struct _GSource GSource;
87typedef struct _GSourcePrivate GSourcePrivate;
88
89/**
90 * GSourceCallbackFuncs:
91 * @ref: Called when a reference is added to the callback object
92 * @unref: Called when a reference to the callback object is dropped
93 * @get: Called to extract the callback function and data from the
94 * callback object.
95 *
96 * The `GSourceCallbackFuncs` struct contains
97 * functions for managing callback objects.
98 */
99typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs;
100
101/**
102 * GSourceFuncs:
103 * @prepare: Called before all the file descriptors are polled. If the
104 * source can determine that it is ready here (without waiting for the
105 * results of the poll() call) it should return %TRUE. It can also return
106 * a @timeout_ value which should be the maximum timeout (in milliseconds)
107 * which should be passed to the poll() call. The actual timeout used will
108 * be -1 if all sources returned -1, or it will be the minimum of all
109 * the @timeout_ values returned which were >= 0. Since 2.36 this may
110 * be %NULL, in which case the effect is as if the function always returns
111 * %FALSE with a timeout of -1. If @prepare returns a
112 * timeout and the source also has a ready time set, then the
113 * lower of the two will be used.
114 * @check: Called after all the file descriptors are polled. The source
115 * should return %TRUE if it is ready to be dispatched. Note that some
116 * time may have passed since the previous prepare function was called,
117 * so the source should be checked again here. Since 2.36 this may
118 * be %NULL, in which case the effect is as if the function always returns
119 * %FALSE.
120 * @dispatch: Called to dispatch the event source, after it has returned
121 * %TRUE in either its @prepare or its @check function, or if a ready time
122 * has been reached. The @dispatch function receives a callback function and
123 * user data. The callback function may be %NULL if the source was never
124 * connected to a callback using [method@GLib.Source.set_callback]. The
125 * @dispatch function should call the callback function with @user_data and
126 * whatever additional parameters are needed for this type of event source.
127 * The return value of the @dispatch function should be
128 * [const@GLib.SOURCE_REMOVE] if the source should be removed or
129 * [const@GLib.SOURCE_CONTINUE] to keep it.
130 * @finalize: Called when the source is finalized. At this point, the source
131 * will have been destroyed, had its callback cleared, and have been removed
132 * from its [struct@GLib.MainContext], but it will still have its final
133 * reference count, so methods can be called on it from within this
134 * function.
135 *
136 * The `GSourceFuncs` struct contains a table of
137 * functions used to handle event sources in a generic manner.
138 *
139 * For idle sources, the prepare and check functions always return %TRUE
140 * to indicate that the source is always ready to be processed. The prepare
141 * function also returns a timeout value of 0 to ensure that the poll() call
142 * doesn't block (since that would be time wasted which could have been spent
143 * running the idle function).
144 *
145 * For timeout sources, the prepare and check functions both return %TRUE
146 * if the timeout interval has expired. The prepare function also returns
147 * a timeout value to ensure that the poll() call doesn't block too long
148 * and miss the next timeout.
149 *
150 * For file descriptor sources, the prepare function typically returns %FALSE,
151 * since it must wait until poll() has been called before it knows whether
152 * any events need to be processed. It sets the returned timeout to -1 to
153 * indicate that it doesn't mind how long the poll() call blocks. In the
154 * check function, it tests the results of the poll() call to see if the
155 * required condition has been met, and returns %TRUE if so.
156 */
157typedef struct _GSourceFuncs GSourceFuncs;
158
159/**
160 * GPid:
161 *
162 * A type which is used to hold a process identification.
163 *
164 * On UNIX, processes are identified by a process id (an integer),
165 * while Windows uses process handles (which are pointers).
166 *
167 * GPid is used in GLib only for descendant processes spawned with
168 * the g_spawn functions.
169 */
170/* defined in glibconfig.h */
171
172/**
173 * G_PID_FORMAT:
174 *
175 * A format specifier that can be used in printf()-style format strings
176 * when printing a #GPid.
177 *
178 * Since: 2.50
179 */
180/* defined in glibconfig.h */
181
182/**
183 * GSourceFunc:
184 * @user_data: data passed to the function, set when the source was
185 * created with one of the above functions
186 *
187 * Specifies the type of function passed to [func@GLib.timeout_add],
188 * [func@GLib.timeout_add_full], [func@GLib.idle_add], and
189 * [func@GLib.idle_add_full].
190 *
191 * When calling [method@GLib.Source.set_callback], you may need to cast a
192 * function of a different type to this type. Use [func@GLib.SOURCE_FUNC] to
193 * avoid warnings about incompatible function types.
194 *
195 * Returns: %FALSE if the source should be removed.
196 * [const@GLib.SOURCE_CONTINUE] and [const@GLib.SOURCE_REMOVE] are more
197 * memorable names for the return value.
198 */
199typedef gboolean (*GSourceFunc) (gpointer user_data);
200
201/**
202 * GSourceOnceFunc:
203 * @user_data: data passed to the function, set when the source was
204 * created
205 *
206 * A source function that is only called once before being removed from the main
207 * context automatically.
208 *
209 * See: [func@GLib.idle_add_once], [func@GLib.timeout_add_once]
210 *
211 * Since: 2.74
212 */
213typedef void (* GSourceOnceFunc) (gpointer user_data);
214
215/**
216 * G_SOURCE_FUNC:
217 * @f: a function pointer.
218 *
219 * Cast a function pointer to a [callback@GLib.SourceFunc], suppressing
220 * warnings from GCC 8 onwards with `-Wextra` or `-Wcast-function-type` enabled
221 * about the function types being incompatible.
222 *
223 * For example, the correct type of callback for a source created by
224 * [func@GLib.child_watch_source_new] is #GChildWatchFunc, which accepts more
225 * arguments than [callback@GLib.SourceFunc]. Casting the function with
226 * `(GSourceFunc)` to call [method@GLib.Source.set_callback] will trigger a
227 * warning, even though it will be cast back to the correct type before it is
228 * called by the source.
229 *
230 * Since: 2.58
231 */
232#define G_SOURCE_FUNC(f) ((GSourceFunc) (void (*)(void)) (f)) GLIB_AVAILABLE_MACRO_IN_2_58
233
234/**
235 * GChildWatchFunc:
236 * @pid: the process id of the child process
237 * @wait_status: Status information about the child process, encoded
238 * in a platform-specific manner
239 * @user_data: user data passed to [func@GLib.child_watch_add]
240 *
241 * Prototype of a #GChildWatchSource callback, called when a child
242 * process has exited.
243 *
244 * To interpret @wait_status, see the documentation for
245 * [func@GLib.spawn_check_wait_status]. In particular,
246 * on Unix platforms, note that it is usually not equal
247 * to the integer passed to `exit()` or returned from `main()`.
248 */
249typedef void (*GChildWatchFunc) (GPid pid,
250 gint wait_status,
251 gpointer user_data);
252
253
254/**
255 * GSourceDisposeFunc:
256 * @source: #GSource that is currently being disposed
257 *
258 * Dispose function for @source. See [method@GLib.Source.set_dispose_function]
259 * for details.
260 *
261 * Since: 2.64
262 */
263GLIB_AVAILABLE_TYPE_IN_2_64
264typedef void (*GSourceDisposeFunc) (GSource *source);
265
266struct _GSource
267{
268 /*< private >*/
269 gpointer callback_data;
270 GSourceCallbackFuncs *callback_funcs;
271
272 const GSourceFuncs *source_funcs;
273 guint ref_count;
274
275 GMainContext *context;
276
277 gint priority;
278 guint flags;
279 guint source_id;
280
281 GSList *poll_fds;
282
283 GSource *prev;
284 GSource *next;
285
286 char *name;
287
288 GSourcePrivate *priv;
289};
290
291struct _GSourceCallbackFuncs
292{
293 void (*ref) (gpointer cb_data);
294 void (*unref) (gpointer cb_data);
295 void (*get) (gpointer cb_data,
296 GSource *source,
297 GSourceFunc *func,
298 gpointer *data);
299};
300
301/**
302 * GSourceDummyMarshal:
303 *
304 * This is just a placeholder for #GClosureMarshal,
305 * which cannot be used here for dependency reasons.
306 */
307typedef void (*GSourceDummyMarshal) (void);
308
309/**
310 * GSourceFuncsPrepareFunc:
311 * @source: The #GSource
312 * @timeout_: (out) (optional): the maximum timeout (in milliseconds) which should be passed to the poll call
313 *
314 * Checks the source for readiness.
315 *
316 * Called before all the file descriptors are polled. If the
317 * source can determine that it is ready here (without waiting for the
318 * results of the poll call) it should return %TRUE. It can also return
319 * a @timeout_ value which should be the maximum timeout (in milliseconds)
320 * which should be passed to the poll call. The actual timeout used will
321 * be `-1` if all sources returned `-1`, or it will be the minimum of all
322 * the @timeout_ values returned which were greater than or equal to `0`.
323 * If the prepare function returns a timeout and the source also has a
324 * ready time set, then the lower of the two will be used.
325 *
326 * Since 2.36 this may be `NULL`, in which case the effect is as if the
327 * function always returns `FALSE` with a timeout of `-1`.
328 *
329 * Returns: %TRUE if the source is ready, %FALSE otherwise
330 *
331 * Since: 2.82
332 */
333typedef gboolean (*GSourceFuncsPrepareFunc) (GSource *source,
334 gint *timeout_);
335
336/**
337 * GSourceFuncsCheckFunc:
338 * @source: The #GSource
339 *
340 * Checks if the source is ready to be dispatched.
341 *
342 * Called after all the file descriptors are polled. The source
343 * should return %TRUE if it is ready to be dispatched. Note that some
344 * time may have passed since the previous prepare function was called,
345 * so the source should be checked again here.
346 *
347 * Since 2.36 this may be `NULL`, in which case the effect is
348 * as if the function always returns `FALSE`.
349 *
350 * Returns: %TRUE if ready to be dispatched, %FALSE otherwise
351 *
352 * Since: 2.82
353 */
354typedef gboolean (*GSourceFuncsCheckFunc) (GSource *source);
355
356/**
357 * GSourceFuncsDispatchFunc:
358 * @source: The #GSource
359 * @callback: (nullable): The #GSourceFunc to call
360 * @user_data: (nullable): data to pass to @callback
361 *
362 * Dispatches the source callback.
363 *
364 * Called to dispatch the event source, after it has returned
365 * `TRUE` in either its prepare or its check function, or if a ready time
366 * has been reached. The dispatch function receives a callback function and
367 * user data. The callback function may be `NULL` if the source was never
368 * connected to a callback using [method@GLib.Source.set_callback]. The dispatch
369 * function should call the callback function with @user_data and whatever
370 * additional parameters are needed for this type of event source. The
371 * return value of the dispatch function should be [const@GLib.SOURCE_REMOVE]
372 * if the source should be removed or [const@GLib.SOURCE_CONTINUE] to keep it.
373 *
374 * Returns: [const@GLib.SOURCE_REMOVE] if the source should be removed,
375 * [const@GLib.SOURCE_CONTINUE] otherwise.
376 *
377 * Since: 2.82
378 */
379typedef gboolean (*GSourceFuncsDispatchFunc) (GSource *source,
380 GSourceFunc callback,
381 gpointer user_data);
382
383/**
384 * GSourceFuncsFinalizeFunc:
385 * @source: The #GSource
386 *
387 * Finalizes the source.
388 *
389 * Called when the source is finalized. At this point, the source
390 * will have been destroyed, had its callback cleared, and have been removed
391 * from its [type@GLib.MainContext], but it will still have its final reference
392 * count, so methods can be called on it from within this function.
393 *
394 * Since: 2.82
395 */
396typedef void (*GSourceFuncsFinalizeFunc) (GSource *source);
397
398struct _GSourceFuncs
399{
400 GSourceFuncsPrepareFunc prepare; /* Can be NULL */
401 GSourceFuncsCheckFunc check; /* Can be NULL */
402 GSourceFuncsDispatchFunc dispatch;
403 GSourceFuncsFinalizeFunc finalize; /* Can be NULL */
404
405 /*< private >*/
406 /* For use by g_source_set_closure */
407 GSourceFunc closure_callback;
408 GSourceDummyMarshal closure_marshal; /* Really is of type GClosureMarshal */
409};
410
411/* Standard priorities */
412
413/**
414 * G_PRIORITY_HIGH:
415 *
416 * Use this for high priority event sources.
417 *
418 * It is not used within GLib or GTK.
419 */
420#define G_PRIORITY_HIGH -100
421
422/**
423 * G_PRIORITY_DEFAULT:
424 *
425 * Use this for default priority event sources.
426 *
427 * In GLib this priority is used when adding timeout functions
428 * with [func@GLib.timeout_add]. In GDK this priority is used for events
429 * from the X server.
430 */
431#define G_PRIORITY_DEFAULT 0
432
433/**
434 * G_PRIORITY_HIGH_IDLE:
435 *
436 * Use this for high priority idle functions.
437 *
438 * GTK uses %G_PRIORITY_HIGH_IDLE + 10 for resizing operations,
439 * and %G_PRIORITY_HIGH_IDLE + 20 for redrawing operations. (This is
440 * done to ensure that any pending resizes are processed before any
441 * pending redraws, so that widgets are not redrawn twice unnecessarily.)
442 */
443#define G_PRIORITY_HIGH_IDLE 100
444
445/**
446 * G_PRIORITY_DEFAULT_IDLE:
447 *
448 * Use this for default priority idle functions.
449 *
450 * In GLib this priority is used when adding idle functions with
451 * [func@GLib.idle_add].
452 */
453#define G_PRIORITY_DEFAULT_IDLE 200
454
455/**
456 * G_PRIORITY_LOW:
457 *
458 * Use this for very low priority background tasks.
459 *
460 * It is not used within GLib or GTK.
461 */
462#define G_PRIORITY_LOW 300
463
464/**
465 * G_SOURCE_REMOVE:
466 *
467 * Use this macro as the return value of a [callback@GLib.SourceFunc] to remove
468 * the [struct@GLib.Source] from the main loop.
469 *
470 * Since: 2.32
471 */
472#define G_SOURCE_REMOVE FALSE
473
474/**
475 * G_SOURCE_CONTINUE:
476 *
477 * Use this macro as the return value of a [callback@GLib.SourceFunc] to leave
478 * the [struct@GLib.Source] in the main loop.
479 *
480 * Since: 2.32
481 */
482#define G_SOURCE_CONTINUE TRUE
483
484/* GMainContext: */
485
486GLIB_AVAILABLE_IN_ALL
487GMainContext *g_main_context_new (void);
488G_GNUC_BEGIN_IGNORE_DEPRECATIONS
489GLIB_AVAILABLE_IN_2_72
490GMainContext *g_main_context_new_with_flags (GMainContextFlags flags);
491G_GNUC_END_IGNORE_DEPRECATIONS
492GLIB_AVAILABLE_IN_ALL
493GMainContext *g_main_context_ref (GMainContext *context);
494GLIB_AVAILABLE_IN_ALL
495void g_main_context_unref (GMainContext *context);
496GLIB_AVAILABLE_IN_ALL
497GMainContext *g_main_context_default (void);
498
499GLIB_AVAILABLE_IN_ALL
500gboolean g_main_context_iteration (GMainContext *context,
501 gboolean may_block);
502GLIB_AVAILABLE_IN_ALL
503gboolean g_main_context_pending (GMainContext *context);
504
505/* For implementation of legacy interfaces
506 */
507GLIB_AVAILABLE_IN_ALL
508GSource *g_main_context_find_source_by_id (GMainContext *context,
509 guint source_id);
510GLIB_AVAILABLE_IN_ALL
511GSource *g_main_context_find_source_by_user_data (GMainContext *context,
512 gpointer user_data);
513GLIB_AVAILABLE_IN_ALL
514GSource *g_main_context_find_source_by_funcs_user_data (GMainContext *context,
515 GSourceFuncs *funcs,
516 gpointer user_data);
517
518/* Low level functions for implementing custom main loops.
519 */
520GLIB_AVAILABLE_IN_ALL
521void g_main_context_wakeup (GMainContext *context);
522GLIB_AVAILABLE_IN_ALL
523gboolean g_main_context_acquire (GMainContext *context);
524GLIB_AVAILABLE_IN_ALL
525void g_main_context_release (GMainContext *context);
526GLIB_AVAILABLE_IN_ALL
527gboolean g_main_context_is_owner (GMainContext *context);
528GLIB_DEPRECATED_IN_2_58_FOR(g_main_context_is_owner)
529gboolean g_main_context_wait (GMainContext *context,
530 GCond *cond,
531 GMutex *mutex);
532
533GLIB_AVAILABLE_IN_ALL
534gboolean g_main_context_prepare (GMainContext *context,
535 gint *priority);
536GLIB_AVAILABLE_IN_ALL
537gint g_main_context_query (GMainContext *context,
538 gint max_priority,
539 gint *timeout_,
540 GPollFD *fds,
541 gint n_fds);
542GLIB_AVAILABLE_IN_ALL
543gboolean g_main_context_check (GMainContext *context,
544 gint max_priority,
545 GPollFD *fds,
546 gint n_fds);
547GLIB_AVAILABLE_IN_ALL
548void g_main_context_dispatch (GMainContext *context);
549
550GLIB_AVAILABLE_IN_ALL
551void g_main_context_set_poll_func (GMainContext *context,
552 GPollFunc func);
553GLIB_AVAILABLE_IN_ALL
554GPollFunc g_main_context_get_poll_func (GMainContext *context);
555
556/* Low level functions for use by source implementations
557 */
558GLIB_AVAILABLE_IN_ALL
559void g_main_context_add_poll (GMainContext *context,
560 GPollFD *fd,
561 gint priority);
562GLIB_AVAILABLE_IN_ALL
563void g_main_context_remove_poll (GMainContext *context,
564 GPollFD *fd);
565
566GLIB_AVAILABLE_IN_ALL
567gint g_main_depth (void);
568GLIB_AVAILABLE_IN_ALL
569GSource *g_main_current_source (void);
570
571/* GMainContexts for other threads
572 */
573GLIB_AVAILABLE_IN_ALL
574void g_main_context_push_thread_default (GMainContext *context);
575GLIB_AVAILABLE_IN_ALL
576void g_main_context_pop_thread_default (GMainContext *context);
577GLIB_AVAILABLE_IN_ALL
578GMainContext *g_main_context_get_thread_default (void);
579GLIB_AVAILABLE_IN_ALL
580GMainContext *g_main_context_ref_thread_default (void);
581
582/**
583 * GMainContextPusher:
584 *
585 * Opaque type. See g_main_context_pusher_new() for details.
586 *
587 * Since: 2.64
588 */
589typedef void GMainContextPusher GLIB_AVAILABLE_TYPE_IN_2_64;
590
591/**
592 * g_main_context_pusher_new:
593 * @main_context: (transfer none): a main context to push
594 *
595 * Push @main_context as the new thread-default main context for the current
596 * thread, using [method@GLib.MainContext.push_thread_default], and return a
597 * new [alias@GLib.MainContextPusher]. Pop with g_main_context_pusher_free().
598 * Using [method@GLib.MainContext.pop_thread_default] on @main_context while a
599 * [alias@GLib.MainContextPusher] exists for it can lead to undefined behaviour.
600 *
601 * Using two [alias@GLib.MainContextPusher]s in the same scope is not allowed,
602 * as it leads to an undefined pop order.
603 *
604 * This is intended to be used with g_autoptr(). Note that g_autoptr()
605 * is only available when using GCC or clang, so the following example
606 * will only work with those compilers:
607 * |[
608 * typedef struct
609 * {
610 * ...
611 * GMainContext *context;
612 * ...
613 * } MyObject;
614 *
615 * static void
616 * my_object_do_stuff (MyObject *self)
617 * {
618 * g_autoptr(GMainContextPusher) pusher = g_main_context_pusher_new (self->context);
619 *
620 * // Code with main context as the thread default here
621 *
622 * if (cond)
623 * // No need to pop
624 * return;
625 *
626 * // Optionally early pop
627 * g_clear_pointer (&pusher, g_main_context_pusher_free);
628 *
629 * // Code with main context no longer the thread default here
630 * }
631 * ]|
632 *
633 * Returns: (transfer full): a #GMainContextPusher
634 * Since: 2.64
635 */
636G_GNUC_BEGIN_IGNORE_DEPRECATIONS
637GLIB_AVAILABLE_STATIC_INLINE_IN_2_64
638static inline GMainContextPusher *
639g_main_context_pusher_new (GMainContext *main_context)
640{
641 g_main_context_push_thread_default (context: main_context);
642 return (GMainContextPusher *) main_context;
643}
644G_GNUC_END_IGNORE_DEPRECATIONS
645
646/**
647 * g_main_context_pusher_free:
648 * @pusher: (transfer full): a #GMainContextPusher
649 *
650 * Pop @pusher’s main context as the thread default main context.
651 * See g_main_context_pusher_new() for details.
652 *
653 * This will pop the [struct@GLib.MainContext] as the current thread-default
654 * main context, but will not call [method@GLib.MainContext.unref] on it.
655 *
656 * Since: 2.64
657 */
658G_GNUC_BEGIN_IGNORE_DEPRECATIONS
659GLIB_AVAILABLE_STATIC_INLINE_IN_2_64
660static inline void
661g_main_context_pusher_free (GMainContextPusher *pusher)
662{
663 g_main_context_pop_thread_default (context: (GMainContext *) pusher);
664}
665G_GNUC_END_IGNORE_DEPRECATIONS
666
667/* GMainLoop: */
668
669GLIB_AVAILABLE_IN_ALL
670GMainLoop *g_main_loop_new (GMainContext *context,
671 gboolean is_running);
672GLIB_AVAILABLE_IN_ALL
673void g_main_loop_run (GMainLoop *loop);
674GLIB_AVAILABLE_IN_ALL
675void g_main_loop_quit (GMainLoop *loop);
676GLIB_AVAILABLE_IN_ALL
677GMainLoop *g_main_loop_ref (GMainLoop *loop);
678GLIB_AVAILABLE_IN_ALL
679void g_main_loop_unref (GMainLoop *loop);
680GLIB_AVAILABLE_IN_ALL
681gboolean g_main_loop_is_running (GMainLoop *loop);
682GLIB_AVAILABLE_IN_ALL
683GMainContext *g_main_loop_get_context (GMainLoop *loop);
684
685/* GSource: */
686
687GLIB_AVAILABLE_IN_ALL
688GSource *g_source_new (GSourceFuncs *source_funcs,
689 guint struct_size);
690
691G_GNUC_BEGIN_IGNORE_DEPRECATIONS
692GLIB_AVAILABLE_IN_2_64
693void g_source_set_dispose_function (GSource *source,
694 GSourceDisposeFunc dispose);
695G_GNUC_END_IGNORE_DEPRECATIONS
696
697GLIB_AVAILABLE_IN_ALL
698GSource *g_source_ref (GSource *source);
699GLIB_AVAILABLE_IN_ALL
700void g_source_unref (GSource *source);
701
702GLIB_AVAILABLE_IN_ALL
703guint g_source_attach (GSource *source,
704 GMainContext *context);
705GLIB_AVAILABLE_IN_ALL
706void g_source_destroy (GSource *source);
707
708GLIB_AVAILABLE_IN_ALL
709void g_source_set_priority (GSource *source,
710 gint priority);
711GLIB_AVAILABLE_IN_ALL
712gint g_source_get_priority (GSource *source);
713GLIB_AVAILABLE_IN_ALL
714void g_source_set_can_recurse (GSource *source,
715 gboolean can_recurse);
716GLIB_AVAILABLE_IN_ALL
717gboolean g_source_get_can_recurse (GSource *source);
718GLIB_AVAILABLE_IN_ALL
719guint g_source_get_id (GSource *source);
720
721GLIB_AVAILABLE_IN_ALL
722GMainContext *g_source_get_context (GSource *source);
723
724GLIB_AVAILABLE_IN_ALL
725void g_source_set_callback (GSource *source,
726 GSourceFunc func,
727 gpointer data,
728 GDestroyNotify notify);
729
730GLIB_AVAILABLE_IN_ALL
731void g_source_set_funcs (GSource *source,
732 GSourceFuncs *funcs);
733GLIB_AVAILABLE_IN_ALL
734gboolean g_source_is_destroyed (GSource *source);
735
736GLIB_AVAILABLE_IN_ALL
737void g_source_set_name (GSource *source,
738 const char *name);
739GLIB_AVAILABLE_IN_2_70
740void g_source_set_static_name (GSource *source,
741 const char *name);
742GLIB_AVAILABLE_IN_ALL
743const char * g_source_get_name (GSource *source);
744GLIB_AVAILABLE_IN_ALL
745void g_source_set_name_by_id (guint tag,
746 const char *name);
747
748GLIB_AVAILABLE_IN_2_36
749void g_source_set_ready_time (GSource *source,
750 gint64 ready_time);
751GLIB_AVAILABLE_IN_2_36
752gint64 g_source_get_ready_time (GSource *source);
753
754#ifdef G_OS_UNIX
755GLIB_AVAILABLE_IN_2_36
756gpointer g_source_add_unix_fd (GSource *source,
757 gint fd,
758 GIOCondition events);
759GLIB_AVAILABLE_IN_2_36
760void g_source_modify_unix_fd (GSource *source,
761 gpointer tag,
762 GIOCondition new_events);
763GLIB_AVAILABLE_IN_2_36
764void g_source_remove_unix_fd (GSource *source,
765 gpointer tag);
766GLIB_AVAILABLE_IN_2_36
767GIOCondition g_source_query_unix_fd (GSource *source,
768 gpointer tag);
769#endif
770
771/* Used to implement g_source_connect_closure and internally*/
772GLIB_AVAILABLE_IN_ALL
773void g_source_set_callback_indirect (GSource *source,
774 gpointer callback_data,
775 GSourceCallbackFuncs *callback_funcs);
776
777GLIB_AVAILABLE_IN_ALL
778void g_source_add_poll (GSource *source,
779 GPollFD *fd);
780GLIB_AVAILABLE_IN_ALL
781void g_source_remove_poll (GSource *source,
782 GPollFD *fd);
783
784GLIB_AVAILABLE_IN_ALL
785void g_source_add_child_source (GSource *source,
786 GSource *child_source);
787GLIB_AVAILABLE_IN_ALL
788void g_source_remove_child_source (GSource *source,
789 GSource *child_source);
790
791G_GNUC_BEGIN_IGNORE_DEPRECATIONS
792GLIB_DEPRECATED_IN_2_28_FOR(g_source_get_time)
793void g_source_get_current_time (GSource *source,
794 GTimeVal *timeval);
795G_GNUC_END_IGNORE_DEPRECATIONS
796
797GLIB_AVAILABLE_IN_ALL
798gint64 g_source_get_time (GSource *source);
799
800 /* void g_source_connect_closure (GSource *source,
801 GClosure *closure);
802 */
803
804/* Specific source types
805 */
806GLIB_AVAILABLE_IN_ALL
807GSource *g_idle_source_new (void);
808GLIB_AVAILABLE_IN_ALL
809GSource *g_child_watch_source_new (GPid pid);
810GLIB_AVAILABLE_IN_ALL
811GSource *g_timeout_source_new (guint interval);
812GLIB_AVAILABLE_IN_ALL
813GSource *g_timeout_source_new_seconds (guint interval);
814
815/* Miscellaneous functions
816 */
817G_GNUC_BEGIN_IGNORE_DEPRECATIONS
818GLIB_DEPRECATED_IN_2_62_FOR(g_get_real_time)
819void g_get_current_time (GTimeVal *result);
820G_GNUC_END_IGNORE_DEPRECATIONS
821
822GLIB_AVAILABLE_IN_ALL
823gint64 g_get_monotonic_time (void);
824GLIB_AVAILABLE_IN_ALL
825gint64 g_get_real_time (void);
826
827
828/* Source manipulation by ID */
829GLIB_AVAILABLE_IN_ALL
830gboolean g_source_remove (guint tag);
831GLIB_AVAILABLE_IN_ALL
832gboolean g_source_remove_by_user_data (gpointer user_data);
833GLIB_AVAILABLE_IN_ALL
834gboolean g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
835 gpointer user_data);
836
837/**
838 * GClearHandleFunc:
839 * @handle_id: the handle ID to clear
840 *
841 * Specifies the type of function passed to [func@GLib.clear_handle_id] The
842 * implementation is expected to free the resource identified by @handle_id;
843 * for instance, if @handle_id is a [struct@GLib.Source] ID,
844 * [func@GLib.Source.remove] can be used.
845 *
846 * Since: 2.56
847 */
848typedef void (* GClearHandleFunc) (guint handle_id);
849
850GLIB_AVAILABLE_IN_2_56
851void g_clear_handle_id (guint *tag_ptr,
852 GClearHandleFunc clear_func);
853
854#define g_clear_handle_id(tag_ptr, clear_func) \
855 G_STMT_START { \
856 G_STATIC_ASSERT (sizeof *(tag_ptr) == sizeof (guint)); \
857 guint *_tag_ptr = (guint *) (tag_ptr); \
858 guint _handle_id; \
859 \
860 _handle_id = *_tag_ptr; \
861 if (_handle_id > 0) \
862 { \
863 *_tag_ptr = 0; \
864 clear_func (_handle_id); \
865 } \
866 } G_STMT_END \
867 GLIB_AVAILABLE_MACRO_IN_2_56
868
869/* Idles, child watchers and timeouts */
870GLIB_AVAILABLE_IN_ALL
871guint g_timeout_add_full (gint priority,
872 guint interval,
873 GSourceFunc function,
874 gpointer data,
875 GDestroyNotify notify);
876GLIB_AVAILABLE_IN_ALL
877guint g_timeout_add (guint interval,
878 GSourceFunc function,
879 gpointer data);
880GLIB_AVAILABLE_IN_2_74
881guint g_timeout_add_once (guint interval,
882 GSourceOnceFunc function,
883 gpointer data);
884GLIB_AVAILABLE_IN_ALL
885guint g_timeout_add_seconds_full (gint priority,
886 guint interval,
887 GSourceFunc function,
888 gpointer data,
889 GDestroyNotify notify);
890GLIB_AVAILABLE_IN_ALL
891guint g_timeout_add_seconds (guint interval,
892 GSourceFunc function,
893 gpointer data);
894GLIB_AVAILABLE_IN_2_78
895guint g_timeout_add_seconds_once (guint interval,
896 GSourceOnceFunc function,
897 gpointer data);
898GLIB_AVAILABLE_IN_ALL
899guint g_child_watch_add_full (gint priority,
900 GPid pid,
901 GChildWatchFunc function,
902 gpointer data,
903 GDestroyNotify notify);
904GLIB_AVAILABLE_IN_ALL
905guint g_child_watch_add (GPid pid,
906 GChildWatchFunc function,
907 gpointer data);
908GLIB_AVAILABLE_IN_ALL
909guint g_idle_add (GSourceFunc function,
910 gpointer data);
911GLIB_AVAILABLE_IN_ALL
912guint g_idle_add_full (gint priority,
913 GSourceFunc function,
914 gpointer data,
915 GDestroyNotify notify);
916GLIB_AVAILABLE_IN_2_74
917guint g_idle_add_once (GSourceOnceFunc function,
918 gpointer data);
919GLIB_AVAILABLE_IN_ALL
920gboolean g_idle_remove_by_data (gpointer data);
921
922GLIB_AVAILABLE_IN_ALL
923void g_main_context_invoke_full (GMainContext *context,
924 gint priority,
925 GSourceFunc function,
926 gpointer data,
927 GDestroyNotify notify);
928GLIB_AVAILABLE_IN_ALL
929void g_main_context_invoke (GMainContext *context,
930 GSourceFunc function,
931 gpointer data);
932
933GLIB_AVAILABLE_STATIC_INLINE_IN_2_70
934static inline int
935g_steal_fd (int *fd_ptr)
936{
937 int fd = *fd_ptr;
938 *fd_ptr = -1;
939 return fd;
940}
941
942/* Hook for GClosure / GSource integration. Don't touch */
943GLIB_VAR GSourceFuncs g_timeout_funcs;
944GLIB_VAR GSourceFuncs g_child_watch_funcs;
945GLIB_VAR GSourceFuncs g_idle_funcs;
946#ifdef G_OS_UNIX
947GLIB_VAR GSourceFuncs g_unix_signal_funcs;
948GLIB_VAR GSourceFuncs g_unix_fd_source_funcs;
949#endif
950
951G_END_DECLS
952
953#endif /* __G_MAIN_H__ */
954