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/.
36 /* Uncomment the next line (and the corresponding line in gpoll.c) to
37 * enable debugging printouts if the environment variable
38 * G_MAIN_POLL_DEBUG is set to some value.
40 /* #define G_MAIN_POLL_DEBUG */
43 /* Always enable debugging printout on Windows, as it is more often
46 #define G_MAIN_POLL_DEBUG
49 #define _GNU_SOURCE /* for pipe2 */
52 #include "gthreadprivate.h"
54 #include <sys/types.h>
57 #ifdef HAVE_SYS_TIME_H
59 #endif /* HAVE_SYS_TIME_H */
62 #endif /* HAVE_UNISTD_H */
68 #endif /* G_OS_WIN32 */
71 #include <sys/socket.h>
73 #endif /* G_OS_BEOS */
84 typedef struct _GTimeoutSource GTimeoutSource;
85 typedef struct _GChildWatchSource GChildWatchSource;
86 typedef struct _GPollRec GPollRec;
87 typedef struct _GSourceCallback GSourceCallback;
91 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
92 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
95 #ifdef G_THREADS_ENABLED
96 typedef struct _GMainWaiter GMainWaiter;
105 typedef struct _GMainDispatch GMainDispatch;
107 struct _GMainDispatch
110 GSList *dispatching_sources; /* stack of current sources */
113 #ifdef G_MAIN_POLL_DEBUG
114 gboolean _g_main_poll_debug = FALSE;
119 #ifdef G_THREADS_ENABLED
120 /* The following lock is used for both the list of sources
121 * and the list of poll records
132 GPtrArray *pending_dispatches;
133 gint timeout; /* Timeout for current iteration */
136 GSource *source_list;
137 gint in_check_or_prepare;
139 GPollRec *poll_records;
140 guint n_poll_records;
141 GPollFD *cached_poll_array;
142 guint cached_poll_array_size;
144 #ifdef G_THREADS_ENABLED
146 /* this pipe is used to wake up the main loop when a source is added.
148 gint wake_up_pipe[2];
149 #else /* G_OS_WIN32 */
150 HANDLE wake_up_semaphore;
151 #endif /* G_OS_WIN32 */
154 gboolean poll_waiting;
156 /* Flag indicating whether the set of fd's changed during a poll */
157 gboolean poll_changed;
158 #endif /* G_THREADS_ENABLED */
162 GTimeVal current_time;
163 gboolean time_is_current;
166 struct _GSourceCallback
171 GDestroyNotify notify;
176 GMainContext *context;
181 struct _GTimeoutSource
189 struct _GChildWatchSource
196 #else /* G_OS_WIN32 */
198 gboolean child_exited;
199 #endif /* G_OS_WIN32 */
209 #ifdef G_THREADS_ENABLED
210 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
211 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
212 #define G_THREAD_SELF g_thread_self ()
214 #define LOCK_CONTEXT(context) (void)0
215 #define UNLOCK_CONTEXT(context) (void)0
216 #define G_THREAD_SELF NULL
219 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
220 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
221 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
223 #define SOURCE_UNREF(source, context) \
225 if ((source)->ref_count > 1) \
226 (source)->ref_count--; \
228 g_source_unref_internal ((source), (context), TRUE); \
232 /* Forward declarations */
234 static void g_source_unref_internal (GSource *source,
235 GMainContext *context,
237 static void g_source_destroy_internal (GSource *source,
238 GMainContext *context,
240 static void g_main_context_poll (GMainContext *context,
245 static void g_main_context_add_poll_unlocked (GMainContext *context,
248 static void g_main_context_remove_poll_unlocked (GMainContext *context,
250 static void g_main_context_wakeup_unlocked (GMainContext *context);
252 static gboolean g_timeout_prepare (GSource *source,
254 static gboolean g_timeout_check (GSource *source);
255 static gboolean g_timeout_dispatch (GSource *source,
256 GSourceFunc callback,
258 static gboolean g_child_watch_prepare (GSource *source,
260 static gboolean g_child_watch_check (GSource *source);
261 static gboolean g_child_watch_dispatch (GSource *source,
262 GSourceFunc callback,
264 static gboolean g_idle_prepare (GSource *source,
266 static gboolean g_idle_check (GSource *source);
267 static gboolean g_idle_dispatch (GSource *source,
268 GSourceFunc callback,
271 G_LOCK_DEFINE_STATIC (main_loop);
272 static GMainContext *default_main_context;
273 static GSList *main_contexts_without_pipe = NULL;
276 /* Child status monitoring code */
278 CHILD_WATCH_UNINITIALIZED,
279 CHILD_WATCH_INITIALIZED_SINGLE,
280 CHILD_WATCH_INITIALIZED_THREADED
282 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
283 static gint child_watch_count = 1;
284 static gint child_watch_wake_up_pipe[2] = {0, 0};
285 #endif /* !G_OS_WIN32 */
286 G_LOCK_DEFINE_STATIC (main_context_list);
287 static GSList *main_context_list = NULL;
289 static gint timer_perturb = -1;
291 GSourceFuncs g_timeout_funcs =
299 GSourceFuncs g_child_watch_funcs =
301 g_child_watch_prepare,
303 g_child_watch_dispatch,
307 GSourceFuncs g_idle_funcs =
316 * g_main_context_ref:
317 * @context: a #GMainContext
319 * Increases the reference count on a #GMainContext object by one.
321 * Returns: the @context that was passed in (since 2.6)
324 g_main_context_ref (GMainContext *context)
326 g_return_val_if_fail (context != NULL, NULL);
327 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
329 g_atomic_int_inc (&context->ref_count);
335 poll_rec_list_free (GMainContext *context,
338 g_slice_free_chain (GPollRec, list, next);
342 * g_main_context_unref:
343 * @context: a #GMainContext
345 * Decreases the reference count on a #GMainContext object by one. If
346 * the result is zero, free the context and free all associated memory.
349 g_main_context_unref (GMainContext *context)
352 g_return_if_fail (context != NULL);
353 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
355 if (!g_atomic_int_dec_and_test (&context->ref_count))
358 G_LOCK (main_context_list);
359 main_context_list = g_slist_remove (main_context_list, context);
360 G_UNLOCK (main_context_list);
362 source = context->source_list;
365 GSource *next = source->next;
366 g_source_destroy_internal (source, context, FALSE);
370 #ifdef G_THREADS_ENABLED
371 g_static_mutex_free (&context->mutex);
374 g_ptr_array_free (context->pending_dispatches, TRUE);
375 g_free (context->cached_poll_array);
377 poll_rec_list_free (context, context->poll_records);
379 #ifdef G_THREADS_ENABLED
380 if (g_thread_supported())
383 close (context->wake_up_pipe[0]);
384 close (context->wake_up_pipe[1]);
386 CloseHandle (context->wake_up_semaphore);
390 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
393 if (context->cond != NULL)
394 g_cond_free (context->cond);
400 #ifdef G_THREADS_ENABLED
402 g_main_context_init_pipe (GMainContext *context)
405 if (context->wake_up_pipe[0] != -1)
409 /* if this fails, we fall through and try pipe */
410 pipe2 (context->wake_up_pipe, O_CLOEXEC);
412 if (context->wake_up_pipe[0] == -1)
414 if (pipe (context->wake_up_pipe) < 0)
415 g_error ("Cannot create pipe main loop wake-up: %s\n",
418 fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC);
419 fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC);
422 context->wake_up_rec.fd = context->wake_up_pipe[0];
423 context->wake_up_rec.events = G_IO_IN;
425 if (context->wake_up_semaphore != NULL)
427 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
428 if (context->wake_up_semaphore == NULL)
429 g_error ("Cannot create wake-up semaphore: %s",
430 g_win32_error_message (GetLastError ()));
431 context->wake_up_rec.fd = (gintptr) context->wake_up_semaphore;
432 context->wake_up_rec.events = G_IO_IN;
434 if (_g_main_poll_debug)
435 g_print ("wake-up semaphore: %p\n", context->wake_up_semaphore);
437 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
441 _g_main_thread_init (void)
443 GSList *curr = main_contexts_without_pipe;
446 g_main_context_init_pipe ((GMainContext *)curr->data);
449 g_slist_free (main_contexts_without_pipe);
450 main_contexts_without_pipe = NULL;
452 #endif /* G_THREADS_ENABLED */
455 * g_main_context_new:
457 * Creates a new #GMainContext structure.
459 * Return value: the new #GMainContext
462 g_main_context_new (void)
464 GMainContext *context = g_new0 (GMainContext, 1);
466 #ifdef G_MAIN_POLL_DEBUG
468 static gboolean beenhere = FALSE;
472 if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
473 _g_main_poll_debug = TRUE;
479 #ifdef G_THREADS_ENABLED
480 g_static_mutex_init (&context->mutex);
482 context->owner = NULL;
483 context->waiters = NULL;
486 context->wake_up_pipe[0] = -1;
487 context->wake_up_pipe[1] = -1;
489 context->wake_up_semaphore = NULL;
493 context->ref_count = 1;
495 context->next_id = 1;
497 context->source_list = NULL;
499 context->poll_func = g_poll;
501 context->cached_poll_array = NULL;
502 context->cached_poll_array_size = 0;
504 context->pending_dispatches = g_ptr_array_new ();
506 context->time_is_current = FALSE;
508 #ifdef G_THREADS_ENABLED
509 if (g_thread_supported ())
510 g_main_context_init_pipe (context);
512 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
516 G_LOCK (main_context_list);
517 main_context_list = g_slist_append (main_context_list, context);
519 #ifdef G_MAIN_POLL_DEBUG
520 if (_g_main_poll_debug)
521 g_print ("created context=%p\n", context);
524 G_UNLOCK (main_context_list);
530 * g_main_context_default:
532 * Returns the global default main context. This is the main context
533 * used for main loop functions when a main loop is not explicitly
534 * specified, and corresponds to the "main" main loop. See also
535 * g_main_context_get_thread_default().
537 * Return value: the global default main context.
540 g_main_context_default (void)
546 if (!default_main_context)
548 default_main_context = g_main_context_new ();
549 #ifdef G_MAIN_POLL_DEBUG
550 if (_g_main_poll_debug)
551 g_print ("default context=%p\n", default_main_context);
555 G_UNLOCK (main_loop);
557 return default_main_context;
560 static GStaticPrivate thread_context_stack = G_STATIC_PRIVATE_INIT;
563 free_context_stack (gpointer data)
565 GQueue *stack = data;
566 GMainContext *context;
568 while (!g_queue_is_empty (stack))
570 context = g_queue_pop_head (stack);
571 g_main_context_release (context);
573 g_main_context_unref (context);
575 g_queue_free (stack);
579 * g_main_context_push_thread_default:
580 * @context: a #GMainContext, or %NULL for the global default context
582 * Acquires @context and sets it as the thread-default context for the
583 * current thread. This will cause certain asynchronous operations
584 * (such as most <link linkend="gio">gio</link>-based I/O) which are
585 * started in this thread to run under @context and deliver their
586 * results to its main loop, rather than running under the global
587 * default context in the main thread. Note that calling this function
588 * changes the context returned by
589 * g_main_context_get_thread_default(), <emphasis>not</emphasis> the
590 * one returned by g_main_context_default(), so it does not affect the
591 * context used by functions like g_idle_add().
593 * Normally you would call this function shortly after creating a new
594 * thread, passing it a #GMainContext which will be run by a
595 * #GMainLoop in that thread, to set a new default context for all
596 * async operations in that thread. (In this case, you don't need to
597 * ever call g_main_context_pop_thread_default().) In some cases
598 * however, you may want to schedule a single operation in a
599 * non-default context, or temporarily use a non-default context in
600 * the main thread. In that case, you can wrap the call to the
601 * asynchronous operation inside a
602 * g_main_context_push_thread_default() /
603 * g_main_context_pop_thread_default() pair, but it is up to you to
604 * ensure that no other asynchronous operations accidentally get
605 * started while the non-default context is active.
607 * Beware that libraries that predate this function may not correctly
608 * handle being used from a thread with a thread-default context. Eg,
609 * see g_file_supports_thread_contexts().
614 g_main_context_push_thread_default (GMainContext *context)
617 gboolean acquired_context;
619 acquired_context = g_main_context_acquire (context);
620 g_return_if_fail (acquired_context);
622 if (context == g_main_context_default ())
625 g_main_context_ref (context);
627 stack = g_static_private_get (&thread_context_stack);
630 stack = g_queue_new ();
631 g_static_private_set (&thread_context_stack, stack,
635 g_queue_push_head (stack, context);
639 * g_main_context_pop_thread_default:
640 * @context: a #GMainContext object, or %NULL
642 * Pops @context off the thread-default context stack (verifying that
643 * it was on the top of the stack).
648 g_main_context_pop_thread_default (GMainContext *context)
652 if (context == g_main_context_default ())
655 stack = g_static_private_get (&thread_context_stack);
657 g_return_if_fail (stack != NULL);
658 g_return_if_fail (g_queue_peek_head (stack) == context);
660 g_queue_pop_head (stack);
662 g_main_context_release (context);
664 g_main_context_unref (context);
668 * g_main_context_get_thread_default:
670 * Gets the thread-default #GMainContext for this thread. Asynchronous
671 * operations that want to be able to be run in contexts other than
672 * the default one should call this method to get a #GMainContext to
673 * add their #GSource<!-- -->s to. (Note that even in single-threaded
674 * programs applications may sometimes want to temporarily push a
675 * non-default context, so it is not safe to assume that this will
676 * always return %NULL if threads are not initialized.)
678 * Returns: the thread-default #GMainContext, or %NULL if the
679 * thread-default context is the global default context.
684 g_main_context_get_thread_default (void)
688 stack = g_static_private_get (&thread_context_stack);
690 return g_queue_peek_head (stack);
695 /* Hooks for adding to the main loop */
699 * @source_funcs: structure containing functions that implement
700 * the sources behavior.
701 * @struct_size: size of the #GSource structure to create.
703 * Creates a new #GSource structure. The size is specified to
704 * allow creating structures derived from #GSource that contain
705 * additional data. The size passed in must be at least
706 * <literal>sizeof (GSource)</literal>.
708 * The source will not initially be associated with any #GMainContext
709 * and must be added to one with g_source_attach() before it will be
712 * Return value: the newly-created #GSource.
715 g_source_new (GSourceFuncs *source_funcs,
720 g_return_val_if_fail (source_funcs != NULL, NULL);
721 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
723 source = (GSource*) g_malloc0 (struct_size);
725 source->source_funcs = source_funcs;
726 source->ref_count = 1;
728 source->priority = G_PRIORITY_DEFAULT;
730 source->flags = G_HOOK_FLAG_ACTIVE;
732 /* NULL/0 initialization for all other fields */
737 /* Holds context's lock
740 g_source_list_add (GSource *source,
741 GMainContext *context)
743 GSource *tmp_source, *last_source;
746 tmp_source = context->source_list;
747 while (tmp_source && tmp_source->priority <= source->priority)
749 last_source = tmp_source;
750 tmp_source = tmp_source->next;
753 source->next = tmp_source;
755 tmp_source->prev = source;
757 source->prev = last_source;
759 last_source->next = source;
761 context->source_list = source;
764 /* Holds context's lock
767 g_source_list_remove (GSource *source,
768 GMainContext *context)
771 source->prev->next = source->next;
773 context->source_list = source->next;
776 source->next->prev = source->prev;
784 * @source: a #GSource
785 * @context: a #GMainContext (if %NULL, the default context will be used)
787 * Adds a #GSource to a @context so that it will be executed within
788 * that context. Remove it by calling g_source_destroy().
790 * Return value: the ID (greater than 0) for the source within the
794 g_source_attach (GSource *source,
795 GMainContext *context)
800 g_return_val_if_fail (source->context == NULL, 0);
801 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
804 context = g_main_context_default ();
806 LOCK_CONTEXT (context);
808 source->context = context;
809 result = source->source_id = context->next_id++;
812 g_source_list_add (source, context);
814 tmp_list = source->poll_fds;
817 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
818 tmp_list = tmp_list->next;
821 #ifdef G_THREADS_ENABLED
822 /* Now wake up the main loop if it is waiting in the poll() */
823 g_main_context_wakeup_unlocked (context);
826 UNLOCK_CONTEXT (context);
832 g_source_destroy_internal (GSource *source,
833 GMainContext *context,
837 LOCK_CONTEXT (context);
839 if (!SOURCE_DESTROYED (source))
842 gpointer old_cb_data;
843 GSourceCallbackFuncs *old_cb_funcs;
845 source->flags &= ~G_HOOK_FLAG_ACTIVE;
847 old_cb_data = source->callback_data;
848 old_cb_funcs = source->callback_funcs;
850 source->callback_data = NULL;
851 source->callback_funcs = NULL;
855 UNLOCK_CONTEXT (context);
856 old_cb_funcs->unref (old_cb_data);
857 LOCK_CONTEXT (context);
860 if (!SOURCE_BLOCKED (source))
862 tmp_list = source->poll_fds;
865 g_main_context_remove_poll_unlocked (context, tmp_list->data);
866 tmp_list = tmp_list->next;
870 g_source_unref_internal (source, context, TRUE);
874 UNLOCK_CONTEXT (context);
879 * @source: a #GSource
881 * Removes a source from its #GMainContext, if any, and mark it as
882 * destroyed. The source cannot be subsequently added to another
886 g_source_destroy (GSource *source)
888 GMainContext *context;
890 g_return_if_fail (source != NULL);
892 context = source->context;
895 g_source_destroy_internal (source, context, FALSE);
897 source->flags &= ~G_HOOK_FLAG_ACTIVE;
902 * @source: a #GSource
904 * Returns the numeric ID for a particular source. The ID of a source
905 * is a positive integer which is unique within a particular main loop
906 * context. The reverse
907 * mapping from ID to source is done by g_main_context_find_source_by_id().
909 * Return value: the ID (greater than 0) for the source
912 g_source_get_id (GSource *source)
916 g_return_val_if_fail (source != NULL, 0);
917 g_return_val_if_fail (source->context != NULL, 0);
919 LOCK_CONTEXT (source->context);
920 result = source->source_id;
921 UNLOCK_CONTEXT (source->context);
927 * g_source_get_context:
928 * @source: a #GSource
930 * Gets the #GMainContext with which the source is associated.
931 * Calling this function on a destroyed source is an error.
933 * Return value: the #GMainContext with which the source is associated,
934 * or %NULL if the context has not yet been added
938 g_source_get_context (GSource *source)
940 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
942 return source->context;
948 * @fd: a #GPollFD structure holding information about a file
949 * descriptor to watch.
951 * Adds a file descriptor to the set of file descriptors polled for
952 * this source. This is usually combined with g_source_new() to add an
953 * event source. The event source's check function will typically test
954 * the @revents field in the #GPollFD struct and return %TRUE if events need
958 g_source_add_poll (GSource *source,
961 GMainContext *context;
963 g_return_if_fail (source != NULL);
964 g_return_if_fail (fd != NULL);
965 g_return_if_fail (!SOURCE_DESTROYED (source));
967 context = source->context;
970 LOCK_CONTEXT (context);
972 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
976 if (!SOURCE_BLOCKED (source))
977 g_main_context_add_poll_unlocked (context, source->priority, fd);
978 UNLOCK_CONTEXT (context);
983 * g_source_remove_poll:
985 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
987 * Removes a file descriptor from the set of file descriptors polled for
991 g_source_remove_poll (GSource *source,
994 GMainContext *context;
996 g_return_if_fail (source != NULL);
997 g_return_if_fail (fd != NULL);
998 g_return_if_fail (!SOURCE_DESTROYED (source));
1000 context = source->context;
1003 LOCK_CONTEXT (context);
1005 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1009 if (!SOURCE_BLOCKED (source))
1010 g_main_context_remove_poll_unlocked (context, fd);
1011 UNLOCK_CONTEXT (context);
1016 * g_source_set_callback_indirect:
1017 * @source: the source
1018 * @callback_data: pointer to callback data "object"
1019 * @callback_funcs: functions for reference counting @callback_data
1020 * and getting the callback and data
1022 * Sets the callback function storing the data as a refcounted callback
1023 * "object". This is used internally. Note that calling
1024 * g_source_set_callback_indirect() assumes
1025 * an initial reference count on @callback_data, and thus
1026 * @callback_funcs->unref will eventually be called once more
1027 * than @callback_funcs->ref.
1030 g_source_set_callback_indirect (GSource *source,
1031 gpointer callback_data,
1032 GSourceCallbackFuncs *callback_funcs)
1034 GMainContext *context;
1035 gpointer old_cb_data;
1036 GSourceCallbackFuncs *old_cb_funcs;
1038 g_return_if_fail (source != NULL);
1039 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1041 context = source->context;
1044 LOCK_CONTEXT (context);
1046 old_cb_data = source->callback_data;
1047 old_cb_funcs = source->callback_funcs;
1049 source->callback_data = callback_data;
1050 source->callback_funcs = callback_funcs;
1053 UNLOCK_CONTEXT (context);
1056 old_cb_funcs->unref (old_cb_data);
1060 g_source_callback_ref (gpointer cb_data)
1062 GSourceCallback *callback = cb_data;
1064 callback->ref_count++;
1069 g_source_callback_unref (gpointer cb_data)
1071 GSourceCallback *callback = cb_data;
1073 callback->ref_count--;
1074 if (callback->ref_count == 0)
1076 if (callback->notify)
1077 callback->notify (callback->data);
1083 g_source_callback_get (gpointer cb_data,
1088 GSourceCallback *callback = cb_data;
1090 *func = callback->func;
1091 *data = callback->data;
1094 static GSourceCallbackFuncs g_source_callback_funcs = {
1095 g_source_callback_ref,
1096 g_source_callback_unref,
1097 g_source_callback_get,
1101 * g_source_set_callback:
1102 * @source: the source
1103 * @func: a callback function
1104 * @data: the data to pass to callback function
1105 * @notify: a function to call when @data is no longer in use, or %NULL.
1107 * Sets the callback function for a source. The callback for a source is
1108 * called from the source's dispatch function.
1110 * The exact type of @func depends on the type of source; ie. you
1111 * should not count on @func being called with @data as its first
1114 * Typically, you won't use this function. Instead use functions specific
1115 * to the type of source you are using.
1118 g_source_set_callback (GSource *source,
1121 GDestroyNotify notify)
1123 GSourceCallback *new_callback;
1125 g_return_if_fail (source != NULL);
1127 new_callback = g_new (GSourceCallback, 1);
1129 new_callback->ref_count = 1;
1130 new_callback->func = func;
1131 new_callback->data = data;
1132 new_callback->notify = notify;
1134 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1139 * g_source_set_funcs:
1140 * @source: a #GSource
1141 * @funcs: the new #GSourceFuncs
1143 * Sets the source functions (can be used to override
1144 * default implementations) of an unattached source.
1149 g_source_set_funcs (GSource *source,
1150 GSourceFuncs *funcs)
1152 g_return_if_fail (source != NULL);
1153 g_return_if_fail (source->context == NULL);
1154 g_return_if_fail (source->ref_count > 0);
1155 g_return_if_fail (funcs != NULL);
1157 source->source_funcs = funcs;
1161 * g_source_set_priority:
1162 * @source: a #GSource
1163 * @priority: the new priority.
1165 * Sets the priority of a source. While the main loop is being
1166 * run, a source will be dispatched if it is ready to be dispatched and no sources
1167 * at a higher (numerically smaller) priority are ready to be dispatched.
1170 g_source_set_priority (GSource *source,
1174 GMainContext *context;
1176 g_return_if_fail (source != NULL);
1178 context = source->context;
1181 LOCK_CONTEXT (context);
1183 source->priority = priority;
1187 /* Remove the source from the context's source and then
1188 * add it back so it is sorted in the correct plcae
1190 g_source_list_remove (source, source->context);
1191 g_source_list_add (source, source->context);
1193 if (!SOURCE_BLOCKED (source))
1195 tmp_list = source->poll_fds;
1198 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1199 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1201 tmp_list = tmp_list->next;
1205 UNLOCK_CONTEXT (source->context);
1210 * g_source_get_priority:
1211 * @source: a #GSource
1213 * Gets the priority of a source.
1215 * Return value: the priority of the source
1218 g_source_get_priority (GSource *source)
1220 g_return_val_if_fail (source != NULL, 0);
1222 return source->priority;
1226 * g_source_set_can_recurse:
1227 * @source: a #GSource
1228 * @can_recurse: whether recursion is allowed for this source
1230 * Sets whether a source can be called recursively. If @can_recurse is
1231 * %TRUE, then while the source is being dispatched then this source
1232 * will be processed normally. Otherwise, all processing of this
1233 * source is blocked until the dispatch function returns.
1236 g_source_set_can_recurse (GSource *source,
1237 gboolean can_recurse)
1239 GMainContext *context;
1241 g_return_if_fail (source != NULL);
1243 context = source->context;
1246 LOCK_CONTEXT (context);
1249 source->flags |= G_SOURCE_CAN_RECURSE;
1251 source->flags &= ~G_SOURCE_CAN_RECURSE;
1254 UNLOCK_CONTEXT (context);
1258 * g_source_get_can_recurse:
1259 * @source: a #GSource
1261 * Checks whether a source is allowed to be called recursively.
1262 * see g_source_set_can_recurse().
1264 * Return value: whether recursion is allowed.
1267 g_source_get_can_recurse (GSource *source)
1269 g_return_val_if_fail (source != NULL, FALSE);
1271 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1276 * @source: a #GSource
1278 * Increases the reference count on a source by one.
1280 * Return value: @source
1283 g_source_ref (GSource *source)
1285 GMainContext *context;
1287 g_return_val_if_fail (source != NULL, NULL);
1289 context = source->context;
1292 LOCK_CONTEXT (context);
1294 source->ref_count++;
1297 UNLOCK_CONTEXT (context);
1302 /* g_source_unref() but possible to call within context lock
1305 g_source_unref_internal (GSource *source,
1306 GMainContext *context,
1309 gpointer old_cb_data = NULL;
1310 GSourceCallbackFuncs *old_cb_funcs = NULL;
1312 g_return_if_fail (source != NULL);
1314 if (!have_lock && context)
1315 LOCK_CONTEXT (context);
1317 source->ref_count--;
1318 if (source->ref_count == 0)
1320 old_cb_data = source->callback_data;
1321 old_cb_funcs = source->callback_funcs;
1323 source->callback_data = NULL;
1324 source->callback_funcs = NULL;
1326 if (context && !SOURCE_DESTROYED (source))
1328 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1329 source->ref_count++;
1332 g_source_list_remove (source, context);
1334 if (source->source_funcs->finalize)
1335 source->source_funcs->finalize (source);
1337 g_slist_free (source->poll_fds);
1338 source->poll_fds = NULL;
1342 if (!have_lock && context)
1343 UNLOCK_CONTEXT (context);
1348 UNLOCK_CONTEXT (context);
1350 old_cb_funcs->unref (old_cb_data);
1353 LOCK_CONTEXT (context);
1359 * @source: a #GSource
1361 * Decreases the reference count of a source by one. If the
1362 * resulting reference count is zero the source and associated
1363 * memory will be destroyed.
1366 g_source_unref (GSource *source)
1368 g_return_if_fail (source != NULL);
1370 g_source_unref_internal (source, source->context, FALSE);
1374 * g_main_context_find_source_by_id:
1375 * @context: a #GMainContext (if %NULL, the default context will be used)
1376 * @source_id: the source ID, as returned by g_source_get_id().
1378 * Finds a #GSource given a pair of context and ID.
1380 * Return value: the #GSource if found, otherwise, %NULL
1383 g_main_context_find_source_by_id (GMainContext *context,
1388 g_return_val_if_fail (source_id > 0, NULL);
1390 if (context == NULL)
1391 context = g_main_context_default ();
1393 LOCK_CONTEXT (context);
1395 source = context->source_list;
1398 if (!SOURCE_DESTROYED (source) &&
1399 source->source_id == source_id)
1401 source = source->next;
1404 UNLOCK_CONTEXT (context);
1410 * g_main_context_find_source_by_funcs_user_data:
1411 * @context: a #GMainContext (if %NULL, the default context will be used).
1412 * @funcs: the @source_funcs passed to g_source_new().
1413 * @user_data: the user data from the callback.
1415 * Finds a source with the given source functions and user data. If
1416 * multiple sources exist with the same source function and user data,
1417 * the first one found will be returned.
1419 * Return value: the source, if one was found, otherwise %NULL
1422 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1423 GSourceFuncs *funcs,
1428 g_return_val_if_fail (funcs != NULL, NULL);
1430 if (context == NULL)
1431 context = g_main_context_default ();
1433 LOCK_CONTEXT (context);
1435 source = context->source_list;
1438 if (!SOURCE_DESTROYED (source) &&
1439 source->source_funcs == funcs &&
1440 source->callback_funcs)
1442 GSourceFunc callback;
1443 gpointer callback_data;
1445 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1447 if (callback_data == user_data)
1450 source = source->next;
1453 UNLOCK_CONTEXT (context);
1459 * g_main_context_find_source_by_user_data:
1460 * @context: a #GMainContext
1461 * @user_data: the user_data for the callback.
1463 * Finds a source with the given user data for the callback. If
1464 * multiple sources exist with the same user data, the first
1465 * one found will be returned.
1467 * Return value: the source, if one was found, otherwise %NULL
1470 g_main_context_find_source_by_user_data (GMainContext *context,
1475 if (context == NULL)
1476 context = g_main_context_default ();
1478 LOCK_CONTEXT (context);
1480 source = context->source_list;
1483 if (!SOURCE_DESTROYED (source) &&
1484 source->callback_funcs)
1486 GSourceFunc callback;
1487 gpointer callback_data = NULL;
1489 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1491 if (callback_data == user_data)
1494 source = source->next;
1497 UNLOCK_CONTEXT (context);
1504 * @tag: the ID of the source to remove.
1506 * Removes the source with the given id from the default main context.
1508 * a #GSource is given by g_source_get_id(), or will be returned by the
1509 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1510 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1511 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1513 * See also g_source_destroy(). You must use g_source_destroy() for sources
1514 * added to a non-default main context.
1516 * Return value: %TRUE if the source was found and removed.
1519 g_source_remove (guint tag)
1523 g_return_val_if_fail (tag > 0, FALSE);
1525 source = g_main_context_find_source_by_id (NULL, tag);
1527 g_source_destroy (source);
1529 return source != NULL;
1533 * g_source_remove_by_user_data:
1534 * @user_data: the user_data for the callback.
1536 * Removes a source from the default main loop context given the user
1537 * data for the callback. If multiple sources exist with the same user
1538 * data, only one will be destroyed.
1540 * Return value: %TRUE if a source was found and removed.
1543 g_source_remove_by_user_data (gpointer user_data)
1547 source = g_main_context_find_source_by_user_data (NULL, user_data);
1550 g_source_destroy (source);
1558 * g_source_remove_by_funcs_user_data:
1559 * @funcs: The @source_funcs passed to g_source_new()
1560 * @user_data: the user data for the callback
1562 * Removes a source from the default main loop context given the
1563 * source functions and user data. If multiple sources exist with the
1564 * same source functions and user data, only one will be destroyed.
1566 * Return value: %TRUE if a source was found and removed.
1569 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1574 g_return_val_if_fail (funcs != NULL, FALSE);
1576 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1579 g_source_destroy (source);
1587 * g_get_current_time:
1588 * @result: #GTimeVal structure in which to store current time.
1590 * Equivalent to the UNIX gettimeofday() function, but portable.
1593 g_get_current_time (GTimeVal *result)
1598 g_return_if_fail (result != NULL);
1600 /*this is required on alpha, there the timeval structs are int's
1601 not longs and a cast only would fail horribly*/
1602 gettimeofday (&r, NULL);
1603 result->tv_sec = r.tv_sec;
1604 result->tv_usec = r.tv_usec;
1609 g_return_if_fail (result != NULL);
1611 GetSystemTimeAsFileTime (&ft);
1612 memmove (&time64, &ft, sizeof (FILETIME));
1614 /* Convert from 100s of nanoseconds since 1601-01-01
1615 * to Unix epoch. Yes, this is Y2038 unsafe.
1617 time64 -= G_GINT64_CONSTANT (116444736000000000);
1620 result->tv_sec = time64 / 1000000;
1621 result->tv_usec = time64 % 1000000;
1626 g_main_dispatch_free (gpointer dispatch)
1628 g_slice_free (GMainDispatch, dispatch);
1631 /* Running the main loop */
1633 static GMainDispatch *
1636 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1637 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1640 dispatch = g_slice_new0 (GMainDispatch);
1641 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1650 * Returns the depth of the stack of calls to
1651 * g_main_context_dispatch() on any #GMainContext in the current thread.
1652 * That is, when called from the toplevel, it gives 0. When
1653 * called from within a callback from g_main_context_iteration()
1654 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1655 * a callback to a recursive call to g_main_context_iterate(),
1656 * it returns 2. And so forth.
1658 * This function is useful in a situation like the following:
1659 * Imagine an extremely simple "garbage collected" system.
1662 * static GList *free_list;
1665 * allocate_memory (gsize size)
1667 * gpointer result = g_malloc (size);
1668 * free_list = g_list_prepend (free_list, result);
1673 * free_allocated_memory (void)
1676 * for (l = free_list; l; l = l->next);
1678 * g_list_free (free_list);
1686 * g_main_context_iteration (NULL, TRUE);
1687 * free_allocated_memory();
1691 * This works from an application, however, if you want to do the same
1692 * thing from a library, it gets more difficult, since you no longer
1693 * control the main loop. You might think you can simply use an idle
1694 * function to make the call to free_allocated_memory(), but that
1695 * doesn't work, since the idle function could be called from a
1696 * recursive callback. This can be fixed by using g_main_depth()
1700 * allocate_memory (gsize size)
1702 * FreeListBlock *block = g_new (FreeListBlock, 1);
1703 * block->mem = g_malloc (size);
1704 * block->depth = g_main_depth ();
1705 * free_list = g_list_prepend (free_list, block);
1706 * return block->mem;
1710 * free_allocated_memory (void)
1714 * int depth = g_main_depth ();
1715 * for (l = free_list; l; );
1717 * GList *next = l->next;
1718 * FreeListBlock *block = l->data;
1719 * if (block->depth > depth)
1721 * g_free (block->mem);
1723 * free_list = g_list_delete_link (free_list, l);
1731 * There is a temptation to use g_main_depth() to solve
1732 * problems with reentrancy. For instance, while waiting for data
1733 * to be received from the network in response to a menu item,
1734 * the menu item might be selected again. It might seem that
1735 * one could make the menu item's callback return immediately
1736 * and do nothing if g_main_depth() returns a value greater than 1.
1737 * However, this should be avoided since the user then sees selecting
1738 * the menu item do nothing. Furthermore, you'll find yourself adding
1739 * these checks all over your code, since there are doubtless many,
1740 * many things that the user could do. Instead, you can use the
1741 * following techniques:
1746 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1747 * the user from interacting with elements while the main
1748 * loop is recursing.
1753 * Avoid main loop recursion in situations where you can't handle
1754 * arbitrary callbacks. Instead, structure your code so that you
1755 * simply return to the main loop and then get called again when
1756 * there is more work to do.
1761 * Return value: The main loop recursion level in the current thread
1766 GMainDispatch *dispatch = get_dispatch ();
1767 return dispatch->depth;
1771 * g_main_current_source:
1773 * Returns the currently firing source for this thread.
1775 * Return value: The currently firing source or %NULL.
1780 g_main_current_source (void)
1782 GMainDispatch *dispatch = get_dispatch ();
1783 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
1787 * g_source_is_destroyed:
1788 * @source: a #GSource
1790 * Returns whether @source has been destroyed.
1792 * This is important when you operate upon your objects
1793 * from within idle handlers, but may have freed the object
1794 * before the dispatch of your idle handler.
1798 * idle_callback (gpointer data)
1800 * SomeWidget *self = data;
1802 * GDK_THREADS_ENTER (<!-- -->);
1803 * /<!-- -->* do stuff with self *<!-- -->/
1804 * self->idle_id = 0;
1805 * GDK_THREADS_LEAVE (<!-- -->);
1811 * some_widget_do_stuff_later (SomeWidget *self)
1813 * self->idle_id = g_idle_add (idle_callback, self);
1817 * some_widget_finalize (GObject *object)
1819 * SomeWidget *self = SOME_WIDGET (object);
1821 * if (self->idle_id)
1822 * g_source_remove (self->idle_id);
1824 * G_OBJECT_CLASS (parent_class)->finalize (object);
1828 * This will fail in a multi-threaded application if the
1829 * widget is destroyed before the idle handler fires due
1830 * to the use after free in the callback. A solution, to
1831 * this particular problem, is to check to if the source
1832 * has already been destroy within the callback.
1836 * idle_callback (gpointer data)
1838 * SomeWidget *self = data;
1840 * GDK_THREADS_ENTER ();
1841 * if (!g_source_is_destroyed (g_main_current_source ()))
1843 * /<!-- -->* do stuff with self *<!-- -->/
1845 * GDK_THREADS_LEAVE ();
1851 * Return value: %TRUE if the source has been destroyed
1856 g_source_is_destroyed (GSource *source)
1858 return SOURCE_DESTROYED (source);
1862 /* Temporarily remove all this source's file descriptors from the
1863 * poll(), so that if data comes available for one of the file descriptors
1864 * we don't continually spin in the poll()
1866 /* HOLDS: source->context's lock */
1868 block_source (GSource *source)
1872 g_return_if_fail (!SOURCE_BLOCKED (source));
1874 tmp_list = source->poll_fds;
1877 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1878 tmp_list = tmp_list->next;
1882 /* HOLDS: source->context's lock */
1884 unblock_source (GSource *source)
1888 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1889 g_return_if_fail (!SOURCE_DESTROYED (source));
1891 tmp_list = source->poll_fds;
1894 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1895 tmp_list = tmp_list->next;
1899 /* HOLDS: context's lock */
1901 g_main_dispatch (GMainContext *context)
1903 GMainDispatch *current = get_dispatch ();
1906 for (i = 0; i < context->pending_dispatches->len; i++)
1908 GSource *source = context->pending_dispatches->pdata[i];
1910 context->pending_dispatches->pdata[i] = NULL;
1913 source->flags &= ~G_SOURCE_READY;
1915 if (!SOURCE_DESTROYED (source))
1917 gboolean was_in_call;
1918 gpointer user_data = NULL;
1919 GSourceFunc callback = NULL;
1920 GSourceCallbackFuncs *cb_funcs;
1922 gboolean need_destroy;
1924 gboolean (*dispatch) (GSource *,
1927 GSList current_source_link;
1929 dispatch = source->source_funcs->dispatch;
1930 cb_funcs = source->callback_funcs;
1931 cb_data = source->callback_data;
1934 cb_funcs->ref (cb_data);
1936 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
1937 block_source (source);
1939 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1940 source->flags |= G_HOOK_FLAG_IN_CALL;
1943 cb_funcs->get (cb_data, source, &callback, &user_data);
1945 UNLOCK_CONTEXT (context);
1948 /* The on-stack allocation of the GSList is unconventional, but
1949 * we know that the lifetime of the link is bounded to this
1950 * function as the link is kept in a thread specific list and
1951 * not manipulated outside of this function and its descendants.
1952 * Avoiding the overhead of a g_slist_alloc() is useful as many
1953 * applications do little more than dispatch events.
1955 * This is a performance hack - do not revert to g_slist_prepend()!
1957 current_source_link.data = source;
1958 current_source_link.next = current->dispatching_sources;
1959 current->dispatching_sources = ¤t_source_link;
1960 need_destroy = ! dispatch (source,
1963 g_assert (current->dispatching_sources == ¤t_source_link);
1964 current->dispatching_sources = current_source_link.next;
1968 cb_funcs->unref (cb_data);
1970 LOCK_CONTEXT (context);
1973 source->flags &= ~G_HOOK_FLAG_IN_CALL;
1975 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
1976 !SOURCE_DESTROYED (source))
1977 unblock_source (source);
1979 /* Note: this depends on the fact that we can't switch
1980 * sources from one main context to another
1982 if (need_destroy && !SOURCE_DESTROYED (source))
1984 g_assert (source->context == context);
1985 g_source_destroy_internal (source, context, TRUE);
1989 SOURCE_UNREF (source, context);
1992 g_ptr_array_set_size (context->pending_dispatches, 0);
1995 /* Holds context's lock */
1996 static inline GSource *
1997 next_valid_source (GMainContext *context,
2000 GSource *new_source = source ? source->next : context->source_list;
2004 if (!SOURCE_DESTROYED (new_source))
2006 new_source->ref_count++;
2010 new_source = new_source->next;
2014 SOURCE_UNREF (source, context);
2020 * g_main_context_acquire:
2021 * @context: a #GMainContext
2023 * Tries to become the owner of the specified context.
2024 * If some other thread is the owner of the context,
2025 * returns %FALSE immediately. Ownership is properly
2026 * recursive: the owner can require ownership again
2027 * and will release ownership when g_main_context_release()
2028 * is called as many times as g_main_context_acquire().
2030 * You must be the owner of a context before you
2031 * can call g_main_context_prepare(), g_main_context_query(),
2032 * g_main_context_check(), g_main_context_dispatch().
2034 * Return value: %TRUE if the operation succeeded, and
2035 * this thread is now the owner of @context.
2038 g_main_context_acquire (GMainContext *context)
2040 #ifdef G_THREADS_ENABLED
2041 gboolean result = FALSE;
2042 GThread *self = G_THREAD_SELF;
2044 if (context == NULL)
2045 context = g_main_context_default ();
2047 LOCK_CONTEXT (context);
2049 if (!context->owner)
2051 context->owner = self;
2052 g_assert (context->owner_count == 0);
2055 if (context->owner == self)
2057 context->owner_count++;
2061 UNLOCK_CONTEXT (context);
2064 #else /* !G_THREADS_ENABLED */
2066 #endif /* G_THREADS_ENABLED */
2070 * g_main_context_release:
2071 * @context: a #GMainContext
2073 * Releases ownership of a context previously acquired by this thread
2074 * with g_main_context_acquire(). If the context was acquired multiple
2075 * times, the ownership will be released only when g_main_context_release()
2076 * is called as many times as it was acquired.
2079 g_main_context_release (GMainContext *context)
2081 #ifdef G_THREADS_ENABLED
2082 if (context == NULL)
2083 context = g_main_context_default ();
2085 LOCK_CONTEXT (context);
2087 context->owner_count--;
2088 if (context->owner_count == 0)
2090 context->owner = NULL;
2092 if (context->waiters)
2094 GMainWaiter *waiter = context->waiters->data;
2095 gboolean loop_internal_waiter =
2096 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2097 context->waiters = g_slist_delete_link (context->waiters,
2099 if (!loop_internal_waiter)
2100 g_mutex_lock (waiter->mutex);
2102 g_cond_signal (waiter->cond);
2104 if (!loop_internal_waiter)
2105 g_mutex_unlock (waiter->mutex);
2109 UNLOCK_CONTEXT (context);
2110 #endif /* G_THREADS_ENABLED */
2114 * g_main_context_wait:
2115 * @context: a #GMainContext
2116 * @cond: a condition variable
2117 * @mutex: a mutex, currently held
2119 * Tries to become the owner of the specified context,
2120 * as with g_main_context_acquire(). But if another thread
2121 * is the owner, atomically drop @mutex and wait on @cond until
2122 * that owner releases ownership or until @cond is signaled, then
2123 * try again (once) to become the owner.
2125 * Return value: %TRUE if the operation succeeded, and
2126 * this thread is now the owner of @context.
2129 g_main_context_wait (GMainContext *context,
2133 #ifdef G_THREADS_ENABLED
2134 gboolean result = FALSE;
2135 GThread *self = G_THREAD_SELF;
2136 gboolean loop_internal_waiter;
2138 if (context == NULL)
2139 context = g_main_context_default ();
2141 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2143 if (!loop_internal_waiter)
2144 LOCK_CONTEXT (context);
2146 if (context->owner && context->owner != self)
2151 waiter.mutex = mutex;
2153 context->waiters = g_slist_append (context->waiters, &waiter);
2155 if (!loop_internal_waiter)
2156 UNLOCK_CONTEXT (context);
2157 g_cond_wait (cond, mutex);
2158 if (!loop_internal_waiter)
2159 LOCK_CONTEXT (context);
2161 context->waiters = g_slist_remove (context->waiters, &waiter);
2164 if (!context->owner)
2166 context->owner = self;
2167 g_assert (context->owner_count == 0);
2170 if (context->owner == self)
2172 context->owner_count++;
2176 if (!loop_internal_waiter)
2177 UNLOCK_CONTEXT (context);
2180 #else /* !G_THREADS_ENABLED */
2182 #endif /* G_THREADS_ENABLED */
2186 * g_main_context_prepare:
2187 * @context: a #GMainContext
2188 * @priority: location to store priority of highest priority
2189 * source already ready.
2191 * Prepares to poll sources within a main loop. The resulting information
2192 * for polling is determined by calling g_main_context_query ().
2194 * Return value: %TRUE if some source is ready to be dispatched
2198 g_main_context_prepare (GMainContext *context,
2203 gint current_priority = G_MAXINT;
2206 if (context == NULL)
2207 context = g_main_context_default ();
2209 LOCK_CONTEXT (context);
2211 context->time_is_current = FALSE;
2213 if (context->in_check_or_prepare)
2215 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2216 "prepare() member.");
2217 UNLOCK_CONTEXT (context);
2221 #ifdef G_THREADS_ENABLED
2222 if (context->poll_waiting)
2224 g_warning("g_main_context_prepare(): main loop already active in another thread");
2225 UNLOCK_CONTEXT (context);
2229 context->poll_waiting = TRUE;
2230 #endif /* G_THREADS_ENABLED */
2233 /* If recursing, finish up current dispatch, before starting over */
2234 if (context->pending_dispatches)
2237 g_main_dispatch (context, ¤t_time);
2239 UNLOCK_CONTEXT (context);
2244 /* If recursing, clear list of pending dispatches */
2246 for (i = 0; i < context->pending_dispatches->len; i++)
2248 if (context->pending_dispatches->pdata[i])
2249 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2251 g_ptr_array_set_size (context->pending_dispatches, 0);
2253 /* Prepare all sources */
2255 context->timeout = -1;
2257 source = next_valid_source (context, NULL);
2260 gint source_timeout = -1;
2262 if ((n_ready > 0) && (source->priority > current_priority))
2264 SOURCE_UNREF (source, context);
2267 if (SOURCE_BLOCKED (source))
2270 if (!(source->flags & G_SOURCE_READY))
2273 gboolean (*prepare) (GSource *source,
2276 prepare = source->source_funcs->prepare;
2277 context->in_check_or_prepare++;
2278 UNLOCK_CONTEXT (context);
2280 result = (*prepare) (source, &source_timeout);
2282 LOCK_CONTEXT (context);
2283 context->in_check_or_prepare--;
2286 source->flags |= G_SOURCE_READY;
2289 if (source->flags & G_SOURCE_READY)
2292 current_priority = source->priority;
2293 context->timeout = 0;
2296 if (source_timeout >= 0)
2298 if (context->timeout < 0)
2299 context->timeout = source_timeout;
2301 context->timeout = MIN (context->timeout, source_timeout);
2305 source = next_valid_source (context, source);
2308 UNLOCK_CONTEXT (context);
2311 *priority = current_priority;
2313 return (n_ready > 0);
2317 * g_main_context_query:
2318 * @context: a #GMainContext
2319 * @max_priority: maximum priority source to check
2320 * @timeout_: location to store timeout to be used in polling
2321 * @fds: location to store #GPollFD records that need to be polled.
2322 * @n_fds: length of @fds.
2324 * Determines information necessary to poll this main loop.
2326 * Return value: the number of records actually stored in @fds,
2327 * or, if more than @n_fds records need to be stored, the number
2328 * of records that need to be stored.
2331 g_main_context_query (GMainContext *context,
2340 LOCK_CONTEXT (context);
2342 pollrec = context->poll_records;
2344 while (pollrec && max_priority >= pollrec->priority)
2346 /* We need to include entries with fd->events == 0 in the array because
2347 * otherwise if the application changes fd->events behind our back and
2348 * makes it non-zero, we'll be out of sync when we check the fds[] array.
2349 * (Changing fd->events after adding an FD wasn't an anticipated use of
2350 * this API, but it occurs in practice.) */
2353 fds[n_poll].fd = pollrec->fd->fd;
2354 /* In direct contradiction to the Unix98 spec, IRIX runs into
2355 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2356 * flags in the events field of the pollfd while it should
2357 * just ignoring them. So we mask them out here.
2359 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2360 fds[n_poll].revents = 0;
2363 pollrec = pollrec->next;
2367 #ifdef G_THREADS_ENABLED
2368 context->poll_changed = FALSE;
2373 *timeout = context->timeout;
2375 context->time_is_current = FALSE;
2378 UNLOCK_CONTEXT (context);
2384 * g_main_context_check:
2385 * @context: a #GMainContext
2386 * @max_priority: the maximum numerical priority of sources to check
2387 * @fds: array of #GPollFD's that was passed to the last call to
2388 * g_main_context_query()
2389 * @n_fds: return value of g_main_context_query()
2391 * Passes the results of polling back to the main loop.
2393 * Return value: %TRUE if some sources are ready to be dispatched.
2396 g_main_context_check (GMainContext *context,
2406 LOCK_CONTEXT (context);
2408 if (context->in_check_or_prepare)
2410 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2411 "prepare() member.");
2412 UNLOCK_CONTEXT (context);
2416 #ifdef G_THREADS_ENABLED
2417 if (!context->poll_waiting)
2421 read (context->wake_up_pipe[0], &a, 1);
2425 context->poll_waiting = FALSE;
2427 /* If the set of poll file descriptors changed, bail out
2428 * and let the main loop rerun
2430 if (context->poll_changed)
2432 UNLOCK_CONTEXT (context);
2435 #endif /* G_THREADS_ENABLED */
2437 pollrec = context->poll_records;
2441 if (pollrec->fd->events)
2442 pollrec->fd->revents = fds[i].revents;
2444 pollrec = pollrec->next;
2448 source = next_valid_source (context, NULL);
2451 if ((n_ready > 0) && (source->priority > max_priority))
2453 SOURCE_UNREF (source, context);
2456 if (SOURCE_BLOCKED (source))
2459 if (!(source->flags & G_SOURCE_READY))
2462 gboolean (*check) (GSource *source);
2464 check = source->source_funcs->check;
2466 context->in_check_or_prepare++;
2467 UNLOCK_CONTEXT (context);
2469 result = (*check) (source);
2471 LOCK_CONTEXT (context);
2472 context->in_check_or_prepare--;
2475 source->flags |= G_SOURCE_READY;
2478 if (source->flags & G_SOURCE_READY)
2480 source->ref_count++;
2481 g_ptr_array_add (context->pending_dispatches, source);
2485 /* never dispatch sources with less priority than the first
2486 * one we choose to dispatch
2488 max_priority = source->priority;
2492 source = next_valid_source (context, source);
2495 UNLOCK_CONTEXT (context);
2501 * g_main_context_dispatch:
2502 * @context: a #GMainContext
2504 * Dispatches all pending sources.
2507 g_main_context_dispatch (GMainContext *context)
2509 LOCK_CONTEXT (context);
2511 if (context->pending_dispatches->len > 0)
2513 g_main_dispatch (context);
2516 UNLOCK_CONTEXT (context);
2519 /* HOLDS context lock */
2521 g_main_context_iterate (GMainContext *context,
2528 gboolean some_ready;
2529 gint nfds, allocated_nfds;
2530 GPollFD *fds = NULL;
2532 UNLOCK_CONTEXT (context);
2534 #ifdef G_THREADS_ENABLED
2535 if (!g_main_context_acquire (context))
2537 gboolean got_ownership;
2539 LOCK_CONTEXT (context);
2541 g_return_val_if_fail (g_thread_supported (), FALSE);
2547 context->cond = g_cond_new ();
2549 got_ownership = g_main_context_wait (context,
2551 g_static_mutex_get_mutex (&context->mutex));
2557 LOCK_CONTEXT (context);
2558 #endif /* G_THREADS_ENABLED */
2560 if (!context->cached_poll_array)
2562 context->cached_poll_array_size = context->n_poll_records;
2563 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2566 allocated_nfds = context->cached_poll_array_size;
2567 fds = context->cached_poll_array;
2569 UNLOCK_CONTEXT (context);
2571 g_main_context_prepare (context, &max_priority);
2573 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2574 allocated_nfds)) > allocated_nfds)
2576 LOCK_CONTEXT (context);
2578 context->cached_poll_array_size = allocated_nfds = nfds;
2579 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2580 UNLOCK_CONTEXT (context);
2586 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2588 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2591 g_main_context_dispatch (context);
2593 #ifdef G_THREADS_ENABLED
2594 g_main_context_release (context);
2595 #endif /* G_THREADS_ENABLED */
2597 LOCK_CONTEXT (context);
2603 * g_main_context_pending:
2604 * @context: a #GMainContext (if %NULL, the default context will be used)
2606 * Checks if any sources have pending events for the given context.
2608 * Return value: %TRUE if events are pending.
2611 g_main_context_pending (GMainContext *context)
2616 context = g_main_context_default();
2618 LOCK_CONTEXT (context);
2619 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2620 UNLOCK_CONTEXT (context);
2626 * g_main_context_iteration:
2627 * @context: a #GMainContext (if %NULL, the default context will be used)
2628 * @may_block: whether the call may block.
2630 * Runs a single iteration for the given main loop. This involves
2631 * checking to see if any event sources are ready to be processed,
2632 * then if no events sources are ready and @may_block is %TRUE, waiting
2633 * for a source to become ready, then dispatching the highest priority
2634 * events sources that are ready. Otherwise, if @may_block is %FALSE
2635 * sources are not waited to become ready, only those highest priority
2636 * events sources will be dispatched (if any), that are ready at this
2637 * given moment without further waiting.
2639 * Note that even when @may_block is %TRUE, it is still possible for
2640 * g_main_context_iteration() to return %FALSE, since the the wait may
2641 * be interrupted for other reasons than an event source becoming ready.
2643 * Return value: %TRUE if events were dispatched.
2646 g_main_context_iteration (GMainContext *context, gboolean may_block)
2651 context = g_main_context_default();
2653 LOCK_CONTEXT (context);
2654 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2655 UNLOCK_CONTEXT (context);
2662 * @context: a #GMainContext (if %NULL, the default context will be used).
2663 * @is_running: set to %TRUE to indicate that the loop is running. This
2664 * is not very important since calling g_main_loop_run() will set this to
2667 * Creates a new #GMainLoop structure.
2669 * Return value: a new #GMainLoop.
2672 g_main_loop_new (GMainContext *context,
2673 gboolean is_running)
2678 context = g_main_context_default();
2680 g_main_context_ref (context);
2682 loop = g_new0 (GMainLoop, 1);
2683 loop->context = context;
2684 loop->is_running = is_running != FALSE;
2685 loop->ref_count = 1;
2692 * @loop: a #GMainLoop
2694 * Increases the reference count on a #GMainLoop object by one.
2696 * Return value: @loop
2699 g_main_loop_ref (GMainLoop *loop)
2701 g_return_val_if_fail (loop != NULL, NULL);
2702 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2704 g_atomic_int_inc (&loop->ref_count);
2710 * g_main_loop_unref:
2711 * @loop: a #GMainLoop
2713 * Decreases the reference count on a #GMainLoop object by one. If
2714 * the result is zero, free the loop and free all associated memory.
2717 g_main_loop_unref (GMainLoop *loop)
2719 g_return_if_fail (loop != NULL);
2720 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2722 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2725 g_main_context_unref (loop->context);
2731 * @loop: a #GMainLoop
2733 * Runs a main loop until g_main_loop_quit() is called on the loop.
2734 * If this is called for the thread of the loop's #GMainContext,
2735 * it will process events from the loop, otherwise it will
2739 g_main_loop_run (GMainLoop *loop)
2741 GThread *self = G_THREAD_SELF;
2743 g_return_if_fail (loop != NULL);
2744 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2746 #ifdef G_THREADS_ENABLED
2747 if (!g_main_context_acquire (loop->context))
2749 gboolean got_ownership = FALSE;
2751 /* Another thread owns this context */
2752 if (!g_thread_supported ())
2754 g_warning ("g_main_loop_run() was called from second thread but "
2755 "g_thread_init() was never called.");
2759 LOCK_CONTEXT (loop->context);
2761 g_atomic_int_inc (&loop->ref_count);
2763 if (!loop->is_running)
2764 loop->is_running = TRUE;
2766 if (!loop->context->cond)
2767 loop->context->cond = g_cond_new ();
2769 while (loop->is_running && !got_ownership)
2770 got_ownership = g_main_context_wait (loop->context,
2771 loop->context->cond,
2772 g_static_mutex_get_mutex (&loop->context->mutex));
2774 if (!loop->is_running)
2776 UNLOCK_CONTEXT (loop->context);
2778 g_main_context_release (loop->context);
2779 g_main_loop_unref (loop);
2783 g_assert (got_ownership);
2786 LOCK_CONTEXT (loop->context);
2787 #endif /* G_THREADS_ENABLED */
2789 if (loop->context->in_check_or_prepare)
2791 g_warning ("g_main_loop_run(): called recursively from within a source's "
2792 "check() or prepare() member, iteration not possible.");
2796 g_atomic_int_inc (&loop->ref_count);
2797 loop->is_running = TRUE;
2798 while (loop->is_running)
2799 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2801 UNLOCK_CONTEXT (loop->context);
2803 #ifdef G_THREADS_ENABLED
2804 g_main_context_release (loop->context);
2805 #endif /* G_THREADS_ENABLED */
2807 g_main_loop_unref (loop);
2812 * @loop: a #GMainLoop
2814 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2815 * for the loop will return.
2817 * Note that sources that have already been dispatched when
2818 * g_main_loop_quit() is called will still be executed.
2821 g_main_loop_quit (GMainLoop *loop)
2823 g_return_if_fail (loop != NULL);
2824 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2826 LOCK_CONTEXT (loop->context);
2827 loop->is_running = FALSE;
2828 g_main_context_wakeup_unlocked (loop->context);
2830 #ifdef G_THREADS_ENABLED
2831 if (loop->context->cond)
2832 g_cond_broadcast (loop->context->cond);
2833 #endif /* G_THREADS_ENABLED */
2835 UNLOCK_CONTEXT (loop->context);
2839 * g_main_loop_is_running:
2840 * @loop: a #GMainLoop.
2842 * Checks to see if the main loop is currently being run via g_main_loop_run().
2844 * Return value: %TRUE if the mainloop is currently being run.
2847 g_main_loop_is_running (GMainLoop *loop)
2849 g_return_val_if_fail (loop != NULL, FALSE);
2850 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2852 return loop->is_running;
2856 * g_main_loop_get_context:
2857 * @loop: a #GMainLoop.
2859 * Returns the #GMainContext of @loop.
2861 * Return value: the #GMainContext of @loop
2864 g_main_loop_get_context (GMainLoop *loop)
2866 g_return_val_if_fail (loop != NULL, NULL);
2867 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2869 return loop->context;
2872 /* HOLDS: context's lock */
2874 g_main_context_poll (GMainContext *context,
2880 #ifdef G_MAIN_POLL_DEBUG
2886 GPollFunc poll_func;
2888 if (n_fds || timeout != 0)
2890 #ifdef G_MAIN_POLL_DEBUG
2891 if (_g_main_poll_debug)
2893 g_print ("polling context=%p n=%d timeout=%d\n",
2894 context, n_fds, timeout);
2895 poll_timer = g_timer_new ();
2899 LOCK_CONTEXT (context);
2901 poll_func = context->poll_func;
2903 UNLOCK_CONTEXT (context);
2904 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2907 g_warning ("poll(2) failed due to: %s.",
2908 g_strerror (errno));
2910 /* If g_poll () returns -1, it has already called g_warning() */
2914 #ifdef G_MAIN_POLL_DEBUG
2915 if (_g_main_poll_debug)
2917 LOCK_CONTEXT (context);
2919 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2922 g_timer_elapsed (poll_timer, NULL));
2923 g_timer_destroy (poll_timer);
2924 pollrec = context->poll_records;
2926 while (pollrec != NULL)
2931 if (fds[i].fd == pollrec->fd->fd &&
2932 pollrec->fd->events &&
2935 g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd);
2936 if (fds[i].revents & G_IO_IN)
2938 if (fds[i].revents & G_IO_OUT)
2940 if (fds[i].revents & G_IO_PRI)
2942 if (fds[i].revents & G_IO_ERR)
2944 if (fds[i].revents & G_IO_HUP)
2946 if (fds[i].revents & G_IO_NVAL)
2952 pollrec = pollrec->next;
2956 UNLOCK_CONTEXT (context);
2959 } /* if (n_fds || timeout != 0) */
2963 * g_main_context_add_poll:
2964 * @context: a #GMainContext (or %NULL for the default context)
2965 * @fd: a #GPollFD structure holding information about a file
2966 * descriptor to watch.
2967 * @priority: the priority for this file descriptor which should be
2968 * the same as the priority used for g_source_attach() to ensure that the
2969 * file descriptor is polled whenever the results may be needed.
2971 * Adds a file descriptor to the set of file descriptors polled for
2972 * this context. This will very seldomly be used directly. Instead
2973 * a typical event source will use g_source_add_poll() instead.
2976 g_main_context_add_poll (GMainContext *context,
2981 context = g_main_context_default ();
2983 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
2984 g_return_if_fail (fd);
2986 LOCK_CONTEXT (context);
2987 g_main_context_add_poll_unlocked (context, priority, fd);
2988 UNLOCK_CONTEXT (context);
2991 /* HOLDS: main_loop_lock */
2993 g_main_context_add_poll_unlocked (GMainContext *context,
2997 GPollRec *lastrec, *pollrec;
2998 GPollRec *newrec = g_slice_new (GPollRec);
3000 /* This file descriptor may be checked before we ever poll */
3003 newrec->priority = priority;
3006 pollrec = context->poll_records;
3007 while (pollrec && priority >= pollrec->priority)
3010 pollrec = pollrec->next;
3014 lastrec->next = newrec;
3016 context->poll_records = newrec;
3018 newrec->next = pollrec;
3020 context->n_poll_records++;
3022 #ifdef G_THREADS_ENABLED
3023 context->poll_changed = TRUE;
3025 /* Now wake up the main loop if it is waiting in the poll() */
3026 g_main_context_wakeup_unlocked (context);
3031 * g_main_context_remove_poll:
3032 * @context:a #GMainContext
3033 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3035 * Removes file descriptor from the set of file descriptors to be
3036 * polled for a particular context.
3039 g_main_context_remove_poll (GMainContext *context,
3043 context = g_main_context_default ();
3045 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3046 g_return_if_fail (fd);
3048 LOCK_CONTEXT (context);
3049 g_main_context_remove_poll_unlocked (context, fd);
3050 UNLOCK_CONTEXT (context);
3054 g_main_context_remove_poll_unlocked (GMainContext *context,
3057 GPollRec *pollrec, *lastrec;
3060 pollrec = context->poll_records;
3064 if (pollrec->fd == fd)
3066 if (lastrec != NULL)
3067 lastrec->next = pollrec->next;
3069 context->poll_records = pollrec->next;
3071 g_slice_free (GPollRec, pollrec);
3073 context->n_poll_records--;
3077 pollrec = pollrec->next;
3080 #ifdef G_THREADS_ENABLED
3081 context->poll_changed = TRUE;
3083 /* Now wake up the main loop if it is waiting in the poll() */
3084 g_main_context_wakeup_unlocked (context);
3089 * g_source_get_current_time:
3090 * @source: a #GSource
3091 * @timeval: #GTimeVal structure in which to store current time.
3093 * Gets the "current time" to be used when checking
3094 * this source. The advantage of calling this function over
3095 * calling g_get_current_time() directly is that when
3096 * checking multiple sources, GLib can cache a single value
3097 * instead of having to repeatedly get the system time.
3100 g_source_get_current_time (GSource *source,
3103 GMainContext *context;
3105 g_return_if_fail (source->context != NULL);
3107 context = source->context;
3109 LOCK_CONTEXT (context);
3111 if (!context->time_is_current)
3113 g_get_current_time (&context->current_time);
3114 context->time_is_current = TRUE;
3117 *timeval = context->current_time;
3119 UNLOCK_CONTEXT (context);
3123 * g_main_context_set_poll_func:
3124 * @context: a #GMainContext
3125 * @func: the function to call to poll all file descriptors
3127 * Sets the function to use to handle polling of file descriptors. It
3128 * will be used instead of the poll() system call
3129 * (or GLib's replacement function, which is used where
3130 * poll() isn't available).
3132 * This function could possibly be used to integrate the GLib event
3133 * loop with an external event loop.
3136 g_main_context_set_poll_func (GMainContext *context,
3140 context = g_main_context_default ();
3142 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3144 LOCK_CONTEXT (context);
3147 context->poll_func = func;
3149 context->poll_func = g_poll;
3151 UNLOCK_CONTEXT (context);
3155 * g_main_context_get_poll_func:
3156 * @context: a #GMainContext
3158 * Gets the poll function set by g_main_context_set_poll_func().
3160 * Return value: the poll function
3163 g_main_context_get_poll_func (GMainContext *context)
3168 context = g_main_context_default ();
3170 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3172 LOCK_CONTEXT (context);
3173 result = context->poll_func;
3174 UNLOCK_CONTEXT (context);
3179 /* HOLDS: context's lock */
3180 /* Wake the main loop up from a poll() */
3182 g_main_context_wakeup_unlocked (GMainContext *context)
3184 #ifdef G_THREADS_ENABLED
3185 if (g_thread_supported() && context->poll_waiting)
3187 context->poll_waiting = FALSE;
3189 write (context->wake_up_pipe[1], "A", 1);
3191 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3198 * g_main_context_wakeup:
3199 * @context: a #GMainContext
3201 * If @context is currently waiting in a poll(), interrupt
3202 * the poll(), and continue the iteration process.
3205 g_main_context_wakeup (GMainContext *context)
3208 context = g_main_context_default ();
3210 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3212 LOCK_CONTEXT (context);
3213 g_main_context_wakeup_unlocked (context);
3214 UNLOCK_CONTEXT (context);
3218 * g_main_context_is_owner:
3219 * @context: a #GMainContext
3221 * Determines whether this thread holds the (recursive)
3222 * ownership of this #GMaincontext. This is useful to
3223 * know before waiting on another thread that may be
3224 * blocking to get ownership of @context.
3226 * Returns: %TRUE if current thread is owner of @context.
3231 g_main_context_is_owner (GMainContext *context)
3236 context = g_main_context_default ();
3238 #ifdef G_THREADS_ENABLED
3239 LOCK_CONTEXT (context);
3240 is_owner = context->owner == G_THREAD_SELF;
3241 UNLOCK_CONTEXT (context);
3252 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3253 GTimeVal *current_time)
3255 guint seconds = timeout_source->interval / 1000;
3256 guint msecs = timeout_source->interval - seconds * 1000;
3258 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3259 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3260 if (timeout_source->expiration.tv_usec >= 1000000)
3262 timeout_source->expiration.tv_usec -= 1000000;
3263 timeout_source->expiration.tv_sec++;
3265 if (timer_perturb==-1)
3268 * we want a per machine/session unique 'random' value; try the dbus
3269 * address first, that has a UUID in it. If there is no dbus, use the
3270 * hostname for hashing.
3272 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3273 if (!session_bus_address)
3274 session_bus_address = g_getenv ("HOSTNAME");
3275 if (session_bus_address)
3276 timer_perturb = ABS ((gint) g_str_hash (session_bus_address));
3280 if (timeout_source->granularity)
3283 gint gran; /* in usecs */
3286 gran = timeout_source->granularity * 1000;
3287 perturb = timer_perturb % gran;
3289 * We want to give each machine a per machine pertubation;
3290 * shift time back first, and forward later after the rounding
3293 timeout_source->expiration.tv_usec -= perturb;
3294 if (timeout_source->expiration.tv_usec < 0)
3296 timeout_source->expiration.tv_usec += 1000000;
3297 timeout_source->expiration.tv_sec--;
3300 remainder = timeout_source->expiration.tv_usec % gran;
3301 if (remainder >= gran/4) /* round up */
3302 timeout_source->expiration.tv_usec += gran;
3303 timeout_source->expiration.tv_usec -= remainder;
3305 timeout_source->expiration.tv_usec += perturb;
3307 /* the rounding may have overflown tv_usec */
3308 while (timeout_source->expiration.tv_usec > 1000000)
3310 timeout_source->expiration.tv_usec -= 1000000;
3311 timeout_source->expiration.tv_sec++;
3317 g_timeout_prepare (GSource *source,
3322 GTimeVal current_time;
3324 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3326 g_source_get_current_time (source, ¤t_time);
3328 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3329 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3331 /* We do the following in a rather convoluted fashion to deal with
3332 * the fact that we don't have an integral type big enough to hold
3333 * the difference of two timevals in millseconds.
3335 if (sec < 0 || (sec == 0 && msec < 0))
3339 glong interval_sec = timeout_source->interval / 1000;
3340 glong interval_msec = timeout_source->interval % 1000;
3348 if (sec > interval_sec ||
3349 (sec == interval_sec && msec > interval_msec))
3351 /* The system time has been set backwards, so we
3352 * reset the expiration time to now + timeout_source->interval;
3353 * this at least avoids hanging for long periods of time.
3355 g_timeout_set_expiration (timeout_source, ¤t_time);
3356 msec = MIN (G_MAXINT, timeout_source->interval);
3360 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3364 *timeout = (gint)msec;
3370 g_timeout_check (GSource *source)
3372 GTimeVal current_time;
3373 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3375 g_source_get_current_time (source, ¤t_time);
3377 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3378 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3379 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3383 g_timeout_dispatch (GSource *source,
3384 GSourceFunc callback,
3387 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3391 g_warning ("Timeout source dispatched without callback\n"
3392 "You must call g_source_set_callback().");
3396 if (callback (user_data))
3398 GTimeVal current_time;
3400 g_source_get_current_time (source, ¤t_time);
3401 g_timeout_set_expiration (timeout_source, ¤t_time);
3410 * g_timeout_source_new:
3411 * @interval: the timeout interval in milliseconds.
3413 * Creates a new timeout source.
3415 * The source will not initially be associated with any #GMainContext
3416 * and must be added to one with g_source_attach() before it will be
3419 * Return value: the newly-created timeout source
3422 g_timeout_source_new (guint interval)
3424 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3425 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3426 GTimeVal current_time;
3428 timeout_source->interval = interval;
3430 g_get_current_time (¤t_time);
3431 g_timeout_set_expiration (timeout_source, ¤t_time);
3437 * g_timeout_source_new_seconds:
3438 * @interval: the timeout interval in seconds
3440 * Creates a new timeout source.
3442 * The source will not initially be associated with any #GMainContext
3443 * and must be added to one with g_source_attach() before it will be
3446 * The scheduling granularity/accuracy of this timeout source will be
3449 * Return value: the newly-created timeout source
3454 g_timeout_source_new_seconds (guint interval)
3456 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3457 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3458 GTimeVal current_time;
3460 timeout_source->interval = 1000*interval;
3461 timeout_source->granularity = 1000;
3463 g_get_current_time (¤t_time);
3464 g_timeout_set_expiration (timeout_source, ¤t_time);
3471 * g_timeout_add_full:
3472 * @priority: the priority of the timeout source. Typically this will be in
3473 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3474 * @interval: the time between calls to the function, in milliseconds
3475 * (1/1000ths of a second)
3476 * @function: function to call
3477 * @data: data to pass to @function
3478 * @notify: function to call when the timeout is removed, or %NULL
3480 * Sets a function to be called at regular intervals, with the given
3481 * priority. The function is called repeatedly until it returns
3482 * %FALSE, at which point the timeout is automatically destroyed and
3483 * the function will not be called again. The @notify function is
3484 * called when the timeout is destroyed. The first call to the
3485 * function will be at the end of the first @interval.
3487 * Note that timeout functions may be delayed, due to the processing of other
3488 * event sources. Thus they should not be relied on for precise timing.
3489 * After each call to the timeout function, the time of the next
3490 * timeout is recalculated based on the current time and the given interval
3491 * (it does not try to 'catch up' time lost in delays).
3493 * This internally creates a main loop source using g_timeout_source_new()
3494 * and attaches it to the main loop context using g_source_attach(). You can
3495 * do these steps manually if you need greater control.
3497 * Return value: the ID (greater than 0) of the event source.
3500 g_timeout_add_full (gint priority,
3502 GSourceFunc function,
3504 GDestroyNotify notify)
3509 g_return_val_if_fail (function != NULL, 0);
3511 source = g_timeout_source_new (interval);
3513 if (priority != G_PRIORITY_DEFAULT)
3514 g_source_set_priority (source, priority);
3516 g_source_set_callback (source, function, data, notify);
3517 id = g_source_attach (source, NULL);
3518 g_source_unref (source);
3525 * @interval: the time between calls to the function, in milliseconds
3526 * (1/1000ths of a second)
3527 * @function: function to call
3528 * @data: data to pass to @function
3530 * Sets a function to be called at regular intervals, with the default
3531 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3532 * until it returns %FALSE, at which point the timeout is automatically
3533 * destroyed and the function will not be called again. The first call
3534 * to the function will be at the end of the first @interval.
3536 * Note that timeout functions may be delayed, due to the processing of other
3537 * event sources. Thus they should not be relied on for precise timing.
3538 * After each call to the timeout function, the time of the next
3539 * timeout is recalculated based on the current time and the given interval
3540 * (it does not try to 'catch up' time lost in delays).
3542 * If you want to have a timer in the "seconds" range and do not care
3543 * about the exact time of the first call of the timer, use the
3544 * g_timeout_add_seconds() function; this function allows for more
3545 * optimizations and more efficient system power usage.
3547 * This internally creates a main loop source using g_timeout_source_new()
3548 * and attaches it to the main loop context using g_source_attach(). You can
3549 * do these steps manually if you need greater control.
3551 * Return value: the ID (greater than 0) of the event source.
3554 g_timeout_add (guint32 interval,
3555 GSourceFunc function,
3558 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3559 interval, function, data, NULL);
3563 * g_timeout_add_seconds_full:
3564 * @priority: the priority of the timeout source. Typically this will be in
3565 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3566 * @interval: the time between calls to the function, in seconds
3567 * @function: function to call
3568 * @data: data to pass to @function
3569 * @notify: function to call when the timeout is removed, or %NULL
3571 * Sets a function to be called at regular intervals, with @priority.
3572 * The function is called repeatedly until it returns %FALSE, at which
3573 * point the timeout is automatically destroyed and the function will
3574 * not be called again.
3576 * Unlike g_timeout_add(), this function operates at whole second granularity.
3577 * The initial starting point of the timer is determined by the implementation
3578 * and the implementation is expected to group multiple timers together so that
3579 * they fire all at the same time.
3580 * To allow this grouping, the @interval to the first timer is rounded
3581 * and can deviate up to one second from the specified interval.
3582 * Subsequent timer iterations will generally run at the specified interval.
3584 * Note that timeout functions may be delayed, due to the processing of other
3585 * event sources. Thus they should not be relied on for precise timing.
3586 * After each call to the timeout function, the time of the next
3587 * timeout is recalculated based on the current time and the given @interval
3589 * If you want timing more precise than whole seconds, use g_timeout_add()
3592 * The grouping of timers to fire at the same time results in a more power
3593 * and CPU efficient behavior so if your timer is in multiples of seconds
3594 * and you don't require the first timer exactly one second from now, the
3595 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3597 * This internally creates a main loop source using
3598 * g_timeout_source_new_seconds() and attaches it to the main loop context
3599 * using g_source_attach(). You can do these steps manually if you need
3602 * Return value: the ID (greater than 0) of the event source.
3607 g_timeout_add_seconds_full (gint priority,
3609 GSourceFunc function,
3611 GDestroyNotify notify)
3616 g_return_val_if_fail (function != NULL, 0);
3618 source = g_timeout_source_new_seconds (interval);
3620 if (priority != G_PRIORITY_DEFAULT)
3621 g_source_set_priority (source, priority);
3623 g_source_set_callback (source, function, data, notify);
3624 id = g_source_attach (source, NULL);
3625 g_source_unref (source);
3631 * g_timeout_add_seconds:
3632 * @interval: the time between calls to the function, in seconds
3633 * @function: function to call
3634 * @data: data to pass to @function
3636 * Sets a function to be called at regular intervals with the default
3637 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3638 * it returns %FALSE, at which point the timeout is automatically destroyed
3639 * and the function will not be called again.
3641 * This internally creates a main loop source using
3642 * g_timeout_source_new_seconds() and attaches it to the main loop context
3643 * using g_source_attach(). You can do these steps manually if you need
3644 * greater control. Also see g_timout_add_seconds_full().
3646 * Return value: the ID (greater than 0) of the event source.
3651 g_timeout_add_seconds (guint interval,
3652 GSourceFunc function,
3655 g_return_val_if_fail (function != NULL, 0);
3657 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3660 /* Child watch functions */
3665 g_child_watch_prepare (GSource *source,
3674 g_child_watch_check (GSource *source)
3676 GChildWatchSource *child_watch_source;
3677 gboolean child_exited;
3679 child_watch_source = (GChildWatchSource *) source;
3681 child_exited = child_watch_source->poll.revents & G_IO_IN;
3688 * Note: We do _not_ check for the special value of STILL_ACTIVE
3689 * since we know that the process has exited and doing so runs into
3690 * problems if the child process "happens to return STILL_ACTIVE(259)"
3691 * as Microsoft's Platform SDK puts it.
3693 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3695 gchar *emsg = g_win32_error_message (GetLastError ());
3696 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3699 child_watch_source->child_status = -1;
3702 child_watch_source->child_status = child_status;
3705 return child_exited;
3708 #else /* G_OS_WIN32 */
3711 check_for_child_exited (GSource *source)
3713 GChildWatchSource *child_watch_source;
3716 /* protect against another SIGCHLD in the middle of this call */
3717 count = child_watch_count;
3719 child_watch_source = (GChildWatchSource *) source;
3721 if (child_watch_source->child_exited)
3724 if (child_watch_source->count < count)
3728 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3730 child_watch_source->child_status = child_status;
3731 child_watch_source->child_exited = TRUE;
3733 child_watch_source->count = count;
3736 return child_watch_source->child_exited;
3740 g_child_watch_prepare (GSource *source,
3745 return check_for_child_exited (source);
3750 g_child_watch_check (GSource *source)
3752 return check_for_child_exited (source);
3755 #endif /* G_OS_WIN32 */
3758 g_child_watch_dispatch (GSource *source,
3759 GSourceFunc callback,
3762 GChildWatchSource *child_watch_source;
3763 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3765 child_watch_source = (GChildWatchSource *) source;
3769 g_warning ("Child watch source dispatched without callback\n"
3770 "You must call g_source_set_callback().");
3774 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3776 /* We never keep a child watch source around as the child is gone */
3783 g_child_watch_signal_handler (int signum)
3785 child_watch_count ++;
3787 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3789 write (child_watch_wake_up_pipe[1], "B", 1);
3793 /* We count on the signal interrupting the poll in the same thread.
3799 g_child_watch_source_init_single (void)
3801 struct sigaction action;
3803 g_assert (! g_thread_supported());
3804 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3806 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3808 action.sa_handler = g_child_watch_signal_handler;
3809 sigemptyset (&action.sa_mask);
3810 action.sa_flags = SA_NOCLDSTOP;
3811 sigaction (SIGCHLD, &action, NULL);
3814 G_GNUC_NORETURN static gpointer
3815 child_watch_helper_thread (gpointer data)
3822 read (child_watch_wake_up_pipe[0], b, 20);
3824 /* We were woken up. Wake up all other contexts in all other threads */
3825 G_LOCK (main_context_list);
3826 for (list = main_context_list; list; list = list->next)
3828 GMainContext *context;
3830 context = list->data;
3831 if (g_atomic_int_get (&context->ref_count) > 0)
3832 /* Due to racing conditions we can find ref_count == 0, in
3833 * that case, however, the context is still not destroyed
3834 * and no poll can be active, otherwise the ref_count
3836 g_main_context_wakeup (context);
3838 G_UNLOCK (main_context_list);
3843 g_child_watch_source_init_multi_threaded (void)
3845 GError *error = NULL;
3846 struct sigaction action;
3848 g_assert (g_thread_supported());
3850 if (pipe (child_watch_wake_up_pipe) < 0)
3851 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3852 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3854 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3855 /* FIXME: Think this through for races */
3856 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3857 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3858 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3860 action.sa_handler = g_child_watch_signal_handler;
3861 sigemptyset (&action.sa_mask);
3862 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3863 sigaction (SIGCHLD, &action, NULL);
3867 g_child_watch_source_init_promote_single_to_threaded (void)
3869 g_child_watch_source_init_multi_threaded ();
3873 g_child_watch_source_init (void)
3875 if (g_thread_supported())
3877 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3878 g_child_watch_source_init_multi_threaded ();
3879 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3880 g_child_watch_source_init_promote_single_to_threaded ();
3884 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3885 g_child_watch_source_init_single ();
3889 #endif /* !G_OS_WIN32 */
3892 * g_child_watch_source_new:
3893 * @pid: process to watch. On POSIX the pid of a child process. On
3894 * Windows a handle for a process (which doesn't have to be a child).
3896 * Creates a new child_watch source.
3898 * The source will not initially be associated with any #GMainContext
3899 * and must be added to one with g_source_attach() before it will be
3902 * Note that child watch sources can only be used in conjunction with
3903 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
3906 * Note that on platforms where #GPid must be explicitly closed
3907 * (see g_spawn_close_pid()) @pid must not be closed while the
3908 * source is still active. Typically, you will want to call
3909 * g_spawn_close_pid() in the callback function for the source.
3911 * Note further that using g_child_watch_source_new() is not
3912 * compatible with calling <literal>waitpid(-1)</literal> in
3913 * the application. Calling waitpid() for individual pids will
3916 * Return value: the newly-created child watch source
3921 g_child_watch_source_new (GPid pid)
3923 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
3924 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
3927 child_watch_source->poll.fd = (gintptr) pid;
3928 child_watch_source->poll.events = G_IO_IN;
3930 g_source_add_poll (source, &child_watch_source->poll);
3931 #else /* G_OS_WIN32 */
3932 g_child_watch_source_init ();
3933 #endif /* G_OS_WIN32 */
3935 child_watch_source->pid = pid;
3941 * g_child_watch_add_full:
3942 * @priority: the priority of the idle source. Typically this will be in the
3943 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3944 * @pid: process to watch. On POSIX the pid of a child process. On
3945 * Windows a handle for a process (which doesn't have to be a child).
3946 * @function: function to call
3947 * @data: data to pass to @function
3948 * @notify: function to call when the idle is removed, or %NULL
3950 * Sets a function to be called when the child indicated by @pid
3951 * exits, at the priority @priority.
3953 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
3954 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
3955 * the spawn function for the child watching to work.
3957 * Note that on platforms where #GPid must be explicitly closed
3958 * (see g_spawn_close_pid()) @pid must not be closed while the
3959 * source is still active. Typically, you will want to call
3960 * g_spawn_close_pid() in the callback function for the source.
3962 * GLib supports only a single callback per process id.
3964 * This internally creates a main loop source using
3965 * g_child_watch_source_new() and attaches it to the main loop context
3966 * using g_source_attach(). You can do these steps manually if you
3967 * need greater control.
3969 * Return value: the ID (greater than 0) of the event source.
3974 g_child_watch_add_full (gint priority,
3976 GChildWatchFunc function,
3978 GDestroyNotify notify)
3983 g_return_val_if_fail (function != NULL, 0);
3985 source = g_child_watch_source_new (pid);
3987 if (priority != G_PRIORITY_DEFAULT)
3988 g_source_set_priority (source, priority);
3990 g_source_set_callback (source, (GSourceFunc) function, data, notify);
3991 id = g_source_attach (source, NULL);
3992 g_source_unref (source);
3998 * g_child_watch_add:
3999 * @pid: process id to watch. On POSIX the pid of a child process. On
4000 * Windows a handle for a process (which doesn't have to be a child).
4001 * @function: function to call
4002 * @data: data to pass to @function
4004 * Sets a function to be called when the child indicated by @pid
4005 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4007 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4008 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4009 * the spawn function for the child watching to work.
4011 * Note that on platforms where #GPid must be explicitly closed
4012 * (see g_spawn_close_pid()) @pid must not be closed while the
4013 * source is still active. Typically, you will want to call
4014 * g_spawn_close_pid() in the callback function for the source.
4016 * GLib supports only a single callback per process id.
4018 * This internally creates a main loop source using
4019 * g_child_watch_source_new() and attaches it to the main loop context
4020 * using g_source_attach(). You can do these steps manually if you
4021 * need greater control.
4023 * Return value: the ID (greater than 0) of the event source.
4028 g_child_watch_add (GPid pid,
4029 GChildWatchFunc function,
4032 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4036 /* Idle functions */
4039 g_idle_prepare (GSource *source,
4048 g_idle_check (GSource *source)
4054 g_idle_dispatch (GSource *source,
4055 GSourceFunc callback,
4060 g_warning ("Idle source dispatched without callback\n"
4061 "You must call g_source_set_callback().");
4065 return callback (user_data);
4069 * g_idle_source_new:
4071 * Creates a new idle source.
4073 * The source will not initially be associated with any #GMainContext
4074 * and must be added to one with g_source_attach() before it will be
4075 * executed. Note that the default priority for idle sources is
4076 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4077 * have a default priority of %G_PRIORITY_DEFAULT.
4079 * Return value: the newly-created idle source
4082 g_idle_source_new (void)
4086 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4087 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4094 * @priority: the priority of the idle source. Typically this will be in the
4095 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4096 * @function: function to call
4097 * @data: data to pass to @function
4098 * @notify: function to call when the idle is removed, or %NULL
4100 * Adds a function to be called whenever there are no higher priority
4101 * events pending. If the function returns %FALSE it is automatically
4102 * removed from the list of event sources and will not be called again.
4104 * This internally creates a main loop source using g_idle_source_new()
4105 * and attaches it to the main loop context using g_source_attach().
4106 * You can do these steps manually if you need greater control.
4108 * Return value: the ID (greater than 0) of the event source.
4111 g_idle_add_full (gint priority,
4112 GSourceFunc function,
4114 GDestroyNotify notify)
4119 g_return_val_if_fail (function != NULL, 0);
4121 source = g_idle_source_new ();
4123 if (priority != G_PRIORITY_DEFAULT_IDLE)
4124 g_source_set_priority (source, priority);
4126 g_source_set_callback (source, function, data, notify);
4127 id = g_source_attach (source, NULL);
4128 g_source_unref (source);
4135 * @function: function to call
4136 * @data: data to pass to @function.
4138 * Adds a function to be called whenever there are no higher priority
4139 * events pending to the default main loop. The function is given the
4140 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4141 * returns %FALSE it is automatically removed from the list of event
4142 * sources and will not be called again.
4144 * This internally creates a main loop source using g_idle_source_new()
4145 * and attaches it to the main loop context using g_source_attach().
4146 * You can do these steps manually if you need greater control.
4148 * Return value: the ID (greater than 0) of the event source.
4151 g_idle_add (GSourceFunc function,
4154 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4158 * g_idle_remove_by_data:
4159 * @data: the data for the idle source's callback.
4161 * Removes the idle function with the given data.
4163 * Return value: %TRUE if an idle source was found and removed.
4166 g_idle_remove_by_data (gpointer data)
4168 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4171 #define __G_MAIN_C__
4172 #include "galiasdef.c"