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 */
83 typedef struct _GTimeoutSource GTimeoutSource;
84 typedef struct _GChildWatchSource GChildWatchSource;
85 typedef struct _GPollRec GPollRec;
86 typedef struct _GSourceCallback GSourceCallback;
90 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
91 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
94 #ifdef G_THREADS_ENABLED
95 typedef struct _GMainWaiter GMainWaiter;
104 typedef struct _GMainDispatch GMainDispatch;
106 struct _GMainDispatch
109 GSList *dispatching_sources; /* stack of current sources */
112 #ifdef G_MAIN_POLL_DEBUG
113 gboolean _g_main_poll_debug = FALSE;
118 #ifdef G_THREADS_ENABLED
119 /* The following lock is used for both the list of sources
120 * and the list of poll records
131 GPtrArray *pending_dispatches;
132 gint timeout; /* Timeout for current iteration */
135 GSource *source_list;
136 gint in_check_or_prepare;
138 GPollRec *poll_records;
139 guint n_poll_records;
140 GPollFD *cached_poll_array;
141 guint cached_poll_array_size;
143 #ifdef G_THREADS_ENABLED
145 /* this pipe is used to wake up the main loop when a source is added.
147 gint wake_up_pipe[2];
148 #else /* G_OS_WIN32 */
149 HANDLE wake_up_semaphore;
150 #endif /* G_OS_WIN32 */
153 gboolean poll_waiting;
155 /* Flag indicating whether the set of fd's changed during a poll */
156 gboolean poll_changed;
157 #endif /* G_THREADS_ENABLED */
161 GTimeVal current_time;
162 gboolean time_is_current;
165 struct _GSourceCallback
170 GDestroyNotify notify;
175 GMainContext *context;
180 struct _GTimeoutSource
188 struct _GChildWatchSource
195 #else /* G_OS_WIN32 */
197 gboolean child_exited;
198 #endif /* G_OS_WIN32 */
208 #ifdef G_THREADS_ENABLED
209 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
210 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
211 #define G_THREAD_SELF g_thread_self ()
213 #define LOCK_CONTEXT(context) (void)0
214 #define UNLOCK_CONTEXT(context) (void)0
215 #define G_THREAD_SELF NULL
218 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
219 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
220 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
222 #define SOURCE_UNREF(source, context) \
224 if ((source)->ref_count > 1) \
225 (source)->ref_count--; \
227 g_source_unref_internal ((source), (context), TRUE); \
231 /* Forward declarations */
233 static void g_source_unref_internal (GSource *source,
234 GMainContext *context,
236 static void g_source_destroy_internal (GSource *source,
237 GMainContext *context,
239 static void g_main_context_poll (GMainContext *context,
244 static void g_main_context_add_poll_unlocked (GMainContext *context,
247 static void g_main_context_remove_poll_unlocked (GMainContext *context,
249 static void g_main_context_wakeup_unlocked (GMainContext *context);
251 static gboolean g_timeout_prepare (GSource *source,
253 static gboolean g_timeout_check (GSource *source);
254 static gboolean g_timeout_dispatch (GSource *source,
255 GSourceFunc callback,
257 static gboolean g_child_watch_prepare (GSource *source,
259 static gboolean g_child_watch_check (GSource *source);
260 static gboolean g_child_watch_dispatch (GSource *source,
261 GSourceFunc callback,
263 static gboolean g_idle_prepare (GSource *source,
265 static gboolean g_idle_check (GSource *source);
266 static gboolean g_idle_dispatch (GSource *source,
267 GSourceFunc callback,
270 G_LOCK_DEFINE_STATIC (main_loop);
271 static GMainContext *default_main_context;
272 static GSList *main_contexts_without_pipe = NULL;
275 /* Child status monitoring code */
277 CHILD_WATCH_UNINITIALIZED,
278 CHILD_WATCH_INITIALIZED_SINGLE,
279 CHILD_WATCH_INITIALIZED_THREADED
281 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
282 static gint child_watch_count = 1;
283 static gint child_watch_wake_up_pipe[2] = {0, 0};
284 #endif /* !G_OS_WIN32 */
285 G_LOCK_DEFINE_STATIC (main_context_list);
286 static GSList *main_context_list = NULL;
288 static gint timer_perturb = -1;
290 GSourceFuncs g_timeout_funcs =
298 GSourceFuncs g_child_watch_funcs =
300 g_child_watch_prepare,
302 g_child_watch_dispatch,
306 GSourceFuncs g_idle_funcs =
315 * g_main_context_ref:
316 * @context: a #GMainContext
318 * Increases the reference count on a #GMainContext object by one.
320 * Returns: the @context that was passed in (since 2.6)
323 g_main_context_ref (GMainContext *context)
325 g_return_val_if_fail (context != NULL, NULL);
326 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
328 g_atomic_int_inc (&context->ref_count);
334 poll_rec_list_free (GMainContext *context,
337 g_slice_free_chain (GPollRec, list, next);
341 * g_main_context_unref:
342 * @context: a #GMainContext
344 * Decreases the reference count on a #GMainContext object by one. If
345 * the result is zero, free the context and free all associated memory.
348 g_main_context_unref (GMainContext *context)
351 g_return_if_fail (context != NULL);
352 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
354 if (!g_atomic_int_dec_and_test (&context->ref_count))
357 G_LOCK (main_context_list);
358 main_context_list = g_slist_remove (main_context_list, context);
359 G_UNLOCK (main_context_list);
361 source = context->source_list;
364 GSource *next = source->next;
365 g_source_destroy_internal (source, context, FALSE);
369 #ifdef G_THREADS_ENABLED
370 g_static_mutex_free (&context->mutex);
373 g_ptr_array_free (context->pending_dispatches, TRUE);
374 g_free (context->cached_poll_array);
376 poll_rec_list_free (context, context->poll_records);
378 #ifdef G_THREADS_ENABLED
379 if (g_thread_supported())
382 close (context->wake_up_pipe[0]);
383 close (context->wake_up_pipe[1]);
385 CloseHandle (context->wake_up_semaphore);
389 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
392 if (context->cond != NULL)
393 g_cond_free (context->cond);
399 #ifdef G_THREADS_ENABLED
401 g_main_context_init_pipe (GMainContext *context)
404 if (context->wake_up_pipe[0] != -1)
408 /* if this fails, we fall through and try pipe */
409 pipe2 (context->wake_up_pipe, O_CLOEXEC);
411 if (context->wake_up_pipe[0] == -1)
413 if (pipe (context->wake_up_pipe) < 0)
414 g_error ("Cannot create pipe main loop wake-up: %s\n",
417 fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC);
418 fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC);
421 context->wake_up_rec.fd = context->wake_up_pipe[0];
422 context->wake_up_rec.events = G_IO_IN;
424 if (context->wake_up_semaphore != NULL)
426 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
427 if (context->wake_up_semaphore == NULL)
428 g_error ("Cannot create wake-up semaphore: %s",
429 g_win32_error_message (GetLastError ()));
430 context->wake_up_rec.fd = (gintptr) context->wake_up_semaphore;
431 context->wake_up_rec.events = G_IO_IN;
433 if (_g_main_poll_debug)
434 g_print ("wake-up semaphore: %p\n", context->wake_up_semaphore);
436 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
440 _g_main_thread_init (void)
442 GSList *curr = main_contexts_without_pipe;
445 g_main_context_init_pipe ((GMainContext *)curr->data);
448 g_slist_free (main_contexts_without_pipe);
449 main_contexts_without_pipe = NULL;
451 #endif /* G_THREADS_ENABLED */
454 * g_main_context_new:
456 * Creates a new #GMainContext structure.
458 * Return value: the new #GMainContext
461 g_main_context_new (void)
463 GMainContext *context = g_new0 (GMainContext, 1);
465 #ifdef G_MAIN_POLL_DEBUG
467 static gboolean beenhere = FALSE;
471 if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
472 _g_main_poll_debug = TRUE;
478 #ifdef G_THREADS_ENABLED
479 g_static_mutex_init (&context->mutex);
481 context->owner = NULL;
482 context->waiters = NULL;
485 context->wake_up_pipe[0] = -1;
486 context->wake_up_pipe[1] = -1;
488 context->wake_up_semaphore = NULL;
492 context->ref_count = 1;
494 context->next_id = 1;
496 context->source_list = NULL;
498 context->poll_func = g_poll;
500 context->cached_poll_array = NULL;
501 context->cached_poll_array_size = 0;
503 context->pending_dispatches = g_ptr_array_new ();
505 context->time_is_current = FALSE;
507 #ifdef G_THREADS_ENABLED
508 if (g_thread_supported ())
509 g_main_context_init_pipe (context);
511 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
515 G_LOCK (main_context_list);
516 main_context_list = g_slist_append (main_context_list, context);
518 #ifdef G_MAIN_POLL_DEBUG
519 if (_g_main_poll_debug)
520 g_print ("created context=%p\n", context);
523 G_UNLOCK (main_context_list);
529 * g_main_context_default:
531 * Returns the global default main context. This is the main context
532 * used for main loop functions when a main loop is not explicitly
533 * specified, and corresponds to the "main" main loop. See also
534 * g_main_context_get_thread_default().
536 * Return value: the global default main context.
539 g_main_context_default (void)
545 if (!default_main_context)
547 default_main_context = g_main_context_new ();
548 #ifdef G_MAIN_POLL_DEBUG
549 if (_g_main_poll_debug)
550 g_print ("default context=%p\n", default_main_context);
554 G_UNLOCK (main_loop);
556 return default_main_context;
559 static GStaticPrivate thread_context_stack = G_STATIC_PRIVATE_INIT;
562 free_context_stack (gpointer data)
564 GQueue *stack = data;
565 GMainContext *context;
567 while (!g_queue_is_empty (stack))
569 context = g_queue_pop_head (stack);
570 g_main_context_release (context);
572 g_main_context_unref (context);
574 g_queue_free (stack);
578 * g_main_context_push_thread_default:
579 * @context: a #GMainContext, or %NULL for the global default context
581 * Acquires @context and sets it as the thread-default context for the
582 * current thread. This will cause certain asynchronous operations
583 * (such as most <link linkend="gio">gio</link>-based I/O) which are
584 * started in this thread to run under @context and deliver their
585 * results to its main loop, rather than running under the global
586 * default context in the main thread. Note that calling this function
587 * changes the context returned by
588 * g_main_context_get_thread_default(), <emphasis>not</emphasis> the
589 * one returned by g_main_context_default(), so it does not affect the
590 * context used by functions like g_idle_add().
592 * Normally you would call this function shortly after creating a new
593 * thread, passing it a #GMainContext which will be run by a
594 * #GMainLoop in that thread, to set a new default context for all
595 * async operations in that thread. (In this case, you don't need to
596 * ever call g_main_context_pop_thread_default().) In some cases
597 * however, you may want to schedule a single operation in a
598 * non-default context, or temporarily use a non-default context in
599 * the main thread. In that case, you can wrap the call to the
600 * asynchronous operation inside a
601 * g_main_context_push_thread_default() /
602 * g_main_context_pop_thread_default() pair, but it is up to you to
603 * ensure that no other asynchronous operations accidentally get
604 * started while the non-default context is active.
606 * Beware that libraries that predate this function may not correctly
607 * handle being used from a thread with a thread-default context. Eg,
608 * see g_file_supports_thread_contexts().
613 g_main_context_push_thread_default (GMainContext *context)
616 gboolean acquired_context;
618 acquired_context = g_main_context_acquire (context);
619 g_return_if_fail (acquired_context);
621 if (context == g_main_context_default ())
624 g_main_context_ref (context);
626 stack = g_static_private_get (&thread_context_stack);
629 stack = g_queue_new ();
630 g_static_private_set (&thread_context_stack, stack,
634 g_queue_push_head (stack, context);
638 * g_main_context_pop_thread_default:
639 * @context: a #GMainContext object, or %NULL
641 * Pops @context off the thread-default context stack (verifying that
642 * it was on the top of the stack).
647 g_main_context_pop_thread_default (GMainContext *context)
651 if (context == g_main_context_default ())
654 stack = g_static_private_get (&thread_context_stack);
656 g_return_if_fail (stack != NULL);
657 g_return_if_fail (g_queue_peek_head (stack) == context);
659 g_queue_pop_head (stack);
661 g_main_context_release (context);
663 g_main_context_unref (context);
667 * g_main_context_get_thread_default:
669 * Gets the thread-default #GMainContext for this thread. Asynchronous
670 * operations that want to be able to be run in contexts other than
671 * the default one should call this method to get a #GMainContext to
672 * add their #GSource<!-- -->s to. (Note that even in single-threaded
673 * programs applications may sometimes want to temporarily push a
674 * non-default context, so it is not safe to assume that this will
675 * always return %NULL if threads are not initialized.)
677 * Returns: the thread-default #GMainContext, or %NULL if the
678 * thread-default context is the global default context.
683 g_main_context_get_thread_default (void)
687 stack = g_static_private_get (&thread_context_stack);
689 return g_queue_peek_head (stack);
694 /* Hooks for adding to the main loop */
698 * @source_funcs: structure containing functions that implement
699 * the sources behavior.
700 * @struct_size: size of the #GSource structure to create.
702 * Creates a new #GSource structure. The size is specified to
703 * allow creating structures derived from #GSource that contain
704 * additional data. The size passed in must be at least
705 * <literal>sizeof (GSource)</literal>.
707 * The source will not initially be associated with any #GMainContext
708 * and must be added to one with g_source_attach() before it will be
711 * Return value: the newly-created #GSource.
714 g_source_new (GSourceFuncs *source_funcs,
719 g_return_val_if_fail (source_funcs != NULL, NULL);
720 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
722 source = (GSource*) g_malloc0 (struct_size);
724 source->source_funcs = source_funcs;
725 source->ref_count = 1;
727 source->priority = G_PRIORITY_DEFAULT;
729 source->flags = G_HOOK_FLAG_ACTIVE;
731 /* NULL/0 initialization for all other fields */
736 /* Holds context's lock
739 g_source_list_add (GSource *source,
740 GMainContext *context)
742 GSource *tmp_source, *last_source;
745 tmp_source = context->source_list;
746 while (tmp_source && tmp_source->priority <= source->priority)
748 last_source = tmp_source;
749 tmp_source = tmp_source->next;
752 source->next = tmp_source;
754 tmp_source->prev = source;
756 source->prev = last_source;
758 last_source->next = source;
760 context->source_list = source;
763 /* Holds context's lock
766 g_source_list_remove (GSource *source,
767 GMainContext *context)
770 source->prev->next = source->next;
772 context->source_list = source->next;
775 source->next->prev = source->prev;
783 * @source: a #GSource
784 * @context: a #GMainContext (if %NULL, the default context will be used)
786 * Adds a #GSource to a @context so that it will be executed within
787 * that context. Remove it by calling g_source_destroy().
789 * Return value: the ID (greater than 0) for the source within the
793 g_source_attach (GSource *source,
794 GMainContext *context)
799 g_return_val_if_fail (source->context == NULL, 0);
800 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
803 context = g_main_context_default ();
805 LOCK_CONTEXT (context);
807 source->context = context;
808 result = source->source_id = context->next_id++;
811 g_source_list_add (source, context);
813 tmp_list = source->poll_fds;
816 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
817 tmp_list = tmp_list->next;
820 #ifdef G_THREADS_ENABLED
821 /* Now wake up the main loop if it is waiting in the poll() */
822 g_main_context_wakeup_unlocked (context);
825 UNLOCK_CONTEXT (context);
831 g_source_destroy_internal (GSource *source,
832 GMainContext *context,
836 LOCK_CONTEXT (context);
838 if (!SOURCE_DESTROYED (source))
841 gpointer old_cb_data;
842 GSourceCallbackFuncs *old_cb_funcs;
844 source->flags &= ~G_HOOK_FLAG_ACTIVE;
846 old_cb_data = source->callback_data;
847 old_cb_funcs = source->callback_funcs;
849 source->callback_data = NULL;
850 source->callback_funcs = NULL;
854 UNLOCK_CONTEXT (context);
855 old_cb_funcs->unref (old_cb_data);
856 LOCK_CONTEXT (context);
859 if (!SOURCE_BLOCKED (source))
861 tmp_list = source->poll_fds;
864 g_main_context_remove_poll_unlocked (context, tmp_list->data);
865 tmp_list = tmp_list->next;
869 g_source_unref_internal (source, context, TRUE);
873 UNLOCK_CONTEXT (context);
878 * @source: a #GSource
880 * Removes a source from its #GMainContext, if any, and mark it as
881 * destroyed. The source cannot be subsequently added to another
885 g_source_destroy (GSource *source)
887 GMainContext *context;
889 g_return_if_fail (source != NULL);
891 context = source->context;
894 g_source_destroy_internal (source, context, FALSE);
896 source->flags &= ~G_HOOK_FLAG_ACTIVE;
901 * @source: a #GSource
903 * Returns the numeric ID for a particular source. The ID of a source
904 * is a positive integer which is unique within a particular main loop
905 * context. The reverse
906 * mapping from ID to source is done by g_main_context_find_source_by_id().
908 * Return value: the ID (greater than 0) for the source
911 g_source_get_id (GSource *source)
915 g_return_val_if_fail (source != NULL, 0);
916 g_return_val_if_fail (source->context != NULL, 0);
918 LOCK_CONTEXT (source->context);
919 result = source->source_id;
920 UNLOCK_CONTEXT (source->context);
926 * g_source_get_context:
927 * @source: a #GSource
929 * Gets the #GMainContext with which the source is associated.
930 * Calling this function on a destroyed source is an error.
932 * Return value: the #GMainContext with which the source is associated,
933 * or %NULL if the context has not yet been added
937 g_source_get_context (GSource *source)
939 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
941 return source->context;
947 * @fd: a #GPollFD structure holding information about a file
948 * descriptor to watch.
950 * Adds a file descriptor to the set of file descriptors polled for
951 * this source. This is usually combined with g_source_new() to add an
952 * event source. The event source's check function will typically test
953 * the @revents field in the #GPollFD struct and return %TRUE if events need
957 g_source_add_poll (GSource *source,
960 GMainContext *context;
962 g_return_if_fail (source != NULL);
963 g_return_if_fail (fd != NULL);
964 g_return_if_fail (!SOURCE_DESTROYED (source));
966 context = source->context;
969 LOCK_CONTEXT (context);
971 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
975 if (!SOURCE_BLOCKED (source))
976 g_main_context_add_poll_unlocked (context, source->priority, fd);
977 UNLOCK_CONTEXT (context);
982 * g_source_remove_poll:
984 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
986 * Removes a file descriptor from the set of file descriptors polled for
990 g_source_remove_poll (GSource *source,
993 GMainContext *context;
995 g_return_if_fail (source != NULL);
996 g_return_if_fail (fd != NULL);
997 g_return_if_fail (!SOURCE_DESTROYED (source));
999 context = source->context;
1002 LOCK_CONTEXT (context);
1004 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1008 if (!SOURCE_BLOCKED (source))
1009 g_main_context_remove_poll_unlocked (context, fd);
1010 UNLOCK_CONTEXT (context);
1015 * g_source_set_callback_indirect:
1016 * @source: the source
1017 * @callback_data: pointer to callback data "object"
1018 * @callback_funcs: functions for reference counting @callback_data
1019 * and getting the callback and data
1021 * Sets the callback function storing the data as a refcounted callback
1022 * "object". This is used internally. Note that calling
1023 * g_source_set_callback_indirect() assumes
1024 * an initial reference count on @callback_data, and thus
1025 * @callback_funcs->unref will eventually be called once more
1026 * than @callback_funcs->ref.
1029 g_source_set_callback_indirect (GSource *source,
1030 gpointer callback_data,
1031 GSourceCallbackFuncs *callback_funcs)
1033 GMainContext *context;
1034 gpointer old_cb_data;
1035 GSourceCallbackFuncs *old_cb_funcs;
1037 g_return_if_fail (source != NULL);
1038 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1040 context = source->context;
1043 LOCK_CONTEXT (context);
1045 old_cb_data = source->callback_data;
1046 old_cb_funcs = source->callback_funcs;
1048 source->callback_data = callback_data;
1049 source->callback_funcs = callback_funcs;
1052 UNLOCK_CONTEXT (context);
1055 old_cb_funcs->unref (old_cb_data);
1059 g_source_callback_ref (gpointer cb_data)
1061 GSourceCallback *callback = cb_data;
1063 callback->ref_count++;
1068 g_source_callback_unref (gpointer cb_data)
1070 GSourceCallback *callback = cb_data;
1072 callback->ref_count--;
1073 if (callback->ref_count == 0)
1075 if (callback->notify)
1076 callback->notify (callback->data);
1082 g_source_callback_get (gpointer cb_data,
1087 GSourceCallback *callback = cb_data;
1089 *func = callback->func;
1090 *data = callback->data;
1093 static GSourceCallbackFuncs g_source_callback_funcs = {
1094 g_source_callback_ref,
1095 g_source_callback_unref,
1096 g_source_callback_get,
1100 * g_source_set_callback:
1101 * @source: the source
1102 * @func: a callback function
1103 * @data: the data to pass to callback function
1104 * @notify: a function to call when @data is no longer in use, or %NULL.
1106 * Sets the callback function for a source. The callback for a source is
1107 * called from the source's dispatch function.
1109 * The exact type of @func depends on the type of source; ie. you
1110 * should not count on @func being called with @data as its first
1113 * Typically, you won't use this function. Instead use functions specific
1114 * to the type of source you are using.
1117 g_source_set_callback (GSource *source,
1120 GDestroyNotify notify)
1122 GSourceCallback *new_callback;
1124 g_return_if_fail (source != NULL);
1126 new_callback = g_new (GSourceCallback, 1);
1128 new_callback->ref_count = 1;
1129 new_callback->func = func;
1130 new_callback->data = data;
1131 new_callback->notify = notify;
1133 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1138 * g_source_set_funcs:
1139 * @source: a #GSource
1140 * @funcs: the new #GSourceFuncs
1142 * Sets the source functions (can be used to override
1143 * default implementations) of an unattached source.
1148 g_source_set_funcs (GSource *source,
1149 GSourceFuncs *funcs)
1151 g_return_if_fail (source != NULL);
1152 g_return_if_fail (source->context == NULL);
1153 g_return_if_fail (source->ref_count > 0);
1154 g_return_if_fail (funcs != NULL);
1156 source->source_funcs = funcs;
1160 * g_source_set_priority:
1161 * @source: a #GSource
1162 * @priority: the new priority.
1164 * Sets the priority of a source. While the main loop is being
1165 * run, a source will be dispatched if it is ready to be dispatched and no sources
1166 * at a higher (numerically smaller) priority are ready to be dispatched.
1169 g_source_set_priority (GSource *source,
1173 GMainContext *context;
1175 g_return_if_fail (source != NULL);
1177 context = source->context;
1180 LOCK_CONTEXT (context);
1182 source->priority = priority;
1186 /* Remove the source from the context's source and then
1187 * add it back so it is sorted in the correct plcae
1189 g_source_list_remove (source, source->context);
1190 g_source_list_add (source, source->context);
1192 if (!SOURCE_BLOCKED (source))
1194 tmp_list = source->poll_fds;
1197 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1198 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1200 tmp_list = tmp_list->next;
1204 UNLOCK_CONTEXT (source->context);
1209 * g_source_get_priority:
1210 * @source: a #GSource
1212 * Gets the priority of a source.
1214 * Return value: the priority of the source
1217 g_source_get_priority (GSource *source)
1219 g_return_val_if_fail (source != NULL, 0);
1221 return source->priority;
1225 * g_source_set_can_recurse:
1226 * @source: a #GSource
1227 * @can_recurse: whether recursion is allowed for this source
1229 * Sets whether a source can be called recursively. If @can_recurse is
1230 * %TRUE, then while the source is being dispatched then this source
1231 * will be processed normally. Otherwise, all processing of this
1232 * source is blocked until the dispatch function returns.
1235 g_source_set_can_recurse (GSource *source,
1236 gboolean can_recurse)
1238 GMainContext *context;
1240 g_return_if_fail (source != NULL);
1242 context = source->context;
1245 LOCK_CONTEXT (context);
1248 source->flags |= G_SOURCE_CAN_RECURSE;
1250 source->flags &= ~G_SOURCE_CAN_RECURSE;
1253 UNLOCK_CONTEXT (context);
1257 * g_source_get_can_recurse:
1258 * @source: a #GSource
1260 * Checks whether a source is allowed to be called recursively.
1261 * see g_source_set_can_recurse().
1263 * Return value: whether recursion is allowed.
1266 g_source_get_can_recurse (GSource *source)
1268 g_return_val_if_fail (source != NULL, FALSE);
1270 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1275 * g_source_set_name:
1276 * @source: a #GSource
1277 * @name: debug name for the source
1279 * Sets a name for the source, used in debugging and profiling.
1280 * The name defaults to #NULL.
1282 * The source name should describe in a human-readable way
1283 * what the source does. For example, "X11 event queue"
1284 * or "GTK+ repaint idle handler" or whatever it is.
1286 * It is permitted to call this function multiple times, but is not
1287 * recommended due to the potential performance impact. For example,
1288 * one could change the name in the "check" function of a #GSourceFuncs
1289 * to include details like the event type in the source name.
1294 g_source_set_name (GSource *source,
1297 g_return_if_fail (source != NULL);
1299 /* setting back to NULL is allowed, just because it's
1300 * weird if get_name can return NULL but you can't
1304 g_free (source->name);
1305 source->name = g_strdup (name);
1309 * g_source_get_name:
1310 * @source: a #GSource
1312 * Gets a name for the source, used in debugging and profiling.
1313 * The name may be #NULL if it has never been set with
1314 * g_source_set_name().
1316 * Return value: the name of the source
1319 G_CONST_RETURN char*
1320 g_source_get_name (GSource *source)
1322 g_return_val_if_fail (source != NULL, NULL);
1324 return source->name;
1328 * g_source_set_name_by_id:
1329 * @tag: a #GSource ID
1330 * @name: debug name for the source
1332 * Sets the name of a source using its ID.
1334 * This is a convenience utility to set source names from the return
1335 * value of g_idle_add(), g_timeout_add(), etc.
1340 g_source_set_name_by_id (guint tag,
1345 g_return_if_fail (tag > 0);
1347 source = g_main_context_find_source_by_id (NULL, tag);
1351 g_source_set_name (source, name);
1357 * @source: a #GSource
1359 * Increases the reference count on a source by one.
1361 * Return value: @source
1364 g_source_ref (GSource *source)
1366 GMainContext *context;
1368 g_return_val_if_fail (source != NULL, NULL);
1370 context = source->context;
1373 LOCK_CONTEXT (context);
1375 source->ref_count++;
1378 UNLOCK_CONTEXT (context);
1383 /* g_source_unref() but possible to call within context lock
1386 g_source_unref_internal (GSource *source,
1387 GMainContext *context,
1390 gpointer old_cb_data = NULL;
1391 GSourceCallbackFuncs *old_cb_funcs = NULL;
1393 g_return_if_fail (source != NULL);
1395 if (!have_lock && context)
1396 LOCK_CONTEXT (context);
1398 source->ref_count--;
1399 if (source->ref_count == 0)
1401 old_cb_data = source->callback_data;
1402 old_cb_funcs = source->callback_funcs;
1404 source->callback_data = NULL;
1405 source->callback_funcs = NULL;
1407 if (context && !SOURCE_DESTROYED (source))
1409 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1410 source->ref_count++;
1413 g_source_list_remove (source, context);
1415 if (source->source_funcs->finalize)
1416 source->source_funcs->finalize (source);
1418 g_free (source->name);
1419 source->name = NULL;
1421 g_slist_free (source->poll_fds);
1422 source->poll_fds = NULL;
1426 if (!have_lock && context)
1427 UNLOCK_CONTEXT (context);
1432 UNLOCK_CONTEXT (context);
1434 old_cb_funcs->unref (old_cb_data);
1437 LOCK_CONTEXT (context);
1443 * @source: a #GSource
1445 * Decreases the reference count of a source by one. If the
1446 * resulting reference count is zero the source and associated
1447 * memory will be destroyed.
1450 g_source_unref (GSource *source)
1452 g_return_if_fail (source != NULL);
1454 g_source_unref_internal (source, source->context, FALSE);
1458 * g_main_context_find_source_by_id:
1459 * @context: a #GMainContext (if %NULL, the default context will be used)
1460 * @source_id: the source ID, as returned by g_source_get_id().
1462 * Finds a #GSource given a pair of context and ID.
1464 * Return value: the #GSource if found, otherwise, %NULL
1467 g_main_context_find_source_by_id (GMainContext *context,
1472 g_return_val_if_fail (source_id > 0, NULL);
1474 if (context == NULL)
1475 context = g_main_context_default ();
1477 LOCK_CONTEXT (context);
1479 source = context->source_list;
1482 if (!SOURCE_DESTROYED (source) &&
1483 source->source_id == source_id)
1485 source = source->next;
1488 UNLOCK_CONTEXT (context);
1494 * g_main_context_find_source_by_funcs_user_data:
1495 * @context: a #GMainContext (if %NULL, the default context will be used).
1496 * @funcs: the @source_funcs passed to g_source_new().
1497 * @user_data: the user data from the callback.
1499 * Finds a source with the given source functions and user data. If
1500 * multiple sources exist with the same source function and user data,
1501 * the first one found will be returned.
1503 * Return value: the source, if one was found, otherwise %NULL
1506 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1507 GSourceFuncs *funcs,
1512 g_return_val_if_fail (funcs != NULL, NULL);
1514 if (context == NULL)
1515 context = g_main_context_default ();
1517 LOCK_CONTEXT (context);
1519 source = context->source_list;
1522 if (!SOURCE_DESTROYED (source) &&
1523 source->source_funcs == funcs &&
1524 source->callback_funcs)
1526 GSourceFunc callback;
1527 gpointer callback_data;
1529 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1531 if (callback_data == user_data)
1534 source = source->next;
1537 UNLOCK_CONTEXT (context);
1543 * g_main_context_find_source_by_user_data:
1544 * @context: a #GMainContext
1545 * @user_data: the user_data for the callback.
1547 * Finds a source with the given user data for the callback. If
1548 * multiple sources exist with the same user data, the first
1549 * one found will be returned.
1551 * Return value: the source, if one was found, otherwise %NULL
1554 g_main_context_find_source_by_user_data (GMainContext *context,
1559 if (context == NULL)
1560 context = g_main_context_default ();
1562 LOCK_CONTEXT (context);
1564 source = context->source_list;
1567 if (!SOURCE_DESTROYED (source) &&
1568 source->callback_funcs)
1570 GSourceFunc callback;
1571 gpointer callback_data = NULL;
1573 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1575 if (callback_data == user_data)
1578 source = source->next;
1581 UNLOCK_CONTEXT (context);
1588 * @tag: the ID of the source to remove.
1590 * Removes the source with the given id from the default main context.
1592 * a #GSource is given by g_source_get_id(), or will be returned by the
1593 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1594 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1595 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1597 * See also g_source_destroy(). You must use g_source_destroy() for sources
1598 * added to a non-default main context.
1600 * Return value: %TRUE if the source was found and removed.
1603 g_source_remove (guint tag)
1607 g_return_val_if_fail (tag > 0, FALSE);
1609 source = g_main_context_find_source_by_id (NULL, tag);
1611 g_source_destroy (source);
1613 return source != NULL;
1617 * g_source_remove_by_user_data:
1618 * @user_data: the user_data for the callback.
1620 * Removes a source from the default main loop context given the user
1621 * data for the callback. If multiple sources exist with the same user
1622 * data, only one will be destroyed.
1624 * Return value: %TRUE if a source was found and removed.
1627 g_source_remove_by_user_data (gpointer user_data)
1631 source = g_main_context_find_source_by_user_data (NULL, user_data);
1634 g_source_destroy (source);
1642 * g_source_remove_by_funcs_user_data:
1643 * @funcs: The @source_funcs passed to g_source_new()
1644 * @user_data: the user data for the callback
1646 * Removes a source from the default main loop context given the
1647 * source functions and user data. If multiple sources exist with the
1648 * same source functions and user data, only one will be destroyed.
1650 * Return value: %TRUE if a source was found and removed.
1653 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1658 g_return_val_if_fail (funcs != NULL, FALSE);
1660 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1663 g_source_destroy (source);
1671 * g_get_current_time:
1672 * @result: #GTimeVal structure in which to store current time.
1674 * Equivalent to the UNIX gettimeofday() function, but portable.
1677 g_get_current_time (GTimeVal *result)
1682 g_return_if_fail (result != NULL);
1684 /*this is required on alpha, there the timeval structs are int's
1685 not longs and a cast only would fail horribly*/
1686 gettimeofday (&r, NULL);
1687 result->tv_sec = r.tv_sec;
1688 result->tv_usec = r.tv_usec;
1693 g_return_if_fail (result != NULL);
1695 GetSystemTimeAsFileTime (&ft);
1696 memmove (&time64, &ft, sizeof (FILETIME));
1698 /* Convert from 100s of nanoseconds since 1601-01-01
1699 * to Unix epoch. Yes, this is Y2038 unsafe.
1701 time64 -= G_GINT64_CONSTANT (116444736000000000);
1704 result->tv_sec = time64 / 1000000;
1705 result->tv_usec = time64 % 1000000;
1710 g_main_dispatch_free (gpointer dispatch)
1712 g_slice_free (GMainDispatch, dispatch);
1715 /* Running the main loop */
1717 static GMainDispatch *
1720 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1721 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1724 dispatch = g_slice_new0 (GMainDispatch);
1725 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1734 * Returns the depth of the stack of calls to
1735 * g_main_context_dispatch() on any #GMainContext in the current thread.
1736 * That is, when called from the toplevel, it gives 0. When
1737 * called from within a callback from g_main_context_iteration()
1738 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1739 * a callback to a recursive call to g_main_context_iterate(),
1740 * it returns 2. And so forth.
1742 * This function is useful in a situation like the following:
1743 * Imagine an extremely simple "garbage collected" system.
1746 * static GList *free_list;
1749 * allocate_memory (gsize size)
1751 * gpointer result = g_malloc (size);
1752 * free_list = g_list_prepend (free_list, result);
1757 * free_allocated_memory (void)
1760 * for (l = free_list; l; l = l->next);
1762 * g_list_free (free_list);
1770 * g_main_context_iteration (NULL, TRUE);
1771 * free_allocated_memory();
1775 * This works from an application, however, if you want to do the same
1776 * thing from a library, it gets more difficult, since you no longer
1777 * control the main loop. You might think you can simply use an idle
1778 * function to make the call to free_allocated_memory(), but that
1779 * doesn't work, since the idle function could be called from a
1780 * recursive callback. This can be fixed by using g_main_depth()
1784 * allocate_memory (gsize size)
1786 * FreeListBlock *block = g_new (FreeListBlock, 1);
1787 * block->mem = g_malloc (size);
1788 * block->depth = g_main_depth ();
1789 * free_list = g_list_prepend (free_list, block);
1790 * return block->mem;
1794 * free_allocated_memory (void)
1798 * int depth = g_main_depth ();
1799 * for (l = free_list; l; );
1801 * GList *next = l->next;
1802 * FreeListBlock *block = l->data;
1803 * if (block->depth > depth)
1805 * g_free (block->mem);
1807 * free_list = g_list_delete_link (free_list, l);
1815 * There is a temptation to use g_main_depth() to solve
1816 * problems with reentrancy. For instance, while waiting for data
1817 * to be received from the network in response to a menu item,
1818 * the menu item might be selected again. It might seem that
1819 * one could make the menu item's callback return immediately
1820 * and do nothing if g_main_depth() returns a value greater than 1.
1821 * However, this should be avoided since the user then sees selecting
1822 * the menu item do nothing. Furthermore, you'll find yourself adding
1823 * these checks all over your code, since there are doubtless many,
1824 * many things that the user could do. Instead, you can use the
1825 * following techniques:
1830 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1831 * the user from interacting with elements while the main
1832 * loop is recursing.
1837 * Avoid main loop recursion in situations where you can't handle
1838 * arbitrary callbacks. Instead, structure your code so that you
1839 * simply return to the main loop and then get called again when
1840 * there is more work to do.
1845 * Return value: The main loop recursion level in the current thread
1850 GMainDispatch *dispatch = get_dispatch ();
1851 return dispatch->depth;
1855 * g_main_current_source:
1857 * Returns the currently firing source for this thread.
1859 * Return value: The currently firing source or %NULL.
1864 g_main_current_source (void)
1866 GMainDispatch *dispatch = get_dispatch ();
1867 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
1871 * g_source_is_destroyed:
1872 * @source: a #GSource
1874 * Returns whether @source has been destroyed.
1876 * This is important when you operate upon your objects
1877 * from within idle handlers, but may have freed the object
1878 * before the dispatch of your idle handler.
1882 * idle_callback (gpointer data)
1884 * SomeWidget *self = data;
1886 * GDK_THREADS_ENTER (<!-- -->);
1887 * /<!-- -->* do stuff with self *<!-- -->/
1888 * self->idle_id = 0;
1889 * GDK_THREADS_LEAVE (<!-- -->);
1895 * some_widget_do_stuff_later (SomeWidget *self)
1897 * self->idle_id = g_idle_add (idle_callback, self);
1901 * some_widget_finalize (GObject *object)
1903 * SomeWidget *self = SOME_WIDGET (object);
1905 * if (self->idle_id)
1906 * g_source_remove (self->idle_id);
1908 * G_OBJECT_CLASS (parent_class)->finalize (object);
1912 * This will fail in a multi-threaded application if the
1913 * widget is destroyed before the idle handler fires due
1914 * to the use after free in the callback. A solution, to
1915 * this particular problem, is to check to if the source
1916 * has already been destroy within the callback.
1920 * idle_callback (gpointer data)
1922 * SomeWidget *self = data;
1924 * GDK_THREADS_ENTER ();
1925 * if (!g_source_is_destroyed (g_main_current_source ()))
1927 * /<!-- -->* do stuff with self *<!-- -->/
1929 * GDK_THREADS_LEAVE ();
1935 * Return value: %TRUE if the source has been destroyed
1940 g_source_is_destroyed (GSource *source)
1942 return SOURCE_DESTROYED (source);
1945 /* Temporarily remove all this source's file descriptors from the
1946 * poll(), so that if data comes available for one of the file descriptors
1947 * we don't continually spin in the poll()
1949 /* HOLDS: source->context's lock */
1951 block_source (GSource *source)
1955 g_return_if_fail (!SOURCE_BLOCKED (source));
1957 tmp_list = source->poll_fds;
1960 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1961 tmp_list = tmp_list->next;
1965 /* HOLDS: source->context's lock */
1967 unblock_source (GSource *source)
1971 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1972 g_return_if_fail (!SOURCE_DESTROYED (source));
1974 tmp_list = source->poll_fds;
1977 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1978 tmp_list = tmp_list->next;
1982 /* HOLDS: context's lock */
1984 g_main_dispatch (GMainContext *context)
1986 GMainDispatch *current = get_dispatch ();
1989 for (i = 0; i < context->pending_dispatches->len; i++)
1991 GSource *source = context->pending_dispatches->pdata[i];
1993 context->pending_dispatches->pdata[i] = NULL;
1996 source->flags &= ~G_SOURCE_READY;
1998 if (!SOURCE_DESTROYED (source))
2000 gboolean was_in_call;
2001 gpointer user_data = NULL;
2002 GSourceFunc callback = NULL;
2003 GSourceCallbackFuncs *cb_funcs;
2005 gboolean need_destroy;
2007 gboolean (*dispatch) (GSource *,
2010 GSList current_source_link;
2012 dispatch = source->source_funcs->dispatch;
2013 cb_funcs = source->callback_funcs;
2014 cb_data = source->callback_data;
2017 cb_funcs->ref (cb_data);
2019 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2020 block_source (source);
2022 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2023 source->flags |= G_HOOK_FLAG_IN_CALL;
2026 cb_funcs->get (cb_data, source, &callback, &user_data);
2028 UNLOCK_CONTEXT (context);
2031 /* The on-stack allocation of the GSList is unconventional, but
2032 * we know that the lifetime of the link is bounded to this
2033 * function as the link is kept in a thread specific list and
2034 * not manipulated outside of this function and its descendants.
2035 * Avoiding the overhead of a g_slist_alloc() is useful as many
2036 * applications do little more than dispatch events.
2038 * This is a performance hack - do not revert to g_slist_prepend()!
2040 current_source_link.data = source;
2041 current_source_link.next = current->dispatching_sources;
2042 current->dispatching_sources = ¤t_source_link;
2043 need_destroy = ! dispatch (source,
2046 g_assert (current->dispatching_sources == ¤t_source_link);
2047 current->dispatching_sources = current_source_link.next;
2051 cb_funcs->unref (cb_data);
2053 LOCK_CONTEXT (context);
2056 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2058 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2059 !SOURCE_DESTROYED (source))
2060 unblock_source (source);
2062 /* Note: this depends on the fact that we can't switch
2063 * sources from one main context to another
2065 if (need_destroy && !SOURCE_DESTROYED (source))
2067 g_assert (source->context == context);
2068 g_source_destroy_internal (source, context, TRUE);
2072 SOURCE_UNREF (source, context);
2075 g_ptr_array_set_size (context->pending_dispatches, 0);
2078 /* Holds context's lock */
2079 static inline GSource *
2080 next_valid_source (GMainContext *context,
2083 GSource *new_source = source ? source->next : context->source_list;
2087 if (!SOURCE_DESTROYED (new_source))
2089 new_source->ref_count++;
2093 new_source = new_source->next;
2097 SOURCE_UNREF (source, context);
2103 * g_main_context_acquire:
2104 * @context: a #GMainContext
2106 * Tries to become the owner of the specified context.
2107 * If some other thread is the owner of the context,
2108 * returns %FALSE immediately. Ownership is properly
2109 * recursive: the owner can require ownership again
2110 * and will release ownership when g_main_context_release()
2111 * is called as many times as g_main_context_acquire().
2113 * You must be the owner of a context before you
2114 * can call g_main_context_prepare(), g_main_context_query(),
2115 * g_main_context_check(), g_main_context_dispatch().
2117 * Return value: %TRUE if the operation succeeded, and
2118 * this thread is now the owner of @context.
2121 g_main_context_acquire (GMainContext *context)
2123 #ifdef G_THREADS_ENABLED
2124 gboolean result = FALSE;
2125 GThread *self = G_THREAD_SELF;
2127 if (context == NULL)
2128 context = g_main_context_default ();
2130 LOCK_CONTEXT (context);
2132 if (!context->owner)
2134 context->owner = self;
2135 g_assert (context->owner_count == 0);
2138 if (context->owner == self)
2140 context->owner_count++;
2144 UNLOCK_CONTEXT (context);
2147 #else /* !G_THREADS_ENABLED */
2149 #endif /* G_THREADS_ENABLED */
2153 * g_main_context_release:
2154 * @context: a #GMainContext
2156 * Releases ownership of a context previously acquired by this thread
2157 * with g_main_context_acquire(). If the context was acquired multiple
2158 * times, the ownership will be released only when g_main_context_release()
2159 * is called as many times as it was acquired.
2162 g_main_context_release (GMainContext *context)
2164 #ifdef G_THREADS_ENABLED
2165 if (context == NULL)
2166 context = g_main_context_default ();
2168 LOCK_CONTEXT (context);
2170 context->owner_count--;
2171 if (context->owner_count == 0)
2173 context->owner = NULL;
2175 if (context->waiters)
2177 GMainWaiter *waiter = context->waiters->data;
2178 gboolean loop_internal_waiter =
2179 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2180 context->waiters = g_slist_delete_link (context->waiters,
2182 if (!loop_internal_waiter)
2183 g_mutex_lock (waiter->mutex);
2185 g_cond_signal (waiter->cond);
2187 if (!loop_internal_waiter)
2188 g_mutex_unlock (waiter->mutex);
2192 UNLOCK_CONTEXT (context);
2193 #endif /* G_THREADS_ENABLED */
2197 * g_main_context_wait:
2198 * @context: a #GMainContext
2199 * @cond: a condition variable
2200 * @mutex: a mutex, currently held
2202 * Tries to become the owner of the specified context,
2203 * as with g_main_context_acquire(). But if another thread
2204 * is the owner, atomically drop @mutex and wait on @cond until
2205 * that owner releases ownership or until @cond is signaled, then
2206 * try again (once) to become the owner.
2208 * Return value: %TRUE if the operation succeeded, and
2209 * this thread is now the owner of @context.
2212 g_main_context_wait (GMainContext *context,
2216 #ifdef G_THREADS_ENABLED
2217 gboolean result = FALSE;
2218 GThread *self = G_THREAD_SELF;
2219 gboolean loop_internal_waiter;
2221 if (context == NULL)
2222 context = g_main_context_default ();
2224 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2226 if (!loop_internal_waiter)
2227 LOCK_CONTEXT (context);
2229 if (context->owner && context->owner != self)
2234 waiter.mutex = mutex;
2236 context->waiters = g_slist_append (context->waiters, &waiter);
2238 if (!loop_internal_waiter)
2239 UNLOCK_CONTEXT (context);
2240 g_cond_wait (cond, mutex);
2241 if (!loop_internal_waiter)
2242 LOCK_CONTEXT (context);
2244 context->waiters = g_slist_remove (context->waiters, &waiter);
2247 if (!context->owner)
2249 context->owner = self;
2250 g_assert (context->owner_count == 0);
2253 if (context->owner == self)
2255 context->owner_count++;
2259 if (!loop_internal_waiter)
2260 UNLOCK_CONTEXT (context);
2263 #else /* !G_THREADS_ENABLED */
2265 #endif /* G_THREADS_ENABLED */
2269 * g_main_context_prepare:
2270 * @context: a #GMainContext
2271 * @priority: location to store priority of highest priority
2272 * source already ready.
2274 * Prepares to poll sources within a main loop. The resulting information
2275 * for polling is determined by calling g_main_context_query ().
2277 * Return value: %TRUE if some source is ready to be dispatched
2281 g_main_context_prepare (GMainContext *context,
2286 gint current_priority = G_MAXINT;
2289 if (context == NULL)
2290 context = g_main_context_default ();
2292 LOCK_CONTEXT (context);
2294 context->time_is_current = FALSE;
2296 if (context->in_check_or_prepare)
2298 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2299 "prepare() member.");
2300 UNLOCK_CONTEXT (context);
2304 #ifdef G_THREADS_ENABLED
2305 if (context->poll_waiting)
2307 g_warning("g_main_context_prepare(): main loop already active in another thread");
2308 UNLOCK_CONTEXT (context);
2312 context->poll_waiting = TRUE;
2313 #endif /* G_THREADS_ENABLED */
2316 /* If recursing, finish up current dispatch, before starting over */
2317 if (context->pending_dispatches)
2320 g_main_dispatch (context, ¤t_time);
2322 UNLOCK_CONTEXT (context);
2327 /* If recursing, clear list of pending dispatches */
2329 for (i = 0; i < context->pending_dispatches->len; i++)
2331 if (context->pending_dispatches->pdata[i])
2332 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2334 g_ptr_array_set_size (context->pending_dispatches, 0);
2336 /* Prepare all sources */
2338 context->timeout = -1;
2340 source = next_valid_source (context, NULL);
2343 gint source_timeout = -1;
2345 if ((n_ready > 0) && (source->priority > current_priority))
2347 SOURCE_UNREF (source, context);
2350 if (SOURCE_BLOCKED (source))
2353 if (!(source->flags & G_SOURCE_READY))
2356 gboolean (*prepare) (GSource *source,
2359 prepare = source->source_funcs->prepare;
2360 context->in_check_or_prepare++;
2361 UNLOCK_CONTEXT (context);
2363 result = (*prepare) (source, &source_timeout);
2365 LOCK_CONTEXT (context);
2366 context->in_check_or_prepare--;
2369 source->flags |= G_SOURCE_READY;
2372 if (source->flags & G_SOURCE_READY)
2375 current_priority = source->priority;
2376 context->timeout = 0;
2379 if (source_timeout >= 0)
2381 if (context->timeout < 0)
2382 context->timeout = source_timeout;
2384 context->timeout = MIN (context->timeout, source_timeout);
2388 source = next_valid_source (context, source);
2391 UNLOCK_CONTEXT (context);
2394 *priority = current_priority;
2396 return (n_ready > 0);
2400 * g_main_context_query:
2401 * @context: a #GMainContext
2402 * @max_priority: maximum priority source to check
2403 * @timeout_: location to store timeout to be used in polling
2404 * @fds: location to store #GPollFD records that need to be polled.
2405 * @n_fds: length of @fds.
2407 * Determines information necessary to poll this main loop.
2409 * Return value: the number of records actually stored in @fds,
2410 * or, if more than @n_fds records need to be stored, the number
2411 * of records that need to be stored.
2414 g_main_context_query (GMainContext *context,
2423 LOCK_CONTEXT (context);
2425 pollrec = context->poll_records;
2427 while (pollrec && max_priority >= pollrec->priority)
2429 /* We need to include entries with fd->events == 0 in the array because
2430 * otherwise if the application changes fd->events behind our back and
2431 * makes it non-zero, we'll be out of sync when we check the fds[] array.
2432 * (Changing fd->events after adding an FD wasn't an anticipated use of
2433 * this API, but it occurs in practice.) */
2436 fds[n_poll].fd = pollrec->fd->fd;
2437 /* In direct contradiction to the Unix98 spec, IRIX runs into
2438 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2439 * flags in the events field of the pollfd while it should
2440 * just ignoring them. So we mask them out here.
2442 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2443 fds[n_poll].revents = 0;
2446 pollrec = pollrec->next;
2450 #ifdef G_THREADS_ENABLED
2451 context->poll_changed = FALSE;
2456 *timeout = context->timeout;
2458 context->time_is_current = FALSE;
2461 UNLOCK_CONTEXT (context);
2467 * g_main_context_check:
2468 * @context: a #GMainContext
2469 * @max_priority: the maximum numerical priority of sources to check
2470 * @fds: array of #GPollFD's that was passed to the last call to
2471 * g_main_context_query()
2472 * @n_fds: return value of g_main_context_query()
2474 * Passes the results of polling back to the main loop.
2476 * Return value: %TRUE if some sources are ready to be dispatched.
2479 g_main_context_check (GMainContext *context,
2489 LOCK_CONTEXT (context);
2491 if (context->in_check_or_prepare)
2493 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2494 "prepare() member.");
2495 UNLOCK_CONTEXT (context);
2499 #ifdef G_THREADS_ENABLED
2500 if (!context->poll_waiting)
2504 read (context->wake_up_pipe[0], &a, 1);
2508 context->poll_waiting = FALSE;
2510 /* If the set of poll file descriptors changed, bail out
2511 * and let the main loop rerun
2513 if (context->poll_changed)
2515 UNLOCK_CONTEXT (context);
2518 #endif /* G_THREADS_ENABLED */
2520 pollrec = context->poll_records;
2524 if (pollrec->fd->events)
2525 pollrec->fd->revents = fds[i].revents;
2527 pollrec = pollrec->next;
2531 source = next_valid_source (context, NULL);
2534 if ((n_ready > 0) && (source->priority > max_priority))
2536 SOURCE_UNREF (source, context);
2539 if (SOURCE_BLOCKED (source))
2542 if (!(source->flags & G_SOURCE_READY))
2545 gboolean (*check) (GSource *source);
2547 check = source->source_funcs->check;
2549 context->in_check_or_prepare++;
2550 UNLOCK_CONTEXT (context);
2552 result = (*check) (source);
2554 LOCK_CONTEXT (context);
2555 context->in_check_or_prepare--;
2558 source->flags |= G_SOURCE_READY;
2561 if (source->flags & G_SOURCE_READY)
2563 source->ref_count++;
2564 g_ptr_array_add (context->pending_dispatches, source);
2568 /* never dispatch sources with less priority than the first
2569 * one we choose to dispatch
2571 max_priority = source->priority;
2575 source = next_valid_source (context, source);
2578 UNLOCK_CONTEXT (context);
2584 * g_main_context_dispatch:
2585 * @context: a #GMainContext
2587 * Dispatches all pending sources.
2590 g_main_context_dispatch (GMainContext *context)
2592 LOCK_CONTEXT (context);
2594 if (context->pending_dispatches->len > 0)
2596 g_main_dispatch (context);
2599 UNLOCK_CONTEXT (context);
2602 /* HOLDS context lock */
2604 g_main_context_iterate (GMainContext *context,
2611 gboolean some_ready;
2612 gint nfds, allocated_nfds;
2613 GPollFD *fds = NULL;
2615 UNLOCK_CONTEXT (context);
2617 #ifdef G_THREADS_ENABLED
2618 if (!g_main_context_acquire (context))
2620 gboolean got_ownership;
2622 LOCK_CONTEXT (context);
2624 g_return_val_if_fail (g_thread_supported (), FALSE);
2630 context->cond = g_cond_new ();
2632 got_ownership = g_main_context_wait (context,
2634 g_static_mutex_get_mutex (&context->mutex));
2640 LOCK_CONTEXT (context);
2641 #endif /* G_THREADS_ENABLED */
2643 if (!context->cached_poll_array)
2645 context->cached_poll_array_size = context->n_poll_records;
2646 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2649 allocated_nfds = context->cached_poll_array_size;
2650 fds = context->cached_poll_array;
2652 UNLOCK_CONTEXT (context);
2654 g_main_context_prepare (context, &max_priority);
2656 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2657 allocated_nfds)) > allocated_nfds)
2659 LOCK_CONTEXT (context);
2661 context->cached_poll_array_size = allocated_nfds = nfds;
2662 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2663 UNLOCK_CONTEXT (context);
2669 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2671 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2674 g_main_context_dispatch (context);
2676 #ifdef G_THREADS_ENABLED
2677 g_main_context_release (context);
2678 #endif /* G_THREADS_ENABLED */
2680 LOCK_CONTEXT (context);
2686 * g_main_context_pending:
2687 * @context: a #GMainContext (if %NULL, the default context will be used)
2689 * Checks if any sources have pending events for the given context.
2691 * Return value: %TRUE if events are pending.
2694 g_main_context_pending (GMainContext *context)
2699 context = g_main_context_default();
2701 LOCK_CONTEXT (context);
2702 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2703 UNLOCK_CONTEXT (context);
2709 * g_main_context_iteration:
2710 * @context: a #GMainContext (if %NULL, the default context will be used)
2711 * @may_block: whether the call may block.
2713 * Runs a single iteration for the given main loop. This involves
2714 * checking to see if any event sources are ready to be processed,
2715 * then if no events sources are ready and @may_block is %TRUE, waiting
2716 * for a source to become ready, then dispatching the highest priority
2717 * events sources that are ready. Otherwise, if @may_block is %FALSE
2718 * sources are not waited to become ready, only those highest priority
2719 * events sources will be dispatched (if any), that are ready at this
2720 * given moment without further waiting.
2722 * Note that even when @may_block is %TRUE, it is still possible for
2723 * g_main_context_iteration() to return %FALSE, since the the wait may
2724 * be interrupted for other reasons than an event source becoming ready.
2726 * Return value: %TRUE if events were dispatched.
2729 g_main_context_iteration (GMainContext *context, gboolean may_block)
2734 context = g_main_context_default();
2736 LOCK_CONTEXT (context);
2737 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2738 UNLOCK_CONTEXT (context);
2745 * @context: a #GMainContext (if %NULL, the default context will be used).
2746 * @is_running: set to %TRUE to indicate that the loop is running. This
2747 * is not very important since calling g_main_loop_run() will set this to
2750 * Creates a new #GMainLoop structure.
2752 * Return value: a new #GMainLoop.
2755 g_main_loop_new (GMainContext *context,
2756 gboolean is_running)
2761 context = g_main_context_default();
2763 g_main_context_ref (context);
2765 loop = g_new0 (GMainLoop, 1);
2766 loop->context = context;
2767 loop->is_running = is_running != FALSE;
2768 loop->ref_count = 1;
2775 * @loop: a #GMainLoop
2777 * Increases the reference count on a #GMainLoop object by one.
2779 * Return value: @loop
2782 g_main_loop_ref (GMainLoop *loop)
2784 g_return_val_if_fail (loop != NULL, NULL);
2785 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2787 g_atomic_int_inc (&loop->ref_count);
2793 * g_main_loop_unref:
2794 * @loop: a #GMainLoop
2796 * Decreases the reference count on a #GMainLoop object by one. If
2797 * the result is zero, free the loop and free all associated memory.
2800 g_main_loop_unref (GMainLoop *loop)
2802 g_return_if_fail (loop != NULL);
2803 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2805 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2808 g_main_context_unref (loop->context);
2814 * @loop: a #GMainLoop
2816 * Runs a main loop until g_main_loop_quit() is called on the loop.
2817 * If this is called for the thread of the loop's #GMainContext,
2818 * it will process events from the loop, otherwise it will
2822 g_main_loop_run (GMainLoop *loop)
2824 GThread *self = G_THREAD_SELF;
2826 g_return_if_fail (loop != NULL);
2827 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2829 #ifdef G_THREADS_ENABLED
2830 if (!g_main_context_acquire (loop->context))
2832 gboolean got_ownership = FALSE;
2834 /* Another thread owns this context */
2835 if (!g_thread_supported ())
2837 g_warning ("g_main_loop_run() was called from second thread but "
2838 "g_thread_init() was never called.");
2842 LOCK_CONTEXT (loop->context);
2844 g_atomic_int_inc (&loop->ref_count);
2846 if (!loop->is_running)
2847 loop->is_running = TRUE;
2849 if (!loop->context->cond)
2850 loop->context->cond = g_cond_new ();
2852 while (loop->is_running && !got_ownership)
2853 got_ownership = g_main_context_wait (loop->context,
2854 loop->context->cond,
2855 g_static_mutex_get_mutex (&loop->context->mutex));
2857 if (!loop->is_running)
2859 UNLOCK_CONTEXT (loop->context);
2861 g_main_context_release (loop->context);
2862 g_main_loop_unref (loop);
2866 g_assert (got_ownership);
2869 LOCK_CONTEXT (loop->context);
2870 #endif /* G_THREADS_ENABLED */
2872 if (loop->context->in_check_or_prepare)
2874 g_warning ("g_main_loop_run(): called recursively from within a source's "
2875 "check() or prepare() member, iteration not possible.");
2879 g_atomic_int_inc (&loop->ref_count);
2880 loop->is_running = TRUE;
2881 while (loop->is_running)
2882 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2884 UNLOCK_CONTEXT (loop->context);
2886 #ifdef G_THREADS_ENABLED
2887 g_main_context_release (loop->context);
2888 #endif /* G_THREADS_ENABLED */
2890 g_main_loop_unref (loop);
2895 * @loop: a #GMainLoop
2897 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2898 * for the loop will return.
2900 * Note that sources that have already been dispatched when
2901 * g_main_loop_quit() is called will still be executed.
2904 g_main_loop_quit (GMainLoop *loop)
2906 g_return_if_fail (loop != NULL);
2907 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2909 LOCK_CONTEXT (loop->context);
2910 loop->is_running = FALSE;
2911 g_main_context_wakeup_unlocked (loop->context);
2913 #ifdef G_THREADS_ENABLED
2914 if (loop->context->cond)
2915 g_cond_broadcast (loop->context->cond);
2916 #endif /* G_THREADS_ENABLED */
2918 UNLOCK_CONTEXT (loop->context);
2922 * g_main_loop_is_running:
2923 * @loop: a #GMainLoop.
2925 * Checks to see if the main loop is currently being run via g_main_loop_run().
2927 * Return value: %TRUE if the mainloop is currently being run.
2930 g_main_loop_is_running (GMainLoop *loop)
2932 g_return_val_if_fail (loop != NULL, FALSE);
2933 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2935 return loop->is_running;
2939 * g_main_loop_get_context:
2940 * @loop: a #GMainLoop.
2942 * Returns the #GMainContext of @loop.
2944 * Return value: the #GMainContext of @loop
2947 g_main_loop_get_context (GMainLoop *loop)
2949 g_return_val_if_fail (loop != NULL, NULL);
2950 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2952 return loop->context;
2955 /* HOLDS: context's lock */
2957 g_main_context_poll (GMainContext *context,
2963 #ifdef G_MAIN_POLL_DEBUG
2969 GPollFunc poll_func;
2971 if (n_fds || timeout != 0)
2973 #ifdef G_MAIN_POLL_DEBUG
2974 if (_g_main_poll_debug)
2976 g_print ("polling context=%p n=%d timeout=%d\n",
2977 context, n_fds, timeout);
2978 poll_timer = g_timer_new ();
2982 LOCK_CONTEXT (context);
2984 poll_func = context->poll_func;
2986 UNLOCK_CONTEXT (context);
2987 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2990 g_warning ("poll(2) failed due to: %s.",
2991 g_strerror (errno));
2993 /* If g_poll () returns -1, it has already called g_warning() */
2997 #ifdef G_MAIN_POLL_DEBUG
2998 if (_g_main_poll_debug)
3000 LOCK_CONTEXT (context);
3002 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3005 g_timer_elapsed (poll_timer, NULL));
3006 g_timer_destroy (poll_timer);
3007 pollrec = context->poll_records;
3009 while (pollrec != NULL)
3014 if (fds[i].fd == pollrec->fd->fd &&
3015 pollrec->fd->events &&
3018 g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd);
3019 if (fds[i].revents & G_IO_IN)
3021 if (fds[i].revents & G_IO_OUT)
3023 if (fds[i].revents & G_IO_PRI)
3025 if (fds[i].revents & G_IO_ERR)
3027 if (fds[i].revents & G_IO_HUP)
3029 if (fds[i].revents & G_IO_NVAL)
3035 pollrec = pollrec->next;
3039 UNLOCK_CONTEXT (context);
3042 } /* if (n_fds || timeout != 0) */
3046 * g_main_context_add_poll:
3047 * @context: a #GMainContext (or %NULL for the default context)
3048 * @fd: a #GPollFD structure holding information about a file
3049 * descriptor to watch.
3050 * @priority: the priority for this file descriptor which should be
3051 * the same as the priority used for g_source_attach() to ensure that the
3052 * file descriptor is polled whenever the results may be needed.
3054 * Adds a file descriptor to the set of file descriptors polled for
3055 * this context. This will very seldomly be used directly. Instead
3056 * a typical event source will use g_source_add_poll() instead.
3059 g_main_context_add_poll (GMainContext *context,
3064 context = g_main_context_default ();
3066 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3067 g_return_if_fail (fd);
3069 LOCK_CONTEXT (context);
3070 g_main_context_add_poll_unlocked (context, priority, fd);
3071 UNLOCK_CONTEXT (context);
3074 /* HOLDS: main_loop_lock */
3076 g_main_context_add_poll_unlocked (GMainContext *context,
3080 GPollRec *lastrec, *pollrec;
3081 GPollRec *newrec = g_slice_new (GPollRec);
3083 /* This file descriptor may be checked before we ever poll */
3086 newrec->priority = priority;
3089 pollrec = context->poll_records;
3090 while (pollrec && priority >= pollrec->priority)
3093 pollrec = pollrec->next;
3097 lastrec->next = newrec;
3099 context->poll_records = newrec;
3101 newrec->next = pollrec;
3103 context->n_poll_records++;
3105 #ifdef G_THREADS_ENABLED
3106 context->poll_changed = TRUE;
3108 /* Now wake up the main loop if it is waiting in the poll() */
3109 g_main_context_wakeup_unlocked (context);
3114 * g_main_context_remove_poll:
3115 * @context:a #GMainContext
3116 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3118 * Removes file descriptor from the set of file descriptors to be
3119 * polled for a particular context.
3122 g_main_context_remove_poll (GMainContext *context,
3126 context = g_main_context_default ();
3128 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3129 g_return_if_fail (fd);
3131 LOCK_CONTEXT (context);
3132 g_main_context_remove_poll_unlocked (context, fd);
3133 UNLOCK_CONTEXT (context);
3137 g_main_context_remove_poll_unlocked (GMainContext *context,
3140 GPollRec *pollrec, *lastrec;
3143 pollrec = context->poll_records;
3147 if (pollrec->fd == fd)
3149 if (lastrec != NULL)
3150 lastrec->next = pollrec->next;
3152 context->poll_records = pollrec->next;
3154 g_slice_free (GPollRec, pollrec);
3156 context->n_poll_records--;
3160 pollrec = pollrec->next;
3163 #ifdef G_THREADS_ENABLED
3164 context->poll_changed = TRUE;
3166 /* Now wake up the main loop if it is waiting in the poll() */
3167 g_main_context_wakeup_unlocked (context);
3172 * g_source_get_current_time:
3173 * @source: a #GSource
3174 * @timeval: #GTimeVal structure in which to store current time.
3176 * Gets the "current time" to be used when checking
3177 * this source. The advantage of calling this function over
3178 * calling g_get_current_time() directly is that when
3179 * checking multiple sources, GLib can cache a single value
3180 * instead of having to repeatedly get the system time.
3183 g_source_get_current_time (GSource *source,
3186 GMainContext *context;
3188 g_return_if_fail (source->context != NULL);
3190 context = source->context;
3192 LOCK_CONTEXT (context);
3194 if (!context->time_is_current)
3196 g_get_current_time (&context->current_time);
3197 context->time_is_current = TRUE;
3200 *timeval = context->current_time;
3202 UNLOCK_CONTEXT (context);
3206 * g_main_context_set_poll_func:
3207 * @context: a #GMainContext
3208 * @func: the function to call to poll all file descriptors
3210 * Sets the function to use to handle polling of file descriptors. It
3211 * will be used instead of the poll() system call
3212 * (or GLib's replacement function, which is used where
3213 * poll() isn't available).
3215 * This function could possibly be used to integrate the GLib event
3216 * loop with an external event loop.
3219 g_main_context_set_poll_func (GMainContext *context,
3223 context = g_main_context_default ();
3225 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3227 LOCK_CONTEXT (context);
3230 context->poll_func = func;
3232 context->poll_func = g_poll;
3234 UNLOCK_CONTEXT (context);
3238 * g_main_context_get_poll_func:
3239 * @context: a #GMainContext
3241 * Gets the poll function set by g_main_context_set_poll_func().
3243 * Return value: the poll function
3246 g_main_context_get_poll_func (GMainContext *context)
3251 context = g_main_context_default ();
3253 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3255 LOCK_CONTEXT (context);
3256 result = context->poll_func;
3257 UNLOCK_CONTEXT (context);
3262 /* HOLDS: context's lock */
3263 /* Wake the main loop up from a poll() */
3265 g_main_context_wakeup_unlocked (GMainContext *context)
3267 #ifdef G_THREADS_ENABLED
3268 if (g_thread_supported() && context->poll_waiting)
3270 context->poll_waiting = FALSE;
3272 write (context->wake_up_pipe[1], "A", 1);
3274 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3281 * g_main_context_wakeup:
3282 * @context: a #GMainContext
3284 * If @context is currently waiting in a poll(), interrupt
3285 * the poll(), and continue the iteration process.
3288 g_main_context_wakeup (GMainContext *context)
3291 context = g_main_context_default ();
3293 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3295 LOCK_CONTEXT (context);
3296 g_main_context_wakeup_unlocked (context);
3297 UNLOCK_CONTEXT (context);
3301 * g_main_context_is_owner:
3302 * @context: a #GMainContext
3304 * Determines whether this thread holds the (recursive)
3305 * ownership of this #GMaincontext. This is useful to
3306 * know before waiting on another thread that may be
3307 * blocking to get ownership of @context.
3309 * Returns: %TRUE if current thread is owner of @context.
3314 g_main_context_is_owner (GMainContext *context)
3319 context = g_main_context_default ();
3321 #ifdef G_THREADS_ENABLED
3322 LOCK_CONTEXT (context);
3323 is_owner = context->owner == G_THREAD_SELF;
3324 UNLOCK_CONTEXT (context);
3335 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3336 GTimeVal *current_time)
3338 guint seconds = timeout_source->interval / 1000;
3339 guint msecs = timeout_source->interval - seconds * 1000;
3341 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3342 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3343 if (timeout_source->expiration.tv_usec >= 1000000)
3345 timeout_source->expiration.tv_usec -= 1000000;
3346 timeout_source->expiration.tv_sec++;
3348 if (timer_perturb==-1)
3351 * we want a per machine/session unique 'random' value; try the dbus
3352 * address first, that has a UUID in it. If there is no dbus, use the
3353 * hostname for hashing.
3355 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3356 if (!session_bus_address)
3357 session_bus_address = g_getenv ("HOSTNAME");
3358 if (session_bus_address)
3359 timer_perturb = ABS ((gint) g_str_hash (session_bus_address));
3363 if (timeout_source->granularity)
3366 gint gran; /* in usecs */
3369 gran = timeout_source->granularity * 1000;
3370 perturb = timer_perturb % gran;
3372 * We want to give each machine a per machine pertubation;
3373 * shift time back first, and forward later after the rounding
3376 timeout_source->expiration.tv_usec -= perturb;
3377 if (timeout_source->expiration.tv_usec < 0)
3379 timeout_source->expiration.tv_usec += 1000000;
3380 timeout_source->expiration.tv_sec--;
3383 remainder = timeout_source->expiration.tv_usec % gran;
3384 if (remainder >= gran/4) /* round up */
3385 timeout_source->expiration.tv_usec += gran;
3386 timeout_source->expiration.tv_usec -= remainder;
3388 timeout_source->expiration.tv_usec += perturb;
3390 /* the rounding may have overflown tv_usec */
3391 while (timeout_source->expiration.tv_usec > 1000000)
3393 timeout_source->expiration.tv_usec -= 1000000;
3394 timeout_source->expiration.tv_sec++;
3400 g_timeout_prepare (GSource *source,
3405 GTimeVal current_time;
3407 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3409 g_source_get_current_time (source, ¤t_time);
3411 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3412 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3414 /* We do the following in a rather convoluted fashion to deal with
3415 * the fact that we don't have an integral type big enough to hold
3416 * the difference of two timevals in millseconds.
3418 if (sec < 0 || (sec == 0 && msec < 0))
3422 glong interval_sec = timeout_source->interval / 1000;
3423 glong interval_msec = timeout_source->interval % 1000;
3431 if (sec > interval_sec ||
3432 (sec == interval_sec && msec > interval_msec))
3434 /* The system time has been set backwards, so we
3435 * reset the expiration time to now + timeout_source->interval;
3436 * this at least avoids hanging for long periods of time.
3438 g_timeout_set_expiration (timeout_source, ¤t_time);
3439 msec = MIN (G_MAXINT, timeout_source->interval);
3443 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3447 *timeout = (gint)msec;
3453 g_timeout_check (GSource *source)
3455 GTimeVal current_time;
3456 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3458 g_source_get_current_time (source, ¤t_time);
3460 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3461 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3462 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3466 g_timeout_dispatch (GSource *source,
3467 GSourceFunc callback,
3470 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3474 g_warning ("Timeout source dispatched without callback\n"
3475 "You must call g_source_set_callback().");
3479 if (callback (user_data))
3481 GTimeVal current_time;
3483 g_source_get_current_time (source, ¤t_time);
3484 g_timeout_set_expiration (timeout_source, ¤t_time);
3493 * g_timeout_source_new:
3494 * @interval: the timeout interval in milliseconds.
3496 * Creates a new timeout source.
3498 * The source will not initially be associated with any #GMainContext
3499 * and must be added to one with g_source_attach() before it will be
3502 * Return value: the newly-created timeout source
3505 g_timeout_source_new (guint interval)
3507 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3508 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3509 GTimeVal current_time;
3511 timeout_source->interval = interval;
3513 g_get_current_time (¤t_time);
3514 g_timeout_set_expiration (timeout_source, ¤t_time);
3520 * g_timeout_source_new_seconds:
3521 * @interval: the timeout interval in seconds
3523 * Creates a new timeout source.
3525 * The source will not initially be associated with any #GMainContext
3526 * and must be added to one with g_source_attach() before it will be
3529 * The scheduling granularity/accuracy of this timeout source will be
3532 * Return value: the newly-created timeout source
3537 g_timeout_source_new_seconds (guint interval)
3539 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3540 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3541 GTimeVal current_time;
3543 timeout_source->interval = 1000*interval;
3544 timeout_source->granularity = 1000;
3546 g_get_current_time (¤t_time);
3547 g_timeout_set_expiration (timeout_source, ¤t_time);
3554 * g_timeout_add_full:
3555 * @priority: the priority of the timeout source. Typically this will be in
3556 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3557 * @interval: the time between calls to the function, in milliseconds
3558 * (1/1000ths of a second)
3559 * @function: function to call
3560 * @data: data to pass to @function
3561 * @notify: function to call when the timeout is removed, or %NULL
3563 * Sets a function to be called at regular intervals, with the given
3564 * priority. The function is called repeatedly until it returns
3565 * %FALSE, at which point the timeout is automatically destroyed and
3566 * the function will not be called again. The @notify function is
3567 * called when the timeout is destroyed. The first call to the
3568 * function will be at the end of the first @interval.
3570 * Note that timeout functions may be delayed, due to the processing of other
3571 * event sources. Thus they should not be relied on for precise timing.
3572 * After each call to the timeout function, the time of the next
3573 * timeout is recalculated based on the current time and the given interval
3574 * (it does not try to 'catch up' time lost in delays).
3576 * This internally creates a main loop source using g_timeout_source_new()
3577 * and attaches it to the main loop context using g_source_attach(). You can
3578 * do these steps manually if you need greater control.
3580 * Return value: the ID (greater than 0) of the event source.
3583 g_timeout_add_full (gint priority,
3585 GSourceFunc function,
3587 GDestroyNotify notify)
3592 g_return_val_if_fail (function != NULL, 0);
3594 source = g_timeout_source_new (interval);
3596 if (priority != G_PRIORITY_DEFAULT)
3597 g_source_set_priority (source, priority);
3599 g_source_set_callback (source, function, data, notify);
3600 id = g_source_attach (source, NULL);
3601 g_source_unref (source);
3608 * @interval: the time between calls to the function, in milliseconds
3609 * (1/1000ths of a second)
3610 * @function: function to call
3611 * @data: data to pass to @function
3613 * Sets a function to be called at regular intervals, with the default
3614 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3615 * until it returns %FALSE, at which point the timeout is automatically
3616 * destroyed and the function will not be called again. The first call
3617 * to the function will be at the end of the first @interval.
3619 * Note that timeout functions may be delayed, due to the processing of other
3620 * event sources. Thus they should not be relied on for precise timing.
3621 * After each call to the timeout function, the time of the next
3622 * timeout is recalculated based on the current time and the given interval
3623 * (it does not try to 'catch up' time lost in delays).
3625 * If you want to have a timer in the "seconds" range and do not care
3626 * about the exact time of the first call of the timer, use the
3627 * g_timeout_add_seconds() function; this function allows for more
3628 * optimizations and more efficient system power usage.
3630 * This internally creates a main loop source using g_timeout_source_new()
3631 * and attaches it to the main loop context using g_source_attach(). You can
3632 * do these steps manually if you need greater control.
3634 * Return value: the ID (greater than 0) of the event source.
3637 g_timeout_add (guint32 interval,
3638 GSourceFunc function,
3641 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3642 interval, function, data, NULL);
3646 * g_timeout_add_seconds_full:
3647 * @priority: the priority of the timeout source. Typically this will be in
3648 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3649 * @interval: the time between calls to the function, in seconds
3650 * @function: function to call
3651 * @data: data to pass to @function
3652 * @notify: function to call when the timeout is removed, or %NULL
3654 * Sets a function to be called at regular intervals, with @priority.
3655 * The function is called repeatedly until it returns %FALSE, at which
3656 * point the timeout is automatically destroyed and the function will
3657 * not be called again.
3659 * Unlike g_timeout_add(), this function operates at whole second granularity.
3660 * The initial starting point of the timer is determined by the implementation
3661 * and the implementation is expected to group multiple timers together so that
3662 * they fire all at the same time.
3663 * To allow this grouping, the @interval to the first timer is rounded
3664 * and can deviate up to one second from the specified interval.
3665 * Subsequent timer iterations will generally run at the specified interval.
3667 * Note that timeout functions may be delayed, due to the processing of other
3668 * event sources. Thus they should not be relied on for precise timing.
3669 * After each call to the timeout function, the time of the next
3670 * timeout is recalculated based on the current time and the given @interval
3672 * If you want timing more precise than whole seconds, use g_timeout_add()
3675 * The grouping of timers to fire at the same time results in a more power
3676 * and CPU efficient behavior so if your timer is in multiples of seconds
3677 * and you don't require the first timer exactly one second from now, the
3678 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3680 * This internally creates a main loop source using
3681 * g_timeout_source_new_seconds() and attaches it to the main loop context
3682 * using g_source_attach(). You can do these steps manually if you need
3685 * Return value: the ID (greater than 0) of the event source.
3690 g_timeout_add_seconds_full (gint priority,
3692 GSourceFunc function,
3694 GDestroyNotify notify)
3699 g_return_val_if_fail (function != NULL, 0);
3701 source = g_timeout_source_new_seconds (interval);
3703 if (priority != G_PRIORITY_DEFAULT)
3704 g_source_set_priority (source, priority);
3706 g_source_set_callback (source, function, data, notify);
3707 id = g_source_attach (source, NULL);
3708 g_source_unref (source);
3714 * g_timeout_add_seconds:
3715 * @interval: the time between calls to the function, in seconds
3716 * @function: function to call
3717 * @data: data to pass to @function
3719 * Sets a function to be called at regular intervals with the default
3720 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3721 * it returns %FALSE, at which point the timeout is automatically destroyed
3722 * and the function will not be called again.
3724 * This internally creates a main loop source using
3725 * g_timeout_source_new_seconds() and attaches it to the main loop context
3726 * using g_source_attach(). You can do these steps manually if you need
3727 * greater control. Also see g_timout_add_seconds_full().
3729 * Return value: the ID (greater than 0) of the event source.
3734 g_timeout_add_seconds (guint interval,
3735 GSourceFunc function,
3738 g_return_val_if_fail (function != NULL, 0);
3740 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3743 /* Child watch functions */
3748 g_child_watch_prepare (GSource *source,
3757 g_child_watch_check (GSource *source)
3759 GChildWatchSource *child_watch_source;
3760 gboolean child_exited;
3762 child_watch_source = (GChildWatchSource *) source;
3764 child_exited = child_watch_source->poll.revents & G_IO_IN;
3771 * Note: We do _not_ check for the special value of STILL_ACTIVE
3772 * since we know that the process has exited and doing so runs into
3773 * problems if the child process "happens to return STILL_ACTIVE(259)"
3774 * as Microsoft's Platform SDK puts it.
3776 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3778 gchar *emsg = g_win32_error_message (GetLastError ());
3779 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3782 child_watch_source->child_status = -1;
3785 child_watch_source->child_status = child_status;
3788 return child_exited;
3791 #else /* G_OS_WIN32 */
3794 check_for_child_exited (GSource *source)
3796 GChildWatchSource *child_watch_source;
3799 /* protect against another SIGCHLD in the middle of this call */
3800 count = child_watch_count;
3802 child_watch_source = (GChildWatchSource *) source;
3804 if (child_watch_source->child_exited)
3807 if (child_watch_source->count < count)
3811 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3813 child_watch_source->child_status = child_status;
3814 child_watch_source->child_exited = TRUE;
3816 child_watch_source->count = count;
3819 return child_watch_source->child_exited;
3823 g_child_watch_prepare (GSource *source,
3828 return check_for_child_exited (source);
3833 g_child_watch_check (GSource *source)
3835 return check_for_child_exited (source);
3838 #endif /* G_OS_WIN32 */
3841 g_child_watch_dispatch (GSource *source,
3842 GSourceFunc callback,
3845 GChildWatchSource *child_watch_source;
3846 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3848 child_watch_source = (GChildWatchSource *) source;
3852 g_warning ("Child watch source dispatched without callback\n"
3853 "You must call g_source_set_callback().");
3857 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3859 /* We never keep a child watch source around as the child is gone */
3866 g_child_watch_signal_handler (int signum)
3868 child_watch_count ++;
3870 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3872 write (child_watch_wake_up_pipe[1], "B", 1);
3876 /* We count on the signal interrupting the poll in the same thread.
3882 g_child_watch_source_init_single (void)
3884 struct sigaction action;
3886 g_assert (! g_thread_supported());
3887 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3889 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3891 action.sa_handler = g_child_watch_signal_handler;
3892 sigemptyset (&action.sa_mask);
3893 action.sa_flags = SA_NOCLDSTOP;
3894 sigaction (SIGCHLD, &action, NULL);
3897 G_GNUC_NORETURN static gpointer
3898 child_watch_helper_thread (gpointer data)
3905 read (child_watch_wake_up_pipe[0], b, 20);
3907 /* We were woken up. Wake up all other contexts in all other threads */
3908 G_LOCK (main_context_list);
3909 for (list = main_context_list; list; list = list->next)
3911 GMainContext *context;
3913 context = list->data;
3914 if (g_atomic_int_get (&context->ref_count) > 0)
3915 /* Due to racing conditions we can find ref_count == 0, in
3916 * that case, however, the context is still not destroyed
3917 * and no poll can be active, otherwise the ref_count
3919 g_main_context_wakeup (context);
3921 G_UNLOCK (main_context_list);
3926 g_child_watch_source_init_multi_threaded (void)
3928 GError *error = NULL;
3929 struct sigaction action;
3931 g_assert (g_thread_supported());
3933 if (pipe (child_watch_wake_up_pipe) < 0)
3934 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3935 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3937 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3938 /* FIXME: Think this through for races */
3939 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3940 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3941 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3943 action.sa_handler = g_child_watch_signal_handler;
3944 sigemptyset (&action.sa_mask);
3945 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3946 sigaction (SIGCHLD, &action, NULL);
3950 g_child_watch_source_init_promote_single_to_threaded (void)
3952 g_child_watch_source_init_multi_threaded ();
3956 g_child_watch_source_init (void)
3958 if (g_thread_supported())
3960 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3961 g_child_watch_source_init_multi_threaded ();
3962 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3963 g_child_watch_source_init_promote_single_to_threaded ();
3967 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3968 g_child_watch_source_init_single ();
3972 #endif /* !G_OS_WIN32 */
3975 * g_child_watch_source_new:
3976 * @pid: process to watch. On POSIX the pid of a child process. On
3977 * Windows a handle for a process (which doesn't have to be a child).
3979 * Creates a new child_watch source.
3981 * The source will not initially be associated with any #GMainContext
3982 * and must be added to one with g_source_attach() before it will be
3985 * Note that child watch sources can only be used in conjunction with
3986 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
3989 * Note that on platforms where #GPid must be explicitly closed
3990 * (see g_spawn_close_pid()) @pid must not be closed while the
3991 * source is still active. Typically, you will want to call
3992 * g_spawn_close_pid() in the callback function for the source.
3994 * Note further that using g_child_watch_source_new() is not
3995 * compatible with calling <literal>waitpid(-1)</literal> in
3996 * the application. Calling waitpid() for individual pids will
3999 * Return value: the newly-created child watch source
4004 g_child_watch_source_new (GPid pid)
4006 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
4007 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
4010 child_watch_source->poll.fd = (gintptr) pid;
4011 child_watch_source->poll.events = G_IO_IN;
4013 g_source_add_poll (source, &child_watch_source->poll);
4014 #else /* G_OS_WIN32 */
4015 g_child_watch_source_init ();
4016 #endif /* G_OS_WIN32 */
4018 child_watch_source->pid = pid;
4024 * g_child_watch_add_full:
4025 * @priority: the priority of the idle source. Typically this will be in the
4026 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4027 * @pid: process to watch. On POSIX the pid of a child process. On
4028 * Windows a handle for a process (which doesn't have to be a child).
4029 * @function: function to call
4030 * @data: data to pass to @function
4031 * @notify: function to call when the idle is removed, or %NULL
4033 * Sets a function to be called when the child indicated by @pid
4034 * exits, at the priority @priority.
4036 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4037 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4038 * the spawn function for the child watching to work.
4040 * Note that on platforms where #GPid must be explicitly closed
4041 * (see g_spawn_close_pid()) @pid must not be closed while the
4042 * source is still active. Typically, you will want to call
4043 * g_spawn_close_pid() in the callback function for the source.
4045 * GLib supports only a single callback per process id.
4047 * This internally creates a main loop source using
4048 * g_child_watch_source_new() and attaches it to the main loop context
4049 * using g_source_attach(). You can do these steps manually if you
4050 * need greater control.
4052 * Return value: the ID (greater than 0) of the event source.
4057 g_child_watch_add_full (gint priority,
4059 GChildWatchFunc function,
4061 GDestroyNotify notify)
4066 g_return_val_if_fail (function != NULL, 0);
4068 source = g_child_watch_source_new (pid);
4070 if (priority != G_PRIORITY_DEFAULT)
4071 g_source_set_priority (source, priority);
4073 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4074 id = g_source_attach (source, NULL);
4075 g_source_unref (source);
4081 * g_child_watch_add:
4082 * @pid: process id to watch. On POSIX the pid of a child process. On
4083 * Windows a handle for a process (which doesn't have to be a child).
4084 * @function: function to call
4085 * @data: data to pass to @function
4087 * Sets a function to be called when the child indicated by @pid
4088 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4090 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4091 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4092 * the spawn function for the child watching to work.
4094 * Note that on platforms where #GPid must be explicitly closed
4095 * (see g_spawn_close_pid()) @pid must not be closed while the
4096 * source is still active. Typically, you will want to call
4097 * g_spawn_close_pid() in the callback function for the source.
4099 * GLib supports only a single callback per process id.
4101 * This internally creates a main loop source using
4102 * g_child_watch_source_new() and attaches it to the main loop context
4103 * using g_source_attach(). You can do these steps manually if you
4104 * need greater control.
4106 * Return value: the ID (greater than 0) of the event source.
4111 g_child_watch_add (GPid pid,
4112 GChildWatchFunc function,
4115 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4119 /* Idle functions */
4122 g_idle_prepare (GSource *source,
4131 g_idle_check (GSource *source)
4137 g_idle_dispatch (GSource *source,
4138 GSourceFunc callback,
4143 g_warning ("Idle source dispatched without callback\n"
4144 "You must call g_source_set_callback().");
4148 return callback (user_data);
4152 * g_idle_source_new:
4154 * Creates a new idle source.
4156 * The source will not initially be associated with any #GMainContext
4157 * and must be added to one with g_source_attach() before it will be
4158 * executed. Note that the default priority for idle sources is
4159 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4160 * have a default priority of %G_PRIORITY_DEFAULT.
4162 * Return value: the newly-created idle source
4165 g_idle_source_new (void)
4169 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4170 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4177 * @priority: the priority of the idle source. Typically this will be in the
4178 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4179 * @function: function to call
4180 * @data: data to pass to @function
4181 * @notify: function to call when the idle is removed, or %NULL
4183 * Adds a function to be called whenever there are no higher priority
4184 * events pending. If the function returns %FALSE it is automatically
4185 * removed from the list of event sources and will not be called again.
4187 * This internally creates a main loop source using g_idle_source_new()
4188 * and attaches it to the main loop context using g_source_attach().
4189 * You can do these steps manually if you need greater control.
4191 * Return value: the ID (greater than 0) of the event source.
4194 g_idle_add_full (gint priority,
4195 GSourceFunc function,
4197 GDestroyNotify notify)
4202 g_return_val_if_fail (function != NULL, 0);
4204 source = g_idle_source_new ();
4206 if (priority != G_PRIORITY_DEFAULT_IDLE)
4207 g_source_set_priority (source, priority);
4209 g_source_set_callback (source, function, data, notify);
4210 id = g_source_attach (source, NULL);
4211 g_source_unref (source);
4218 * @function: function to call
4219 * @data: data to pass to @function.
4221 * Adds a function to be called whenever there are no higher priority
4222 * events pending to the default main loop. The function is given the
4223 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4224 * returns %FALSE it is automatically removed from the list of event
4225 * sources and will not be called again.
4227 * This internally creates a main loop source using g_idle_source_new()
4228 * and attaches it to the main loop context using g_source_attach().
4229 * You can do these steps manually if you need greater control.
4231 * Return value: the ID (greater than 0) of the event source.
4234 g_idle_add (GSourceFunc function,
4237 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4241 * g_idle_remove_by_data:
4242 * @data: the data for the idle source's callback.
4244 * Removes the idle function with the given data.
4246 * Return value: %TRUE if an idle source was found and removed.
4249 g_idle_remove_by_data (gpointer data)
4251 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);