1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
35 #include "glibconfig.h"
37 /* Uncomment the next line (and the corresponding line in gpoll.c) to
38 * enable debugging printouts if the environment variable
39 * G_MAIN_POLL_DEBUG is set to some value.
41 /* #define G_MAIN_POLL_DEBUG */
44 /* Always enable debugging printout on Windows, as it is more often
47 #define G_MAIN_POLL_DEBUG
51 #include "glib-unix.h"
54 #include <sys/eventfd.h>
59 #include <sys/types.h>
62 #ifdef HAVE_SYS_TIME_H
64 #endif /* HAVE_SYS_TIME_H */
67 #endif /* HAVE_UNISTD_H */
74 #endif /* G_OS_WIN32 */
77 #include <sys/socket.h>
79 #endif /* G_OS_BEOS */
84 #include "giochannel.h"
88 #include "gstrfuncs.h"
89 #include "gtestutils.h"
95 #ifdef G_MAIN_POLL_DEBUG
100 #include "gmain-internal.h"
101 #include "glib-private.h"
105 * @title: The Main Event Loop
106 * @short_description: manages all available sources of events
108 * The main event loop manages all the available sources of events for
109 * GLib and GTK+ applications. These events can come from any number of
110 * different types of sources such as file descriptors (plain files,
111 * pipes or sockets) and timeouts. New types of event sources can also
112 * be added using g_source_attach().
114 * To allow multiple independent sets of sources to be handled in
115 * different threads, each source is associated with a #GMainContext.
116 * A GMainContext can only be running in a single thread, but
117 * sources can be added to it and removed from it from other threads.
119 * Each event source is assigned a priority. The default priority,
120 * #G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
121 * Values greater than 0 denote lower priorities. Events from high priority
122 * sources are always processed before events from lower priority sources.
124 * Idle functions can also be added, and assigned a priority. These will
125 * be run whenever no events with a higher priority are ready to be processed.
127 * The #GMainLoop data type represents a main event loop. A GMainLoop is
128 * created with g_main_loop_new(). After adding the initial event sources,
129 * g_main_loop_run() is called. This continuously checks for new events from
130 * each of the event sources and dispatches them. Finally, the processing of
131 * an event from one of the sources leads to a call to g_main_loop_quit() to
132 * exit the main loop, and g_main_loop_run() returns.
134 * It is possible to create new instances of #GMainLoop recursively.
135 * This is often used in GTK+ applications when showing modal dialog
136 * boxes. Note that event sources are associated with a particular
137 * #GMainContext, and will be checked and dispatched for all main
138 * loops associated with that GMainContext.
140 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
141 * gtk_main_quit() and gtk_events_pending().
143 * <refsect2><title>Creating new source types</title>
144 * <para>One of the unusual features of the #GMainLoop functionality
145 * is that new types of event source can be created and used in
146 * addition to the builtin type of event source. A new event source
147 * type is used for handling GDK events. A new source type is created
148 * by <firstterm>deriving</firstterm> from the #GSource structure.
149 * The derived type of source is represented by a structure that has
150 * the #GSource structure as a first element, and other elements specific
151 * to the new source type. To create an instance of the new source type,
152 * call g_source_new() passing in the size of the derived structure and
153 * a table of functions. These #GSourceFuncs determine the behavior of
154 * the new source type.</para>
155 * <para>New source types basically interact with the main context
156 * in two ways. Their prepare function in #GSourceFuncs can set a timeout
157 * to determine the maximum amount of time that the main loop will sleep
158 * before checking the source again. In addition, or as well, the source
159 * can add file descriptors to the set that the main context checks using
160 * g_source_add_poll().</para>
162 * <refsect2><title>Customizing the main loop iteration</title>
163 * <para>Single iterations of a #GMainContext can be run with
164 * g_main_context_iteration(). In some cases, more detailed control
165 * of exactly how the details of the main loop work is desired, for
166 * instance, when integrating the #GMainLoop with an external main loop.
167 * In such cases, you can call the component functions of
168 * g_main_context_iteration() directly. These functions are
169 * g_main_context_prepare(), g_main_context_query(),
170 * g_main_context_check() and g_main_context_dispatch().</para>
171 * <para>The operation of these functions can best be seen in terms
172 * of a state diagram, as shown in <xref linkend="mainloop-states"/>.</para>
173 * <figure id="mainloop-states"><title>States of a Main Context</title>
174 * <graphic fileref="mainloop-states.gif" format="GIF"></graphic>
178 * On Unix, the GLib mainloop is incompatible with fork(). Any program
179 * using the mainloop must either exec() or exit() from the child
180 * without returning to the mainloop.
185 typedef struct _GTimeoutSource GTimeoutSource;
186 typedef struct _GChildWatchSource GChildWatchSource;
187 typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource;
188 typedef struct _GPollRec GPollRec;
189 typedef struct _GSourceCallback GSourceCallback;
193 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
194 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1),
195 G_SOURCE_BLOCKED = 1 << (G_HOOK_FLAG_USER_SHIFT + 2)
198 typedef struct _GSourceList GSourceList;
202 GSource *head, *tail;
206 typedef struct _GMainWaiter GMainWaiter;
214 typedef struct _GMainDispatch GMainDispatch;
216 struct _GMainDispatch
219 GSList *dispatching_sources; /* stack of current sources */
222 #ifdef G_MAIN_POLL_DEBUG
223 gboolean _g_main_poll_debug = FALSE;
228 /* The following lock is used for both the list of sources
229 * and the list of poll records
239 GPtrArray *pending_dispatches;
240 gint timeout; /* Timeout for current iteration */
244 gint in_check_or_prepare;
246 GPollRec *poll_records, *poll_records_tail;
247 guint n_poll_records;
248 GPollFD *cached_poll_array;
249 guint cached_poll_array_size;
255 /* Flag indicating whether the set of fd's changed during a poll */
256 gboolean poll_changed;
261 gboolean time_is_fresh;
264 struct _GSourceCallback
269 GDestroyNotify notify;
274 GMainContext *context;
279 struct _GTimeoutSource
287 struct _GChildWatchSource
294 #else /* G_OS_WIN32 */
295 gboolean child_exited;
296 #endif /* G_OS_WIN32 */
299 struct _GUnixSignalWatchSource
314 struct _GSourcePrivate
316 GSList *child_sources;
317 GSource *parent_source;
320 typedef struct _GSourceIter
322 GMainContext *context;
328 #define LOCK_CONTEXT(context) g_mutex_lock (&context->mutex)
329 #define UNLOCK_CONTEXT(context) g_mutex_unlock (&context->mutex)
330 #define G_THREAD_SELF g_thread_self ()
332 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
333 #define SOURCE_BLOCKED(source) (((source)->flags & G_SOURCE_BLOCKED) != 0)
335 #define SOURCE_UNREF(source, context) \
337 if ((source)->ref_count > 1) \
338 (source)->ref_count--; \
340 g_source_unref_internal ((source), (context), TRUE); \
344 /* Forward declarations */
346 static void g_source_unref_internal (GSource *source,
347 GMainContext *context,
349 static void g_source_destroy_internal (GSource *source,
350 GMainContext *context,
352 static void g_source_set_priority_unlocked (GSource *source,
353 GMainContext *context,
355 static void g_child_source_remove_internal (GSource *child_source,
356 GMainContext *context);
358 static void g_main_context_poll (GMainContext *context,
363 static void g_main_context_add_poll_unlocked (GMainContext *context,
366 static void g_main_context_remove_poll_unlocked (GMainContext *context,
369 static void g_source_iter_init (GSourceIter *iter,
370 GMainContext *context,
371 gboolean may_modify);
372 static gboolean g_source_iter_next (GSourceIter *iter,
374 static void g_source_iter_clear (GSourceIter *iter);
376 static gboolean g_timeout_prepare (GSource *source,
378 static gboolean g_timeout_check (GSource *source);
379 static gboolean g_timeout_dispatch (GSource *source,
380 GSourceFunc callback,
382 static gboolean g_child_watch_prepare (GSource *source,
384 static gboolean g_child_watch_check (GSource *source);
385 static gboolean g_child_watch_dispatch (GSource *source,
386 GSourceFunc callback,
388 static void g_child_watch_finalize (GSource *source);
390 static void g_unix_signal_handler (int signum);
391 static gboolean g_unix_signal_watch_prepare (GSource *source,
393 static gboolean g_unix_signal_watch_check (GSource *source);
394 static gboolean g_unix_signal_watch_dispatch (GSource *source,
395 GSourceFunc callback,
397 static void g_unix_signal_watch_finalize (GSource *source);
399 static gboolean g_idle_prepare (GSource *source,
401 static gboolean g_idle_check (GSource *source);
402 static gboolean g_idle_dispatch (GSource *source,
403 GSourceFunc callback,
406 static void block_source (GSource *source);
408 static GMainContext *glib_worker_context;
410 G_LOCK_DEFINE_STATIC (main_loop);
411 static GMainContext *default_main_context;
416 /* UNIX signals work by marking one of these variables then waking the
417 * worker context to check on them and dispatch accordingly.
419 #ifdef HAVE_SIG_ATOMIC_T
420 static volatile sig_atomic_t unix_signal_pending[NSIG];
421 static volatile sig_atomic_t any_unix_signal_pending;
423 static volatile int unix_signal_pending[NSIG];
424 static volatile int any_unix_signal_pending;
427 /* Guards all the data below */
428 G_LOCK_DEFINE_STATIC (unix_signal_lock);
429 static GSList *unix_signal_watches;
430 static GSList *unix_child_watches;
432 static GSourceFuncs g_unix_signal_funcs =
434 g_unix_signal_watch_prepare,
435 g_unix_signal_watch_check,
436 g_unix_signal_watch_dispatch,
437 g_unix_signal_watch_finalize
439 #endif /* !G_OS_WIN32 */
440 G_LOCK_DEFINE_STATIC (main_context_list);
441 static GSList *main_context_list = NULL;
443 GSourceFuncs g_timeout_funcs =
451 GSourceFuncs g_child_watch_funcs =
453 g_child_watch_prepare,
455 g_child_watch_dispatch,
456 g_child_watch_finalize
459 GSourceFuncs g_idle_funcs =
468 * g_main_context_ref:
469 * @context: a #GMainContext
471 * Increases the reference count on a #GMainContext object by one.
473 * Returns: the @context that was passed in (since 2.6)
476 g_main_context_ref (GMainContext *context)
478 g_return_val_if_fail (context != NULL, NULL);
479 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
481 g_atomic_int_inc (&context->ref_count);
487 poll_rec_list_free (GMainContext *context,
490 g_slice_free_chain (GPollRec, list, next);
494 * g_main_context_unref:
495 * @context: a #GMainContext
497 * Decreases the reference count on a #GMainContext object by one. If
498 * the result is zero, free the context and free all associated memory.
501 g_main_context_unref (GMainContext *context)
508 g_return_if_fail (context != NULL);
509 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
511 if (!g_atomic_int_dec_and_test (&context->ref_count))
514 G_LOCK (main_context_list);
515 main_context_list = g_slist_remove (main_context_list, context);
516 G_UNLOCK (main_context_list);
518 g_source_iter_init (&iter, context, TRUE);
519 while (g_source_iter_next (&iter, &source))
521 source->context = NULL;
522 g_source_destroy_internal (source, context, FALSE);
524 for (sl_iter = context->source_lists; sl_iter; sl_iter = sl_iter->next)
526 list = sl_iter->data;
527 g_slice_free (GSourceList, list);
529 g_list_free (context->source_lists);
531 g_mutex_clear (&context->mutex);
533 g_ptr_array_free (context->pending_dispatches, TRUE);
534 g_free (context->cached_poll_array);
536 poll_rec_list_free (context, context->poll_records);
538 g_wakeup_free (context->wakeup);
539 g_cond_clear (&context->cond);
545 * g_main_context_new:
547 * Creates a new #GMainContext structure.
549 * Return value: the new #GMainContext
552 g_main_context_new (void)
554 static gsize initialised;
555 GMainContext *context;
557 if (g_once_init_enter (&initialised))
559 #ifdef G_MAIN_POLL_DEBUG
560 if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
561 _g_main_poll_debug = TRUE;
564 g_once_init_leave (&initialised, TRUE);
567 context = g_new0 (GMainContext, 1);
569 g_mutex_init (&context->mutex);
570 g_cond_init (&context->cond);
572 context->owner = NULL;
573 context->waiters = NULL;
575 context->ref_count = 1;
577 context->next_id = 1;
579 context->source_lists = NULL;
581 context->poll_func = g_poll;
583 context->cached_poll_array = NULL;
584 context->cached_poll_array_size = 0;
586 context->pending_dispatches = g_ptr_array_new ();
588 context->time_is_fresh = FALSE;
590 context->wakeup = g_wakeup_new ();
591 g_wakeup_get_pollfd (context->wakeup, &context->wake_up_rec);
592 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
594 G_LOCK (main_context_list);
595 main_context_list = g_slist_append (main_context_list, context);
597 #ifdef G_MAIN_POLL_DEBUG
598 if (_g_main_poll_debug)
599 g_print ("created context=%p\n", context);
602 G_UNLOCK (main_context_list);
608 * g_main_context_default:
610 * Returns the global default main context. This is the main context
611 * used for main loop functions when a main loop is not explicitly
612 * specified, and corresponds to the "main" main loop. See also
613 * g_main_context_get_thread_default().
615 * Return value: (transfer none): the global default main context.
618 g_main_context_default (void)
624 if (!default_main_context)
626 default_main_context = g_main_context_new ();
627 #ifdef G_MAIN_POLL_DEBUG
628 if (_g_main_poll_debug)
629 g_print ("default context=%p\n", default_main_context);
633 G_UNLOCK (main_loop);
635 return default_main_context;
639 free_context (gpointer data)
641 GMainContext *context = data;
643 g_main_context_release (context);
645 g_main_context_unref (context);
649 free_context_stack (gpointer data)
651 g_queue_free_full((GQueue *) data, (GDestroyNotify) free_context);
654 static GPrivate thread_context_stack = G_PRIVATE_INIT (free_context_stack);
657 * g_main_context_push_thread_default:
658 * @context: (allow-none): a #GMainContext, or %NULL for the global default context
660 * Acquires @context and sets it as the thread-default context for the
661 * current thread. This will cause certain asynchronous operations
662 * (such as most <link linkend="gio">gio</link>-based I/O) which are
663 * started in this thread to run under @context and deliver their
664 * results to its main loop, rather than running under the global
665 * default context in the main thread. Note that calling this function
666 * changes the context returned by
667 * g_main_context_get_thread_default(), <emphasis>not</emphasis> the
668 * one returned by g_main_context_default(), so it does not affect the
669 * context used by functions like g_idle_add().
671 * Normally you would call this function shortly after creating a new
672 * thread, passing it a #GMainContext which will be run by a
673 * #GMainLoop in that thread, to set a new default context for all
674 * async operations in that thread. (In this case, you don't need to
675 * ever call g_main_context_pop_thread_default().) In some cases
676 * however, you may want to schedule a single operation in a
677 * non-default context, or temporarily use a non-default context in
678 * the main thread. In that case, you can wrap the call to the
679 * asynchronous operation inside a
680 * g_main_context_push_thread_default() /
681 * g_main_context_pop_thread_default() pair, but it is up to you to
682 * ensure that no other asynchronous operations accidentally get
683 * started while the non-default context is active.
685 * Beware that libraries that predate this function may not correctly
686 * handle being used from a thread with a thread-default context. Eg,
687 * see g_file_supports_thread_contexts().
692 g_main_context_push_thread_default (GMainContext *context)
695 gboolean acquired_context;
697 acquired_context = g_main_context_acquire (context);
698 g_return_if_fail (acquired_context);
700 if (context == g_main_context_default ())
703 g_main_context_ref (context);
705 stack = g_private_get (&thread_context_stack);
708 stack = g_queue_new ();
709 g_private_set (&thread_context_stack, stack);
712 g_queue_push_head (stack, context);
716 * g_main_context_pop_thread_default:
717 * @context: (allow-none): a #GMainContext object, or %NULL
719 * Pops @context off the thread-default context stack (verifying that
720 * it was on the top of the stack).
725 g_main_context_pop_thread_default (GMainContext *context)
729 if (context == g_main_context_default ())
732 stack = g_private_get (&thread_context_stack);
734 g_return_if_fail (stack != NULL);
735 g_return_if_fail (g_queue_peek_head (stack) == context);
737 g_queue_pop_head (stack);
739 g_main_context_release (context);
741 g_main_context_unref (context);
745 * g_main_context_get_thread_default:
747 * Gets the thread-default #GMainContext for this thread. Asynchronous
748 * operations that want to be able to be run in contexts other than
749 * the default one should call this method or
750 * g_main_context_ref_thread_default() to get a #GMainContext to add
751 * their #GSource<!-- -->s to. (Note that even in single-threaded
752 * programs applications may sometimes want to temporarily push a
753 * non-default context, so it is not safe to assume that this will
754 * always return %NULL if you are running in the default thread.)
756 * If you need to hold a reference on the context, use
757 * g_main_context_ref_thread_default() instead.
759 * Returns: (transfer none): the thread-default #GMainContext, or
760 * %NULL if the thread-default context is the global default context.
765 g_main_context_get_thread_default (void)
769 stack = g_private_get (&thread_context_stack);
771 return g_queue_peek_head (stack);
777 * g_main_context_ref_thread_default:
779 * Gets the thread-default #GMainContext for this thread, as with
780 * g_main_context_get_thread_default(), but also adds a reference to
781 * it with g_main_context_ref(). In addition, unlike
782 * g_main_context_get_thread_default(), if the thread-default context
783 * is the global default context, this will return that #GMainContext
784 * (with a ref added to it) rather than returning %NULL.
786 * Returns: (transfer full): the thread-default #GMainContext. Unref
787 * with g_main_context_unref() when you are done with it.
792 g_main_context_ref_thread_default (void)
794 GMainContext *context;
796 context = g_main_context_get_thread_default ();
798 context = g_main_context_default ();
799 return g_main_context_ref (context);
802 /* Hooks for adding to the main loop */
806 * @source_funcs: structure containing functions that implement
807 * the sources behavior.
808 * @struct_size: size of the #GSource structure to create.
810 * Creates a new #GSource structure. The size is specified to
811 * allow creating structures derived from #GSource that contain
812 * additional data. The size passed in must be at least
813 * <literal>sizeof (GSource)</literal>.
815 * The source will not initially be associated with any #GMainContext
816 * and must be added to one with g_source_attach() before it will be
819 * Return value: the newly-created #GSource.
822 g_source_new (GSourceFuncs *source_funcs,
827 g_return_val_if_fail (source_funcs != NULL, NULL);
828 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
830 source = (GSource*) g_malloc0 (struct_size);
831 source->priv = g_slice_new0 (GSourcePrivate);
832 source->source_funcs = source_funcs;
833 source->ref_count = 1;
835 source->priority = G_PRIORITY_DEFAULT;
837 source->flags = G_HOOK_FLAG_ACTIVE;
839 /* NULL/0 initialization for all other fields */
844 /* Holds context's lock */
846 g_source_iter_init (GSourceIter *iter,
847 GMainContext *context,
850 iter->context = context;
851 iter->current_list = NULL;
853 iter->may_modify = may_modify;
856 /* Holds context's lock */
858 g_source_iter_next (GSourceIter *iter, GSource **source)
860 GSource *next_source;
863 next_source = iter->source->next;
869 if (iter->current_list)
870 iter->current_list = iter->current_list->next;
872 iter->current_list = iter->context->source_lists;
874 if (iter->current_list)
876 GSourceList *source_list = iter->current_list->data;
878 next_source = source_list->head;
882 /* Note: unreffing iter->source could potentially cause its
883 * GSourceList to be removed from source_lists (if iter->source is
884 * the only source in its list, and it is destroyed), so we have to
885 * keep it reffed until after we advance iter->current_list, above.
888 if (iter->source && iter->may_modify)
889 SOURCE_UNREF (iter->source, iter->context);
890 iter->source = next_source;
891 if (iter->source && iter->may_modify)
892 iter->source->ref_count++;
894 *source = iter->source;
895 return *source != NULL;
898 /* Holds context's lock. Only necessary to call if you broke out of
899 * the g_source_iter_next() loop early.
902 g_source_iter_clear (GSourceIter *iter)
904 if (iter->source && iter->may_modify)
906 SOURCE_UNREF (iter->source, iter->context);
911 /* Holds context's lock
914 find_source_list_for_priority (GMainContext *context,
919 GSourceList *source_list;
922 for (iter = context->source_lists; iter != NULL; last = iter, iter = iter->next)
924 source_list = iter->data;
926 if (source_list->priority == priority)
929 if (source_list->priority > priority)
934 source_list = g_slice_new0 (GSourceList);
935 source_list->priority = priority;
936 context->source_lists = g_list_insert_before (context->source_lists,
946 source_list = g_slice_new0 (GSourceList);
947 source_list->priority = priority;
950 context->source_lists = g_list_append (NULL, source_list);
953 /* This just appends source_list to the end of
954 * context->source_lists without having to walk the list again.
956 last = g_list_append (last, source_list);
961 /* Holds context's lock
964 source_add_to_context (GSource *source,
965 GMainContext *context)
967 GSourceList *source_list;
968 GSource *prev, *next;
970 source_list = find_source_list_for_priority (context, source->priority, TRUE);
972 if (source->priv->parent_source)
974 g_assert (source_list->head != NULL);
976 /* Put the source immediately before its parent */
977 prev = source->priv->parent_source->prev;
978 next = source->priv->parent_source;
982 prev = source_list->tail;
990 source_list->tail = source;
996 source_list->head = source;
999 /* Holds context's lock
1002 source_remove_from_context (GSource *source,
1003 GMainContext *context)
1005 GSourceList *source_list;
1007 source_list = find_source_list_for_priority (context, source->priority, FALSE);
1008 g_return_if_fail (source_list != NULL);
1011 source->prev->next = source->next;
1013 source_list->head = source->next;
1016 source->next->prev = source->prev;
1018 source_list->tail = source->prev;
1020 source->prev = NULL;
1021 source->next = NULL;
1023 if (source_list->head == NULL)
1025 context->source_lists = g_list_remove (context->source_lists, source_list);
1026 g_slice_free (GSourceList, source_list);
1031 g_source_attach_unlocked (GSource *source,
1032 GMainContext *context)
1037 source->context = context;
1038 result = source->source_id = context->next_id++;
1040 source->ref_count++;
1041 source_add_to_context (source, context);
1043 tmp_list = source->poll_fds;
1046 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
1047 tmp_list = tmp_list->next;
1050 tmp_list = source->priv->child_sources;
1053 g_source_attach_unlocked (tmp_list->data, context);
1054 tmp_list = tmp_list->next;
1062 * @source: a #GSource
1063 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used)
1065 * Adds a #GSource to a @context so that it will be executed within
1066 * that context. Remove it by calling g_source_destroy().
1068 * Return value: the ID (greater than 0) for the source within the
1072 g_source_attach (GSource *source,
1073 GMainContext *context)
1077 g_return_val_if_fail (source->context == NULL, 0);
1078 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
1081 context = g_main_context_default ();
1083 LOCK_CONTEXT (context);
1085 result = g_source_attach_unlocked (source, context);
1087 /* If another thread has acquired the context, wake it up since it
1088 * might be in poll() right now.
1090 if (context->owner && context->owner != G_THREAD_SELF)
1091 g_wakeup_signal (context->wakeup);
1093 UNLOCK_CONTEXT (context);
1099 g_source_destroy_internal (GSource *source,
1100 GMainContext *context,
1104 LOCK_CONTEXT (context);
1106 if (!SOURCE_DESTROYED (source))
1109 gpointer old_cb_data;
1110 GSourceCallbackFuncs *old_cb_funcs;
1112 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1114 old_cb_data = source->callback_data;
1115 old_cb_funcs = source->callback_funcs;
1117 source->callback_data = NULL;
1118 source->callback_funcs = NULL;
1122 UNLOCK_CONTEXT (context);
1123 old_cb_funcs->unref (old_cb_data);
1124 LOCK_CONTEXT (context);
1127 if (!SOURCE_BLOCKED (source))
1129 tmp_list = source->poll_fds;
1132 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1133 tmp_list = tmp_list->next;
1137 while (source->priv->child_sources)
1138 g_child_source_remove_internal (source->priv->child_sources->data, context);
1140 if (source->priv->parent_source)
1141 g_child_source_remove_internal (source, context);
1143 g_source_unref_internal (source, context, TRUE);
1147 UNLOCK_CONTEXT (context);
1152 * @source: a #GSource
1154 * Removes a source from its #GMainContext, if any, and mark it as
1155 * destroyed. The source cannot be subsequently added to another
1159 g_source_destroy (GSource *source)
1161 GMainContext *context;
1163 g_return_if_fail (source != NULL);
1165 context = source->context;
1168 g_source_destroy_internal (source, context, FALSE);
1170 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1175 * @source: a #GSource
1177 * Returns the numeric ID for a particular source. The ID of a source
1178 * is a positive integer which is unique within a particular main loop
1179 * context. The reverse
1180 * mapping from ID to source is done by g_main_context_find_source_by_id().
1182 * Return value: the ID (greater than 0) for the source
1185 g_source_get_id (GSource *source)
1189 g_return_val_if_fail (source != NULL, 0);
1190 g_return_val_if_fail (source->context != NULL, 0);
1192 LOCK_CONTEXT (source->context);
1193 result = source->source_id;
1194 UNLOCK_CONTEXT (source->context);
1200 * g_source_get_context:
1201 * @source: a #GSource
1203 * Gets the #GMainContext with which the source is associated.
1205 * You can call this on a source that has been destroyed, provided
1206 * that the #GMainContext it was attached to still exists (in which
1207 * case it will return that #GMainContext). In particular, you can
1208 * always call this function on the source returned from
1209 * g_main_current_source(). But calling this function on a source
1210 * whose #GMainContext has been destroyed is an error.
1212 * Return value: (transfer none) (allow-none): the #GMainContext with which the
1213 * source is associated, or %NULL if the context has not
1214 * yet been added to a source.
1217 g_source_get_context (GSource *source)
1219 g_return_val_if_fail (source->context != NULL || !SOURCE_DESTROYED (source), NULL);
1221 return source->context;
1225 * g_source_add_poll:
1226 * @source:a #GSource
1227 * @fd: a #GPollFD structure holding information about a file
1228 * descriptor to watch.
1230 * Adds a file descriptor to the set of file descriptors polled for
1231 * this source. This is usually combined with g_source_new() to add an
1232 * event source. The event source's check function will typically test
1233 * the @revents field in the #GPollFD struct and return %TRUE if events need
1237 g_source_add_poll (GSource *source,
1240 GMainContext *context;
1242 g_return_if_fail (source != NULL);
1243 g_return_if_fail (fd != NULL);
1244 g_return_if_fail (!SOURCE_DESTROYED (source));
1246 context = source->context;
1249 LOCK_CONTEXT (context);
1251 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1255 if (!SOURCE_BLOCKED (source))
1256 g_main_context_add_poll_unlocked (context, source->priority, fd);
1257 UNLOCK_CONTEXT (context);
1262 * g_source_remove_poll:
1263 * @source:a #GSource
1264 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1266 * Removes a file descriptor from the set of file descriptors polled for
1270 g_source_remove_poll (GSource *source,
1273 GMainContext *context;
1275 g_return_if_fail (source != NULL);
1276 g_return_if_fail (fd != NULL);
1277 g_return_if_fail (!SOURCE_DESTROYED (source));
1279 context = source->context;
1282 LOCK_CONTEXT (context);
1284 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1288 if (!SOURCE_BLOCKED (source))
1289 g_main_context_remove_poll_unlocked (context, fd);
1290 UNLOCK_CONTEXT (context);
1295 * g_source_add_child_source:
1296 * @source:a #GSource
1297 * @child_source: a second #GSource that @source should "poll"
1299 * Adds @child_source to @source as a "polled" source; when @source is
1300 * added to a #GMainContext, @child_source will be automatically added
1301 * with the same priority, when @child_source is triggered, it will
1302 * cause @source to dispatch (in addition to calling its own
1303 * callback), and when @source is destroyed, it will destroy
1304 * @child_source as well. (@source will also still be dispatched if
1305 * its own prepare/check functions indicate that it is ready.)
1307 * If you don't need @child_source to do anything on its own when it
1308 * triggers, you can call g_source_set_dummy_callback() on it to set a
1309 * callback that does nothing (except return %TRUE if appropriate).
1311 * @source will hold a reference on @child_source while @child_source
1312 * is attached to it.
1317 g_source_add_child_source (GSource *source,
1318 GSource *child_source)
1320 GMainContext *context;
1322 g_return_if_fail (source != NULL);
1323 g_return_if_fail (child_source != NULL);
1324 g_return_if_fail (!SOURCE_DESTROYED (source));
1325 g_return_if_fail (!SOURCE_DESTROYED (child_source));
1326 g_return_if_fail (child_source->context == NULL);
1327 g_return_if_fail (child_source->priv->parent_source == NULL);
1329 context = source->context;
1332 LOCK_CONTEXT (context);
1334 source->priv->child_sources = g_slist_prepend (source->priv->child_sources,
1335 g_source_ref (child_source));
1336 child_source->priv->parent_source = source;
1337 g_source_set_priority_unlocked (child_source, NULL, source->priority);
1338 if (SOURCE_BLOCKED (source))
1339 block_source (child_source);
1343 UNLOCK_CONTEXT (context);
1344 g_source_attach (child_source, context);
1349 g_child_source_remove_internal (GSource *child_source,
1350 GMainContext *context)
1352 GSource *parent_source = child_source->priv->parent_source;
1354 parent_source->priv->child_sources =
1355 g_slist_remove (parent_source->priv->child_sources, child_source);
1356 child_source->priv->parent_source = NULL;
1358 g_source_destroy_internal (child_source, context, TRUE);
1359 g_source_unref_internal (child_source, context, TRUE);
1363 * g_source_remove_child_source:
1364 * @source:a #GSource
1365 * @child_source: a #GSource previously passed to
1366 * g_source_add_child_source().
1368 * Detaches @child_source from @source and destroys it.
1373 g_source_remove_child_source (GSource *source,
1374 GSource *child_source)
1376 GMainContext *context;
1378 g_return_if_fail (source != NULL);
1379 g_return_if_fail (child_source != NULL);
1380 g_return_if_fail (child_source->priv->parent_source == source);
1381 g_return_if_fail (!SOURCE_DESTROYED (source));
1382 g_return_if_fail (!SOURCE_DESTROYED (child_source));
1384 context = source->context;
1387 LOCK_CONTEXT (context);
1389 g_child_source_remove_internal (child_source, context);
1392 UNLOCK_CONTEXT (context);
1396 * g_source_set_callback_indirect:
1397 * @source: the source
1398 * @callback_data: pointer to callback data "object"
1399 * @callback_funcs: functions for reference counting @callback_data
1400 * and getting the callback and data
1402 * Sets the callback function storing the data as a refcounted callback
1403 * "object". This is used internally. Note that calling
1404 * g_source_set_callback_indirect() assumes
1405 * an initial reference count on @callback_data, and thus
1406 * @callback_funcs->unref will eventually be called once more
1407 * than @callback_funcs->ref.
1410 g_source_set_callback_indirect (GSource *source,
1411 gpointer callback_data,
1412 GSourceCallbackFuncs *callback_funcs)
1414 GMainContext *context;
1415 gpointer old_cb_data;
1416 GSourceCallbackFuncs *old_cb_funcs;
1418 g_return_if_fail (source != NULL);
1419 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1421 context = source->context;
1424 LOCK_CONTEXT (context);
1426 old_cb_data = source->callback_data;
1427 old_cb_funcs = source->callback_funcs;
1429 source->callback_data = callback_data;
1430 source->callback_funcs = callback_funcs;
1433 UNLOCK_CONTEXT (context);
1436 old_cb_funcs->unref (old_cb_data);
1440 g_source_callback_ref (gpointer cb_data)
1442 GSourceCallback *callback = cb_data;
1444 callback->ref_count++;
1449 g_source_callback_unref (gpointer cb_data)
1451 GSourceCallback *callback = cb_data;
1453 callback->ref_count--;
1454 if (callback->ref_count == 0)
1456 if (callback->notify)
1457 callback->notify (callback->data);
1463 g_source_callback_get (gpointer cb_data,
1468 GSourceCallback *callback = cb_data;
1470 *func = callback->func;
1471 *data = callback->data;
1474 static GSourceCallbackFuncs g_source_callback_funcs = {
1475 g_source_callback_ref,
1476 g_source_callback_unref,
1477 g_source_callback_get,
1481 * g_source_set_callback:
1482 * @source: the source
1483 * @func: a callback function
1484 * @data: the data to pass to callback function
1485 * @notify: (allow-none): a function to call when @data is no longer in use, or %NULL.
1487 * Sets the callback function for a source. The callback for a source is
1488 * called from the source's dispatch function.
1490 * The exact type of @func depends on the type of source; ie. you
1491 * should not count on @func being called with @data as its first
1494 * Typically, you won't use this function. Instead use functions specific
1495 * to the type of source you are using.
1498 g_source_set_callback (GSource *source,
1501 GDestroyNotify notify)
1503 GSourceCallback *new_callback;
1505 g_return_if_fail (source != NULL);
1507 new_callback = g_new (GSourceCallback, 1);
1509 new_callback->ref_count = 1;
1510 new_callback->func = func;
1511 new_callback->data = data;
1512 new_callback->notify = notify;
1514 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1519 * g_source_set_funcs:
1520 * @source: a #GSource
1521 * @funcs: the new #GSourceFuncs
1523 * Sets the source functions (can be used to override
1524 * default implementations) of an unattached source.
1529 g_source_set_funcs (GSource *source,
1530 GSourceFuncs *funcs)
1532 g_return_if_fail (source != NULL);
1533 g_return_if_fail (source->context == NULL);
1534 g_return_if_fail (source->ref_count > 0);
1535 g_return_if_fail (funcs != NULL);
1537 source->source_funcs = funcs;
1541 g_source_set_priority_unlocked (GSource *source,
1542 GMainContext *context,
1547 g_return_if_fail (source->priv->parent_source == NULL ||
1548 source->priv->parent_source->priority == priority);
1552 /* Remove the source from the context's source and then
1553 * add it back after so it is sorted in the correct place
1555 source_remove_from_context (source, source->context);
1558 source->priority = priority;
1562 source_add_to_context (source, source->context);
1564 if (!SOURCE_BLOCKED (source))
1566 tmp_list = source->poll_fds;
1569 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1570 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1572 tmp_list = tmp_list->next;
1577 if (source->priv->child_sources)
1579 tmp_list = source->priv->child_sources;
1582 g_source_set_priority_unlocked (tmp_list->data, context, priority);
1583 tmp_list = tmp_list->next;
1589 * g_source_set_priority:
1590 * @source: a #GSource
1591 * @priority: the new priority.
1593 * Sets the priority of a source. While the main loop is being run, a
1594 * source will be dispatched if it is ready to be dispatched and no
1595 * sources at a higher (numerically smaller) priority are ready to be
1599 g_source_set_priority (GSource *source,
1602 GMainContext *context;
1604 g_return_if_fail (source != NULL);
1606 context = source->context;
1609 LOCK_CONTEXT (context);
1610 g_source_set_priority_unlocked (source, context, priority);
1612 UNLOCK_CONTEXT (source->context);
1616 * g_source_get_priority:
1617 * @source: a #GSource
1619 * Gets the priority of a source.
1621 * Return value: the priority of the source
1624 g_source_get_priority (GSource *source)
1626 g_return_val_if_fail (source != NULL, 0);
1628 return source->priority;
1632 * g_source_set_can_recurse:
1633 * @source: a #GSource
1634 * @can_recurse: whether recursion is allowed for this source
1636 * Sets whether a source can be called recursively. If @can_recurse is
1637 * %TRUE, then while the source is being dispatched then this source
1638 * will be processed normally. Otherwise, all processing of this
1639 * source is blocked until the dispatch function returns.
1642 g_source_set_can_recurse (GSource *source,
1643 gboolean can_recurse)
1645 GMainContext *context;
1647 g_return_if_fail (source != NULL);
1649 context = source->context;
1652 LOCK_CONTEXT (context);
1655 source->flags |= G_SOURCE_CAN_RECURSE;
1657 source->flags &= ~G_SOURCE_CAN_RECURSE;
1660 UNLOCK_CONTEXT (context);
1664 * g_source_get_can_recurse:
1665 * @source: a #GSource
1667 * Checks whether a source is allowed to be called recursively.
1668 * see g_source_set_can_recurse().
1670 * Return value: whether recursion is allowed.
1673 g_source_get_can_recurse (GSource *source)
1675 g_return_val_if_fail (source != NULL, FALSE);
1677 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1682 * g_source_set_name:
1683 * @source: a #GSource
1684 * @name: debug name for the source
1686 * Sets a name for the source, used in debugging and profiling.
1687 * The name defaults to #NULL.
1689 * The source name should describe in a human-readable way
1690 * what the source does. For example, "X11 event queue"
1691 * or "GTK+ repaint idle handler" or whatever it is.
1693 * It is permitted to call this function multiple times, but is not
1694 * recommended due to the potential performance impact. For example,
1695 * one could change the name in the "check" function of a #GSourceFuncs
1696 * to include details like the event type in the source name.
1701 g_source_set_name (GSource *source,
1704 g_return_if_fail (source != NULL);
1706 /* setting back to NULL is allowed, just because it's
1707 * weird if get_name can return NULL but you can't
1711 g_free (source->name);
1712 source->name = g_strdup (name);
1716 * g_source_get_name:
1717 * @source: a #GSource
1719 * Gets a name for the source, used in debugging and profiling.
1720 * The name may be #NULL if it has never been set with
1721 * g_source_set_name().
1723 * Return value: the name of the source
1727 g_source_get_name (GSource *source)
1729 g_return_val_if_fail (source != NULL, NULL);
1731 return source->name;
1735 * g_source_set_name_by_id:
1736 * @tag: a #GSource ID
1737 * @name: debug name for the source
1739 * Sets the name of a source using its ID.
1741 * This is a convenience utility to set source names from the return
1742 * value of g_idle_add(), g_timeout_add(), etc.
1747 g_source_set_name_by_id (guint tag,
1752 g_return_if_fail (tag > 0);
1754 source = g_main_context_find_source_by_id (NULL, tag);
1758 g_source_set_name (source, name);
1764 * @source: a #GSource
1766 * Increases the reference count on a source by one.
1768 * Return value: @source
1771 g_source_ref (GSource *source)
1773 GMainContext *context;
1775 g_return_val_if_fail (source != NULL, NULL);
1777 context = source->context;
1780 LOCK_CONTEXT (context);
1782 source->ref_count++;
1785 UNLOCK_CONTEXT (context);
1790 /* g_source_unref() but possible to call within context lock
1793 g_source_unref_internal (GSource *source,
1794 GMainContext *context,
1797 gpointer old_cb_data = NULL;
1798 GSourceCallbackFuncs *old_cb_funcs = NULL;
1800 g_return_if_fail (source != NULL);
1802 if (!have_lock && context)
1803 LOCK_CONTEXT (context);
1805 source->ref_count--;
1806 if (source->ref_count == 0)
1808 old_cb_data = source->callback_data;
1809 old_cb_funcs = source->callback_funcs;
1811 source->callback_data = NULL;
1812 source->callback_funcs = NULL;
1816 if (!SOURCE_DESTROYED (source))
1817 g_warning (G_STRLOC ": ref_count == 0, but source was still attached to a context!");
1818 source_remove_from_context (source, context);
1821 if (source->source_funcs->finalize)
1824 UNLOCK_CONTEXT (context);
1825 source->source_funcs->finalize (source);
1827 LOCK_CONTEXT (context);
1830 g_free (source->name);
1831 source->name = NULL;
1833 g_slist_free (source->poll_fds);
1834 source->poll_fds = NULL;
1836 g_slice_free (GSourcePrivate, source->priv);
1837 source->priv = NULL;
1842 if (!have_lock && context)
1843 UNLOCK_CONTEXT (context);
1848 UNLOCK_CONTEXT (context);
1850 old_cb_funcs->unref (old_cb_data);
1853 LOCK_CONTEXT (context);
1859 * @source: a #GSource
1861 * Decreases the reference count of a source by one. If the
1862 * resulting reference count is zero the source and associated
1863 * memory will be destroyed.
1866 g_source_unref (GSource *source)
1868 g_return_if_fail (source != NULL);
1870 g_source_unref_internal (source, source->context, FALSE);
1874 * g_main_context_find_source_by_id:
1875 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used)
1876 * @source_id: the source ID, as returned by g_source_get_id().
1878 * Finds a #GSource given a pair of context and ID.
1880 * Return value: (transfer none): the #GSource if found, otherwise, %NULL
1883 g_main_context_find_source_by_id (GMainContext *context,
1889 g_return_val_if_fail (source_id > 0, NULL);
1891 if (context == NULL)
1892 context = g_main_context_default ();
1894 LOCK_CONTEXT (context);
1896 g_source_iter_init (&iter, context, FALSE);
1897 while (g_source_iter_next (&iter, &source))
1899 if (!SOURCE_DESTROYED (source) &&
1900 source->source_id == source_id)
1903 g_source_iter_clear (&iter);
1905 UNLOCK_CONTEXT (context);
1911 * g_main_context_find_source_by_funcs_user_data:
1912 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used).
1913 * @funcs: the @source_funcs passed to g_source_new().
1914 * @user_data: the user data from the callback.
1916 * Finds a source with the given source functions and user data. If
1917 * multiple sources exist with the same source function and user data,
1918 * the first one found will be returned.
1920 * Return value: (transfer none): the source, if one was found, otherwise %NULL
1923 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1924 GSourceFuncs *funcs,
1930 g_return_val_if_fail (funcs != NULL, NULL);
1932 if (context == NULL)
1933 context = g_main_context_default ();
1935 LOCK_CONTEXT (context);
1937 g_source_iter_init (&iter, context, FALSE);
1938 while (g_source_iter_next (&iter, &source))
1940 if (!SOURCE_DESTROYED (source) &&
1941 source->source_funcs == funcs &&
1942 source->callback_funcs)
1944 GSourceFunc callback;
1945 gpointer callback_data;
1947 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1949 if (callback_data == user_data)
1953 g_source_iter_clear (&iter);
1955 UNLOCK_CONTEXT (context);
1961 * g_main_context_find_source_by_user_data:
1962 * @context: a #GMainContext
1963 * @user_data: the user_data for the callback.
1965 * Finds a source with the given user data for the callback. If
1966 * multiple sources exist with the same user data, the first
1967 * one found will be returned.
1969 * Return value: (transfer none): the source, if one was found, otherwise %NULL
1972 g_main_context_find_source_by_user_data (GMainContext *context,
1978 if (context == NULL)
1979 context = g_main_context_default ();
1981 LOCK_CONTEXT (context);
1983 g_source_iter_init (&iter, context, FALSE);
1984 while (g_source_iter_next (&iter, &source))
1986 if (!SOURCE_DESTROYED (source) &&
1987 source->callback_funcs)
1989 GSourceFunc callback;
1990 gpointer callback_data = NULL;
1992 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1994 if (callback_data == user_data)
1998 g_source_iter_clear (&iter);
2000 UNLOCK_CONTEXT (context);
2007 * @tag: the ID of the source to remove.
2009 * Removes the source with the given id from the default main context.
2011 * a #GSource is given by g_source_get_id(), or will be returned by the
2012 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
2013 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
2014 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
2016 * See also g_source_destroy(). You must use g_source_destroy() for sources
2017 * added to a non-default main context.
2019 * Return value: %TRUE if the source was found and removed.
2022 g_source_remove (guint tag)
2026 g_return_val_if_fail (tag > 0, FALSE);
2028 source = g_main_context_find_source_by_id (NULL, tag);
2030 g_source_destroy (source);
2032 return source != NULL;
2036 * g_source_remove_by_user_data:
2037 * @user_data: the user_data for the callback.
2039 * Removes a source from the default main loop context given the user
2040 * data for the callback. If multiple sources exist with the same user
2041 * data, only one will be destroyed.
2043 * Return value: %TRUE if a source was found and removed.
2046 g_source_remove_by_user_data (gpointer user_data)
2050 source = g_main_context_find_source_by_user_data (NULL, user_data);
2053 g_source_destroy (source);
2061 * g_source_remove_by_funcs_user_data:
2062 * @funcs: The @source_funcs passed to g_source_new()
2063 * @user_data: the user data for the callback
2065 * Removes a source from the default main loop context given the
2066 * source functions and user data. If multiple sources exist with the
2067 * same source functions and user data, only one will be destroyed.
2069 * Return value: %TRUE if a source was found and removed.
2072 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
2077 g_return_val_if_fail (funcs != NULL, FALSE);
2079 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
2082 g_source_destroy (source);
2090 * g_get_current_time:
2091 * @result: #GTimeVal structure in which to store current time.
2093 * Equivalent to the UNIX gettimeofday() function, but portable.
2095 * You may find g_get_real_time() to be more convenient.
2098 g_get_current_time (GTimeVal *result)
2103 g_return_if_fail (result != NULL);
2105 /*this is required on alpha, there the timeval structs are int's
2106 not longs and a cast only would fail horribly*/
2107 gettimeofday (&r, NULL);
2108 result->tv_sec = r.tv_sec;
2109 result->tv_usec = r.tv_usec;
2114 g_return_if_fail (result != NULL);
2116 GetSystemTimeAsFileTime (&ft);
2117 memmove (&time64, &ft, sizeof (FILETIME));
2119 /* Convert from 100s of nanoseconds since 1601-01-01
2120 * to Unix epoch. Yes, this is Y2038 unsafe.
2122 time64 -= G_GINT64_CONSTANT (116444736000000000);
2125 result->tv_sec = time64 / 1000000;
2126 result->tv_usec = time64 % 1000000;
2133 * Queries the system wall-clock time.
2135 * This call is functionally equivalent to g_get_current_time() except
2136 * that the return value is often more convenient than dealing with a
2139 * You should only use this call if you are actually interested in the real
2140 * wall-clock time. g_get_monotonic_time() is probably more useful for
2141 * measuring intervals.
2143 * Returns: the number of microseconds since January 1, 1970 UTC.
2148 g_get_real_time (void)
2152 g_get_current_time (&tv);
2154 return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec;
2158 static ULONGLONG (*g_GetTickCount64) (void) = NULL;
2159 static guint32 g_win32_tick_epoch = 0;
2161 G_GNUC_INTERNAL void
2162 g_clock_win32_init (void)
2166 g_GetTickCount64 = NULL;
2167 kernel32 = GetModuleHandle ("KERNEL32.DLL");
2168 if (kernel32 != NULL)
2169 g_GetTickCount64 = (void *) GetProcAddress (kernel32, "GetTickCount64");
2170 g_win32_tick_epoch = ((guint32)GetTickCount()) >> 31;
2175 * g_get_monotonic_time:
2177 * Queries the system monotonic time, if available.
2179 * On POSIX systems with clock_gettime() and <literal>CLOCK_MONOTONIC</literal> this call
2180 * is a very shallow wrapper for that. Otherwise, we make a best effort
2181 * that probably involves returning the wall clock time (with at least
2182 * microsecond accuracy, subject to the limitations of the OS kernel).
2184 * It's important to note that POSIX <literal>CLOCK_MONOTONIC</literal> does
2185 * not count time spent while the machine is suspended.
2187 * On Windows, "limitations of the OS kernel" is a rather substantial
2188 * statement. Depending on the configuration of the system, the wall
2189 * clock time is updated as infrequently as 64 times a second (which
2190 * is approximately every 16ms). Also, on XP (but not on Vista or later)
2191 * the monotonic clock is locally monotonic, but may differ in exact
2192 * value between processes due to timer wrap handling.
2194 * Returns: the monotonic time, in microseconds
2199 g_get_monotonic_time (void)
2201 #ifdef HAVE_CLOCK_GETTIME
2202 /* librt clock_gettime() is our first choice */
2205 #ifdef CLOCK_MONOTONIC
2206 clock_gettime (CLOCK_MONOTONIC, &ts);
2208 clock_gettime (CLOCK_REALTIME, &ts);
2211 /* In theory monotonic time can have any epoch.
2213 * glib presently assumes the following:
2215 * 1) The epoch comes some time after the birth of Jesus of Nazareth, but
2216 * not more than 10000 years later.
2218 * 2) The current time also falls sometime within this range.
2220 * These two reasonable assumptions leave us with a maximum deviation from
2221 * the epoch of 10000 years, or 315569520000000000 seconds.
2223 * If we restrict ourselves to this range then the number of microseconds
2224 * will always fit well inside the constraints of a int64 (by a factor of
2227 * If you actually hit the following assertion, probably you should file a
2228 * bug against your operating system for being excessively silly.
2230 g_assert (G_GINT64_CONSTANT (-315569520000000000) < ts.tv_sec &&
2231 ts.tv_sec < G_GINT64_CONSTANT (315569520000000000));
2233 return (((gint64) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
2235 #elif defined (G_OS_WIN32)
2239 /* There are four sources for the monotonic time on Windows:
2241 * Three are based on a (1 msec accuracy, but only read periodically) clock chip:
2242 * - GetTickCount (GTC)
2243 * 32bit msec counter, updated each ~15msec, wraps in ~50 days
2244 * - GetTickCount64 (GTC64)
2245 * Same as GetTickCount, but extended to 64bit, so no wrap
2246 * Only available in Vista or later
2247 * - timeGetTime (TGT)
2248 * similar to GetTickCount by default: 15msec, 50 day wrap.
2249 * available in winmm.dll (thus known as the multimedia timers)
2250 * However apps can raise the system timer clock frequency using timeBeginPeriod()
2251 * increasing the accuracy up to 1 msec, at a cost in general system performance
2254 * One is based on high precision clocks:
2255 * - QueryPrecisionCounter (QPC)
2256 * This has much higher accuracy, but is not guaranteed monotonic, and
2257 * has lots of complications like clock jumps and different times on different
2258 * CPUs. It also has lower long term accuracy (i.e. it will drift compared to
2259 * the low precision clocks.
2261 * Additionally, the precision available in the timer-based wakeup such as
2262 * MsgWaitForMultipleObjectsEx (which is what the mainloop is based on) is based
2263 * on the TGT resolution, so by default it is ~15msec, but can be increased by apps.
2265 * The QPC timer has too many issues to be used as is. The only way it could be used
2266 * is to use it to interpolate the lower precision clocks. Firefox does something like
2268 * https://bugzilla.mozilla.org/show_bug.cgi?id=363258
2270 * However this seems quite complicated, so we're not doing this right now.
2272 * The approach we take instead is to use the TGT timer, extending it to 64bit
2273 * either by using the GTC64 value, or if that is not available, a process local
2274 * time epoch that we increment when we detect a timer wrap (assumes that we read
2275 * the time at least once every 50 days).
2278 * - We have a globally consistent monotonic clock on Vista and later
2279 * - We have a locally monotonic clock on XP
2280 * - Apps that need higher precision in timeouts and clock reads can call
2281 * timeBeginPeriod() to increase it as much as they want
2284 if (g_GetTickCount64 != NULL)
2286 guint32 ticks_as_32bit;
2288 ticks = g_GetTickCount64 ();
2289 ticks32 = timeGetTime();
2291 /* GTC64 and TGT are sampled at different times, however they
2292 * have the same base and source (msecs since system boot).
2293 * They can differ by as much as -16 to +16 msecs.
2294 * We can't just inject the low bits into the 64bit counter
2295 * as one of the counters can have wrapped in 32bit space and
2296 * the other not. Instead we calculate the signed difference
2297 * in 32bit space and apply that difference to the 64bit counter.
2299 ticks_as_32bit = (guint32)ticks;
2301 /* We could do some 2's complement hack, but we play it safe */
2302 if (ticks32 - ticks_as_32bit <= G_MAXINT32)
2303 ticks += ticks32 - ticks_as_32bit;
2305 ticks -= ticks_as_32bit - ticks32;
2311 epoch = g_atomic_int_get (&g_win32_tick_epoch);
2313 /* Must read ticks after the epoch. Then we're guaranteed
2314 * that the ticks value we read is higher or equal to any
2315 * previous ones that lead to the writing of the epoch.
2317 ticks32 = timeGetTime();
2319 /* We store the MSB of the current time as the LSB
2320 * of the epoch. Comparing these bits lets us detect when
2321 * the 32bit counter has wrapped so we can increase the
2324 * This will work as long as this function is called at
2325 * least once every ~24 days, which is half the wrap time
2326 * of a 32bit msec counter. I think this is pretty likely.
2328 * Note that g_win32_tick_epoch is a process local state,
2329 * so the monotonic clock will not be the same between
2332 if ((ticks32 >> 31) != (epoch & 1))
2335 g_atomic_int_set (&g_win32_tick_epoch, epoch);
2339 ticks = (guint64)ticks32 | ((guint64)epoch) << 31;
2342 return ticks * 1000;
2344 #else /* !HAVE_CLOCK_GETTIME && ! G_OS_WIN32*/
2348 g_get_current_time (&tv);
2350 return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec;
2355 g_main_dispatch_free (gpointer dispatch)
2357 g_slice_free (GMainDispatch, dispatch);
2360 /* Running the main loop */
2362 static GMainDispatch *
2365 static GPrivate depth_private = G_PRIVATE_INIT (g_main_dispatch_free);
2366 GMainDispatch *dispatch;
2368 dispatch = g_private_get (&depth_private);
2372 dispatch = g_slice_new0 (GMainDispatch);
2373 g_private_set (&depth_private, dispatch);
2382 * Returns the depth of the stack of calls to
2383 * g_main_context_dispatch() on any #GMainContext in the current thread.
2384 * That is, when called from the toplevel, it gives 0. When
2385 * called from within a callback from g_main_context_iteration()
2386 * (or g_main_loop_run(), etc.) it returns 1. When called from within
2387 * a callback to a recursive call to g_main_context_iteration(),
2388 * it returns 2. And so forth.
2390 * This function is useful in a situation like the following:
2391 * Imagine an extremely simple "garbage collected" system.
2394 * static GList *free_list;
2397 * allocate_memory (gsize size)
2399 * gpointer result = g_malloc (size);
2400 * free_list = g_list_prepend (free_list, result);
2405 * free_allocated_memory (void)
2408 * for (l = free_list; l; l = l->next);
2410 * g_list_free (free_list);
2418 * g_main_context_iteration (NULL, TRUE);
2419 * free_allocated_memory();
2423 * This works from an application, however, if you want to do the same
2424 * thing from a library, it gets more difficult, since you no longer
2425 * control the main loop. You might think you can simply use an idle
2426 * function to make the call to free_allocated_memory(), but that
2427 * doesn't work, since the idle function could be called from a
2428 * recursive callback. This can be fixed by using g_main_depth()
2432 * allocate_memory (gsize size)
2434 * FreeListBlock *block = g_new (FreeListBlock, 1);
2435 * block->mem = g_malloc (size);
2436 * block->depth = g_main_depth ();
2437 * free_list = g_list_prepend (free_list, block);
2438 * return block->mem;
2442 * free_allocated_memory (void)
2446 * int depth = g_main_depth ();
2447 * for (l = free_list; l; );
2449 * GList *next = l->next;
2450 * FreeListBlock *block = l->data;
2451 * if (block->depth > depth)
2453 * g_free (block->mem);
2455 * free_list = g_list_delete_link (free_list, l);
2463 * There is a temptation to use g_main_depth() to solve
2464 * problems with reentrancy. For instance, while waiting for data
2465 * to be received from the network in response to a menu item,
2466 * the menu item might be selected again. It might seem that
2467 * one could make the menu item's callback return immediately
2468 * and do nothing if g_main_depth() returns a value greater than 1.
2469 * However, this should be avoided since the user then sees selecting
2470 * the menu item do nothing. Furthermore, you'll find yourself adding
2471 * these checks all over your code, since there are doubtless many,
2472 * many things that the user could do. Instead, you can use the
2473 * following techniques:
2478 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
2479 * the user from interacting with elements while the main
2480 * loop is recursing.
2485 * Avoid main loop recursion in situations where you can't handle
2486 * arbitrary callbacks. Instead, structure your code so that you
2487 * simply return to the main loop and then get called again when
2488 * there is more work to do.
2493 * Return value: The main loop recursion level in the current thread
2498 GMainDispatch *dispatch = get_dispatch ();
2499 return dispatch->depth;
2503 * g_main_current_source:
2505 * Returns the currently firing source for this thread.
2507 * Return value: (transfer none): The currently firing source or %NULL.
2512 g_main_current_source (void)
2514 GMainDispatch *dispatch = get_dispatch ();
2515 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
2519 * g_source_is_destroyed:
2520 * @source: a #GSource
2522 * Returns whether @source has been destroyed.
2524 * This is important when you operate upon your objects
2525 * from within idle handlers, but may have freed the object
2526 * before the dispatch of your idle handler.
2530 * idle_callback (gpointer data)
2532 * SomeWidget *self = data;
2534 * GDK_THREADS_ENTER (<!-- -->);
2535 * /<!-- -->* do stuff with self *<!-- -->/
2536 * self->idle_id = 0;
2537 * GDK_THREADS_LEAVE (<!-- -->);
2539 * return G_SOURCE_REMOVE;
2543 * some_widget_do_stuff_later (SomeWidget *self)
2545 * self->idle_id = g_idle_add (idle_callback, self);
2549 * some_widget_finalize (GObject *object)
2551 * SomeWidget *self = SOME_WIDGET (object);
2553 * if (self->idle_id)
2554 * g_source_remove (self->idle_id);
2556 * G_OBJECT_CLASS (parent_class)->finalize (object);
2560 * This will fail in a multi-threaded application if the
2561 * widget is destroyed before the idle handler fires due
2562 * to the use after free in the callback. A solution, to
2563 * this particular problem, is to check to if the source
2564 * has already been destroy within the callback.
2568 * idle_callback (gpointer data)
2570 * SomeWidget *self = data;
2572 * GDK_THREADS_ENTER ();
2573 * if (!g_source_is_destroyed (g_main_current_source ()))
2575 * /<!-- -->* do stuff with self *<!-- -->/
2577 * GDK_THREADS_LEAVE ();
2583 * Return value: %TRUE if the source has been destroyed
2588 g_source_is_destroyed (GSource *source)
2590 return SOURCE_DESTROYED (source);
2593 /* Temporarily remove all this source's file descriptors from the
2594 * poll(), so that if data comes available for one of the file descriptors
2595 * we don't continually spin in the poll()
2597 /* HOLDS: source->context's lock */
2599 block_source (GSource *source)
2603 g_return_if_fail (!SOURCE_BLOCKED (source));
2605 source->flags |= G_SOURCE_BLOCKED;
2607 tmp_list = source->poll_fds;
2610 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
2611 tmp_list = tmp_list->next;
2614 if (source->priv && source->priv->child_sources)
2616 tmp_list = source->priv->child_sources;
2619 block_source (tmp_list->data);
2620 tmp_list = tmp_list->next;
2625 /* HOLDS: source->context's lock */
2627 unblock_source (GSource *source)
2631 g_return_if_fail (SOURCE_BLOCKED (source)); /* Source already unblocked */
2632 g_return_if_fail (!SOURCE_DESTROYED (source));
2634 source->flags &= ~G_SOURCE_BLOCKED;
2636 tmp_list = source->poll_fds;
2639 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
2640 tmp_list = tmp_list->next;
2643 if (source->priv && source->priv->child_sources)
2645 tmp_list = source->priv->child_sources;
2648 unblock_source (tmp_list->data);
2649 tmp_list = tmp_list->next;
2654 /* HOLDS: context's lock */
2656 g_main_dispatch (GMainContext *context)
2658 GMainDispatch *current = get_dispatch ();
2661 for (i = 0; i < context->pending_dispatches->len; i++)
2663 GSource *source = context->pending_dispatches->pdata[i];
2665 context->pending_dispatches->pdata[i] = NULL;
2668 source->flags &= ~G_SOURCE_READY;
2670 if (!SOURCE_DESTROYED (source))
2672 gboolean was_in_call;
2673 gpointer user_data = NULL;
2674 GSourceFunc callback = NULL;
2675 GSourceCallbackFuncs *cb_funcs;
2677 gboolean need_destroy;
2679 gboolean (*dispatch) (GSource *,
2682 GSList current_source_link;
2684 dispatch = source->source_funcs->dispatch;
2685 cb_funcs = source->callback_funcs;
2686 cb_data = source->callback_data;
2689 cb_funcs->ref (cb_data);
2691 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2692 block_source (source);
2694 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2695 source->flags |= G_HOOK_FLAG_IN_CALL;
2698 cb_funcs->get (cb_data, source, &callback, &user_data);
2700 UNLOCK_CONTEXT (context);
2703 /* The on-stack allocation of the GSList is unconventional, but
2704 * we know that the lifetime of the link is bounded to this
2705 * function as the link is kept in a thread specific list and
2706 * not manipulated outside of this function and its descendants.
2707 * Avoiding the overhead of a g_slist_alloc() is useful as many
2708 * applications do little more than dispatch events.
2710 * This is a performance hack - do not revert to g_slist_prepend()!
2712 current_source_link.data = source;
2713 current_source_link.next = current->dispatching_sources;
2714 current->dispatching_sources = ¤t_source_link;
2715 need_destroy = ! dispatch (source,
2718 g_assert (current->dispatching_sources == ¤t_source_link);
2719 current->dispatching_sources = current_source_link.next;
2723 cb_funcs->unref (cb_data);
2725 LOCK_CONTEXT (context);
2728 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2730 if (SOURCE_BLOCKED (source) && !SOURCE_DESTROYED (source))
2731 unblock_source (source);
2733 /* Note: this depends on the fact that we can't switch
2734 * sources from one main context to another
2736 if (need_destroy && !SOURCE_DESTROYED (source))
2738 g_assert (source->context == context);
2739 g_source_destroy_internal (source, context, TRUE);
2743 SOURCE_UNREF (source, context);
2746 g_ptr_array_set_size (context->pending_dispatches, 0);
2750 * g_main_context_acquire:
2751 * @context: a #GMainContext
2753 * Tries to become the owner of the specified context.
2754 * If some other thread is the owner of the context,
2755 * returns %FALSE immediately. Ownership is properly
2756 * recursive: the owner can require ownership again
2757 * and will release ownership when g_main_context_release()
2758 * is called as many times as g_main_context_acquire().
2760 * You must be the owner of a context before you
2761 * can call g_main_context_prepare(), g_main_context_query(),
2762 * g_main_context_check(), g_main_context_dispatch().
2764 * Return value: %TRUE if the operation succeeded, and
2765 * this thread is now the owner of @context.
2768 g_main_context_acquire (GMainContext *context)
2770 gboolean result = FALSE;
2771 GThread *self = G_THREAD_SELF;
2773 if (context == NULL)
2774 context = g_main_context_default ();
2776 LOCK_CONTEXT (context);
2778 if (!context->owner)
2780 context->owner = self;
2781 g_assert (context->owner_count == 0);
2784 if (context->owner == self)
2786 context->owner_count++;
2790 UNLOCK_CONTEXT (context);
2796 * g_main_context_release:
2797 * @context: a #GMainContext
2799 * Releases ownership of a context previously acquired by this thread
2800 * with g_main_context_acquire(). If the context was acquired multiple
2801 * times, the ownership will be released only when g_main_context_release()
2802 * is called as many times as it was acquired.
2805 g_main_context_release (GMainContext *context)
2807 if (context == NULL)
2808 context = g_main_context_default ();
2810 LOCK_CONTEXT (context);
2812 context->owner_count--;
2813 if (context->owner_count == 0)
2815 context->owner = NULL;
2817 if (context->waiters)
2819 GMainWaiter *waiter = context->waiters->data;
2820 gboolean loop_internal_waiter = (waiter->mutex == &context->mutex);
2821 context->waiters = g_slist_delete_link (context->waiters,
2823 if (!loop_internal_waiter)
2824 g_mutex_lock (waiter->mutex);
2826 g_cond_signal (waiter->cond);
2828 if (!loop_internal_waiter)
2829 g_mutex_unlock (waiter->mutex);
2833 UNLOCK_CONTEXT (context);
2837 * g_main_context_wait:
2838 * @context: a #GMainContext
2839 * @cond: a condition variable
2840 * @mutex: a mutex, currently held
2842 * Tries to become the owner of the specified context,
2843 * as with g_main_context_acquire(). But if another thread
2844 * is the owner, atomically drop @mutex and wait on @cond until
2845 * that owner releases ownership or until @cond is signaled, then
2846 * try again (once) to become the owner.
2848 * Return value: %TRUE if the operation succeeded, and
2849 * this thread is now the owner of @context.
2852 g_main_context_wait (GMainContext *context,
2856 gboolean result = FALSE;
2857 GThread *self = G_THREAD_SELF;
2858 gboolean loop_internal_waiter;
2860 if (context == NULL)
2861 context = g_main_context_default ();
2863 loop_internal_waiter = (mutex == &context->mutex);
2865 if (!loop_internal_waiter)
2866 LOCK_CONTEXT (context);
2868 if (context->owner && context->owner != self)
2873 waiter.mutex = mutex;
2875 context->waiters = g_slist_append (context->waiters, &waiter);
2877 if (!loop_internal_waiter)
2878 UNLOCK_CONTEXT (context);
2879 g_cond_wait (cond, mutex);
2880 if (!loop_internal_waiter)
2881 LOCK_CONTEXT (context);
2883 context->waiters = g_slist_remove (context->waiters, &waiter);
2886 if (!context->owner)
2888 context->owner = self;
2889 g_assert (context->owner_count == 0);
2892 if (context->owner == self)
2894 context->owner_count++;
2898 if (!loop_internal_waiter)
2899 UNLOCK_CONTEXT (context);
2905 * g_main_context_prepare:
2906 * @context: a #GMainContext
2907 * @priority: location to store priority of highest priority
2908 * source already ready.
2910 * Prepares to poll sources within a main loop. The resulting information
2911 * for polling is determined by calling g_main_context_query ().
2913 * Return value: %TRUE if some source is ready to be dispatched
2917 g_main_context_prepare (GMainContext *context,
2922 gint current_priority = G_MAXINT;
2926 if (context == NULL)
2927 context = g_main_context_default ();
2929 LOCK_CONTEXT (context);
2931 context->time_is_fresh = FALSE;
2933 if (context->in_check_or_prepare)
2935 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2936 "prepare() member.");
2937 UNLOCK_CONTEXT (context);
2942 /* If recursing, finish up current dispatch, before starting over */
2943 if (context->pending_dispatches)
2946 g_main_dispatch (context, ¤t_time);
2948 UNLOCK_CONTEXT (context);
2953 /* If recursing, clear list of pending dispatches */
2955 for (i = 0; i < context->pending_dispatches->len; i++)
2957 if (context->pending_dispatches->pdata[i])
2958 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2960 g_ptr_array_set_size (context->pending_dispatches, 0);
2962 /* Prepare all sources */
2964 context->timeout = -1;
2966 g_source_iter_init (&iter, context, TRUE);
2967 while (g_source_iter_next (&iter, &source))
2969 gint source_timeout = -1;
2971 if (SOURCE_DESTROYED (source) || SOURCE_BLOCKED (source))
2973 if ((n_ready > 0) && (source->priority > current_priority))
2976 if (!(source->flags & G_SOURCE_READY))
2979 gboolean (*prepare) (GSource *source,
2982 prepare = source->source_funcs->prepare;
2983 context->in_check_or_prepare++;
2984 UNLOCK_CONTEXT (context);
2986 result = (*prepare) (source, &source_timeout);
2988 LOCK_CONTEXT (context);
2989 context->in_check_or_prepare--;
2993 GSource *ready_source = source;
2995 while (ready_source)
2997 ready_source->flags |= G_SOURCE_READY;
2998 ready_source = ready_source->priv->parent_source;
3003 if (source->flags & G_SOURCE_READY)
3006 current_priority = source->priority;
3007 context->timeout = 0;
3010 if (source_timeout >= 0)
3012 if (context->timeout < 0)
3013 context->timeout = source_timeout;
3015 context->timeout = MIN (context->timeout, source_timeout);
3018 g_source_iter_clear (&iter);
3020 UNLOCK_CONTEXT (context);
3023 *priority = current_priority;
3025 return (n_ready > 0);
3029 * g_main_context_query:
3030 * @context: a #GMainContext
3031 * @max_priority: maximum priority source to check
3032 * @timeout_: (out): location to store timeout to be used in polling
3033 * @fds: (out caller-allocates) (array length=n_fds): location to
3034 * store #GPollFD records that need to be polled.
3035 * @n_fds: length of @fds.
3037 * Determines information necessary to poll this main loop.
3039 * Return value: the number of records actually stored in @fds,
3040 * or, if more than @n_fds records need to be stored, the number
3041 * of records that need to be stored.
3044 g_main_context_query (GMainContext *context,
3053 LOCK_CONTEXT (context);
3055 pollrec = context->poll_records;
3057 while (pollrec && max_priority >= pollrec->priority)
3059 /* We need to include entries with fd->events == 0 in the array because
3060 * otherwise if the application changes fd->events behind our back and
3061 * makes it non-zero, we'll be out of sync when we check the fds[] array.
3062 * (Changing fd->events after adding an FD wasn't an anticipated use of
3063 * this API, but it occurs in practice.) */
3066 fds[n_poll].fd = pollrec->fd->fd;
3067 /* In direct contradiction to the Unix98 spec, IRIX runs into
3068 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
3069 * flags in the events field of the pollfd while it should
3070 * just ignoring them. So we mask them out here.
3072 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
3073 fds[n_poll].revents = 0;
3076 pollrec = pollrec->next;
3080 context->poll_changed = FALSE;
3084 *timeout = context->timeout;
3086 context->time_is_fresh = FALSE;
3089 UNLOCK_CONTEXT (context);
3095 * g_main_context_check:
3096 * @context: a #GMainContext
3097 * @max_priority: the maximum numerical priority of sources to check
3098 * @fds: (array length=n_fds): array of #GPollFD's that was passed to
3099 * the last call to g_main_context_query()
3100 * @n_fds: return value of g_main_context_query()
3102 * Passes the results of polling back to the main loop.
3104 * Return value: %TRUE if some sources are ready to be dispatched.
3107 g_main_context_check (GMainContext *context,
3118 LOCK_CONTEXT (context);
3120 if (context->in_check_or_prepare)
3122 g_warning ("g_main_context_check() called recursively from within a source's check() or "
3123 "prepare() member.");
3124 UNLOCK_CONTEXT (context);
3128 if (context->wake_up_rec.revents)
3129 g_wakeup_acknowledge (context->wakeup);
3131 /* If the set of poll file descriptors changed, bail out
3132 * and let the main loop rerun
3134 if (context->poll_changed)
3136 UNLOCK_CONTEXT (context);
3140 pollrec = context->poll_records;
3144 if (pollrec->fd->events)
3145 pollrec->fd->revents = fds[i].revents;
3147 pollrec = pollrec->next;
3151 g_source_iter_init (&iter, context, TRUE);
3152 while (g_source_iter_next (&iter, &source))
3154 if (SOURCE_DESTROYED (source) || SOURCE_BLOCKED (source))
3156 if ((n_ready > 0) && (source->priority > max_priority))
3159 if (!(source->flags & G_SOURCE_READY))
3162 gboolean (*check) (GSource *source);
3164 check = source->source_funcs->check;
3166 context->in_check_or_prepare++;
3167 UNLOCK_CONTEXT (context);
3169 result = (*check) (source);
3171 LOCK_CONTEXT (context);
3172 context->in_check_or_prepare--;
3176 GSource *ready_source = source;
3178 while (ready_source)
3180 ready_source->flags |= G_SOURCE_READY;
3181 ready_source = ready_source->priv->parent_source;
3186 if (source->flags & G_SOURCE_READY)
3188 source->ref_count++;
3189 g_ptr_array_add (context->pending_dispatches, source);
3193 /* never dispatch sources with less priority than the first
3194 * one we choose to dispatch
3196 max_priority = source->priority;
3199 g_source_iter_clear (&iter);
3201 UNLOCK_CONTEXT (context);
3207 * g_main_context_dispatch:
3208 * @context: a #GMainContext
3210 * Dispatches all pending sources.
3213 g_main_context_dispatch (GMainContext *context)
3215 LOCK_CONTEXT (context);
3217 if (context->pending_dispatches->len > 0)
3219 g_main_dispatch (context);
3222 UNLOCK_CONTEXT (context);
3225 /* HOLDS context lock */
3227 g_main_context_iterate (GMainContext *context,
3234 gboolean some_ready;
3235 gint nfds, allocated_nfds;
3236 GPollFD *fds = NULL;
3238 UNLOCK_CONTEXT (context);
3240 if (!g_main_context_acquire (context))
3242 gboolean got_ownership;
3244 LOCK_CONTEXT (context);
3249 got_ownership = g_main_context_wait (context,
3257 LOCK_CONTEXT (context);
3259 if (!context->cached_poll_array)
3261 context->cached_poll_array_size = context->n_poll_records;
3262 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
3265 allocated_nfds = context->cached_poll_array_size;
3266 fds = context->cached_poll_array;
3268 UNLOCK_CONTEXT (context);
3270 g_main_context_prepare (context, &max_priority);
3272 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
3273 allocated_nfds)) > allocated_nfds)
3275 LOCK_CONTEXT (context);
3277 context->cached_poll_array_size = allocated_nfds = nfds;
3278 context->cached_poll_array = fds = g_new (GPollFD, nfds);
3279 UNLOCK_CONTEXT (context);
3285 g_main_context_poll (context, timeout, max_priority, fds, nfds);
3287 some_ready = g_main_context_check (context, max_priority, fds, nfds);
3290 g_main_context_dispatch (context);
3292 g_main_context_release (context);
3294 LOCK_CONTEXT (context);
3300 * g_main_context_pending:
3301 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used)
3303 * Checks if any sources have pending events for the given context.
3305 * Return value: %TRUE if events are pending.
3308 g_main_context_pending (GMainContext *context)
3313 context = g_main_context_default();
3315 LOCK_CONTEXT (context);
3316 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
3317 UNLOCK_CONTEXT (context);
3323 * g_main_context_iteration:
3324 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used)
3325 * @may_block: whether the call may block.
3327 * Runs a single iteration for the given main loop. This involves
3328 * checking to see if any event sources are ready to be processed,
3329 * then if no events sources are ready and @may_block is %TRUE, waiting
3330 * for a source to become ready, then dispatching the highest priority
3331 * events sources that are ready. Otherwise, if @may_block is %FALSE
3332 * sources are not waited to become ready, only those highest priority
3333 * events sources will be dispatched (if any), that are ready at this
3334 * given moment without further waiting.
3336 * Note that even when @may_block is %TRUE, it is still possible for
3337 * g_main_context_iteration() to return %FALSE, since the wait may
3338 * be interrupted for other reasons than an event source becoming ready.
3340 * Return value: %TRUE if events were dispatched.
3343 g_main_context_iteration (GMainContext *context, gboolean may_block)
3348 context = g_main_context_default();
3350 LOCK_CONTEXT (context);
3351 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
3352 UNLOCK_CONTEXT (context);
3359 * @context: (allow-none): a #GMainContext (if %NULL, the default context will be used).
3360 * @is_running: set to %TRUE to indicate that the loop is running. This
3361 * is not very important since calling g_main_loop_run() will set this to
3364 * Creates a new #GMainLoop structure.
3366 * Return value: a new #GMainLoop.
3369 g_main_loop_new (GMainContext *context,
3370 gboolean is_running)
3375 context = g_main_context_default();
3377 g_main_context_ref (context);
3379 loop = g_new0 (GMainLoop, 1);
3380 loop->context = context;
3381 loop->is_running = is_running != FALSE;
3382 loop->ref_count = 1;
3389 * @loop: a #GMainLoop
3391 * Increases the reference count on a #GMainLoop object by one.
3393 * Return value: @loop
3396 g_main_loop_ref (GMainLoop *loop)
3398 g_return_val_if_fail (loop != NULL, NULL);
3399 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
3401 g_atomic_int_inc (&loop->ref_count);
3407 * g_main_loop_unref:
3408 * @loop: a #GMainLoop
3410 * Decreases the reference count on a #GMainLoop object by one. If
3411 * the result is zero, free the loop and free all associated memory.
3414 g_main_loop_unref (GMainLoop *loop)
3416 g_return_if_fail (loop != NULL);
3417 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
3419 if (!g_atomic_int_dec_and_test (&loop->ref_count))
3422 g_main_context_unref (loop->context);
3428 * @loop: a #GMainLoop
3430 * Runs a main loop until g_main_loop_quit() is called on the loop.
3431 * If this is called for the thread of the loop's #GMainContext,
3432 * it will process events from the loop, otherwise it will
3436 g_main_loop_run (GMainLoop *loop)
3438 GThread *self = G_THREAD_SELF;
3440 g_return_if_fail (loop != NULL);
3441 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
3443 if (!g_main_context_acquire (loop->context))
3445 gboolean got_ownership = FALSE;
3447 /* Another thread owns this context */
3448 LOCK_CONTEXT (loop->context);
3450 g_atomic_int_inc (&loop->ref_count);
3452 if (!loop->is_running)
3453 loop->is_running = TRUE;
3455 while (loop->is_running && !got_ownership)
3456 got_ownership = g_main_context_wait (loop->context,
3457 &loop->context->cond,
3458 &loop->context->mutex);
3460 if (!loop->is_running)
3462 UNLOCK_CONTEXT (loop->context);
3464 g_main_context_release (loop->context);
3465 g_main_loop_unref (loop);
3469 g_assert (got_ownership);
3472 LOCK_CONTEXT (loop->context);
3474 if (loop->context->in_check_or_prepare)
3476 g_warning ("g_main_loop_run(): called recursively from within a source's "
3477 "check() or prepare() member, iteration not possible.");
3481 g_atomic_int_inc (&loop->ref_count);
3482 loop->is_running = TRUE;
3483 while (loop->is_running)
3484 g_main_context_iterate (loop->context, TRUE, TRUE, self);
3486 UNLOCK_CONTEXT (loop->context);
3488 g_main_context_release (loop->context);
3490 g_main_loop_unref (loop);
3495 * @loop: a #GMainLoop
3497 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
3498 * for the loop will return.
3500 * Note that sources that have already been dispatched when
3501 * g_main_loop_quit() is called will still be executed.
3504 g_main_loop_quit (GMainLoop *loop)
3506 g_return_if_fail (loop != NULL);
3507 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
3509 LOCK_CONTEXT (loop->context);
3510 loop->is_running = FALSE;
3511 g_wakeup_signal (loop->context->wakeup);
3513 g_cond_broadcast (&loop->context->cond);
3515 UNLOCK_CONTEXT (loop->context);
3519 * g_main_loop_is_running:
3520 * @loop: a #GMainLoop.
3522 * Checks to see if the main loop is currently being run via g_main_loop_run().
3524 * Return value: %TRUE if the mainloop is currently being run.
3527 g_main_loop_is_running (GMainLoop *loop)
3529 g_return_val_if_fail (loop != NULL, FALSE);
3530 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
3532 return loop->is_running;
3536 * g_main_loop_get_context:
3537 * @loop: a #GMainLoop.
3539 * Returns the #GMainContext of @loop.
3541 * Return value: (transfer none): the #GMainContext of @loop
3544 g_main_loop_get_context (GMainLoop *loop)
3546 g_return_val_if_fail (loop != NULL, NULL);
3547 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
3549 return loop->context;
3552 /* HOLDS: context's lock */
3554 g_main_context_poll (GMainContext *context,
3560 #ifdef G_MAIN_POLL_DEBUG
3566 GPollFunc poll_func;
3568 if (n_fds || timeout != 0)
3570 #ifdef G_MAIN_POLL_DEBUG
3571 if (_g_main_poll_debug)
3573 g_print ("polling context=%p n=%d timeout=%d\n",
3574 context, n_fds, timeout);
3575 poll_timer = g_timer_new ();
3579 LOCK_CONTEXT (context);
3581 poll_func = context->poll_func;
3583 UNLOCK_CONTEXT (context);
3584 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
3587 g_warning ("poll(2) failed due to: %s.",
3588 g_strerror (errno));
3590 /* If g_poll () returns -1, it has already called g_warning() */
3594 #ifdef G_MAIN_POLL_DEBUG
3595 if (_g_main_poll_debug)
3597 LOCK_CONTEXT (context);
3599 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3602 g_timer_elapsed (poll_timer, NULL));
3603 g_timer_destroy (poll_timer);
3604 pollrec = context->poll_records;
3606 while (pollrec != NULL)
3611 if (fds[i].fd == pollrec->fd->fd &&
3612 pollrec->fd->events &&
3615 g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd);
3616 if (fds[i].revents & G_IO_IN)
3618 if (fds[i].revents & G_IO_OUT)
3620 if (fds[i].revents & G_IO_PRI)
3622 if (fds[i].revents & G_IO_ERR)
3624 if (fds[i].revents & G_IO_HUP)
3626 if (fds[i].revents & G_IO_NVAL)
3632 pollrec = pollrec->next;
3636 UNLOCK_CONTEXT (context);
3639 } /* if (n_fds || timeout != 0) */
3643 * g_main_context_add_poll:
3644 * @context: (allow-none): a #GMainContext (or %NULL for the default context)
3645 * @fd: a #GPollFD structure holding information about a file
3646 * descriptor to watch.
3647 * @priority: the priority for this file descriptor which should be
3648 * the same as the priority used for g_source_attach() to ensure that the
3649 * file descriptor is polled whenever the results may be needed.
3651 * Adds a file descriptor to the set of file descriptors polled for
3652 * this context. This will very seldom be used directly. Instead
3653 * a typical event source will use g_source_add_poll() instead.
3656 g_main_context_add_poll (GMainContext *context,
3661 context = g_main_context_default ();
3663 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3664 g_return_if_fail (fd);
3666 LOCK_CONTEXT (context);
3667 g_main_context_add_poll_unlocked (context, priority, fd);
3668 UNLOCK_CONTEXT (context);
3671 /* HOLDS: main_loop_lock */
3673 g_main_context_add_poll_unlocked (GMainContext *context,
3677 GPollRec *prevrec, *nextrec;
3678 GPollRec *newrec = g_slice_new (GPollRec);
3680 /* This file descriptor may be checked before we ever poll */
3683 newrec->priority = priority;
3685 prevrec = context->poll_records_tail;
3687 while (prevrec && priority < prevrec->priority)
3690 prevrec = prevrec->prev;
3694 prevrec->next = newrec;
3696 context->poll_records = newrec;
3698 newrec->prev = prevrec;
3699 newrec->next = nextrec;
3702 nextrec->prev = newrec;
3704 context->poll_records_tail = newrec;
3706 context->n_poll_records++;
3708 context->poll_changed = TRUE;
3710 /* Now wake up the main loop if it is waiting in the poll() */
3711 g_wakeup_signal (context->wakeup);
3715 * g_main_context_remove_poll:
3716 * @context:a #GMainContext
3717 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3719 * Removes file descriptor from the set of file descriptors to be
3720 * polled for a particular context.
3723 g_main_context_remove_poll (GMainContext *context,
3727 context = g_main_context_default ();
3729 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3730 g_return_if_fail (fd);
3732 LOCK_CONTEXT (context);
3733 g_main_context_remove_poll_unlocked (context, fd);
3734 UNLOCK_CONTEXT (context);
3738 g_main_context_remove_poll_unlocked (GMainContext *context,
3741 GPollRec *pollrec, *prevrec, *nextrec;
3744 pollrec = context->poll_records;
3748 nextrec = pollrec->next;
3749 if (pollrec->fd == fd)
3751 if (prevrec != NULL)
3752 prevrec->next = nextrec;
3754 context->poll_records = nextrec;
3756 if (nextrec != NULL)
3757 nextrec->prev = prevrec;
3759 context->poll_records_tail = prevrec;
3761 g_slice_free (GPollRec, pollrec);
3763 context->n_poll_records--;
3770 context->poll_changed = TRUE;
3772 /* Now wake up the main loop if it is waiting in the poll() */
3773 g_wakeup_signal (context->wakeup);
3777 * g_source_get_current_time:
3778 * @source: a #GSource
3779 * @timeval: #GTimeVal structure in which to store current time.
3781 * This function ignores @source and is otherwise the same as
3782 * g_get_current_time().
3784 * Deprecated: 2.28: use g_source_get_time() instead
3787 g_source_get_current_time (GSource *source,
3790 g_get_current_time (timeval);
3794 * g_source_get_time:
3795 * @source: a #GSource
3797 * Gets the time to be used when checking this source. The advantage of
3798 * calling this function over calling g_get_monotonic_time() directly is
3799 * that when checking multiple sources, GLib can cache a single value
3800 * instead of having to repeatedly get the system monotonic time.
3802 * The time here is the system monotonic time, if available, or some
3803 * other reasonable alternative otherwise. See g_get_monotonic_time().
3805 * Returns: the monotonic time in microseconds
3810 g_source_get_time (GSource *source)
3812 GMainContext *context;
3815 g_return_val_if_fail (source->context != NULL, 0);
3817 context = source->context;
3819 LOCK_CONTEXT (context);
3821 if (!context->time_is_fresh)
3823 context->time = g_get_monotonic_time ();
3824 context->time_is_fresh = TRUE;
3827 result = context->time;
3829 UNLOCK_CONTEXT (context);
3835 * g_main_context_set_poll_func:
3836 * @context: a #GMainContext
3837 * @func: the function to call to poll all file descriptors
3839 * Sets the function to use to handle polling of file descriptors. It
3840 * will be used instead of the poll() system call
3841 * (or GLib's replacement function, which is used where
3842 * poll() isn't available).
3844 * This function could possibly be used to integrate the GLib event
3845 * loop with an external event loop.
3848 g_main_context_set_poll_func (GMainContext *context,
3852 context = g_main_context_default ();
3854 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3856 LOCK_CONTEXT (context);
3859 context->poll_func = func;
3861 context->poll_func = g_poll;
3863 UNLOCK_CONTEXT (context);
3867 * g_main_context_get_poll_func:
3868 * @context: a #GMainContext
3870 * Gets the poll function set by g_main_context_set_poll_func().
3872 * Return value: the poll function
3875 g_main_context_get_poll_func (GMainContext *context)
3880 context = g_main_context_default ();
3882 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3884 LOCK_CONTEXT (context);
3885 result = context->poll_func;
3886 UNLOCK_CONTEXT (context);
3892 * g_main_context_wakeup:
3893 * @context: a #GMainContext
3895 * If @context is currently waiting in a poll(), interrupt
3896 * the poll(), and continue the iteration process.
3899 g_main_context_wakeup (GMainContext *context)
3902 context = g_main_context_default ();
3904 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3906 g_wakeup_signal (context->wakeup);
3910 * g_main_context_is_owner:
3911 * @context: a #GMainContext
3913 * Determines whether this thread holds the (recursive)
3914 * ownership of this #GMainContext. This is useful to
3915 * know before waiting on another thread that may be
3916 * blocking to get ownership of @context.
3918 * Returns: %TRUE if current thread is owner of @context.
3923 g_main_context_is_owner (GMainContext *context)
3928 context = g_main_context_default ();
3930 LOCK_CONTEXT (context);
3931 is_owner = context->owner == G_THREAD_SELF;
3932 UNLOCK_CONTEXT (context);
3940 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3941 gint64 current_time)
3943 timeout_source->expiration = current_time +
3944 (guint64) timeout_source->interval * 1000;
3946 if (timeout_source->seconds)
3949 static gint timer_perturb = -1;
3951 if (timer_perturb == -1)
3954 * we want a per machine/session unique 'random' value; try the dbus
3955 * address first, that has a UUID in it. If there is no dbus, use the
3956 * hostname for hashing.
3958 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3959 if (!session_bus_address)
3960 session_bus_address = g_getenv ("HOSTNAME");
3961 if (session_bus_address)
3962 timer_perturb = ABS ((gint) g_str_hash (session_bus_address)) % 1000000;
3967 /* We want the microseconds part of the timeout to land on the
3968 * 'timer_perturb' mark, but we need to make sure we don't try to
3969 * set the timeout in the past. We do this by ensuring that we
3970 * always only *increase* the expiration time by adding a full
3971 * second in the case that the microsecond portion decreases.
3973 timeout_source->expiration -= timer_perturb;
3975 remainder = timeout_source->expiration % 1000000;
3976 if (remainder >= 1000000/4)
3977 timeout_source->expiration += 1000000;
3979 timeout_source->expiration -= remainder;
3980 timeout_source->expiration += timer_perturb;
3985 g_timeout_prepare (GSource *source,
3988 GTimeoutSource *timeout_source = (GTimeoutSource *) source;
3989 gint64 now = g_source_get_time (source);
3991 if (now < timeout_source->expiration)
3993 /* Round up to ensure that we don't try again too early */
3994 *timeout = (timeout_source->expiration - now + 999) / 1000;
4003 g_timeout_check (GSource *source)
4005 GTimeoutSource *timeout_source = (GTimeoutSource *) source;
4006 gint64 now = g_source_get_time (source);
4008 return timeout_source->expiration <= now;
4012 g_timeout_dispatch (GSource *source,
4013 GSourceFunc callback,
4016 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
4021 g_warning ("Timeout source dispatched without callback\n"
4022 "You must call g_source_set_callback().");
4026 again = callback (user_data);
4029 g_timeout_set_expiration (timeout_source, g_source_get_time (source));
4035 * g_timeout_source_new:
4036 * @interval: the timeout interval in milliseconds.
4038 * Creates a new timeout source.
4040 * The source will not initially be associated with any #GMainContext
4041 * and must be added to one with g_source_attach() before it will be
4044 * The interval given is in terms of monotonic time, not wall clock
4045 * time. See g_get_monotonic_time().
4047 * Return value: the newly-created timeout source
4050 g_timeout_source_new (guint interval)
4052 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
4053 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
4055 timeout_source->interval = interval;
4056 g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
4062 * g_timeout_source_new_seconds:
4063 * @interval: the timeout interval in seconds
4065 * Creates a new timeout source.
4067 * The source will not initially be associated with any #GMainContext
4068 * and must be added to one with g_source_attach() before it will be
4071 * The scheduling granularity/accuracy of this timeout source will be
4074 * The interval given in terms of monotonic time, not wall clock time.
4075 * See g_get_monotonic_time().
4077 * Return value: the newly-created timeout source
4082 g_timeout_source_new_seconds (guint interval)
4084 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
4085 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
4087 timeout_source->interval = 1000 * interval;
4088 timeout_source->seconds = TRUE;
4090 g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
4097 * g_timeout_add_full:
4098 * @priority: the priority of the timeout source. Typically this will be in
4099 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
4100 * @interval: the time between calls to the function, in milliseconds
4101 * (1/1000ths of a second)
4102 * @function: function to call
4103 * @data: data to pass to @function
4104 * @notify: (allow-none): function to call when the timeout is removed, or %NULL
4106 * Sets a function to be called at regular intervals, with the given
4107 * priority. The function is called repeatedly until it returns
4108 * %FALSE, at which point the timeout is automatically destroyed and
4109 * the function will not be called again. The @notify function is
4110 * called when the timeout is destroyed. The first call to the
4111 * function will be at the end of the first @interval.
4113 * Note that timeout functions may be delayed, due to the processing of other
4114 * event sources. Thus they should not be relied on for precise timing.
4115 * After each call to the timeout function, the time of the next
4116 * timeout is recalculated based on the current time and the given interval
4117 * (it does not try to 'catch up' time lost in delays).
4119 * This internally creates a main loop source using g_timeout_source_new()
4120 * and attaches it to the main loop context using g_source_attach(). You can
4121 * do these steps manually if you need greater control.
4123 * The interval given in terms of monotonic time, not wall clock time.
4124 * See g_get_monotonic_time().
4126 * Return value: the ID (greater than 0) of the event source.
4127 * Rename to: g_timeout_add
4130 g_timeout_add_full (gint priority,
4132 GSourceFunc function,
4134 GDestroyNotify notify)
4139 g_return_val_if_fail (function != NULL, 0);
4141 source = g_timeout_source_new (interval);
4143 if (priority != G_PRIORITY_DEFAULT)
4144 g_source_set_priority (source, priority);
4146 g_source_set_callback (source, function, data, notify);
4147 id = g_source_attach (source, NULL);
4148 g_source_unref (source);
4155 * @interval: the time between calls to the function, in milliseconds
4156 * (1/1000ths of a second)
4157 * @function: function to call
4158 * @data: data to pass to @function
4160 * Sets a function to be called at regular intervals, with the default
4161 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
4162 * until it returns %FALSE, at which point the timeout is automatically
4163 * destroyed and the function will not be called again. The first call
4164 * to the function will be at the end of the first @interval.
4166 * Note that timeout functions may be delayed, due to the processing of other
4167 * event sources. Thus they should not be relied on for precise timing.
4168 * After each call to the timeout function, the time of the next
4169 * timeout is recalculated based on the current time and the given interval
4170 * (it does not try to 'catch up' time lost in delays).
4172 * If you want to have a timer in the "seconds" range and do not care
4173 * about the exact time of the first call of the timer, use the
4174 * g_timeout_add_seconds() function; this function allows for more
4175 * optimizations and more efficient system power usage.
4177 * This internally creates a main loop source using g_timeout_source_new()
4178 * and attaches it to the main loop context using g_source_attach(). You can
4179 * do these steps manually if you need greater control.
4181 * The interval given is in terms of monotonic time, not wall clock
4182 * time. See g_get_monotonic_time().
4184 * Return value: the ID (greater than 0) of the event source.
4187 g_timeout_add (guint32 interval,
4188 GSourceFunc function,
4191 return g_timeout_add_full (G_PRIORITY_DEFAULT,
4192 interval, function, data, NULL);
4196 * g_timeout_add_seconds_full:
4197 * @priority: the priority of the timeout source. Typically this will be in
4198 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
4199 * @interval: the time between calls to the function, in seconds
4200 * @function: function to call
4201 * @data: data to pass to @function
4202 * @notify: (allow-none): function to call when the timeout is removed, or %NULL
4204 * Sets a function to be called at regular intervals, with @priority.
4205 * The function is called repeatedly until it returns %FALSE, at which
4206 * point the timeout is automatically destroyed and the function will
4207 * not be called again.
4209 * Unlike g_timeout_add(), this function operates at whole second granularity.
4210 * The initial starting point of the timer is determined by the implementation
4211 * and the implementation is expected to group multiple timers together so that
4212 * they fire all at the same time.
4213 * To allow this grouping, the @interval to the first timer is rounded
4214 * and can deviate up to one second from the specified interval.
4215 * Subsequent timer iterations will generally run at the specified interval.
4217 * Note that timeout functions may be delayed, due to the processing of other
4218 * event sources. Thus they should not be relied on for precise timing.
4219 * After each call to the timeout function, the time of the next
4220 * timeout is recalculated based on the current time and the given @interval
4222 * If you want timing more precise than whole seconds, use g_timeout_add()
4225 * The grouping of timers to fire at the same time results in a more power
4226 * and CPU efficient behavior so if your timer is in multiples of seconds
4227 * and you don't require the first timer exactly one second from now, the
4228 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
4230 * This internally creates a main loop source using
4231 * g_timeout_source_new_seconds() and attaches it to the main loop context
4232 * using g_source_attach(). You can do these steps manually if you need
4235 * The interval given is in terms of monotonic time, not wall clock
4236 * time. See g_get_monotonic_time().
4238 * Return value: the ID (greater than 0) of the event source.
4240 * Rename to: g_timeout_add_seconds
4244 g_timeout_add_seconds_full (gint priority,
4246 GSourceFunc function,
4248 GDestroyNotify notify)
4253 g_return_val_if_fail (function != NULL, 0);
4255 source = g_timeout_source_new_seconds (interval);
4257 if (priority != G_PRIORITY_DEFAULT)
4258 g_source_set_priority (source, priority);
4260 g_source_set_callback (source, function, data, notify);
4261 id = g_source_attach (source, NULL);
4262 g_source_unref (source);
4268 * g_timeout_add_seconds:
4269 * @interval: the time between calls to the function, in seconds
4270 * @function: function to call
4271 * @data: data to pass to @function
4273 * Sets a function to be called at regular intervals with the default
4274 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
4275 * it returns %FALSE, at which point the timeout is automatically destroyed
4276 * and the function will not be called again.
4278 * This internally creates a main loop source using
4279 * g_timeout_source_new_seconds() and attaches it to the main loop context
4280 * using g_source_attach(). You can do these steps manually if you need
4281 * greater control. Also see g_timeout_add_seconds_full().
4283 * Note that the first call of the timer may not be precise for timeouts
4284 * of one second. If you need finer precision and have such a timeout,
4285 * you may want to use g_timeout_add() instead.
4287 * The interval given is in terms of monotonic time, not wall clock
4288 * time. See g_get_monotonic_time().
4290 * Return value: the ID (greater than 0) of the event source.
4295 g_timeout_add_seconds (guint interval,
4296 GSourceFunc function,
4299 g_return_val_if_fail (function != NULL, 0);
4301 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
4304 /* Child watch functions */
4309 g_child_watch_prepare (GSource *source,
4317 g_child_watch_check (GSource *source)
4319 GChildWatchSource *child_watch_source;
4320 gboolean child_exited;
4322 child_watch_source = (GChildWatchSource *) source;
4324 child_exited = child_watch_source->poll.revents & G_IO_IN;
4331 * Note: We do _not_ check for the special value of STILL_ACTIVE
4332 * since we know that the process has exited and doing so runs into
4333 * problems if the child process "happens to return STILL_ACTIVE(259)"
4334 * as Microsoft's Platform SDK puts it.
4336 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
4338 gchar *emsg = g_win32_error_message (GetLastError ());
4339 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
4342 child_watch_source->child_status = -1;
4345 child_watch_source->child_status = child_status;
4348 return child_exited;
4352 g_child_watch_finalize (GSource *source)
4356 #else /* G_OS_WIN32 */
4359 wake_source (GSource *source)
4361 GMainContext *context;
4363 /* This should be thread-safe:
4365 * - if the source is currently being added to a context, that
4366 * context will be woken up anyway
4368 * - if the source is currently being destroyed, we simply need not
4371 * - the memory for the source will remain valid until after the
4372 * source finalize function was called (which would remove the
4373 * source from the global list which we are currently holding the
4376 * - the GMainContext will either be NULL or point to a live
4379 * - the GMainContext will remain valid since we hold the
4380 * main_context_list lock
4382 * Since we are holding a lot of locks here, don't try to enter any
4383 * more GMainContext functions for fear of dealock -- just hit the
4384 * GWakeup and run. Even if that's safe now, it could easily become
4385 * unsafe with some very minor changes in the future, and signal
4386 * handling is not the most well-tested codepath.
4388 G_LOCK(main_context_list);
4389 context = source->context;
4391 g_wakeup_signal (context->wakeup);
4392 G_UNLOCK(main_context_list);
4396 dispatch_unix_signals (void)
4400 /* clear this first incase another one arrives while we're processing */
4401 any_unix_signal_pending = FALSE;
4403 G_LOCK(unix_signal_lock);
4405 /* handle GChildWatchSource instances */
4406 if (unix_signal_pending[SIGCHLD])
4408 unix_signal_pending[SIGCHLD] = FALSE;
4410 /* The only way we can do this is to scan all of the children.
4412 * The docs promise that we will not reap children that we are not
4413 * explicitly watching, so that ties our hands from calling
4414 * waitpid(-1). We also can't use siginfo's si_pid field since if
4415 * multiple SIGCHLD arrive at the same time, one of them can be
4416 * dropped (since a given UNIX signal can only be pending once).
4418 for (node = unix_child_watches; node; node = node->next)
4420 GChildWatchSource *source = node->data;
4422 if (!source->child_exited)
4427 pid = waitpid (source->pid, &source->child_status, WNOHANG);
4430 source->child_exited = TRUE;
4431 wake_source ((GSource *) source);
4433 else if (pid == -1 && errno == ECHILD)
4435 g_warning ("GChildWatchSource: Exit status of a child process was requested but ECHILD was received by waitpid(). Most likely the process is ignoring SIGCHLD, or some other thread is invoking waitpid() with a nonpositive first argument; either behavior can break applications that use g_child_watch_add()/g_spawn_sync() either directly or indirectly.");
4436 source->child_exited = TRUE;
4437 source->child_status = 0;
4438 wake_source ((GSource *) source);
4441 while (pid == -1 && errno == EINTR);
4446 /* handle GUnixSignalWatchSource instances */
4447 for (node = unix_signal_watches; node; node = node->next)
4449 GUnixSignalWatchSource *source = node->data;
4451 if (!source->pending)
4453 if (unix_signal_pending[source->signum])
4455 unix_signal_pending[source->signum] = FALSE;
4456 source->pending = TRUE;
4458 wake_source ((GSource *) source);
4463 G_UNLOCK(unix_signal_lock);
4467 g_child_watch_prepare (GSource *source,
4470 GChildWatchSource *child_watch_source;
4472 child_watch_source = (GChildWatchSource *) source;
4474 return child_watch_source->child_exited;
4478 g_child_watch_check (GSource *source)
4480 GChildWatchSource *child_watch_source;
4482 child_watch_source = (GChildWatchSource *) source;
4484 return child_watch_source->child_exited;
4488 g_unix_signal_watch_prepare (GSource *source,
4491 GUnixSignalWatchSource *unix_signal_source;
4493 unix_signal_source = (GUnixSignalWatchSource *) source;
4495 return unix_signal_source->pending;
4499 g_unix_signal_watch_check (GSource *source)
4501 GUnixSignalWatchSource *unix_signal_source;
4503 unix_signal_source = (GUnixSignalWatchSource *) source;
4505 return unix_signal_source->pending;
4509 g_unix_signal_watch_dispatch (GSource *source,
4510 GSourceFunc callback,
4513 GUnixSignalWatchSource *unix_signal_source;
4516 unix_signal_source = (GUnixSignalWatchSource *) source;
4520 g_warning ("Unix signal source dispatched without callback\n"
4521 "You must call g_source_set_callback().");
4525 again = (callback) (user_data);
4527 unix_signal_source->pending = FALSE;
4533 ensure_unix_signal_handler_installed_unlocked (int signum)
4535 static sigset_t installed_signal_mask;
4536 static gboolean initialized;
4537 struct sigaction action;
4541 sigemptyset (&installed_signal_mask);
4542 g_get_worker_context ();
4546 if (sigismember (&installed_signal_mask, signum))
4549 sigaddset (&installed_signal_mask, signum);
4551 action.sa_handler = g_unix_signal_handler;
4552 sigemptyset (&action.sa_mask);
4553 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
4554 sigaction (signum, &action, NULL);
4558 _g_main_create_unix_signal_watch (int signum)
4561 GUnixSignalWatchSource *unix_signal_source;
4563 source = g_source_new (&g_unix_signal_funcs, sizeof (GUnixSignalWatchSource));
4564 unix_signal_source = (GUnixSignalWatchSource *) source;
4566 unix_signal_source->signum = signum;
4567 unix_signal_source->pending = FALSE;
4569 G_LOCK (unix_signal_lock);
4570 ensure_unix_signal_handler_installed_unlocked (signum);
4571 unix_signal_watches = g_slist_prepend (unix_signal_watches, unix_signal_source);
4572 if (unix_signal_pending[signum])
4573 unix_signal_source->pending = TRUE;
4574 unix_signal_pending[signum] = FALSE;
4575 G_UNLOCK (unix_signal_lock);
4581 g_unix_signal_watch_finalize (GSource *source)
4583 G_LOCK (unix_signal_lock);
4584 unix_signal_watches = g_slist_remove (unix_signal_watches, source);
4585 G_UNLOCK (unix_signal_lock);
4589 g_child_watch_finalize (GSource *source)
4591 G_LOCK (unix_signal_lock);
4592 unix_child_watches = g_slist_remove (unix_child_watches, source);
4593 G_UNLOCK (unix_signal_lock);
4596 #endif /* G_OS_WIN32 */
4599 g_child_watch_dispatch (GSource *source,
4600 GSourceFunc callback,
4603 GChildWatchSource *child_watch_source;
4604 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
4606 child_watch_source = (GChildWatchSource *) source;
4610 g_warning ("Child watch source dispatched without callback\n"
4611 "You must call g_source_set_callback().");
4615 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
4617 /* We never keep a child watch source around as the child is gone */
4624 g_unix_signal_handler (int signum)
4626 unix_signal_pending[signum] = TRUE;
4627 any_unix_signal_pending = TRUE;
4629 g_wakeup_signal (glib_worker_context->wakeup);
4632 #endif /* !G_OS_WIN32 */
4635 * g_child_watch_source_new:
4636 * @pid: process to watch. On POSIX the pid of a child process. On
4637 * Windows a handle for a process (which doesn't have to be a child).
4639 * Creates a new child_watch source.
4641 * The source will not initially be associated with any #GMainContext
4642 * and must be added to one with g_source_attach() before it will be
4645 * Note that child watch sources can only be used in conjunction with
4646 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
4649 * Note that on platforms where #GPid must be explicitly closed
4650 * (see g_spawn_close_pid()) @pid must not be closed while the
4651 * source is still active. Typically, you will want to call
4652 * g_spawn_close_pid() in the callback function for the source.
4654 * Note further that using g_child_watch_source_new() is not
4655 * compatible with calling <literal>waitpid</literal> with a
4656 * nonpositive first argument in the application. Calling waitpid()
4657 * for individual pids will still work fine.
4659 * Return value: the newly-created child watch source
4664 g_child_watch_source_new (GPid pid)
4666 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
4667 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
4669 child_watch_source->pid = pid;
4672 child_watch_source->poll.fd = (gintptr) pid;
4673 child_watch_source->poll.events = G_IO_IN;
4675 g_source_add_poll (source, &child_watch_source->poll);
4676 #else /* G_OS_WIN32 */
4677 G_LOCK (unix_signal_lock);
4678 ensure_unix_signal_handler_installed_unlocked (SIGCHLD);
4679 unix_child_watches = g_slist_prepend (unix_child_watches, child_watch_source);
4680 if (waitpid (pid, &child_watch_source->child_status, WNOHANG) > 0)
4681 child_watch_source->child_exited = TRUE;
4682 G_UNLOCK (unix_signal_lock);
4683 #endif /* G_OS_WIN32 */
4689 * g_child_watch_add_full:
4690 * @priority: the priority of the idle source. Typically this will be in the
4691 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4692 * @pid: process to watch. On POSIX the pid of a child process. On
4693 * Windows a handle for a process (which doesn't have to be a child).
4694 * @function: function to call
4695 * @data: data to pass to @function
4696 * @notify: (allow-none): function to call when the idle is removed, or %NULL
4698 * Sets a function to be called when the child indicated by @pid
4699 * exits, at the priority @priority.
4701 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4702 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4703 * the spawn function for the child watching to work.
4705 * In many programs, you will want to call g_spawn_check_exit_status()
4706 * in the callback to determine whether or not the child exited
4709 * Also, note that on platforms where #GPid must be explicitly closed
4710 * (see g_spawn_close_pid()) @pid must not be closed while the source
4711 * is still active. Typically, you should invoke g_spawn_close_pid()
4712 * in the callback function for the source.
4714 * GLib supports only a single callback per process id.
4716 * This internally creates a main loop source using
4717 * g_child_watch_source_new() and attaches it to the main loop context
4718 * using g_source_attach(). You can do these steps manually if you
4719 * need greater control.
4721 * Return value: the ID (greater than 0) of the event source.
4723 * Rename to: g_child_watch_add
4727 g_child_watch_add_full (gint priority,
4729 GChildWatchFunc function,
4731 GDestroyNotify notify)
4736 g_return_val_if_fail (function != NULL, 0);
4738 source = g_child_watch_source_new (pid);
4740 if (priority != G_PRIORITY_DEFAULT)
4741 g_source_set_priority (source, priority);
4743 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4744 id = g_source_attach (source, NULL);
4745 g_source_unref (source);
4751 * g_child_watch_add:
4752 * @pid: process id to watch. On POSIX the pid of a child process. On
4753 * Windows a handle for a process (which doesn't have to be a child).
4754 * @function: function to call
4755 * @data: data to pass to @function
4757 * Sets a function to be called when the child indicated by @pid
4758 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4760 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4761 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4762 * the spawn function for the child watching to work.
4764 * Note that on platforms where #GPid must be explicitly closed
4765 * (see g_spawn_close_pid()) @pid must not be closed while the
4766 * source is still active. Typically, you will want to call
4767 * g_spawn_close_pid() in the callback function for the source.
4769 * GLib supports only a single callback per process id.
4771 * This internally creates a main loop source using
4772 * g_child_watch_source_new() and attaches it to the main loop context
4773 * using g_source_attach(). You can do these steps manually if you
4774 * need greater control.
4776 * Return value: the ID (greater than 0) of the event source.
4781 g_child_watch_add (GPid pid,
4782 GChildWatchFunc function,
4785 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4789 /* Idle functions */
4792 g_idle_prepare (GSource *source,
4801 g_idle_check (GSource *source)
4807 g_idle_dispatch (GSource *source,
4808 GSourceFunc callback,
4813 g_warning ("Idle source dispatched without callback\n"
4814 "You must call g_source_set_callback().");
4818 return callback (user_data);
4822 * g_idle_source_new:
4824 * Creates a new idle source.
4826 * The source will not initially be associated with any #GMainContext
4827 * and must be added to one with g_source_attach() before it will be
4828 * executed. Note that the default priority for idle sources is
4829 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4830 * have a default priority of %G_PRIORITY_DEFAULT.
4832 * Return value: the newly-created idle source
4835 g_idle_source_new (void)
4839 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4840 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4847 * @priority: the priority of the idle source. Typically this will be in the
4848 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4849 * @function: function to call
4850 * @data: data to pass to @function
4851 * @notify: (allow-none): function to call when the idle is removed, or %NULL
4853 * Adds a function to be called whenever there are no higher priority
4854 * events pending. If the function returns %FALSE it is automatically
4855 * removed from the list of event sources and will not be called again.
4857 * This internally creates a main loop source using g_idle_source_new()
4858 * and attaches it to the main loop context using g_source_attach().
4859 * You can do these steps manually if you need greater control.
4861 * Return value: the ID (greater than 0) of the event source.
4862 * Rename to: g_idle_add
4865 g_idle_add_full (gint priority,
4866 GSourceFunc function,
4868 GDestroyNotify notify)
4873 g_return_val_if_fail (function != NULL, 0);
4875 source = g_idle_source_new ();
4877 if (priority != G_PRIORITY_DEFAULT_IDLE)
4878 g_source_set_priority (source, priority);
4880 g_source_set_callback (source, function, data, notify);
4881 id = g_source_attach (source, NULL);
4882 g_source_unref (source);
4889 * @function: function to call
4890 * @data: data to pass to @function.
4892 * Adds a function to be called whenever there are no higher priority
4893 * events pending to the default main loop. The function is given the
4894 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4895 * returns %FALSE it is automatically removed from the list of event
4896 * sources and will not be called again.
4898 * This internally creates a main loop source using g_idle_source_new()
4899 * and attaches it to the main loop context using g_source_attach().
4900 * You can do these steps manually if you need greater control.
4902 * Return value: the ID (greater than 0) of the event source.
4905 g_idle_add (GSourceFunc function,
4908 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4912 * g_idle_remove_by_data:
4913 * @data: the data for the idle source's callback.
4915 * Removes the idle function with the given data.
4917 * Return value: %TRUE if an idle source was found and removed.
4920 g_idle_remove_by_data (gpointer data)
4922 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4926 * g_main_context_invoke:
4927 * @context: (allow-none): a #GMainContext, or %NULL
4928 * @function: function to call
4929 * @data: data to pass to @function
4931 * Invokes a function in such a way that @context is owned during the
4932 * invocation of @function.
4934 * If @context is %NULL then the global default main context — as
4935 * returned by g_main_context_default() — is used.
4937 * If @context is owned by the current thread, @function is called
4938 * directly. Otherwise, if @context is the thread-default main context
4939 * of the current thread and g_main_context_acquire() succeeds, then
4940 * @function is called and g_main_context_release() is called
4943 * In any other case, an idle source is created to call @function and
4944 * that source is attached to @context (presumably to be run in another
4945 * thread). The idle source is attached with #G_PRIORITY_DEFAULT
4946 * priority. If you want a different priority, use
4947 * g_main_context_invoke_full().
4949 * Note that, as with normal idle functions, @function should probably
4950 * return %FALSE. If it returns %TRUE, it will be continuously run in a
4951 * loop (and may prevent this call from returning).
4956 g_main_context_invoke (GMainContext *context,
4957 GSourceFunc function,
4960 g_main_context_invoke_full (context,
4962 function, data, NULL);
4966 * g_main_context_invoke_full:
4967 * @context: (allow-none): a #GMainContext, or %NULL
4968 * @priority: the priority at which to run @function
4969 * @function: function to call
4970 * @data: data to pass to @function
4971 * @notify: (allow-none): a function to call when @data is no longer in use, or %NULL.
4973 * Invokes a function in such a way that @context is owned during the
4974 * invocation of @function.
4976 * This function is the same as g_main_context_invoke() except that it
4977 * lets you specify the priority incase @function ends up being
4978 * scheduled as an idle and also lets you give a #GDestroyNotify for @data.
4980 * @notify should not assume that it is called from any particular
4981 * thread or with any particular context acquired.
4986 g_main_context_invoke_full (GMainContext *context,
4988 GSourceFunc function,
4990 GDestroyNotify notify)
4992 g_return_if_fail (function != NULL);
4995 context = g_main_context_default ();
4997 if (g_main_context_is_owner (context))
4999 while (function (data));
5006 GMainContext *thread_default;
5008 thread_default = g_main_context_get_thread_default ();
5010 if (!thread_default)
5011 thread_default = g_main_context_default ();
5013 if (thread_default == context && g_main_context_acquire (context))
5015 while (function (data));
5017 g_main_context_release (context);
5026 source = g_idle_source_new ();
5027 g_source_set_priority (source, priority);
5028 g_source_set_callback (source, function, data, notify);
5029 g_source_attach (source, context);
5030 g_source_unref (source);
5036 glib_worker_main (gpointer data)
5040 g_main_context_iteration (glib_worker_context, TRUE);
5043 if (any_unix_signal_pending)
5044 dispatch_unix_signals ();
5048 return NULL; /* worst GCC warning message ever... */
5052 g_get_worker_context (void)
5054 static gsize initialised;
5056 if (g_once_init_enter (&initialised))
5058 /* mask all signals in the worker thread */
5064 pthread_sigmask (SIG_SETMASK, &all, &prev_mask);
5066 glib_worker_context = g_main_context_new ();
5067 g_thread_new ("gmain", glib_worker_main, NULL);
5069 pthread_sigmask (SIG_SETMASK, &prev_mask, NULL);
5071 g_once_init_leave (&initialised, TRUE);
5074 return glib_worker_context;