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 /* for pipe2; need to define it first to avoid
36 * other headers pulling in unistd.h
38 /* The meaning of_GNU_SOURCE that is intended here is present only on
39 * Linux; avoid the possibility that some misguided header in MinGW
40 * looks at it. Ideally we should define _GNU_SOURCE only on platforms
41 * where we know what it means and that is what we want here
42 * (i.e. Linux with glibc). After all, there might be some other POSIX
43 * platform even where _GNU_SOURCE is used for some unrelated change
44 * in semantics that isn't wanted. Sigh.
50 #include "glibconfig.h"
52 /* Uncomment the next line (and the corresponding line in gpoll.c) to
53 * enable debugging printouts if the environment variable
54 * G_MAIN_POLL_DEBUG is set to some value.
56 /* #define G_MAIN_POLL_DEBUG */
59 /* Always enable debugging printout on Windows, as it is more often
62 #define G_MAIN_POLL_DEBUG
66 #include "glib-unix.h"
70 #include <sys/types.h>
73 #ifdef HAVE_SYS_TIME_H
75 #endif /* HAVE_SYS_TIME_H */
78 #endif /* HAVE_UNISTD_H */
85 #endif /* G_OS_WIN32 */
88 #include <sys/socket.h>
90 #endif /* G_OS_BEOS */
95 #include "giochannel.h"
99 #include "gstrfuncs.h"
100 #include "gtestutils.h"
101 #include "gthreadprivate.h"
107 #ifdef G_MAIN_POLL_DEBUG
113 * @title: The Main Event Loop
114 * @short_description: manages all available sources of events
116 * The main event loop manages all the available sources of events for
117 * GLib and GTK+ applications. These events can come from any number of
118 * different types of sources such as file descriptors (plain files,
119 * pipes or sockets) and timeouts. New types of event sources can also
120 * be added using g_source_attach().
122 * To allow multiple independent sets of sources to be handled in
123 * different threads, each source is associated with a #GMainContext.
124 * A GMainContext can only be running in a single thread, but
125 * sources can be added to it and removed from it from other threads.
127 * Each event source is assigned a priority. The default priority,
128 * #G_PRIORITY_DEFAULT, is 0. Values less than 0 denote higher priorities.
129 * Values greater than 0 denote lower priorities. Events from high priority
130 * sources are always processed before events from lower priority sources.
132 * Idle functions can also be added, and assigned a priority. These will
133 * be run whenever no events with a higher priority are ready to be processed.
135 * The #GMainLoop data type represents a main event loop. A GMainLoop is
136 * created with g_main_loop_new(). After adding the initial event sources,
137 * g_main_loop_run() is called. This continuously checks for new events from
138 * each of the event sources and dispatches them. Finally, the processing of
139 * an event from one of the sources leads to a call to g_main_loop_quit() to
140 * exit the main loop, and g_main_loop_run() returns.
142 * It is possible to create new instances of #GMainLoop recursively.
143 * This is often used in GTK+ applications when showing modal dialog
144 * boxes. Note that event sources are associated with a particular
145 * #GMainContext, and will be checked and dispatched for all main
146 * loops associated with that GMainContext.
148 * GTK+ contains wrappers of some of these functions, e.g. gtk_main(),
149 * gtk_main_quit() and gtk_events_pending().
151 * <refsect2><title>Creating new source types</title>
152 * <para>One of the unusual features of the #GMainLoop functionality
153 * is that new types of event source can be created and used in
154 * addition to the builtin type of event source. A new event source
155 * type is used for handling GDK events. A new source type is created
156 * by <firstterm>deriving</firstterm> from the #GSource structure.
157 * The derived type of source is represented by a structure that has
158 * the #GSource structure as a first element, and other elements specific
159 * to the new source type. To create an instance of the new source type,
160 * call g_source_new() passing in the size of the derived structure and
161 * a table of functions. These #GSourceFuncs determine the behavior of
162 * the new source type.</para>
163 * <para>New source types basically interact with the main context
164 * in two ways. Their prepare function in #GSourceFuncs can set a timeout
165 * to determine the maximum amount of time that the main loop will sleep
166 * before checking the source again. In addition, or as well, the source
167 * can add file descriptors to the set that the main context checks using
168 * g_source_add_poll().</para>
170 * <refsect2><title>Customizing the main loop iteration</title>
171 * <para>Single iterations of a #GMainContext can be run with
172 * g_main_context_iteration(). In some cases, more detailed control
173 * of exactly how the details of the main loop work is desired, for
174 * instance, when integrating the #GMainLoop with an external main loop.
175 * In such cases, you can call the component functions of
176 * g_main_context_iteration() directly. These functions are
177 * g_main_context_prepare(), g_main_context_query(),
178 * g_main_context_check() and g_main_context_dispatch().</para>
179 * <para>The operation of these functions can best be seen in terms
180 * of a state diagram, as shown in <xref linkend="mainloop-states"/>.</para>
181 * <figure id="mainloop-states"><title>States of a Main Context</title>
182 * <graphic fileref="mainloop-states.gif" format="GIF"></graphic>
189 typedef struct _GTimeoutSource GTimeoutSource;
190 typedef struct _GChildWatchSource GChildWatchSource;
191 typedef struct _GUnixSignalWatchSource GUnixSignalWatchSource;
192 typedef struct _GPollRec GPollRec;
193 typedef struct _GSourceCallback GSourceCallback;
197 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
198 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
201 #ifdef G_THREADS_ENABLED
202 typedef struct _GMainWaiter GMainWaiter;
211 typedef struct _GMainDispatch GMainDispatch;
213 struct _GMainDispatch
216 GSList *dispatching_sources; /* stack of current sources */
219 #ifdef G_MAIN_POLL_DEBUG
220 gboolean _g_main_poll_debug = FALSE;
225 #ifdef G_THREADS_ENABLED
226 /* The following lock is used for both the list of sources
227 * and the list of poll records
238 GPtrArray *pending_dispatches;
239 gint timeout; /* Timeout for current iteration */
242 GSource *source_list;
243 gint in_check_or_prepare;
245 GPollRec *poll_records;
246 guint n_poll_records;
247 GPollFD *cached_poll_array;
248 guint cached_poll_array_size;
250 #ifdef G_THREADS_ENABLED
252 /* this pipe is used to wake up the main loop when a source is added.
254 gint wake_up_pipe[2];
255 #else /* G_OS_WIN32 */
256 HANDLE wake_up_semaphore;
257 #endif /* G_OS_WIN32 */
260 gboolean poll_waiting;
262 /* Flag indicating whether the set of fd's changed during a poll */
263 gboolean poll_changed;
264 #endif /* G_THREADS_ENABLED */
269 gboolean time_is_fresh;
271 gboolean real_time_is_fresh;
274 struct _GSourceCallback
279 GDestroyNotify notify;
284 GMainContext *context;
289 struct _GTimeoutSource
297 struct _GChildWatchSource
304 #else /* G_OS_WIN32 */
306 gboolean child_exited;
307 #endif /* G_OS_WIN32 */
310 struct _GUnixSignalWatchSource
324 struct _GSourcePrivate
326 GSList *child_sources;
327 GSource *parent_source;
330 #ifdef G_THREADS_ENABLED
331 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
332 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
333 #define G_THREAD_SELF g_thread_self ()
335 #define LOCK_CONTEXT(context) (void)0
336 #define UNLOCK_CONTEXT(context) (void)0
337 #define G_THREAD_SELF NULL
340 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
341 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
342 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
344 #define SOURCE_UNREF(source, context) \
346 if ((source)->ref_count > 1) \
347 (source)->ref_count--; \
349 g_source_unref_internal ((source), (context), TRUE); \
353 /* Forward declarations */
355 static void g_source_unref_internal (GSource *source,
356 GMainContext *context,
358 static void g_source_destroy_internal (GSource *source,
359 GMainContext *context,
361 static void g_source_set_priority_unlocked (GSource *source,
362 GMainContext *context,
364 static void g_main_context_poll (GMainContext *context,
369 static void g_main_context_add_poll_unlocked (GMainContext *context,
372 static void g_main_context_remove_poll_unlocked (GMainContext *context,
374 static void g_main_context_wakeup_unlocked (GMainContext *context);
376 static void _g_main_wake_up_all_contexts (void);
378 static gboolean g_timeout_prepare (GSource *source,
380 static gboolean g_timeout_check (GSource *source);
381 static gboolean g_timeout_dispatch (GSource *source,
382 GSourceFunc callback,
384 static gboolean g_child_watch_prepare (GSource *source,
386 static gboolean g_child_watch_check (GSource *source);
387 static gboolean g_child_watch_dispatch (GSource *source,
388 GSourceFunc callback,
391 static void g_unix_signal_handler (int signum);
392 static void init_unix_signal_wakeup_state_unlocked (void);
393 static void init_unix_signal_wakeup_state (void);
394 static gboolean g_unix_signal_watch_prepare (GSource *source,
396 static gboolean g_unix_signal_watch_check (GSource *source);
397 static gboolean g_unix_signal_watch_dispatch (GSource *source,
398 GSourceFunc callback,
400 static void g_unix_signal_watch_finalize (GSource *source);
402 static gboolean g_idle_prepare (GSource *source,
404 static gboolean g_idle_check (GSource *source);
405 static gboolean g_idle_dispatch (GSource *source,
406 GSourceFunc callback,
409 G_LOCK_DEFINE_STATIC (main_loop);
410 static GMainContext *default_main_context;
411 static GSList *main_contexts_without_pipe = NULL;
415 /* The UNIX signal pipe contains a single byte specifying which
416 * signal was received.
418 #define _UNIX_SIGNAL_PIPE_SIGCHLD_CHAR 'C'
419 #define _UNIX_SIGNAL_PIPE_SIGHUP_CHAR 'H'
420 #define _UNIX_SIGNAL_PIPE_SIGINT_CHAR 'I'
421 #define _UNIX_SIGNAL_PIPE_SIGTERM_CHAR 'T'
422 /* Guards all the data below */
423 G_LOCK_DEFINE_STATIC (unix_signal_lock);
425 UNIX_SIGNAL_UNINITIALIZED = 0,
426 UNIX_SIGNAL_INITIALIZED_SINGLE,
427 UNIX_SIGNAL_INITIALIZED_THREADED
429 static gint unix_signal_init_state = UNIX_SIGNAL_UNINITIALIZED;
431 gboolean sigchld_handler_installed : 1;
432 gboolean sighup_handler_installed : 1;
433 gboolean sigint_handler_installed : 1;
434 gboolean sigterm_handler_installed : 1;
436 /* These are only used in the UNIX_SIGNAL_INITIALIZED_SINGLE case */
437 gboolean sighup_delivered : 1;
438 gboolean sigint_delivered : 1;
439 gboolean sigterm_delivered : 1;
441 static UnixSignalState unix_signal_state;
442 static gint unix_signal_wake_up_pipe[2];
443 GSList *unix_signal_watches;
445 /* Not guarded ( FIXME should it be? ) */
446 static gint child_watch_count = 1;
448 static GSourceFuncs g_unix_signal_funcs =
450 g_unix_signal_watch_prepare,
451 g_unix_signal_watch_check,
452 g_unix_signal_watch_dispatch,
453 g_unix_signal_watch_finalize
455 #endif /* !G_OS_WIN32 */
456 G_LOCK_DEFINE_STATIC (main_context_list);
457 static GSList *main_context_list = NULL;
459 GSourceFuncs g_timeout_funcs =
467 GSourceFuncs g_child_watch_funcs =
469 g_child_watch_prepare,
471 g_child_watch_dispatch,
475 GSourceFuncs g_idle_funcs =
484 * g_main_context_ref:
485 * @context: a #GMainContext
487 * Increases the reference count on a #GMainContext object by one.
489 * Returns: the @context that was passed in (since 2.6)
492 g_main_context_ref (GMainContext *context)
494 g_return_val_if_fail (context != NULL, NULL);
495 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
497 g_atomic_int_inc (&context->ref_count);
503 poll_rec_list_free (GMainContext *context,
506 g_slice_free_chain (GPollRec, list, next);
510 * g_main_context_unref:
511 * @context: a #GMainContext
513 * Decreases the reference count on a #GMainContext object by one. If
514 * the result is zero, free the context and free all associated memory.
517 g_main_context_unref (GMainContext *context)
520 g_return_if_fail (context != NULL);
521 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
523 if (!g_atomic_int_dec_and_test (&context->ref_count))
526 G_LOCK (main_context_list);
527 main_context_list = g_slist_remove (main_context_list, context);
528 G_UNLOCK (main_context_list);
530 source = context->source_list;
533 GSource *next = source->next;
534 g_source_destroy_internal (source, context, FALSE);
538 #ifdef G_THREADS_ENABLED
539 g_static_mutex_free (&context->mutex);
542 g_ptr_array_free (context->pending_dispatches, TRUE);
543 g_free (context->cached_poll_array);
545 poll_rec_list_free (context, context->poll_records);
547 #ifdef G_THREADS_ENABLED
548 if (g_thread_supported())
551 close (context->wake_up_pipe[0]);
552 close (context->wake_up_pipe[1]);
554 CloseHandle (context->wake_up_semaphore);
558 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
561 if (context->cond != NULL)
562 g_cond_free (context->cond);
568 #ifdef G_THREADS_ENABLED
570 g_main_context_init_pipe (GMainContext *context)
572 GError *error = NULL;
575 if (context->wake_up_pipe[0] != -1)
578 if (!g_unix_pipe_flags (context->wake_up_pipe, FD_CLOEXEC, &error))
579 g_error ("Cannot create pipe main loop wake-up: %s", error->message);
581 context->wake_up_rec.fd = context->wake_up_pipe[0];
582 context->wake_up_rec.events = G_IO_IN;
584 if (context->wake_up_semaphore != NULL)
586 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
587 if (context->wake_up_semaphore == NULL)
588 g_error ("Cannot create wake-up semaphore: %s",
589 g_win32_error_message (GetLastError ()));
590 context->wake_up_rec.fd = (gintptr) context->wake_up_semaphore;
591 context->wake_up_rec.events = G_IO_IN;
593 if (_g_main_poll_debug)
594 g_print ("wake-up semaphore: %p\n", context->wake_up_semaphore);
596 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
600 _g_main_thread_init (void)
602 GSList *curr = main_contexts_without_pipe;
605 g_main_context_init_pipe ((GMainContext *)curr->data);
608 g_slist_free (main_contexts_without_pipe);
609 main_contexts_without_pipe = NULL;
611 #endif /* G_THREADS_ENABLED */
614 * g_main_context_new:
616 * Creates a new #GMainContext structure.
618 * Return value: the new #GMainContext
621 g_main_context_new (void)
623 GMainContext *context = g_new0 (GMainContext, 1);
625 #ifdef G_MAIN_POLL_DEBUG
627 static gboolean beenhere = FALSE;
631 if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
632 _g_main_poll_debug = TRUE;
638 #ifdef G_THREADS_ENABLED
639 g_static_mutex_init (&context->mutex);
641 context->owner = NULL;
642 context->waiters = NULL;
645 context->wake_up_pipe[0] = -1;
646 context->wake_up_pipe[1] = -1;
648 context->wake_up_semaphore = NULL;
652 context->ref_count = 1;
654 context->next_id = 1;
656 context->source_list = NULL;
658 context->poll_func = g_poll;
660 context->cached_poll_array = NULL;
661 context->cached_poll_array_size = 0;
663 context->pending_dispatches = g_ptr_array_new ();
665 context->time_is_fresh = FALSE;
666 context->real_time_is_fresh = FALSE;
668 #ifdef G_THREADS_ENABLED
669 if (g_thread_supported ())
670 g_main_context_init_pipe (context);
672 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
676 G_LOCK (main_context_list);
677 main_context_list = g_slist_append (main_context_list, context);
679 #ifdef G_MAIN_POLL_DEBUG
680 if (_g_main_poll_debug)
681 g_print ("created context=%p\n", context);
684 G_UNLOCK (main_context_list);
690 * g_main_context_default:
692 * Returns the global default main context. This is the main context
693 * used for main loop functions when a main loop is not explicitly
694 * specified, and corresponds to the "main" main loop. See also
695 * g_main_context_get_thread_default().
697 * Return value: the global default main context.
700 g_main_context_default (void)
706 if (!default_main_context)
708 default_main_context = g_main_context_new ();
709 #ifdef G_MAIN_POLL_DEBUG
710 if (_g_main_poll_debug)
711 g_print ("default context=%p\n", default_main_context);
715 G_UNLOCK (main_loop);
717 return default_main_context;
720 static GStaticPrivate thread_context_stack = G_STATIC_PRIVATE_INIT;
723 free_context_stack (gpointer data)
725 GQueue *stack = data;
726 GMainContext *context;
728 while (!g_queue_is_empty (stack))
730 context = g_queue_pop_head (stack);
731 g_main_context_release (context);
733 g_main_context_unref (context);
735 g_queue_free (stack);
739 * g_main_context_push_thread_default:
740 * @context: a #GMainContext, or %NULL for the global default context
742 * Acquires @context and sets it as the thread-default context for the
743 * current thread. This will cause certain asynchronous operations
744 * (such as most <link linkend="gio">gio</link>-based I/O) which are
745 * started in this thread to run under @context and deliver their
746 * results to its main loop, rather than running under the global
747 * default context in the main thread. Note that calling this function
748 * changes the context returned by
749 * g_main_context_get_thread_default(), <emphasis>not</emphasis> the
750 * one returned by g_main_context_default(), so it does not affect the
751 * context used by functions like g_idle_add().
753 * Normally you would call this function shortly after creating a new
754 * thread, passing it a #GMainContext which will be run by a
755 * #GMainLoop in that thread, to set a new default context for all
756 * async operations in that thread. (In this case, you don't need to
757 * ever call g_main_context_pop_thread_default().) In some cases
758 * however, you may want to schedule a single operation in a
759 * non-default context, or temporarily use a non-default context in
760 * the main thread. In that case, you can wrap the call to the
761 * asynchronous operation inside a
762 * g_main_context_push_thread_default() /
763 * g_main_context_pop_thread_default() pair, but it is up to you to
764 * ensure that no other asynchronous operations accidentally get
765 * started while the non-default context is active.
767 * Beware that libraries that predate this function may not correctly
768 * handle being used from a thread with a thread-default context. Eg,
769 * see g_file_supports_thread_contexts().
774 g_main_context_push_thread_default (GMainContext *context)
777 gboolean acquired_context;
779 acquired_context = g_main_context_acquire (context);
780 g_return_if_fail (acquired_context);
782 if (context == g_main_context_default ())
785 g_main_context_ref (context);
787 stack = g_static_private_get (&thread_context_stack);
790 stack = g_queue_new ();
791 g_static_private_set (&thread_context_stack, stack,
795 g_queue_push_head (stack, context);
799 * g_main_context_pop_thread_default:
800 * @context: a #GMainContext object, or %NULL
802 * Pops @context off the thread-default context stack (verifying that
803 * it was on the top of the stack).
808 g_main_context_pop_thread_default (GMainContext *context)
812 if (context == g_main_context_default ())
815 stack = g_static_private_get (&thread_context_stack);
817 g_return_if_fail (stack != NULL);
818 g_return_if_fail (g_queue_peek_head (stack) == context);
820 g_queue_pop_head (stack);
822 g_main_context_release (context);
824 g_main_context_unref (context);
828 * g_main_context_get_thread_default:
830 * Gets the thread-default #GMainContext for this thread. Asynchronous
831 * operations that want to be able to be run in contexts other than
832 * the default one should call this method to get a #GMainContext to
833 * add their #GSource<!-- -->s to. (Note that even in single-threaded
834 * programs applications may sometimes want to temporarily push a
835 * non-default context, so it is not safe to assume that this will
836 * always return %NULL if threads are not initialized.)
838 * Returns: the thread-default #GMainContext, or %NULL if the
839 * thread-default context is the global default context.
844 g_main_context_get_thread_default (void)
848 stack = g_static_private_get (&thread_context_stack);
850 return g_queue_peek_head (stack);
855 /* Hooks for adding to the main loop */
859 * @source_funcs: structure containing functions that implement
860 * the sources behavior.
861 * @struct_size: size of the #GSource structure to create.
863 * Creates a new #GSource structure. The size is specified to
864 * allow creating structures derived from #GSource that contain
865 * additional data. The size passed in must be at least
866 * <literal>sizeof (GSource)</literal>.
868 * The source will not initially be associated with any #GMainContext
869 * and must be added to one with g_source_attach() before it will be
872 * Return value: the newly-created #GSource.
875 g_source_new (GSourceFuncs *source_funcs,
880 g_return_val_if_fail (source_funcs != NULL, NULL);
881 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
883 source = (GSource*) g_malloc0 (struct_size);
885 source->source_funcs = source_funcs;
886 source->ref_count = 1;
888 source->priority = G_PRIORITY_DEFAULT;
890 source->flags = G_HOOK_FLAG_ACTIVE;
892 /* NULL/0 initialization for all other fields */
897 /* Holds context's lock
900 g_source_list_add (GSource *source,
901 GMainContext *context)
903 GSource *tmp_source, *last_source;
905 if (source->priv && source->priv->parent_source)
907 /* Put the source immediately before its parent */
908 tmp_source = source->priv->parent_source;
909 last_source = source->priv->parent_source->prev;
914 tmp_source = context->source_list;
915 while (tmp_source && tmp_source->priority <= source->priority)
917 last_source = tmp_source;
918 tmp_source = tmp_source->next;
922 source->next = tmp_source;
924 tmp_source->prev = source;
926 source->prev = last_source;
928 last_source->next = source;
930 context->source_list = source;
933 /* Holds context's lock
936 g_source_list_remove (GSource *source,
937 GMainContext *context)
940 source->prev->next = source->next;
942 context->source_list = source->next;
945 source->next->prev = source->prev;
952 g_source_attach_unlocked (GSource *source,
953 GMainContext *context)
958 source->context = context;
959 result = source->source_id = context->next_id++;
962 g_source_list_add (source, context);
964 tmp_list = source->poll_fds;
967 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
968 tmp_list = tmp_list->next;
973 tmp_list = source->priv->child_sources;
976 g_source_attach_unlocked (tmp_list->data, context);
977 tmp_list = tmp_list->next;
986 * @source: a #GSource
987 * @context: a #GMainContext (if %NULL, the default context will be used)
989 * Adds a #GSource to a @context so that it will be executed within
990 * that context. Remove it by calling g_source_destroy().
992 * Return value: the ID (greater than 0) for the source within the
996 g_source_attach (GSource *source,
997 GMainContext *context)
1001 g_return_val_if_fail (source->context == NULL, 0);
1002 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
1005 context = g_main_context_default ();
1007 LOCK_CONTEXT (context);
1009 result = g_source_attach_unlocked (source, context);
1011 #ifdef G_THREADS_ENABLED
1012 /* Now wake up the main loop if it is waiting in the poll() */
1013 g_main_context_wakeup_unlocked (context);
1016 UNLOCK_CONTEXT (context);
1022 g_source_destroy_internal (GSource *source,
1023 GMainContext *context,
1027 LOCK_CONTEXT (context);
1029 if (!SOURCE_DESTROYED (source))
1032 gpointer old_cb_data;
1033 GSourceCallbackFuncs *old_cb_funcs;
1035 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1037 old_cb_data = source->callback_data;
1038 old_cb_funcs = source->callback_funcs;
1040 source->callback_data = NULL;
1041 source->callback_funcs = NULL;
1045 UNLOCK_CONTEXT (context);
1046 old_cb_funcs->unref (old_cb_data);
1047 LOCK_CONTEXT (context);
1050 if (!SOURCE_BLOCKED (source))
1052 tmp_list = source->poll_fds;
1055 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1056 tmp_list = tmp_list->next;
1060 if (source->priv && source->priv->child_sources)
1062 /* This is safe because even if a child_source finalizer or
1063 * closure notify tried to modify source->priv->child_sources
1064 * from outside the lock, it would fail since
1065 * SOURCE_DESTROYED(source) is now TRUE.
1067 tmp_list = source->priv->child_sources;
1070 g_source_destroy_internal (tmp_list->data, context, TRUE);
1071 g_source_unref_internal (tmp_list->data, context, TRUE);
1072 tmp_list = tmp_list->next;
1074 g_slist_free (source->priv->child_sources);
1075 source->priv->child_sources = NULL;
1078 g_source_unref_internal (source, context, TRUE);
1082 UNLOCK_CONTEXT (context);
1087 * @source: a #GSource
1089 * Removes a source from its #GMainContext, if any, and mark it as
1090 * destroyed. The source cannot be subsequently added to another
1094 g_source_destroy (GSource *source)
1096 GMainContext *context;
1098 g_return_if_fail (source != NULL);
1100 context = source->context;
1103 g_source_destroy_internal (source, context, FALSE);
1105 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1110 * @source: a #GSource
1112 * Returns the numeric ID for a particular source. The ID of a source
1113 * is a positive integer which is unique within a particular main loop
1114 * context. The reverse
1115 * mapping from ID to source is done by g_main_context_find_source_by_id().
1117 * Return value: the ID (greater than 0) for the source
1120 g_source_get_id (GSource *source)
1124 g_return_val_if_fail (source != NULL, 0);
1125 g_return_val_if_fail (source->context != NULL, 0);
1127 LOCK_CONTEXT (source->context);
1128 result = source->source_id;
1129 UNLOCK_CONTEXT (source->context);
1135 * g_source_get_context:
1136 * @source: a #GSource
1138 * Gets the #GMainContext with which the source is associated.
1139 * Calling this function on a destroyed source is an error.
1141 * Return value: the #GMainContext with which the source is associated,
1142 * or %NULL if the context has not yet been added
1146 g_source_get_context (GSource *source)
1148 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
1150 return source->context;
1154 * g_source_add_poll:
1155 * @source:a #GSource
1156 * @fd: a #GPollFD structure holding information about a file
1157 * descriptor to watch.
1159 * Adds a file descriptor to the set of file descriptors polled for
1160 * this source. This is usually combined with g_source_new() to add an
1161 * event source. The event source's check function will typically test
1162 * the @revents field in the #GPollFD struct and return %TRUE if events need
1166 g_source_add_poll (GSource *source,
1169 GMainContext *context;
1171 g_return_if_fail (source != NULL);
1172 g_return_if_fail (fd != NULL);
1173 g_return_if_fail (!SOURCE_DESTROYED (source));
1175 context = source->context;
1178 LOCK_CONTEXT (context);
1180 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1184 if (!SOURCE_BLOCKED (source))
1185 g_main_context_add_poll_unlocked (context, source->priority, fd);
1186 UNLOCK_CONTEXT (context);
1191 * g_source_remove_poll:
1192 * @source:a #GSource
1193 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1195 * Removes a file descriptor from the set of file descriptors polled for
1199 g_source_remove_poll (GSource *source,
1202 GMainContext *context;
1204 g_return_if_fail (source != NULL);
1205 g_return_if_fail (fd != NULL);
1206 g_return_if_fail (!SOURCE_DESTROYED (source));
1208 context = source->context;
1211 LOCK_CONTEXT (context);
1213 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1217 if (!SOURCE_BLOCKED (source))
1218 g_main_context_remove_poll_unlocked (context, fd);
1219 UNLOCK_CONTEXT (context);
1224 * g_source_add_child_source:
1225 * @source:a #GSource
1226 * @child_source: a second #GSource that @source should "poll"
1228 * Adds @child_source to @source as a "polled" source; when @source is
1229 * added to a #GMainContext, @child_source will be automatically added
1230 * with the same priority, when @child_source is triggered, it will
1231 * cause @source to dispatch (in addition to calling its own
1232 * callback), and when @source is destroyed, it will destroy
1233 * @child_source as well. (@source will also still be dispatched if
1234 * its own prepare/check functions indicate that it is ready.)
1236 * If you don't need @child_source to do anything on its own when it
1237 * triggers, you can call g_source_set_dummy_callback() on it to set a
1238 * callback that does nothing (except return %TRUE if appropriate).
1240 * @source will hold a reference on @child_source while @child_source
1241 * is attached to it.
1246 g_source_add_child_source (GSource *source,
1247 GSource *child_source)
1249 GMainContext *context;
1251 g_return_if_fail (source != NULL);
1252 g_return_if_fail (child_source != NULL);
1253 g_return_if_fail (!SOURCE_DESTROYED (source));
1254 g_return_if_fail (!SOURCE_DESTROYED (child_source));
1255 g_return_if_fail (child_source->context == NULL);
1256 g_return_if_fail (child_source->priv == NULL || child_source->priv->parent_source == NULL);
1258 context = source->context;
1261 LOCK_CONTEXT (context);
1264 source->priv = g_slice_new0 (GSourcePrivate);
1265 if (!child_source->priv)
1266 child_source->priv = g_slice_new0 (GSourcePrivate);
1268 source->priv->child_sources = g_slist_prepend (source->priv->child_sources,
1269 g_source_ref (child_source));
1270 child_source->priv->parent_source = source;
1271 g_source_set_priority_unlocked (child_source, context, source->priority);
1275 UNLOCK_CONTEXT (context);
1276 g_source_attach (child_source, context);
1281 * g_source_remove_child_source:
1282 * @source:a #GSource
1283 * @child_source: a #GSource previously passed to
1284 * g_source_add_child_source().
1286 * Detaches @child_source from @source and destroys it.
1291 g_source_remove_child_source (GSource *source,
1292 GSource *child_source)
1294 GMainContext *context;
1296 g_return_if_fail (source != NULL);
1297 g_return_if_fail (child_source != NULL);
1298 g_return_if_fail (child_source->priv != NULL && child_source->priv->parent_source == source);
1299 g_return_if_fail (!SOURCE_DESTROYED (source));
1300 g_return_if_fail (!SOURCE_DESTROYED (child_source));
1302 context = source->context;
1305 LOCK_CONTEXT (context);
1307 source->priv->child_sources = g_slist_remove (source->priv->child_sources, child_source);
1308 g_source_destroy_internal (child_source, context, TRUE);
1309 g_source_unref_internal (child_source, context, TRUE);
1312 UNLOCK_CONTEXT (context);
1316 * g_source_set_callback_indirect:
1317 * @source: the source
1318 * @callback_data: pointer to callback data "object"
1319 * @callback_funcs: functions for reference counting @callback_data
1320 * and getting the callback and data
1322 * Sets the callback function storing the data as a refcounted callback
1323 * "object". This is used internally. Note that calling
1324 * g_source_set_callback_indirect() assumes
1325 * an initial reference count on @callback_data, and thus
1326 * @callback_funcs->unref will eventually be called once more
1327 * than @callback_funcs->ref.
1330 g_source_set_callback_indirect (GSource *source,
1331 gpointer callback_data,
1332 GSourceCallbackFuncs *callback_funcs)
1334 GMainContext *context;
1335 gpointer old_cb_data;
1336 GSourceCallbackFuncs *old_cb_funcs;
1338 g_return_if_fail (source != NULL);
1339 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1341 context = source->context;
1344 LOCK_CONTEXT (context);
1346 old_cb_data = source->callback_data;
1347 old_cb_funcs = source->callback_funcs;
1349 source->callback_data = callback_data;
1350 source->callback_funcs = callback_funcs;
1353 UNLOCK_CONTEXT (context);
1356 old_cb_funcs->unref (old_cb_data);
1360 g_source_callback_ref (gpointer cb_data)
1362 GSourceCallback *callback = cb_data;
1364 callback->ref_count++;
1369 g_source_callback_unref (gpointer cb_data)
1371 GSourceCallback *callback = cb_data;
1373 callback->ref_count--;
1374 if (callback->ref_count == 0)
1376 if (callback->notify)
1377 callback->notify (callback->data);
1383 g_source_callback_get (gpointer cb_data,
1388 GSourceCallback *callback = cb_data;
1390 *func = callback->func;
1391 *data = callback->data;
1394 static GSourceCallbackFuncs g_source_callback_funcs = {
1395 g_source_callback_ref,
1396 g_source_callback_unref,
1397 g_source_callback_get,
1401 * g_source_set_callback:
1402 * @source: the source
1403 * @func: a callback function
1404 * @data: the data to pass to callback function
1405 * @notify: a function to call when @data is no longer in use, or %NULL.
1407 * Sets the callback function for a source. The callback for a source is
1408 * called from the source's dispatch function.
1410 * The exact type of @func depends on the type of source; ie. you
1411 * should not count on @func being called with @data as its first
1414 * Typically, you won't use this function. Instead use functions specific
1415 * to the type of source you are using.
1418 g_source_set_callback (GSource *source,
1421 GDestroyNotify notify)
1423 GSourceCallback *new_callback;
1425 g_return_if_fail (source != NULL);
1427 new_callback = g_new (GSourceCallback, 1);
1429 new_callback->ref_count = 1;
1430 new_callback->func = func;
1431 new_callback->data = data;
1432 new_callback->notify = notify;
1434 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1439 * g_source_set_funcs:
1440 * @source: a #GSource
1441 * @funcs: the new #GSourceFuncs
1443 * Sets the source functions (can be used to override
1444 * default implementations) of an unattached source.
1449 g_source_set_funcs (GSource *source,
1450 GSourceFuncs *funcs)
1452 g_return_if_fail (source != NULL);
1453 g_return_if_fail (source->context == NULL);
1454 g_return_if_fail (source->ref_count > 0);
1455 g_return_if_fail (funcs != NULL);
1457 source->source_funcs = funcs;
1461 g_source_set_priority_unlocked (GSource *source,
1462 GMainContext *context,
1467 source->priority = priority;
1471 /* Remove the source from the context's source and then
1472 * add it back so it is sorted in the correct place
1474 g_source_list_remove (source, source->context);
1475 g_source_list_add (source, source->context);
1477 if (!SOURCE_BLOCKED (source))
1479 tmp_list = source->poll_fds;
1482 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1483 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1485 tmp_list = tmp_list->next;
1490 if (source->priv && source->priv->child_sources)
1492 tmp_list = source->priv->child_sources;
1495 g_source_set_priority_unlocked (tmp_list->data, context, priority);
1496 tmp_list = tmp_list->next;
1502 * g_source_set_priority:
1503 * @source: a #GSource
1504 * @priority: the new priority.
1506 * Sets the priority of a source. While the main loop is being run, a
1507 * source will be dispatched if it is ready to be dispatched and no
1508 * sources at a higher (numerically smaller) priority are ready to be
1512 g_source_set_priority (GSource *source,
1515 GMainContext *context;
1517 g_return_if_fail (source != NULL);
1519 context = source->context;
1522 LOCK_CONTEXT (context);
1523 g_source_set_priority_unlocked (source, context, priority);
1525 UNLOCK_CONTEXT (source->context);
1529 * g_source_get_priority:
1530 * @source: a #GSource
1532 * Gets the priority of a source.
1534 * Return value: the priority of the source
1537 g_source_get_priority (GSource *source)
1539 g_return_val_if_fail (source != NULL, 0);
1541 return source->priority;
1545 * g_source_set_can_recurse:
1546 * @source: a #GSource
1547 * @can_recurse: whether recursion is allowed for this source
1549 * Sets whether a source can be called recursively. If @can_recurse is
1550 * %TRUE, then while the source is being dispatched then this source
1551 * will be processed normally. Otherwise, all processing of this
1552 * source is blocked until the dispatch function returns.
1555 g_source_set_can_recurse (GSource *source,
1556 gboolean can_recurse)
1558 GMainContext *context;
1560 g_return_if_fail (source != NULL);
1562 context = source->context;
1565 LOCK_CONTEXT (context);
1568 source->flags |= G_SOURCE_CAN_RECURSE;
1570 source->flags &= ~G_SOURCE_CAN_RECURSE;
1573 UNLOCK_CONTEXT (context);
1577 * g_source_get_can_recurse:
1578 * @source: a #GSource
1580 * Checks whether a source is allowed to be called recursively.
1581 * see g_source_set_can_recurse().
1583 * Return value: whether recursion is allowed.
1586 g_source_get_can_recurse (GSource *source)
1588 g_return_val_if_fail (source != NULL, FALSE);
1590 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1595 * g_source_set_name:
1596 * @source: a #GSource
1597 * @name: debug name for the source
1599 * Sets a name for the source, used in debugging and profiling.
1600 * The name defaults to #NULL.
1602 * The source name should describe in a human-readable way
1603 * what the source does. For example, "X11 event queue"
1604 * or "GTK+ repaint idle handler" or whatever it is.
1606 * It is permitted to call this function multiple times, but is not
1607 * recommended due to the potential performance impact. For example,
1608 * one could change the name in the "check" function of a #GSourceFuncs
1609 * to include details like the event type in the source name.
1614 g_source_set_name (GSource *source,
1617 g_return_if_fail (source != NULL);
1619 /* setting back to NULL is allowed, just because it's
1620 * weird if get_name can return NULL but you can't
1624 g_free (source->name);
1625 source->name = g_strdup (name);
1629 * g_source_get_name:
1630 * @source: a #GSource
1632 * Gets a name for the source, used in debugging and profiling.
1633 * The name may be #NULL if it has never been set with
1634 * g_source_set_name().
1636 * Return value: the name of the source
1639 G_CONST_RETURN char*
1640 g_source_get_name (GSource *source)
1642 g_return_val_if_fail (source != NULL, NULL);
1644 return source->name;
1648 * g_source_set_name_by_id:
1649 * @tag: a #GSource ID
1650 * @name: debug name for the source
1652 * Sets the name of a source using its ID.
1654 * This is a convenience utility to set source names from the return
1655 * value of g_idle_add(), g_timeout_add(), etc.
1660 g_source_set_name_by_id (guint tag,
1665 g_return_if_fail (tag > 0);
1667 source = g_main_context_find_source_by_id (NULL, tag);
1671 g_source_set_name (source, name);
1677 * @source: a #GSource
1679 * Increases the reference count on a source by one.
1681 * Return value: @source
1684 g_source_ref (GSource *source)
1686 GMainContext *context;
1688 g_return_val_if_fail (source != NULL, NULL);
1690 context = source->context;
1693 LOCK_CONTEXT (context);
1695 source->ref_count++;
1698 UNLOCK_CONTEXT (context);
1703 /* g_source_unref() but possible to call within context lock
1706 g_source_unref_internal (GSource *source,
1707 GMainContext *context,
1710 gpointer old_cb_data = NULL;
1711 GSourceCallbackFuncs *old_cb_funcs = NULL;
1713 g_return_if_fail (source != NULL);
1715 if (!have_lock && context)
1716 LOCK_CONTEXT (context);
1718 source->ref_count--;
1719 if (source->ref_count == 0)
1721 old_cb_data = source->callback_data;
1722 old_cb_funcs = source->callback_funcs;
1724 source->callback_data = NULL;
1725 source->callback_funcs = NULL;
1729 if (!SOURCE_DESTROYED (source))
1730 g_warning (G_STRLOC ": ref_count == 0, but source was still attached to a context!");
1731 g_source_list_remove (source, context);
1734 if (source->source_funcs->finalize)
1737 UNLOCK_CONTEXT (context);
1738 source->source_funcs->finalize (source);
1740 LOCK_CONTEXT (context);
1743 g_free (source->name);
1744 source->name = NULL;
1746 g_slist_free (source->poll_fds);
1747 source->poll_fds = NULL;
1751 g_slice_free (GSourcePrivate, source->priv);
1752 source->priv = NULL;
1758 if (!have_lock && context)
1759 UNLOCK_CONTEXT (context);
1764 UNLOCK_CONTEXT (context);
1766 old_cb_funcs->unref (old_cb_data);
1769 LOCK_CONTEXT (context);
1775 * @source: a #GSource
1777 * Decreases the reference count of a source by one. If the
1778 * resulting reference count is zero the source and associated
1779 * memory will be destroyed.
1782 g_source_unref (GSource *source)
1784 g_return_if_fail (source != NULL);
1786 g_source_unref_internal (source, source->context, FALSE);
1790 * g_main_context_find_source_by_id:
1791 * @context: a #GMainContext (if %NULL, the default context will be used)
1792 * @source_id: the source ID, as returned by g_source_get_id().
1794 * Finds a #GSource given a pair of context and ID.
1796 * Return value: the #GSource if found, otherwise, %NULL
1799 g_main_context_find_source_by_id (GMainContext *context,
1804 g_return_val_if_fail (source_id > 0, NULL);
1806 if (context == NULL)
1807 context = g_main_context_default ();
1809 LOCK_CONTEXT (context);
1811 source = context->source_list;
1814 if (!SOURCE_DESTROYED (source) &&
1815 source->source_id == source_id)
1817 source = source->next;
1820 UNLOCK_CONTEXT (context);
1826 * g_main_context_find_source_by_funcs_user_data:
1827 * @context: a #GMainContext (if %NULL, the default context will be used).
1828 * @funcs: the @source_funcs passed to g_source_new().
1829 * @user_data: the user data from the callback.
1831 * Finds a source with the given source functions and user data. If
1832 * multiple sources exist with the same source function and user data,
1833 * the first one found will be returned.
1835 * Return value: the source, if one was found, otherwise %NULL
1838 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1839 GSourceFuncs *funcs,
1844 g_return_val_if_fail (funcs != NULL, NULL);
1846 if (context == NULL)
1847 context = g_main_context_default ();
1849 LOCK_CONTEXT (context);
1851 source = context->source_list;
1854 if (!SOURCE_DESTROYED (source) &&
1855 source->source_funcs == funcs &&
1856 source->callback_funcs)
1858 GSourceFunc callback;
1859 gpointer callback_data;
1861 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1863 if (callback_data == user_data)
1866 source = source->next;
1869 UNLOCK_CONTEXT (context);
1875 * g_main_context_find_source_by_user_data:
1876 * @context: a #GMainContext
1877 * @user_data: the user_data for the callback.
1879 * Finds a source with the given user data for the callback. If
1880 * multiple sources exist with the same user data, the first
1881 * one found will be returned.
1883 * Return value: the source, if one was found, otherwise %NULL
1886 g_main_context_find_source_by_user_data (GMainContext *context,
1891 if (context == NULL)
1892 context = g_main_context_default ();
1894 LOCK_CONTEXT (context);
1896 source = context->source_list;
1899 if (!SOURCE_DESTROYED (source) &&
1900 source->callback_funcs)
1902 GSourceFunc callback;
1903 gpointer callback_data = NULL;
1905 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1907 if (callback_data == user_data)
1910 source = source->next;
1913 UNLOCK_CONTEXT (context);
1920 * @tag: the ID of the source to remove.
1922 * Removes the source with the given id from the default main context.
1924 * a #GSource is given by g_source_get_id(), or will be returned by the
1925 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1926 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1927 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1929 * See also g_source_destroy(). You must use g_source_destroy() for sources
1930 * added to a non-default main context.
1932 * Return value: %TRUE if the source was found and removed.
1935 g_source_remove (guint tag)
1939 g_return_val_if_fail (tag > 0, FALSE);
1941 source = g_main_context_find_source_by_id (NULL, tag);
1943 g_source_destroy (source);
1945 return source != NULL;
1949 * g_source_remove_by_user_data:
1950 * @user_data: the user_data for the callback.
1952 * Removes a source from the default main loop context given the user
1953 * data for the callback. If multiple sources exist with the same user
1954 * data, only one will be destroyed.
1956 * Return value: %TRUE if a source was found and removed.
1959 g_source_remove_by_user_data (gpointer user_data)
1963 source = g_main_context_find_source_by_user_data (NULL, user_data);
1966 g_source_destroy (source);
1974 * g_source_remove_by_funcs_user_data:
1975 * @funcs: The @source_funcs passed to g_source_new()
1976 * @user_data: the user data for the callback
1978 * Removes a source from the default main loop context given the
1979 * source functions and user data. If multiple sources exist with the
1980 * same source functions and user data, only one will be destroyed.
1982 * Return value: %TRUE if a source was found and removed.
1985 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1990 g_return_val_if_fail (funcs != NULL, FALSE);
1992 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1995 g_source_destroy (source);
2003 * g_get_current_time:
2004 * @result: #GTimeVal structure in which to store current time.
2006 * Equivalent to the UNIX gettimeofday() function, but portable.
2008 * You may find g_get_real_time() to be more convenient.
2011 g_get_current_time (GTimeVal *result)
2016 g_return_if_fail (result != NULL);
2018 /*this is required on alpha, there the timeval structs are int's
2019 not longs and a cast only would fail horribly*/
2020 gettimeofday (&r, NULL);
2021 result->tv_sec = r.tv_sec;
2022 result->tv_usec = r.tv_usec;
2027 g_return_if_fail (result != NULL);
2029 GetSystemTimeAsFileTime (&ft);
2030 memmove (&time64, &ft, sizeof (FILETIME));
2032 /* Convert from 100s of nanoseconds since 1601-01-01
2033 * to Unix epoch. Yes, this is Y2038 unsafe.
2035 time64 -= G_GINT64_CONSTANT (116444736000000000);
2038 result->tv_sec = time64 / 1000000;
2039 result->tv_usec = time64 % 1000000;
2046 * Queries the system wall-clock time.
2048 * This call is functionally equivalent to g_get_current_time() except
2049 * that the return value is often more convenient than dealing with a
2052 * You should only use this call if you are actually interested in the real
2053 * wall-clock time. g_get_monotonic_time() is probably more useful for
2054 * measuring intervals.
2056 * Returns: the number of microseconds since January 1, 1970 UTC.
2061 g_get_real_time (void)
2065 g_get_current_time (&tv);
2067 return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec;
2071 * g_get_monotonic_time:
2073 * Queries the system monotonic time, if available.
2075 * On POSIX systems with clock_gettime() and %CLOCK_MONOTONIC this call
2076 * is a very shallow wrapper for that. Otherwise, we make a best effort
2077 * that probably involves returning the wall clock time (with at least
2078 * microsecond accuracy, subject to the limitations of the OS kernel).
2080 * Note that, on Windows, "limitations of the OS kernel" is a rather
2081 * substantial statement. Depending on the configuration of the system,
2082 * the wall clock time is updated as infrequently as 64 times a second
2083 * (which is approximately every 16ms).
2085 * Returns: the monotonic time, in microseconds
2090 g_get_monotonic_time (void)
2092 #ifdef HAVE_CLOCK_GETTIME
2093 /* librt clock_gettime() is our first choice */
2095 static int clockid = CLOCK_REALTIME;
2098 #ifdef HAVE_MONOTONIC_CLOCK
2099 /* We have to check if we actually have monotonic clock support.
2101 * There is no thread safety issue here since there is no harm if we
2105 static gboolean checked;
2107 if G_UNLIKELY (!checked)
2109 if (sysconf (_SC_MONOTONIC_CLOCK) >= 0)
2110 clockid = CLOCK_MONOTONIC;
2116 clock_gettime (clockid, &ts);
2118 /* In theory monotonic time can have any epoch.
2120 * glib presently assumes the following:
2122 * 1) The epoch comes some time after the birth of Jesus of Nazareth, but
2123 * not more than 10000 years later.
2125 * 2) The current time also falls sometime within this range.
2127 * These two reasonable assumptions leave us with a maximum deviation from
2128 * the epoch of 10000 years, or 315569520000000000 seconds.
2130 * If we restrict ourselves to this range then the number of microseconds
2131 * will always fit well inside the constraints of a int64 (by a factor of
2134 * If you actually hit the following assertion, probably you should file a
2135 * bug against your operating system for being excessively silly.
2137 g_assert (G_GINT64_CONSTANT (-315569520000000000) < ts.tv_sec &&
2138 ts.tv_sec < G_GINT64_CONSTANT (315569520000000000));
2140 return (((gint64) ts.tv_sec) * 1000000) + (ts.tv_nsec / 1000);
2143 /* It may look like we are discarding accuracy on Windows (since its
2144 * current time is expressed in 100s of nanoseconds) but according to
2145 * many sources, the time is only updated 64 times per second, so
2146 * microsecond accuracy is more than enough.
2151 g_get_current_time (&tv);
2153 return (((gint64) tv.tv_sec) * 1000000) + tv.tv_usec;
2159 g_main_dispatch_free (gpointer dispatch)
2161 g_slice_free (GMainDispatch, dispatch);
2164 /* Running the main loop */
2166 static GMainDispatch *
2169 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
2170 GMainDispatch *dispatch = g_static_private_get (&depth_private);
2173 dispatch = g_slice_new0 (GMainDispatch);
2174 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
2183 * Returns the depth of the stack of calls to
2184 * g_main_context_dispatch() on any #GMainContext in the current thread.
2185 * That is, when called from the toplevel, it gives 0. When
2186 * called from within a callback from g_main_context_iteration()
2187 * (or g_main_loop_run(), etc.) it returns 1. When called from within
2188 * a callback to a recursive call to g_main_context_iterate(),
2189 * it returns 2. And so forth.
2191 * This function is useful in a situation like the following:
2192 * Imagine an extremely simple "garbage collected" system.
2195 * static GList *free_list;
2198 * allocate_memory (gsize size)
2200 * gpointer result = g_malloc (size);
2201 * free_list = g_list_prepend (free_list, result);
2206 * free_allocated_memory (void)
2209 * for (l = free_list; l; l = l->next);
2211 * g_list_free (free_list);
2219 * g_main_context_iteration (NULL, TRUE);
2220 * free_allocated_memory();
2224 * This works from an application, however, if you want to do the same
2225 * thing from a library, it gets more difficult, since you no longer
2226 * control the main loop. You might think you can simply use an idle
2227 * function to make the call to free_allocated_memory(), but that
2228 * doesn't work, since the idle function could be called from a
2229 * recursive callback. This can be fixed by using g_main_depth()
2233 * allocate_memory (gsize size)
2235 * FreeListBlock *block = g_new (FreeListBlock, 1);
2236 * block->mem = g_malloc (size);
2237 * block->depth = g_main_depth ();
2238 * free_list = g_list_prepend (free_list, block);
2239 * return block->mem;
2243 * free_allocated_memory (void)
2247 * int depth = g_main_depth ();
2248 * for (l = free_list; l; );
2250 * GList *next = l->next;
2251 * FreeListBlock *block = l->data;
2252 * if (block->depth > depth)
2254 * g_free (block->mem);
2256 * free_list = g_list_delete_link (free_list, l);
2264 * There is a temptation to use g_main_depth() to solve
2265 * problems with reentrancy. For instance, while waiting for data
2266 * to be received from the network in response to a menu item,
2267 * the menu item might be selected again. It might seem that
2268 * one could make the menu item's callback return immediately
2269 * and do nothing if g_main_depth() returns a value greater than 1.
2270 * However, this should be avoided since the user then sees selecting
2271 * the menu item do nothing. Furthermore, you'll find yourself adding
2272 * these checks all over your code, since there are doubtless many,
2273 * many things that the user could do. Instead, you can use the
2274 * following techniques:
2279 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
2280 * the user from interacting with elements while the main
2281 * loop is recursing.
2286 * Avoid main loop recursion in situations where you can't handle
2287 * arbitrary callbacks. Instead, structure your code so that you
2288 * simply return to the main loop and then get called again when
2289 * there is more work to do.
2294 * Return value: The main loop recursion level in the current thread
2299 GMainDispatch *dispatch = get_dispatch ();
2300 return dispatch->depth;
2304 * g_main_current_source:
2306 * Returns the currently firing source for this thread.
2308 * Return value: The currently firing source or %NULL.
2313 g_main_current_source (void)
2315 GMainDispatch *dispatch = get_dispatch ();
2316 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
2320 * g_source_is_destroyed:
2321 * @source: a #GSource
2323 * Returns whether @source has been destroyed.
2325 * This is important when you operate upon your objects
2326 * from within idle handlers, but may have freed the object
2327 * before the dispatch of your idle handler.
2331 * idle_callback (gpointer data)
2333 * SomeWidget *self = data;
2335 * GDK_THREADS_ENTER (<!-- -->);
2336 * /<!-- -->* do stuff with self *<!-- -->/
2337 * self->idle_id = 0;
2338 * GDK_THREADS_LEAVE (<!-- -->);
2344 * some_widget_do_stuff_later (SomeWidget *self)
2346 * self->idle_id = g_idle_add (idle_callback, self);
2350 * some_widget_finalize (GObject *object)
2352 * SomeWidget *self = SOME_WIDGET (object);
2354 * if (self->idle_id)
2355 * g_source_remove (self->idle_id);
2357 * G_OBJECT_CLASS (parent_class)->finalize (object);
2361 * This will fail in a multi-threaded application if the
2362 * widget is destroyed before the idle handler fires due
2363 * to the use after free in the callback. A solution, to
2364 * this particular problem, is to check to if the source
2365 * has already been destroy within the callback.
2369 * idle_callback (gpointer data)
2371 * SomeWidget *self = data;
2373 * GDK_THREADS_ENTER ();
2374 * if (!g_source_is_destroyed (g_main_current_source ()))
2376 * /<!-- -->* do stuff with self *<!-- -->/
2378 * GDK_THREADS_LEAVE ();
2384 * Return value: %TRUE if the source has been destroyed
2389 g_source_is_destroyed (GSource *source)
2391 return SOURCE_DESTROYED (source);
2394 /* Temporarily remove all this source's file descriptors from the
2395 * poll(), so that if data comes available for one of the file descriptors
2396 * we don't continually spin in the poll()
2398 /* HOLDS: source->context's lock */
2400 block_source (GSource *source)
2404 g_return_if_fail (!SOURCE_BLOCKED (source));
2406 tmp_list = source->poll_fds;
2409 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
2410 tmp_list = tmp_list->next;
2414 /* HOLDS: source->context's lock */
2416 unblock_source (GSource *source)
2420 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
2421 g_return_if_fail (!SOURCE_DESTROYED (source));
2423 tmp_list = source->poll_fds;
2426 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
2427 tmp_list = tmp_list->next;
2431 /* HOLDS: context's lock */
2433 g_main_dispatch (GMainContext *context)
2435 GMainDispatch *current = get_dispatch ();
2438 for (i = 0; i < context->pending_dispatches->len; i++)
2440 GSource *source = context->pending_dispatches->pdata[i];
2442 context->pending_dispatches->pdata[i] = NULL;
2445 source->flags &= ~G_SOURCE_READY;
2447 if (!SOURCE_DESTROYED (source))
2449 gboolean was_in_call;
2450 gpointer user_data = NULL;
2451 GSourceFunc callback = NULL;
2452 GSourceCallbackFuncs *cb_funcs;
2454 gboolean need_destroy;
2456 gboolean (*dispatch) (GSource *,
2459 GSList current_source_link;
2461 dispatch = source->source_funcs->dispatch;
2462 cb_funcs = source->callback_funcs;
2463 cb_data = source->callback_data;
2466 cb_funcs->ref (cb_data);
2468 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2469 block_source (source);
2471 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2472 source->flags |= G_HOOK_FLAG_IN_CALL;
2475 cb_funcs->get (cb_data, source, &callback, &user_data);
2477 UNLOCK_CONTEXT (context);
2480 /* The on-stack allocation of the GSList is unconventional, but
2481 * we know that the lifetime of the link is bounded to this
2482 * function as the link is kept in a thread specific list and
2483 * not manipulated outside of this function and its descendants.
2484 * Avoiding the overhead of a g_slist_alloc() is useful as many
2485 * applications do little more than dispatch events.
2487 * This is a performance hack - do not revert to g_slist_prepend()!
2489 current_source_link.data = source;
2490 current_source_link.next = current->dispatching_sources;
2491 current->dispatching_sources = ¤t_source_link;
2492 need_destroy = ! dispatch (source,
2495 g_assert (current->dispatching_sources == ¤t_source_link);
2496 current->dispatching_sources = current_source_link.next;
2500 cb_funcs->unref (cb_data);
2502 LOCK_CONTEXT (context);
2505 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2507 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2508 !SOURCE_DESTROYED (source))
2509 unblock_source (source);
2511 /* Note: this depends on the fact that we can't switch
2512 * sources from one main context to another
2514 if (need_destroy && !SOURCE_DESTROYED (source))
2516 g_assert (source->context == context);
2517 g_source_destroy_internal (source, context, TRUE);
2521 SOURCE_UNREF (source, context);
2524 g_ptr_array_set_size (context->pending_dispatches, 0);
2527 /* Holds context's lock */
2528 static inline GSource *
2529 next_valid_source (GMainContext *context,
2532 GSource *new_source = source ? source->next : context->source_list;
2536 if (!SOURCE_DESTROYED (new_source))
2538 new_source->ref_count++;
2542 new_source = new_source->next;
2546 SOURCE_UNREF (source, context);
2552 * g_main_context_acquire:
2553 * @context: a #GMainContext
2555 * Tries to become the owner of the specified context.
2556 * If some other thread is the owner of the context,
2557 * returns %FALSE immediately. Ownership is properly
2558 * recursive: the owner can require ownership again
2559 * and will release ownership when g_main_context_release()
2560 * is called as many times as g_main_context_acquire().
2562 * You must be the owner of a context before you
2563 * can call g_main_context_prepare(), g_main_context_query(),
2564 * g_main_context_check(), g_main_context_dispatch().
2566 * Return value: %TRUE if the operation succeeded, and
2567 * this thread is now the owner of @context.
2570 g_main_context_acquire (GMainContext *context)
2572 #ifdef G_THREADS_ENABLED
2573 gboolean result = FALSE;
2574 GThread *self = G_THREAD_SELF;
2576 if (context == NULL)
2577 context = g_main_context_default ();
2579 LOCK_CONTEXT (context);
2581 if (!context->owner)
2583 context->owner = self;
2584 g_assert (context->owner_count == 0);
2587 if (context->owner == self)
2589 context->owner_count++;
2593 UNLOCK_CONTEXT (context);
2596 #else /* !G_THREADS_ENABLED */
2598 #endif /* G_THREADS_ENABLED */
2602 * g_main_context_release:
2603 * @context: a #GMainContext
2605 * Releases ownership of a context previously acquired by this thread
2606 * with g_main_context_acquire(). If the context was acquired multiple
2607 * times, the ownership will be released only when g_main_context_release()
2608 * is called as many times as it was acquired.
2611 g_main_context_release (GMainContext *context)
2613 #ifdef G_THREADS_ENABLED
2614 if (context == NULL)
2615 context = g_main_context_default ();
2617 LOCK_CONTEXT (context);
2619 context->owner_count--;
2620 if (context->owner_count == 0)
2622 context->owner = NULL;
2624 if (context->waiters)
2626 GMainWaiter *waiter = context->waiters->data;
2627 gboolean loop_internal_waiter =
2628 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2629 context->waiters = g_slist_delete_link (context->waiters,
2631 if (!loop_internal_waiter)
2632 g_mutex_lock (waiter->mutex);
2634 g_cond_signal (waiter->cond);
2636 if (!loop_internal_waiter)
2637 g_mutex_unlock (waiter->mutex);
2641 UNLOCK_CONTEXT (context);
2642 #endif /* G_THREADS_ENABLED */
2646 * g_main_context_wait:
2647 * @context: a #GMainContext
2648 * @cond: a condition variable
2649 * @mutex: a mutex, currently held
2651 * Tries to become the owner of the specified context,
2652 * as with g_main_context_acquire(). But if another thread
2653 * is the owner, atomically drop @mutex and wait on @cond until
2654 * that owner releases ownership or until @cond is signaled, then
2655 * try again (once) to become the owner.
2657 * Return value: %TRUE if the operation succeeded, and
2658 * this thread is now the owner of @context.
2661 g_main_context_wait (GMainContext *context,
2665 #ifdef G_THREADS_ENABLED
2666 gboolean result = FALSE;
2667 GThread *self = G_THREAD_SELF;
2668 gboolean loop_internal_waiter;
2670 if (context == NULL)
2671 context = g_main_context_default ();
2673 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2675 if (!loop_internal_waiter)
2676 LOCK_CONTEXT (context);
2678 if (context->owner && context->owner != self)
2683 waiter.mutex = mutex;
2685 context->waiters = g_slist_append (context->waiters, &waiter);
2687 if (!loop_internal_waiter)
2688 UNLOCK_CONTEXT (context);
2689 g_cond_wait (cond, mutex);
2690 if (!loop_internal_waiter)
2691 LOCK_CONTEXT (context);
2693 context->waiters = g_slist_remove (context->waiters, &waiter);
2696 if (!context->owner)
2698 context->owner = self;
2699 g_assert (context->owner_count == 0);
2702 if (context->owner == self)
2704 context->owner_count++;
2708 if (!loop_internal_waiter)
2709 UNLOCK_CONTEXT (context);
2712 #else /* !G_THREADS_ENABLED */
2714 #endif /* G_THREADS_ENABLED */
2718 * g_main_context_prepare:
2719 * @context: a #GMainContext
2720 * @priority: location to store priority of highest priority
2721 * source already ready.
2723 * Prepares to poll sources within a main loop. The resulting information
2724 * for polling is determined by calling g_main_context_query ().
2726 * Return value: %TRUE if some source is ready to be dispatched
2730 g_main_context_prepare (GMainContext *context,
2735 gint current_priority = G_MAXINT;
2738 if (context == NULL)
2739 context = g_main_context_default ();
2741 LOCK_CONTEXT (context);
2743 context->time_is_fresh = FALSE;
2744 context->real_time_is_fresh = FALSE;
2746 if (context->in_check_or_prepare)
2748 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2749 "prepare() member.");
2750 UNLOCK_CONTEXT (context);
2754 #ifdef G_THREADS_ENABLED
2755 if (context->poll_waiting)
2757 g_warning("g_main_context_prepare(): main loop already active in another thread");
2758 UNLOCK_CONTEXT (context);
2762 context->poll_waiting = TRUE;
2763 #endif /* G_THREADS_ENABLED */
2766 /* If recursing, finish up current dispatch, before starting over */
2767 if (context->pending_dispatches)
2770 g_main_dispatch (context, ¤t_time);
2772 UNLOCK_CONTEXT (context);
2777 /* If recursing, clear list of pending dispatches */
2779 for (i = 0; i < context->pending_dispatches->len; i++)
2781 if (context->pending_dispatches->pdata[i])
2782 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2784 g_ptr_array_set_size (context->pending_dispatches, 0);
2786 /* Prepare all sources */
2788 context->timeout = -1;
2790 source = next_valid_source (context, NULL);
2793 gint source_timeout = -1;
2795 if ((n_ready > 0) && (source->priority > current_priority))
2797 SOURCE_UNREF (source, context);
2800 if (SOURCE_BLOCKED (source))
2803 if (!(source->flags & G_SOURCE_READY))
2806 gboolean (*prepare) (GSource *source,
2809 prepare = source->source_funcs->prepare;
2810 context->in_check_or_prepare++;
2811 UNLOCK_CONTEXT (context);
2813 result = (*prepare) (source, &source_timeout);
2815 LOCK_CONTEXT (context);
2816 context->in_check_or_prepare--;
2820 GSource *ready_source = source;
2822 while (ready_source)
2824 ready_source->flags |= G_SOURCE_READY;
2825 ready_source = ready_source->priv ? ready_source->priv->parent_source : NULL;
2830 if (source->flags & G_SOURCE_READY)
2833 current_priority = source->priority;
2834 context->timeout = 0;
2837 if (source_timeout >= 0)
2839 if (context->timeout < 0)
2840 context->timeout = source_timeout;
2842 context->timeout = MIN (context->timeout, source_timeout);
2846 source = next_valid_source (context, source);
2849 UNLOCK_CONTEXT (context);
2852 *priority = current_priority;
2854 return (n_ready > 0);
2858 * g_main_context_query:
2859 * @context: a #GMainContext
2860 * @max_priority: maximum priority source to check
2861 * @timeout_: location to store timeout to be used in polling
2862 * @fds: location to store #GPollFD records that need to be polled.
2863 * @n_fds: length of @fds.
2865 * Determines information necessary to poll this main loop.
2867 * Return value: the number of records actually stored in @fds,
2868 * or, if more than @n_fds records need to be stored, the number
2869 * of records that need to be stored.
2872 g_main_context_query (GMainContext *context,
2881 LOCK_CONTEXT (context);
2883 pollrec = context->poll_records;
2885 while (pollrec && max_priority >= pollrec->priority)
2887 /* We need to include entries with fd->events == 0 in the array because
2888 * otherwise if the application changes fd->events behind our back and
2889 * makes it non-zero, we'll be out of sync when we check the fds[] array.
2890 * (Changing fd->events after adding an FD wasn't an anticipated use of
2891 * this API, but it occurs in practice.) */
2894 fds[n_poll].fd = pollrec->fd->fd;
2895 /* In direct contradiction to the Unix98 spec, IRIX runs into
2896 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2897 * flags in the events field of the pollfd while it should
2898 * just ignoring them. So we mask them out here.
2900 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2901 fds[n_poll].revents = 0;
2904 pollrec = pollrec->next;
2908 #ifdef G_THREADS_ENABLED
2909 context->poll_changed = FALSE;
2914 *timeout = context->timeout;
2917 context->time_is_fresh = FALSE;
2918 context->real_time_is_fresh = FALSE;
2922 UNLOCK_CONTEXT (context);
2928 * g_main_context_check:
2929 * @context: a #GMainContext
2930 * @max_priority: the maximum numerical priority of sources to check
2931 * @fds: array of #GPollFD's that was passed to the last call to
2932 * g_main_context_query()
2933 * @n_fds: return value of g_main_context_query()
2935 * Passes the results of polling back to the main loop.
2937 * Return value: %TRUE if some sources are ready to be dispatched.
2940 g_main_context_check (GMainContext *context,
2950 LOCK_CONTEXT (context);
2952 if (context->in_check_or_prepare)
2954 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2955 "prepare() member.");
2956 UNLOCK_CONTEXT (context);
2960 #ifdef G_THREADS_ENABLED
2961 if (!context->poll_waiting)
2965 read (context->wake_up_pipe[0], &a, 1);
2969 context->poll_waiting = FALSE;
2971 /* If the set of poll file descriptors changed, bail out
2972 * and let the main loop rerun
2974 if (context->poll_changed)
2976 UNLOCK_CONTEXT (context);
2979 #endif /* G_THREADS_ENABLED */
2981 pollrec = context->poll_records;
2985 if (pollrec->fd->events)
2986 pollrec->fd->revents = fds[i].revents;
2988 pollrec = pollrec->next;
2992 source = next_valid_source (context, NULL);
2995 if ((n_ready > 0) && (source->priority > max_priority))
2997 SOURCE_UNREF (source, context);
3000 if (SOURCE_BLOCKED (source))
3003 if (!(source->flags & G_SOURCE_READY))
3006 gboolean (*check) (GSource *source);
3008 check = source->source_funcs->check;
3010 context->in_check_or_prepare++;
3011 UNLOCK_CONTEXT (context);
3013 result = (*check) (source);
3015 LOCK_CONTEXT (context);
3016 context->in_check_or_prepare--;
3020 GSource *ready_source = source;
3022 while (ready_source)
3024 ready_source->flags |= G_SOURCE_READY;
3025 ready_source = ready_source->priv ? ready_source->priv->parent_source : NULL;
3030 if (source->flags & G_SOURCE_READY)
3032 source->ref_count++;
3033 g_ptr_array_add (context->pending_dispatches, source);
3037 /* never dispatch sources with less priority than the first
3038 * one we choose to dispatch
3040 max_priority = source->priority;
3044 source = next_valid_source (context, source);
3047 UNLOCK_CONTEXT (context);
3053 * g_main_context_dispatch:
3054 * @context: a #GMainContext
3056 * Dispatches all pending sources.
3059 g_main_context_dispatch (GMainContext *context)
3061 LOCK_CONTEXT (context);
3063 if (context->pending_dispatches->len > 0)
3065 g_main_dispatch (context);
3068 UNLOCK_CONTEXT (context);
3071 /* HOLDS context lock */
3073 g_main_context_iterate (GMainContext *context,
3080 gboolean some_ready;
3081 gint nfds, allocated_nfds;
3082 GPollFD *fds = NULL;
3084 UNLOCK_CONTEXT (context);
3086 #ifdef G_THREADS_ENABLED
3087 if (!g_main_context_acquire (context))
3089 gboolean got_ownership;
3091 LOCK_CONTEXT (context);
3093 g_return_val_if_fail (g_thread_supported (), FALSE);
3099 context->cond = g_cond_new ();
3101 got_ownership = g_main_context_wait (context,
3103 g_static_mutex_get_mutex (&context->mutex));
3109 LOCK_CONTEXT (context);
3110 #endif /* G_THREADS_ENABLED */
3112 if (!context->cached_poll_array)
3114 context->cached_poll_array_size = context->n_poll_records;
3115 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
3118 allocated_nfds = context->cached_poll_array_size;
3119 fds = context->cached_poll_array;
3121 UNLOCK_CONTEXT (context);
3123 g_main_context_prepare (context, &max_priority);
3125 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
3126 allocated_nfds)) > allocated_nfds)
3128 LOCK_CONTEXT (context);
3130 context->cached_poll_array_size = allocated_nfds = nfds;
3131 context->cached_poll_array = fds = g_new (GPollFD, nfds);
3132 UNLOCK_CONTEXT (context);
3138 g_main_context_poll (context, timeout, max_priority, fds, nfds);
3140 some_ready = g_main_context_check (context, max_priority, fds, nfds);
3143 g_main_context_dispatch (context);
3145 #ifdef G_THREADS_ENABLED
3146 g_main_context_release (context);
3147 #endif /* G_THREADS_ENABLED */
3149 LOCK_CONTEXT (context);
3155 * g_main_context_pending:
3156 * @context: a #GMainContext (if %NULL, the default context will be used)
3158 * Checks if any sources have pending events for the given context.
3160 * Return value: %TRUE if events are pending.
3163 g_main_context_pending (GMainContext *context)
3168 context = g_main_context_default();
3170 LOCK_CONTEXT (context);
3171 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
3172 UNLOCK_CONTEXT (context);
3178 * g_main_context_iteration:
3179 * @context: a #GMainContext (if %NULL, the default context will be used)
3180 * @may_block: whether the call may block.
3182 * Runs a single iteration for the given main loop. This involves
3183 * checking to see if any event sources are ready to be processed,
3184 * then if no events sources are ready and @may_block is %TRUE, waiting
3185 * for a source to become ready, then dispatching the highest priority
3186 * events sources that are ready. Otherwise, if @may_block is %FALSE
3187 * sources are not waited to become ready, only those highest priority
3188 * events sources will be dispatched (if any), that are ready at this
3189 * given moment without further waiting.
3191 * Note that even when @may_block is %TRUE, it is still possible for
3192 * g_main_context_iteration() to return %FALSE, since the the wait may
3193 * be interrupted for other reasons than an event source becoming ready.
3195 * Return value: %TRUE if events were dispatched.
3198 g_main_context_iteration (GMainContext *context, gboolean may_block)
3203 context = g_main_context_default();
3205 LOCK_CONTEXT (context);
3206 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
3207 UNLOCK_CONTEXT (context);
3214 * @context: a #GMainContext (if %NULL, the default context will be used).
3215 * @is_running: set to %TRUE to indicate that the loop is running. This
3216 * is not very important since calling g_main_loop_run() will set this to
3219 * Creates a new #GMainLoop structure.
3221 * Return value: a new #GMainLoop.
3224 g_main_loop_new (GMainContext *context,
3225 gboolean is_running)
3230 context = g_main_context_default();
3232 g_main_context_ref (context);
3234 loop = g_new0 (GMainLoop, 1);
3235 loop->context = context;
3236 loop->is_running = is_running != FALSE;
3237 loop->ref_count = 1;
3244 * @loop: a #GMainLoop
3246 * Increases the reference count on a #GMainLoop object by one.
3248 * Return value: @loop
3251 g_main_loop_ref (GMainLoop *loop)
3253 g_return_val_if_fail (loop != NULL, NULL);
3254 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
3256 g_atomic_int_inc (&loop->ref_count);
3262 * g_main_loop_unref:
3263 * @loop: a #GMainLoop
3265 * Decreases the reference count on a #GMainLoop object by one. If
3266 * the result is zero, free the loop and free all associated memory.
3269 g_main_loop_unref (GMainLoop *loop)
3271 g_return_if_fail (loop != NULL);
3272 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
3274 if (!g_atomic_int_dec_and_test (&loop->ref_count))
3277 g_main_context_unref (loop->context);
3283 * @loop: a #GMainLoop
3285 * Runs a main loop until g_main_loop_quit() is called on the loop.
3286 * If this is called for the thread of the loop's #GMainContext,
3287 * it will process events from the loop, otherwise it will
3291 g_main_loop_run (GMainLoop *loop)
3293 GThread *self = G_THREAD_SELF;
3295 g_return_if_fail (loop != NULL);
3296 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
3298 #ifdef G_THREADS_ENABLED
3299 if (!g_main_context_acquire (loop->context))
3301 gboolean got_ownership = FALSE;
3303 /* Another thread owns this context */
3304 if (!g_thread_supported ())
3306 g_warning ("g_main_loop_run() was called from second thread but "
3307 "g_thread_init() was never called.");
3311 LOCK_CONTEXT (loop->context);
3313 g_atomic_int_inc (&loop->ref_count);
3315 if (!loop->is_running)
3316 loop->is_running = TRUE;
3318 if (!loop->context->cond)
3319 loop->context->cond = g_cond_new ();
3321 while (loop->is_running && !got_ownership)
3322 got_ownership = g_main_context_wait (loop->context,
3323 loop->context->cond,
3324 g_static_mutex_get_mutex (&loop->context->mutex));
3326 if (!loop->is_running)
3328 UNLOCK_CONTEXT (loop->context);
3330 g_main_context_release (loop->context);
3331 g_main_loop_unref (loop);
3335 g_assert (got_ownership);
3338 LOCK_CONTEXT (loop->context);
3339 #endif /* G_THREADS_ENABLED */
3341 if (loop->context->in_check_or_prepare)
3343 g_warning ("g_main_loop_run(): called recursively from within a source's "
3344 "check() or prepare() member, iteration not possible.");
3348 g_atomic_int_inc (&loop->ref_count);
3349 loop->is_running = TRUE;
3350 while (loop->is_running)
3351 g_main_context_iterate (loop->context, TRUE, TRUE, self);
3353 UNLOCK_CONTEXT (loop->context);
3355 #ifdef G_THREADS_ENABLED
3356 g_main_context_release (loop->context);
3357 #endif /* G_THREADS_ENABLED */
3359 g_main_loop_unref (loop);
3364 * @loop: a #GMainLoop
3366 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
3367 * for the loop will return.
3369 * Note that sources that have already been dispatched when
3370 * g_main_loop_quit() is called will still be executed.
3373 g_main_loop_quit (GMainLoop *loop)
3375 g_return_if_fail (loop != NULL);
3376 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
3378 LOCK_CONTEXT (loop->context);
3379 loop->is_running = FALSE;
3380 g_main_context_wakeup_unlocked (loop->context);
3382 #ifdef G_THREADS_ENABLED
3383 if (loop->context->cond)
3384 g_cond_broadcast (loop->context->cond);
3385 #endif /* G_THREADS_ENABLED */
3387 UNLOCK_CONTEXT (loop->context);
3391 * g_main_loop_is_running:
3392 * @loop: a #GMainLoop.
3394 * Checks to see if the main loop is currently being run via g_main_loop_run().
3396 * Return value: %TRUE if the mainloop is currently being run.
3399 g_main_loop_is_running (GMainLoop *loop)
3401 g_return_val_if_fail (loop != NULL, FALSE);
3402 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
3404 return loop->is_running;
3408 * g_main_loop_get_context:
3409 * @loop: a #GMainLoop.
3411 * Returns the #GMainContext of @loop.
3413 * Return value: the #GMainContext of @loop
3416 g_main_loop_get_context (GMainLoop *loop)
3418 g_return_val_if_fail (loop != NULL, NULL);
3419 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
3421 return loop->context;
3424 /* HOLDS: context's lock */
3426 g_main_context_poll (GMainContext *context,
3432 #ifdef G_MAIN_POLL_DEBUG
3438 GPollFunc poll_func;
3440 if (n_fds || timeout != 0)
3442 #ifdef G_MAIN_POLL_DEBUG
3443 if (_g_main_poll_debug)
3445 g_print ("polling context=%p n=%d timeout=%d\n",
3446 context, n_fds, timeout);
3447 poll_timer = g_timer_new ();
3451 LOCK_CONTEXT (context);
3453 poll_func = context->poll_func;
3455 UNLOCK_CONTEXT (context);
3456 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
3459 g_warning ("poll(2) failed due to: %s.",
3460 g_strerror (errno));
3462 /* If g_poll () returns -1, it has already called g_warning() */
3466 #ifdef G_MAIN_POLL_DEBUG
3467 if (_g_main_poll_debug)
3469 LOCK_CONTEXT (context);
3471 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3474 g_timer_elapsed (poll_timer, NULL));
3475 g_timer_destroy (poll_timer);
3476 pollrec = context->poll_records;
3478 while (pollrec != NULL)
3483 if (fds[i].fd == pollrec->fd->fd &&
3484 pollrec->fd->events &&
3487 g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd);
3488 if (fds[i].revents & G_IO_IN)
3490 if (fds[i].revents & G_IO_OUT)
3492 if (fds[i].revents & G_IO_PRI)
3494 if (fds[i].revents & G_IO_ERR)
3496 if (fds[i].revents & G_IO_HUP)
3498 if (fds[i].revents & G_IO_NVAL)
3504 pollrec = pollrec->next;
3508 UNLOCK_CONTEXT (context);
3511 } /* if (n_fds || timeout != 0) */
3515 * g_main_context_add_poll:
3516 * @context: a #GMainContext (or %NULL for the default context)
3517 * @fd: a #GPollFD structure holding information about a file
3518 * descriptor to watch.
3519 * @priority: the priority for this file descriptor which should be
3520 * the same as the priority used for g_source_attach() to ensure that the
3521 * file descriptor is polled whenever the results may be needed.
3523 * Adds a file descriptor to the set of file descriptors polled for
3524 * this context. This will very seldomly be used directly. Instead
3525 * a typical event source will use g_source_add_poll() instead.
3528 g_main_context_add_poll (GMainContext *context,
3533 context = g_main_context_default ();
3535 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3536 g_return_if_fail (fd);
3538 LOCK_CONTEXT (context);
3539 g_main_context_add_poll_unlocked (context, priority, fd);
3540 UNLOCK_CONTEXT (context);
3543 /* HOLDS: main_loop_lock */
3545 g_main_context_add_poll_unlocked (GMainContext *context,
3549 GPollRec *lastrec, *pollrec;
3550 GPollRec *newrec = g_slice_new (GPollRec);
3552 /* This file descriptor may be checked before we ever poll */
3555 newrec->priority = priority;
3558 pollrec = context->poll_records;
3559 while (pollrec && priority >= pollrec->priority)
3562 pollrec = pollrec->next;
3566 lastrec->next = newrec;
3568 context->poll_records = newrec;
3570 newrec->next = pollrec;
3572 context->n_poll_records++;
3574 #ifdef G_THREADS_ENABLED
3575 context->poll_changed = TRUE;
3577 /* Now wake up the main loop if it is waiting in the poll() */
3578 g_main_context_wakeup_unlocked (context);
3583 * g_main_context_remove_poll:
3584 * @context:a #GMainContext
3585 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3587 * Removes file descriptor from the set of file descriptors to be
3588 * polled for a particular context.
3591 g_main_context_remove_poll (GMainContext *context,
3595 context = g_main_context_default ();
3597 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3598 g_return_if_fail (fd);
3600 LOCK_CONTEXT (context);
3601 g_main_context_remove_poll_unlocked (context, fd);
3602 UNLOCK_CONTEXT (context);
3606 g_main_context_remove_poll_unlocked (GMainContext *context,
3609 GPollRec *pollrec, *lastrec;
3612 pollrec = context->poll_records;
3616 if (pollrec->fd == fd)
3618 if (lastrec != NULL)
3619 lastrec->next = pollrec->next;
3621 context->poll_records = pollrec->next;
3623 g_slice_free (GPollRec, pollrec);
3625 context->n_poll_records--;
3629 pollrec = pollrec->next;
3632 #ifdef G_THREADS_ENABLED
3633 context->poll_changed = TRUE;
3635 /* Now wake up the main loop if it is waiting in the poll() */
3636 g_main_context_wakeup_unlocked (context);
3641 * g_source_get_current_time:
3642 * @source: a #GSource
3643 * @timeval: #GTimeVal structure in which to store current time.
3645 * Gets the "current time" to be used when checking
3646 * this source. The advantage of calling this function over
3647 * calling g_get_current_time() directly is that when
3648 * checking multiple sources, GLib can cache a single value
3649 * instead of having to repeatedly get the system time.
3651 * Deprecated: 2.28: use g_source_get_time() instead
3654 g_source_get_current_time (GSource *source,
3657 GMainContext *context;
3659 g_return_if_fail (source->context != NULL);
3661 context = source->context;
3663 LOCK_CONTEXT (context);
3665 if (!context->real_time_is_fresh)
3667 context->real_time = g_get_real_time ();
3668 context->real_time_is_fresh = TRUE;
3671 timeval->tv_sec = context->real_time / 1000000;
3672 timeval->tv_usec = context->real_time % 1000000;
3674 UNLOCK_CONTEXT (context);
3678 * g_source_get_time:
3679 * @source: a #GSource
3681 * Gets the time to be used when checking this source. The advantage of
3682 * calling this function over calling g_get_monotonic_time() directly is
3683 * that when checking multiple sources, GLib can cache a single value
3684 * instead of having to repeatedly get the system monotonic time.
3686 * The time here is the system monotonic time, if available, or some
3687 * other reasonable alternative otherwise. See g_get_monotonic_time().
3689 * Returns: the monotonic time in microseconds
3694 g_source_get_time (GSource *source)
3696 GMainContext *context;
3699 g_return_val_if_fail (source->context != NULL, 0);
3701 context = source->context;
3703 LOCK_CONTEXT (context);
3705 if (!context->time_is_fresh)
3707 context->time = g_get_monotonic_time ();
3708 context->time_is_fresh = TRUE;
3711 result = context->time;
3713 UNLOCK_CONTEXT (context);
3719 * g_main_context_set_poll_func:
3720 * @context: a #GMainContext
3721 * @func: the function to call to poll all file descriptors
3723 * Sets the function to use to handle polling of file descriptors. It
3724 * will be used instead of the poll() system call
3725 * (or GLib's replacement function, which is used where
3726 * poll() isn't available).
3728 * This function could possibly be used to integrate the GLib event
3729 * loop with an external event loop.
3732 g_main_context_set_poll_func (GMainContext *context,
3736 context = g_main_context_default ();
3738 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3740 LOCK_CONTEXT (context);
3743 context->poll_func = func;
3745 context->poll_func = g_poll;
3747 UNLOCK_CONTEXT (context);
3751 * g_main_context_get_poll_func:
3752 * @context: a #GMainContext
3754 * Gets the poll function set by g_main_context_set_poll_func().
3756 * Return value: the poll function
3759 g_main_context_get_poll_func (GMainContext *context)
3764 context = g_main_context_default ();
3766 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3768 LOCK_CONTEXT (context);
3769 result = context->poll_func;
3770 UNLOCK_CONTEXT (context);
3776 _g_main_wake_up_all_contexts (void)
3780 /* We were woken up. Wake up all other contexts in all other threads */
3781 G_LOCK (main_context_list);
3782 for (list = main_context_list; list; list = list->next)
3784 GMainContext *context;
3786 context = list->data;
3787 if (g_atomic_int_get (&context->ref_count) > 0)
3788 /* Due to racing conditions we can find ref_count == 0, in
3789 * that case, however, the context is still not destroyed
3790 * and no poll can be active, otherwise the ref_count
3793 g_main_context_wakeup (context);
3795 G_UNLOCK (main_context_list);
3799 /* HOLDS: context's lock */
3800 /* Wake the main loop up from a poll() */
3802 g_main_context_wakeup_unlocked (GMainContext *context)
3804 #ifdef G_THREADS_ENABLED
3805 if (g_thread_supported() && context->poll_waiting)
3807 context->poll_waiting = FALSE;
3809 write (context->wake_up_pipe[1], "A", 1);
3811 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3818 * g_main_context_wakeup:
3819 * @context: a #GMainContext
3821 * If @context is currently waiting in a poll(), interrupt
3822 * the poll(), and continue the iteration process.
3825 g_main_context_wakeup (GMainContext *context)
3828 context = g_main_context_default ();
3830 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3832 LOCK_CONTEXT (context);
3833 g_main_context_wakeup_unlocked (context);
3834 UNLOCK_CONTEXT (context);
3838 * g_main_context_is_owner:
3839 * @context: a #GMainContext
3841 * Determines whether this thread holds the (recursive)
3842 * ownership of this #GMaincontext. This is useful to
3843 * know before waiting on another thread that may be
3844 * blocking to get ownership of @context.
3846 * Returns: %TRUE if current thread is owner of @context.
3851 g_main_context_is_owner (GMainContext *context)
3856 context = g_main_context_default ();
3858 #ifdef G_THREADS_ENABLED
3859 LOCK_CONTEXT (context);
3860 is_owner = context->owner == G_THREAD_SELF;
3861 UNLOCK_CONTEXT (context);
3872 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3873 gint64 current_time)
3875 timeout_source->expiration = current_time +
3876 (guint64) timeout_source->interval * 1000;
3878 if (timeout_source->seconds)
3881 static gint timer_perturb = -1;
3883 if (timer_perturb == -1)
3886 * we want a per machine/session unique 'random' value; try the dbus
3887 * address first, that has a UUID in it. If there is no dbus, use the
3888 * hostname for hashing.
3890 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3891 if (!session_bus_address)
3892 session_bus_address = g_getenv ("HOSTNAME");
3893 if (session_bus_address)
3894 timer_perturb = ABS ((gint) g_str_hash (session_bus_address)) % 1000000;
3899 /* We want the microseconds part of the timeout to land on the
3900 * 'timer_perturb' mark, but we need to make sure we don't try to
3901 * set the timeout in the past. We do this by ensuring that we
3902 * always only *increase* the expiration time by adding a full
3903 * second in the case that the microsecond portion decreases.
3905 timeout_source->expiration -= timer_perturb;
3907 remainder = timeout_source->expiration % 1000000;
3908 if (remainder >= 1000000/4)
3909 timeout_source->expiration += 1000000;
3911 timeout_source->expiration -= remainder;
3912 timeout_source->expiration += timer_perturb;
3917 g_timeout_prepare (GSource *source,
3920 GTimeoutSource *timeout_source = (GTimeoutSource *) source;
3921 gint64 now = g_source_get_time (source);
3923 if (now < timeout_source->expiration)
3925 /* Round up to ensure that we don't try again too early */
3926 *timeout = (timeout_source->expiration - now + 999) / 1000;
3935 g_timeout_check (GSource *source)
3937 GTimeoutSource *timeout_source = (GTimeoutSource *) source;
3938 gint64 now = g_source_get_time (source);
3940 return timeout_source->expiration <= now;
3944 g_timeout_dispatch (GSource *source,
3945 GSourceFunc callback,
3948 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3953 g_warning ("Timeout source dispatched without callback\n"
3954 "You must call g_source_set_callback().");
3958 again = callback (user_data);
3961 g_timeout_set_expiration (timeout_source, g_source_get_time (source));
3967 * g_timeout_source_new:
3968 * @interval: the timeout interval in milliseconds.
3970 * Creates a new timeout source.
3972 * The source will not initially be associated with any #GMainContext
3973 * and must be added to one with g_source_attach() before it will be
3976 * Return value: the newly-created timeout source
3979 g_timeout_source_new (guint interval)
3981 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3982 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3984 timeout_source->interval = interval;
3985 g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
3991 * g_timeout_source_new_seconds:
3992 * @interval: the timeout interval in seconds
3994 * Creates a new timeout source.
3996 * The source will not initially be associated with any #GMainContext
3997 * and must be added to one with g_source_attach() before it will be
4000 * The scheduling granularity/accuracy of this timeout source will be
4003 * Return value: the newly-created timeout source
4008 g_timeout_source_new_seconds (guint interval)
4010 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
4011 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
4013 timeout_source->interval = 1000 * interval;
4014 timeout_source->seconds = TRUE;
4016 g_timeout_set_expiration (timeout_source, g_get_monotonic_time ());
4023 * g_timeout_add_full:
4024 * @priority: the priority of the timeout source. Typically this will be in
4025 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
4026 * @interval: the time between calls to the function, in milliseconds
4027 * (1/1000ths of a second)
4028 * @function: function to call
4029 * @data: data to pass to @function
4030 * @notify: function to call when the timeout is removed, or %NULL
4032 * Sets a function to be called at regular intervals, with the given
4033 * priority. The function is called repeatedly until it returns
4034 * %FALSE, at which point the timeout is automatically destroyed and
4035 * the function will not be called again. The @notify function is
4036 * called when the timeout is destroyed. The first call to the
4037 * function will be at the end of the first @interval.
4039 * Note that timeout functions may be delayed, due to the processing of other
4040 * event sources. Thus they should not be relied on for precise timing.
4041 * After each call to the timeout function, the time of the next
4042 * timeout is recalculated based on the current time and the given interval
4043 * (it does not try to 'catch up' time lost in delays).
4045 * This internally creates a main loop source using g_timeout_source_new()
4046 * and attaches it to the main loop context using g_source_attach(). You can
4047 * do these steps manually if you need greater control.
4049 * Return value: the ID (greater than 0) of the event source.
4052 g_timeout_add_full (gint priority,
4054 GSourceFunc function,
4056 GDestroyNotify notify)
4061 g_return_val_if_fail (function != NULL, 0);
4063 source = g_timeout_source_new (interval);
4065 if (priority != G_PRIORITY_DEFAULT)
4066 g_source_set_priority (source, priority);
4068 g_source_set_callback (source, function, data, notify);
4069 id = g_source_attach (source, NULL);
4070 g_source_unref (source);
4077 * @interval: the time between calls to the function, in milliseconds
4078 * (1/1000ths of a second)
4079 * @function: function to call
4080 * @data: data to pass to @function
4082 * Sets a function to be called at regular intervals, with the default
4083 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
4084 * until it returns %FALSE, at which point the timeout is automatically
4085 * destroyed and the function will not be called again. The first call
4086 * to the function will be at the end of the first @interval.
4088 * Note that timeout functions may be delayed, due to the processing of other
4089 * event sources. Thus they should not be relied on for precise timing.
4090 * After each call to the timeout function, the time of the next
4091 * timeout is recalculated based on the current time and the given interval
4092 * (it does not try to 'catch up' time lost in delays).
4094 * If you want to have a timer in the "seconds" range and do not care
4095 * about the exact time of the first call of the timer, use the
4096 * g_timeout_add_seconds() function; this function allows for more
4097 * optimizations and more efficient system power usage.
4099 * This internally creates a main loop source using g_timeout_source_new()
4100 * and attaches it to the main loop context using g_source_attach(). You can
4101 * do these steps manually if you need greater control.
4103 * Return value: the ID (greater than 0) of the event source.
4106 g_timeout_add (guint32 interval,
4107 GSourceFunc function,
4110 return g_timeout_add_full (G_PRIORITY_DEFAULT,
4111 interval, function, data, NULL);
4115 * g_timeout_add_seconds_full:
4116 * @priority: the priority of the timeout source. Typically this will be in
4117 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
4118 * @interval: the time between calls to the function, in seconds
4119 * @function: function to call
4120 * @data: data to pass to @function
4121 * @notify: function to call when the timeout is removed, or %NULL
4123 * Sets a function to be called at regular intervals, with @priority.
4124 * The function is called repeatedly until it returns %FALSE, at which
4125 * point the timeout is automatically destroyed and the function will
4126 * not be called again.
4128 * Unlike g_timeout_add(), this function operates at whole second granularity.
4129 * The initial starting point of the timer is determined by the implementation
4130 * and the implementation is expected to group multiple timers together so that
4131 * they fire all at the same time.
4132 * To allow this grouping, the @interval to the first timer is rounded
4133 * and can deviate up to one second from the specified interval.
4134 * Subsequent timer iterations will generally run at the specified interval.
4136 * Note that timeout functions may be delayed, due to the processing of other
4137 * event sources. Thus they should not be relied on for precise timing.
4138 * After each call to the timeout function, the time of the next
4139 * timeout is recalculated based on the current time and the given @interval
4141 * If you want timing more precise than whole seconds, use g_timeout_add()
4144 * The grouping of timers to fire at the same time results in a more power
4145 * and CPU efficient behavior so if your timer is in multiples of seconds
4146 * and you don't require the first timer exactly one second from now, the
4147 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
4149 * This internally creates a main loop source using
4150 * g_timeout_source_new_seconds() and attaches it to the main loop context
4151 * using g_source_attach(). You can do these steps manually if you need
4154 * Return value: the ID (greater than 0) of the event source.
4159 g_timeout_add_seconds_full (gint priority,
4161 GSourceFunc function,
4163 GDestroyNotify notify)
4168 g_return_val_if_fail (function != NULL, 0);
4170 source = g_timeout_source_new_seconds (interval);
4172 if (priority != G_PRIORITY_DEFAULT)
4173 g_source_set_priority (source, priority);
4175 g_source_set_callback (source, function, data, notify);
4176 id = g_source_attach (source, NULL);
4177 g_source_unref (source);
4183 * g_timeout_add_seconds:
4184 * @interval: the time between calls to the function, in seconds
4185 * @function: function to call
4186 * @data: data to pass to @function
4188 * Sets a function to be called at regular intervals with the default
4189 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
4190 * it returns %FALSE, at which point the timeout is automatically destroyed
4191 * and the function will not be called again.
4193 * This internally creates a main loop source using
4194 * g_timeout_source_new_seconds() and attaches it to the main loop context
4195 * using g_source_attach(). You can do these steps manually if you need
4196 * greater control. Also see g_timout_add_seconds_full().
4198 * Note that the first call of the timer may not be precise for timeouts
4199 * of one second. If you need finer precision and have such a timeout,
4200 * you may want to use g_timeout_add() instead.
4202 * Return value: the ID (greater than 0) of the event source.
4207 g_timeout_add_seconds (guint interval,
4208 GSourceFunc function,
4211 g_return_val_if_fail (function != NULL, 0);
4213 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
4216 /* Child watch functions */
4221 g_child_watch_prepare (GSource *source,
4230 g_child_watch_check (GSource *source)
4232 GChildWatchSource *child_watch_source;
4233 gboolean child_exited;
4235 child_watch_source = (GChildWatchSource *) source;
4237 child_exited = child_watch_source->poll.revents & G_IO_IN;
4244 * Note: We do _not_ check for the special value of STILL_ACTIVE
4245 * since we know that the process has exited and doing so runs into
4246 * problems if the child process "happens to return STILL_ACTIVE(259)"
4247 * as Microsoft's Platform SDK puts it.
4249 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
4251 gchar *emsg = g_win32_error_message (GetLastError ());
4252 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
4255 child_watch_source->child_status = -1;
4258 child_watch_source->child_status = child_status;
4261 return child_exited;
4264 #else /* G_OS_WIN32 */
4267 check_for_child_exited (GSource *source)
4269 GChildWatchSource *child_watch_source;
4272 /* protect against another SIGCHLD in the middle of this call */
4273 count = child_watch_count;
4275 child_watch_source = (GChildWatchSource *) source;
4277 if (child_watch_source->child_exited)
4280 if (child_watch_source->count < count)
4284 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
4286 child_watch_source->child_status = child_status;
4287 child_watch_source->child_exited = TRUE;
4289 child_watch_source->count = count;
4292 return child_watch_source->child_exited;
4296 g_child_watch_prepare (GSource *source,
4301 return check_for_child_exited (source);
4305 g_child_watch_check (GSource *source)
4307 return check_for_child_exited (source);
4311 check_for_signal_delivery (GSource *source)
4313 GUnixSignalWatchSource *unix_signal_source = (GUnixSignalWatchSource*) source;
4316 G_LOCK (unix_signal_lock);
4317 if (unix_signal_init_state == UNIX_SIGNAL_INITIALIZED_SINGLE)
4319 switch (unix_signal_source->signum)
4322 delivered = unix_signal_state.sighup_delivered;
4325 delivered = unix_signal_state.sigint_delivered;
4328 delivered = unix_signal_state.sigterm_delivered;
4331 g_assert_not_reached ();
4338 g_assert (unix_signal_init_state == UNIX_SIGNAL_INITIALIZED_THREADED);
4339 delivered = unix_signal_source->pending;
4341 G_UNLOCK (unix_signal_lock);
4347 g_unix_signal_watch_prepare (GSource *source,
4352 return check_for_signal_delivery (source);
4356 g_unix_signal_watch_check (GSource *source)
4358 return check_for_signal_delivery (source);
4362 g_unix_signal_watch_dispatch (GSource *source,
4363 GSourceFunc callback,
4366 GUnixSignalWatchSource *unix_signal_source;
4368 unix_signal_source = (GUnixSignalWatchSource *) source;
4372 g_warning ("Unix signal source dispatched without callback\n"
4373 "You must call g_source_set_callback().");
4377 (callback) (user_data);
4379 G_LOCK (unix_signal_lock);
4380 if (unix_signal_init_state == UNIX_SIGNAL_INITIALIZED_SINGLE)
4382 switch (unix_signal_source->signum)
4385 unix_signal_state.sighup_delivered = FALSE;
4388 unix_signal_state.sigint_delivered = FALSE;
4391 unix_signal_state.sigterm_delivered = FALSE;
4397 g_assert (unix_signal_init_state == UNIX_SIGNAL_INITIALIZED_THREADED);
4398 unix_signal_source->pending = FALSE;
4400 G_UNLOCK (unix_signal_lock);
4406 ensure_unix_signal_handler_installed_unlocked (int signum)
4408 struct sigaction action;
4413 if (unix_signal_state.sighup_handler_installed)
4415 unix_signal_state.sighup_handler_installed = TRUE;
4418 if (unix_signal_state.sigint_handler_installed)
4420 unix_signal_state.sigint_handler_installed = TRUE;
4423 if (unix_signal_state.sigterm_handler_installed)
4425 unix_signal_state.sigterm_handler_installed = TRUE;
4429 init_unix_signal_wakeup_state_unlocked ();
4431 action.sa_handler = g_unix_signal_handler;
4432 sigemptyset (&action.sa_mask);
4433 action.sa_flags = 0;
4434 sigaction (signum, &action, NULL);
4438 _g_main_create_unix_signal_watch (int signum)
4441 GUnixSignalWatchSource *unix_signal_source;
4443 init_unix_signal_wakeup_state ();
4445 source = g_source_new (&g_unix_signal_funcs, sizeof (GUnixSignalWatchSource));
4446 unix_signal_source = (GUnixSignalWatchSource *) source;
4448 unix_signal_source->signum = signum;
4449 unix_signal_source->pending = FALSE;
4451 G_LOCK (unix_signal_lock);
4452 ensure_unix_signal_handler_installed_unlocked (signum);
4453 unix_signal_watches = g_slist_prepend (unix_signal_watches, unix_signal_source);
4454 G_UNLOCK (unix_signal_lock);
4460 g_unix_signal_watch_finalize (GSource *source)
4462 G_LOCK (unix_signal_lock);
4463 unix_signal_watches = g_slist_remove (unix_signal_watches, source);
4464 G_UNLOCK (unix_signal_lock);
4467 #endif /* G_OS_WIN32 */
4470 g_child_watch_dispatch (GSource *source,
4471 GSourceFunc callback,
4474 GChildWatchSource *child_watch_source;
4475 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
4477 child_watch_source = (GChildWatchSource *) source;
4481 g_warning ("Child watch source dispatched without callback\n"
4482 "You must call g_source_set_callback().");
4486 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
4488 /* We never keep a child watch source around as the child is gone */
4495 g_unix_signal_handler (int signum)
4497 if (signum == SIGCHLD)
4498 child_watch_count ++;
4500 if (unix_signal_init_state == UNIX_SIGNAL_INITIALIZED_THREADED)
4506 buf[0] = _UNIX_SIGNAL_PIPE_SIGCHLD_CHAR;
4509 buf[0] = _UNIX_SIGNAL_PIPE_SIGHUP_CHAR;
4512 buf[0] = _UNIX_SIGNAL_PIPE_SIGINT_CHAR;
4515 buf[0] = _UNIX_SIGNAL_PIPE_SIGTERM_CHAR;
4518 /* Shouldn't happen */
4521 write (unix_signal_wake_up_pipe[1], buf, 1);
4525 /* We count on the signal interrupting the poll in the same thread. */
4529 /* Nothing to do - the handler will call waitpid() */
4532 unix_signal_state.sighup_delivered = TRUE;
4535 unix_signal_state.sigint_delivered = TRUE;
4538 unix_signal_state.sigterm_delivered = TRUE;
4541 g_assert_not_reached ();
4548 deliver_unix_signal (int signum)
4551 g_assert (signum == SIGHUP || signum == SIGINT || signum == SIGTERM);
4553 G_LOCK (unix_signal_lock);
4554 for (iter = unix_signal_watches; iter; iter = iter->next)
4556 GUnixSignalWatchSource *source = iter->data;
4558 if (source->signum != signum)
4561 source->pending = TRUE;
4563 G_UNLOCK (unix_signal_lock);
4566 static gpointer unix_signal_helper_thread (gpointer data) G_GNUC_NORETURN;
4569 * This thread is created whenever anything in GLib needs
4570 * to deal with UNIX signals; at present, just SIGCHLD
4571 * from g_child_watch_source_new().
4573 * Note: We could eventually make this thread a more public interface
4574 * and allow e.g. GDBus to use it instead of its own worker thread.
4577 unix_signal_helper_thread (gpointer data)
4582 ssize_t i, bytes_read;
4583 gboolean sigterm_received = FALSE;
4584 gboolean sigint_received = FALSE;
4585 gboolean sighup_received = FALSE;
4587 bytes_read = read (unix_signal_wake_up_pipe[0], b, sizeof (b));
4590 g_warning ("Failed to read from child watch wake up pipe: %s",
4592 /* Not much we can do here sanely; just wait a second and hope
4595 g_usleep (G_USEC_PER_SEC);
4598 for (i = 0; i < bytes_read; i++)
4602 case _UNIX_SIGNAL_PIPE_SIGCHLD_CHAR:
4603 /* The child watch source will call waitpid() in its
4604 * prepare() and check() methods; however, we don't
4605 * know which pid exited, so we need to wake up
4606 * all contexts. Note: actually we could get the pid
4607 * from the "siginfo_t" via the handler, but to pass
4608 * that info down the pipe would require a more structured
4609 * data stream (as opposed to a single byte).
4612 case _UNIX_SIGNAL_PIPE_SIGTERM_CHAR:
4613 sigterm_received = TRUE;
4615 case _UNIX_SIGNAL_PIPE_SIGHUP_CHAR:
4616 sighup_received = TRUE;
4618 case _UNIX_SIGNAL_PIPE_SIGINT_CHAR:
4619 sigint_received = TRUE;
4622 g_warning ("Invalid char '%c' read from child watch pipe", b[i]);
4625 if (sigterm_received)
4626 deliver_unix_signal (SIGTERM);
4627 if (sigint_received)
4628 deliver_unix_signal (SIGINT);
4629 if (sighup_received)
4630 deliver_unix_signal (SIGHUP);
4631 _g_main_wake_up_all_contexts ();
4637 init_unix_signal_wakeup_state_unlocked (void)
4639 GError *error = NULL;
4641 if (!g_thread_supported ())
4643 /* There is nothing to do for initializing in the non-threaded
4646 if (unix_signal_init_state == UNIX_SIGNAL_UNINITIALIZED)
4647 unix_signal_init_state = UNIX_SIGNAL_INITIALIZED_SINGLE;
4651 if (unix_signal_init_state == UNIX_SIGNAL_INITIALIZED_THREADED)
4654 if (!g_unix_pipe_flags (unix_signal_wake_up_pipe, FD_CLOEXEC, &error))
4655 g_error ("Cannot create UNIX signal wake up pipe: %s\n", error->message);
4656 fcntl (unix_signal_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (unix_signal_wake_up_pipe[1], F_GETFL));
4658 /* We create a helper thread that polls on the wakeup pipe indefinitely */
4659 if (g_thread_create (unix_signal_helper_thread, NULL, FALSE, &error) == NULL)
4660 g_error ("Cannot create a thread to monitor UNIX signals: %s\n", error->message);
4662 unix_signal_init_state = UNIX_SIGNAL_INITIALIZED_THREADED;
4666 init_unix_signal_wakeup_state (void)
4668 G_LOCK (unix_signal_lock);
4670 init_unix_signal_wakeup_state_unlocked ();
4672 G_UNLOCK (unix_signal_lock);
4676 g_child_watch_source_init (void)
4678 init_unix_signal_wakeup_state ();
4680 G_LOCK (unix_signal_lock);
4681 if (!unix_signal_state.sigchld_handler_installed)
4683 struct sigaction action;
4684 action.sa_handler = g_unix_signal_handler;
4685 sigemptyset (&action.sa_mask);
4686 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
4687 sigaction (SIGCHLD, &action, NULL);
4688 unix_signal_state.sigchld_handler_installed = TRUE;
4690 G_UNLOCK (unix_signal_lock);
4693 #endif /* !G_OS_WIN32 */
4696 * g_child_watch_source_new:
4697 * @pid: process to watch. On POSIX the pid of a child process. On
4698 * Windows a handle for a process (which doesn't have to be a child).
4700 * Creates a new child_watch source.
4702 * The source will not initially be associated with any #GMainContext
4703 * and must be added to one with g_source_attach() before it will be
4706 * Note that child watch sources can only be used in conjunction with
4707 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
4710 * Note that on platforms where #GPid must be explicitly closed
4711 * (see g_spawn_close_pid()) @pid must not be closed while the
4712 * source is still active. Typically, you will want to call
4713 * g_spawn_close_pid() in the callback function for the source.
4715 * Note further that using g_child_watch_source_new() is not
4716 * compatible with calling <literal>waitpid(-1)</literal> in
4717 * the application. Calling waitpid() for individual pids will
4720 * Return value: the newly-created child watch source
4725 g_child_watch_source_new (GPid pid)
4727 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
4728 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
4731 child_watch_source->poll.fd = (gintptr) pid;
4732 child_watch_source->poll.events = G_IO_IN;
4734 g_source_add_poll (source, &child_watch_source->poll);
4735 #else /* G_OS_WIN32 */
4736 g_child_watch_source_init ();
4737 #endif /* G_OS_WIN32 */
4739 child_watch_source->pid = pid;
4745 * g_child_watch_add_full:
4746 * @priority: the priority of the idle source. Typically this will be in the
4747 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4748 * @pid: process to watch. On POSIX the pid of a child process. On
4749 * Windows a handle for a process (which doesn't have to be a child).
4750 * @function: function to call
4751 * @data: data to pass to @function
4752 * @notify: function to call when the idle is removed, or %NULL
4754 * Sets a function to be called when the child indicated by @pid
4755 * exits, at the priority @priority.
4757 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4758 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4759 * the spawn function for the child watching to work.
4761 * Note that on platforms where #GPid must be explicitly closed
4762 * (see g_spawn_close_pid()) @pid must not be closed while the
4763 * source is still active. Typically, you will want to call
4764 * g_spawn_close_pid() in the callback function for the source.
4766 * GLib supports only a single callback per process id.
4768 * This internally creates a main loop source using
4769 * g_child_watch_source_new() and attaches it to the main loop context
4770 * using g_source_attach(). You can do these steps manually if you
4771 * need greater control.
4773 * Return value: the ID (greater than 0) of the event source.
4778 g_child_watch_add_full (gint priority,
4780 GChildWatchFunc function,
4782 GDestroyNotify notify)
4787 g_return_val_if_fail (function != NULL, 0);
4789 source = g_child_watch_source_new (pid);
4791 if (priority != G_PRIORITY_DEFAULT)
4792 g_source_set_priority (source, priority);
4794 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4795 id = g_source_attach (source, NULL);
4796 g_source_unref (source);
4802 * g_child_watch_add:
4803 * @pid: process id to watch. On POSIX the pid of a child process. On
4804 * Windows a handle for a process (which doesn't have to be a child).
4805 * @function: function to call
4806 * @data: data to pass to @function
4808 * Sets a function to be called when the child indicated by @pid
4809 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4811 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4812 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4813 * the spawn function for the child watching to work.
4815 * Note that on platforms where #GPid must be explicitly closed
4816 * (see g_spawn_close_pid()) @pid must not be closed while the
4817 * source is still active. Typically, you will want to call
4818 * g_spawn_close_pid() in the callback function for the source.
4820 * GLib supports only a single callback per process id.
4822 * This internally creates a main loop source using
4823 * g_child_watch_source_new() and attaches it to the main loop context
4824 * using g_source_attach(). You can do these steps manually if you
4825 * need greater control.
4827 * Return value: the ID (greater than 0) of the event source.
4832 g_child_watch_add (GPid pid,
4833 GChildWatchFunc function,
4836 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4840 /* Idle functions */
4843 g_idle_prepare (GSource *source,
4852 g_idle_check (GSource *source)
4858 g_idle_dispatch (GSource *source,
4859 GSourceFunc callback,
4864 g_warning ("Idle source dispatched without callback\n"
4865 "You must call g_source_set_callback().");
4869 return callback (user_data);
4873 * g_idle_source_new:
4875 * Creates a new idle source.
4877 * The source will not initially be associated with any #GMainContext
4878 * and must be added to one with g_source_attach() before it will be
4879 * executed. Note that the default priority for idle sources is
4880 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4881 * have a default priority of %G_PRIORITY_DEFAULT.
4883 * Return value: the newly-created idle source
4886 g_idle_source_new (void)
4890 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4891 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4898 * @priority: the priority of the idle source. Typically this will be in the
4899 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4900 * @function: function to call
4901 * @data: data to pass to @function
4902 * @notify: function to call when the idle is removed, or %NULL
4904 * Adds a function to be called whenever there are no higher priority
4905 * events pending. If the function returns %FALSE it is automatically
4906 * removed from the list of event sources and will not be called again.
4908 * This internally creates a main loop source using g_idle_source_new()
4909 * and attaches it to the main loop context using g_source_attach().
4910 * You can do these steps manually if you need greater control.
4912 * Return value: the ID (greater than 0) of the event source.
4915 g_idle_add_full (gint priority,
4916 GSourceFunc function,
4918 GDestroyNotify notify)
4923 g_return_val_if_fail (function != NULL, 0);
4925 source = g_idle_source_new ();
4927 if (priority != G_PRIORITY_DEFAULT_IDLE)
4928 g_source_set_priority (source, priority);
4930 g_source_set_callback (source, function, data, notify);
4931 id = g_source_attach (source, NULL);
4932 g_source_unref (source);
4939 * @function: function to call
4940 * @data: data to pass to @function.
4942 * Adds a function to be called whenever there are no higher priority
4943 * events pending to the default main loop. The function is given the
4944 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4945 * returns %FALSE it is automatically removed from the list of event
4946 * sources and will not be called again.
4948 * This internally creates a main loop source using g_idle_source_new()
4949 * and attaches it to the main loop context using g_source_attach().
4950 * You can do these steps manually if you need greater control.
4952 * Return value: the ID (greater than 0) of the event source.
4955 g_idle_add (GSourceFunc function,
4958 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4962 * g_idle_remove_by_data:
4963 * @data: the data for the idle source's callback.
4965 * Removes the idle function with the given data.
4967 * Return value: %TRUE if an idle source was found and removed.
4970 g_idle_remove_by_data (gpointer data)
4972 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4976 * g_main_context_invoke:
4977 * @context: a #GMainContext, or %NULL
4978 * @function: function to call
4979 * @data: data to pass to @function
4981 * Invokes a function in such a way that @context is owned during the
4982 * invocation of @function.
4984 * If @context is %NULL then the global default main context — as
4985 * returned by g_main_context_default() — is used.
4987 * If @context is owned by the current thread, @function is called
4988 * directly. Otherwise, if @context is the thread-default main context
4989 * of the current thread and g_main_context_acquire() succeeds, then
4990 * @function is called and g_main_context_release() is called
4993 * In any other case, an idle source is created to call @function and
4994 * that source is attached to @context (presumably to be run in another
4995 * thread). The idle source is attached with #G_PRIORITY_DEFAULT
4996 * priority. If you want a different priority, use
4997 * g_main_context_invoke_full().
4999 * Note that, as with normal idle functions, @function should probably
5000 * return %FALSE. If it returns %TRUE, it will be continuously run in a
5001 * loop (and may prevent this call from returning).
5006 g_main_context_invoke (GMainContext *context,
5007 GSourceFunc function,
5010 g_main_context_invoke_full (context,
5012 function, data, NULL);
5016 * g_main_context_invoke_full:
5017 * @context: a #GMainContext, or %NULL
5018 * @priority: the priority at which to run @function
5019 * @function: function to call
5020 * @data: data to pass to @function
5021 * @notify: a function to call when @data is no longer in use, or %NULL.
5023 * Invokes a function in such a way that @context is owned during the
5024 * invocation of @function.
5026 * This function is the same as g_main_context_invoke() except that it
5027 * lets you specify the priority incase @function ends up being
5028 * scheduled as an idle and also lets you give a #GDestroyNotify for @data.
5030 * @notify should not assume that it is called from any particular
5031 * thread or with any particular context acquired.
5036 g_main_context_invoke_full (GMainContext *context,
5038 GSourceFunc function,
5040 GDestroyNotify notify)
5042 g_return_if_fail (function != NULL);
5045 context = g_main_context_default ();
5047 if (g_main_context_is_owner (context))
5049 while (function (data));
5056 GMainContext *thread_default;
5058 thread_default = g_main_context_get_thread_default ();
5060 if (!thread_default)
5061 thread_default = g_main_context_default ();
5063 if (thread_default == context && g_main_context_acquire (context))
5065 while (function (data));
5067 g_main_context_release (context);
5076 source = g_idle_source_new ();
5077 g_source_set_priority (source, priority);
5078 g_source_set_callback (source, function, data, notify);
5079 g_source_attach (source, context);
5080 g_source_unref (source);