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 to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
40 #include "gthreadprivate.h"
42 #include <sys/types.h>
45 #ifdef HAVE_SYS_TIME_H
47 #endif /* HAVE_SYS_TIME_H */
48 #ifdef GLIB_HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
51 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
53 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
54 * so we prefer our own poll emulation.
56 #if defined(_POLL_EMUL_H_) || defined(BROKEN_POLL)
60 #endif /* GLIB_HAVE_SYS_POLL_H */
63 #endif /* HAVE_UNISTD_H */
69 #endif /* G_OS_WIN32 */
72 #include <sys/socket.h>
74 #endif /* G_OS_BEOS */
85 typedef struct _GTimeoutSource GTimeoutSource;
86 typedef struct _GChildWatchSource GChildWatchSource;
87 typedef struct _GPollRec GPollRec;
88 typedef struct _GSourceCallback GSourceCallback;
92 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
93 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
96 #ifdef G_THREADS_ENABLED
97 typedef struct _GMainWaiter GMainWaiter;
106 typedef struct _GMainDispatch GMainDispatch;
108 struct _GMainDispatch
111 GSList *source; /* stack of current sources */
116 #ifdef G_THREADS_ENABLED
117 /* The following lock is used for both the list of sources
118 * and the list of poll records
129 GPtrArray *pending_dispatches;
130 gint timeout; /* Timeout for current iteration */
133 GSource *source_list;
134 gint in_check_or_prepare;
136 GPollRec *poll_records;
137 guint n_poll_records;
138 GPollFD *cached_poll_array;
139 guint cached_poll_array_size;
141 #ifdef G_THREADS_ENABLED
143 /* this pipe is used to wake up the main loop when a source is added.
145 gint wake_up_pipe[2];
146 #else /* G_OS_WIN32 */
147 HANDLE wake_up_semaphore;
148 #endif /* G_OS_WIN32 */
151 gboolean poll_waiting;
153 /* Flag indicating whether the set of fd's changed during a poll */
154 gboolean poll_changed;
155 #endif /* G_THREADS_ENABLED */
159 GTimeVal current_time;
160 gboolean time_is_current;
163 struct _GSourceCallback
168 GDestroyNotify notify;
173 GMainContext *context;
178 struct _GTimeoutSource
186 struct _GChildWatchSource
193 #else /* G_OS_WIN32 */
195 gboolean child_exited;
196 #endif /* G_OS_WIN32 */
206 #ifdef G_THREADS_ENABLED
207 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
208 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
209 #define G_THREAD_SELF g_thread_self ()
211 #define LOCK_CONTEXT(context) (void)0
212 #define UNLOCK_CONTEXT(context) (void)0
213 #define G_THREAD_SELF NULL
216 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
217 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
218 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
220 #define SOURCE_UNREF(source, context) \
222 if ((source)->ref_count > 1) \
223 (source)->ref_count--; \
225 g_source_unref_internal ((source), (context), TRUE); \
229 /* Forward declarations */
231 static void g_source_unref_internal (GSource *source,
232 GMainContext *context,
234 static void g_source_destroy_internal (GSource *source,
235 GMainContext *context,
237 static void g_main_context_poll (GMainContext *context,
242 static void g_main_context_add_poll_unlocked (GMainContext *context,
245 static void g_main_context_remove_poll_unlocked (GMainContext *context,
247 static void g_main_context_wakeup_unlocked (GMainContext *context);
249 static gboolean g_timeout_prepare (GSource *source,
251 static gboolean g_timeout_check (GSource *source);
252 static gboolean g_timeout_dispatch (GSource *source,
253 GSourceFunc callback,
255 static gboolean g_child_watch_prepare (GSource *source,
257 static gboolean g_child_watch_check (GSource *source);
258 static gboolean g_child_watch_dispatch (GSource *source,
259 GSourceFunc callback,
261 static gboolean g_idle_prepare (GSource *source,
263 static gboolean g_idle_check (GSource *source);
264 static gboolean g_idle_dispatch (GSource *source,
265 GSourceFunc callback,
268 G_LOCK_DEFINE_STATIC (main_loop);
269 static GMainContext *default_main_context;
270 static GSList *main_contexts_without_pipe = NULL;
273 /* Child status monitoring code */
275 CHILD_WATCH_UNINITIALIZED,
276 CHILD_WATCH_INITIALIZED_SINGLE,
277 CHILD_WATCH_INITIALIZED_THREADED
279 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
280 static gint child_watch_count = 1;
281 static gint child_watch_wake_up_pipe[2] = {0, 0};
282 #endif /* !G_OS_WIN32 */
283 G_LOCK_DEFINE_STATIC (main_context_list);
284 static GSList *main_context_list = NULL;
286 static gint timer_perturb = -1;
288 GSourceFuncs g_timeout_funcs =
296 GSourceFuncs g_child_watch_funcs =
298 g_child_watch_prepare,
300 g_child_watch_dispatch,
304 GSourceFuncs g_idle_funcs =
313 /* SunOS has poll, but doesn't provide a prototype. */
314 # if defined (sun) && !defined (__SVR4)
315 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
317 #else /* !HAVE_POLL */
322 g_poll (GPollFD *fds,
326 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
327 gboolean poll_msgs = FALSE;
333 for (f = fds; f < &fds[nfds]; ++f)
336 if (f->fd == G_WIN32_MSG_HANDLE)
338 else if (nhandles == MAXIMUM_WAIT_OBJECTS)
340 g_warning (G_STRLOC ": Too many handles to wait for!\n");
345 #ifdef G_MAIN_POLL_DEBUG
346 g_print ("g_poll: waiting for %#x\n", f->fd);
348 handles[nhandles++] = (HANDLE) f->fd;
357 /* Waiting for messages, and maybe events
358 * -> First PeekMessage
360 #ifdef G_MAIN_POLL_DEBUG
361 g_print ("PeekMessage\n");
363 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
364 ready = WAIT_OBJECT_0 + nhandles;
367 /* Wait for either message or event
368 * -> Use MsgWaitForMultipleObjectsEx
370 #ifdef G_MAIN_POLL_DEBUG
371 g_print ("MsgWaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
373 ready = MsgWaitForMultipleObjectsEx (nhandles, handles, timeout,
374 QS_ALLINPUT, MWMO_ALERTABLE);
376 if (ready == WAIT_FAILED)
378 gchar *emsg = g_win32_error_message (GetLastError ());
379 g_warning (G_STRLOC ": MsgWaitForMultipleObjectsEx() failed: %s", emsg);
384 else if (nhandles == 0)
386 /* No handles to wait for, just the timeout */
387 if (timeout == INFINITE)
391 SleepEx (timeout, TRUE);
392 ready = WAIT_TIMEOUT;
397 /* Wait for just events
398 * -> Use WaitForMultipleObjectsEx
400 #ifdef G_MAIN_POLL_DEBUG
401 g_print ("WaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
403 ready = WaitForMultipleObjectsEx (nhandles, handles, FALSE, timeout, TRUE);
404 if (ready == WAIT_FAILED)
406 gchar *emsg = g_win32_error_message (GetLastError ());
407 g_warning (G_STRLOC ": WaitForMultipleObjectsEx() failed: %s", emsg);
412 #ifdef G_MAIN_POLL_DEBUG
413 g_print ("wait returns %ld%s\n",
415 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
416 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
417 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
419 for (f = fds; f < &fds[nfds]; ++f)
422 if (ready == WAIT_FAILED)
424 else if (ready == WAIT_TIMEOUT ||
425 ready == WAIT_IO_COMPLETION)
427 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
429 for (f = fds; f < &fds[nfds]; ++f)
432 if (f->events & G_IO_IN)
433 if (f->fd == G_WIN32_MSG_HANDLE)
434 f->revents |= G_IO_IN;
437 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
438 for (f = fds; f < &fds[nfds]; ++f)
440 if (f->fd == (gint) handles[ready - WAIT_OBJECT_0])
442 f->revents = f->events;
443 #ifdef G_MAIN_POLL_DEBUG
444 g_print ("g_poll: got event %#x\n", f->fd);
452 #else /* !G_OS_WIN32 */
454 /* The following implementation of poll() comes from the GNU C Library.
455 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
458 #include <string.h> /* for bzero on BSD systems */
460 #ifdef HAVE_SYS_SELECT_H
461 #include <sys/select.h>
462 #endif /* HAVE_SYS_SELECT_H */
466 #endif /* G_OS_BEOS */
469 # define SELECT_MASK fd_set
470 #else /* !NO_FD_SET */
472 typedef long fd_mask;
475 # define SELECT_MASK void
477 # define SELECT_MASK int
478 # endif /* !_IBMR2 */
479 #endif /* !NO_FD_SET */
482 g_poll (GPollFD *fds,
487 SELECT_MASK rset, wset, xset;
496 for (f = fds; f < &fds[nfds]; ++f)
499 if (f->events & G_IO_IN)
500 FD_SET (f->fd, &rset);
501 if (f->events & G_IO_OUT)
502 FD_SET (f->fd, &wset);
503 if (f->events & G_IO_PRI)
504 FD_SET (f->fd, &xset);
505 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
509 tv.tv_sec = timeout / 1000;
510 tv.tv_usec = (timeout % 1000) * 1000;
512 ready = select (maxfd + 1, &rset, &wset, &xset,
513 timeout == -1 ? NULL : &tv);
515 for (f = fds; f < &fds[nfds]; ++f)
520 if (FD_ISSET (f->fd, &rset))
521 f->revents |= G_IO_IN;
522 if (FD_ISSET (f->fd, &wset))
523 f->revents |= G_IO_OUT;
524 if (FD_ISSET (f->fd, &xset))
525 f->revents |= G_IO_PRI;
532 #endif /* !G_OS_WIN32 */
534 #endif /* !HAVE_POLL */
537 * g_main_context_ref:
538 * @context: a #GMainContext
540 * Increases the reference count on a #GMainContext object by one.
542 * Returns: the @context that was passed in (since 2.6)
545 g_main_context_ref (GMainContext *context)
547 g_return_val_if_fail (context != NULL, NULL);
548 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
550 g_atomic_int_inc (&context->ref_count);
556 poll_rec_list_free (GMainContext *context,
559 g_slice_free_chain (GPollRec, list, next);
563 * g_main_context_unref:
564 * @context: a #GMainContext
566 * Decreases the reference count on a #GMainContext object by one. If
567 * the result is zero, free the context and free all associated memory.
570 g_main_context_unref (GMainContext *context)
573 g_return_if_fail (context != NULL);
574 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
576 if (!g_atomic_int_dec_and_test (&context->ref_count))
579 G_LOCK (main_context_list);
580 main_context_list = g_slist_remove (main_context_list, context);
581 G_UNLOCK (main_context_list);
583 source = context->source_list;
586 GSource *next = source->next;
587 g_source_destroy_internal (source, context, FALSE);
591 #ifdef G_THREADS_ENABLED
592 g_static_mutex_free (&context->mutex);
595 g_ptr_array_free (context->pending_dispatches, TRUE);
596 g_free (context->cached_poll_array);
598 poll_rec_list_free (context, context->poll_records);
600 #ifdef G_THREADS_ENABLED
601 if (g_thread_supported())
604 close (context->wake_up_pipe[0]);
605 close (context->wake_up_pipe[1]);
607 CloseHandle (context->wake_up_semaphore);
611 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
614 if (context->cond != NULL)
615 g_cond_free (context->cond);
621 #ifdef G_THREADS_ENABLED
623 g_main_context_init_pipe (GMainContext *context)
626 if (context->wake_up_pipe[0] != -1)
628 if (pipe (context->wake_up_pipe) < 0)
629 g_error ("Cannot create pipe main loop wake-up: %s\n",
632 context->wake_up_rec.fd = context->wake_up_pipe[0];
633 context->wake_up_rec.events = G_IO_IN;
635 if (context->wake_up_semaphore != NULL)
637 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
638 if (context->wake_up_semaphore == NULL)
639 g_error ("Cannot create wake-up semaphore: %s",
640 g_win32_error_message (GetLastError ()));
641 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
642 context->wake_up_rec.events = G_IO_IN;
643 # ifdef G_MAIN_POLL_DEBUG
644 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
647 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
651 _g_main_thread_init (void)
653 GSList *curr = main_contexts_without_pipe;
656 g_main_context_init_pipe ((GMainContext *)curr->data);
659 g_slist_free (main_contexts_without_pipe);
660 main_contexts_without_pipe = NULL;
662 #endif /* G_THREADS_ENABLED */
665 * g_main_context_new:
667 * Creates a new #GMainContext structure.
669 * Return value: the new #GMainContext
672 g_main_context_new (void)
674 GMainContext *context = g_new0 (GMainContext, 1);
676 #ifdef G_THREADS_ENABLED
677 g_static_mutex_init (&context->mutex);
679 context->owner = NULL;
680 context->waiters = NULL;
683 context->wake_up_pipe[0] = -1;
684 context->wake_up_pipe[1] = -1;
686 context->wake_up_semaphore = NULL;
690 context->ref_count = 1;
692 context->next_id = 1;
694 context->source_list = NULL;
697 context->poll_func = (GPollFunc)poll;
699 context->poll_func = g_poll;
702 context->cached_poll_array = NULL;
703 context->cached_poll_array_size = 0;
705 context->pending_dispatches = g_ptr_array_new ();
707 context->time_is_current = FALSE;
709 #ifdef G_THREADS_ENABLED
710 if (g_thread_supported ())
711 g_main_context_init_pipe (context);
713 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
717 G_LOCK (main_context_list);
718 main_context_list = g_slist_append (main_context_list, context);
719 G_UNLOCK (main_context_list);
725 * g_main_context_default:
727 * Returns the default main context. This is the main context used
728 * for main loop functions when a main loop is not explicitly
731 * Return value: the default main context.
734 g_main_context_default (void)
740 if (!default_main_context)
741 default_main_context = g_main_context_new ();
743 G_UNLOCK (main_loop);
745 return default_main_context;
748 /* Hooks for adding to the main loop */
752 * @source_funcs: structure containing functions that implement
753 * the sources behavior.
754 * @struct_size: size of the #GSource structure to create.
756 * Creates a new #GSource structure. The size is specified to
757 * allow creating structures derived from #GSource that contain
758 * additional data. The size passed in must be at least
759 * <literal>sizeof (GSource)</literal>.
761 * The source will not initially be associated with any #GMainContext
762 * and must be added to one with g_source_attach() before it will be
765 * Return value: the newly-created #GSource.
768 g_source_new (GSourceFuncs *source_funcs,
773 g_return_val_if_fail (source_funcs != NULL, NULL);
774 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
776 source = (GSource*) g_malloc0 (struct_size);
778 source->source_funcs = source_funcs;
779 source->ref_count = 1;
781 source->priority = G_PRIORITY_DEFAULT;
783 source->flags = G_HOOK_FLAG_ACTIVE;
785 /* NULL/0 initialization for all other fields */
790 /* Holds context's lock
793 g_source_list_add (GSource *source,
794 GMainContext *context)
796 GSource *tmp_source, *last_source;
799 tmp_source = context->source_list;
800 while (tmp_source && tmp_source->priority <= source->priority)
802 last_source = tmp_source;
803 tmp_source = tmp_source->next;
806 source->next = tmp_source;
808 tmp_source->prev = source;
810 source->prev = last_source;
812 last_source->next = source;
814 context->source_list = source;
817 /* Holds context's lock
820 g_source_list_remove (GSource *source,
821 GMainContext *context)
824 source->prev->next = source->next;
826 context->source_list = source->next;
829 source->next->prev = source->prev;
837 * @source: a #GSource
838 * @context: a #GMainContext (if %NULL, the default context will be used)
840 * Adds a #GSource to a @context so that it will be executed within
843 * Return value: the ID (greater than 0) for the source within the
847 g_source_attach (GSource *source,
848 GMainContext *context)
853 g_return_val_if_fail (source->context == NULL, 0);
854 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
857 context = g_main_context_default ();
859 LOCK_CONTEXT (context);
861 source->context = context;
862 result = source->source_id = context->next_id++;
865 g_source_list_add (source, context);
867 tmp_list = source->poll_fds;
870 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
871 tmp_list = tmp_list->next;
874 #ifdef G_THREADS_ENABLED
875 /* Now wake up the main loop if it is waiting in the poll() */
876 g_main_context_wakeup_unlocked (context);
879 UNLOCK_CONTEXT (context);
885 g_source_destroy_internal (GSource *source,
886 GMainContext *context,
890 LOCK_CONTEXT (context);
892 if (!SOURCE_DESTROYED (source))
895 gpointer old_cb_data;
896 GSourceCallbackFuncs *old_cb_funcs;
898 source->flags &= ~G_HOOK_FLAG_ACTIVE;
900 old_cb_data = source->callback_data;
901 old_cb_funcs = source->callback_funcs;
903 source->callback_data = NULL;
904 source->callback_funcs = NULL;
908 UNLOCK_CONTEXT (context);
909 old_cb_funcs->unref (old_cb_data);
910 LOCK_CONTEXT (context);
913 if (!SOURCE_BLOCKED (source))
915 tmp_list = source->poll_fds;
918 g_main_context_remove_poll_unlocked (context, tmp_list->data);
919 tmp_list = tmp_list->next;
923 g_source_unref_internal (source, context, TRUE);
927 UNLOCK_CONTEXT (context);
932 * @source: a #GSource
934 * Removes a source from its #GMainContext, if any, and mark it as
935 * destroyed. The source cannot be subsequently added to another
939 g_source_destroy (GSource *source)
941 GMainContext *context;
943 g_return_if_fail (source != NULL);
945 context = source->context;
948 g_source_destroy_internal (source, context, FALSE);
950 source->flags &= ~G_HOOK_FLAG_ACTIVE;
955 * @source: a #GSource
957 * Returns the numeric ID for a particular source. The ID of a source
958 * is a positive integer which is unique within a particular main loop
959 * context. The reverse
960 * mapping from ID to source is done by g_main_context_find_source_by_id().
962 * Return value: the ID (greater than 0) for the source
965 g_source_get_id (GSource *source)
969 g_return_val_if_fail (source != NULL, 0);
970 g_return_val_if_fail (source->context != NULL, 0);
972 LOCK_CONTEXT (source->context);
973 result = source->source_id;
974 UNLOCK_CONTEXT (source->context);
980 * g_source_get_context:
981 * @source: a #GSource
983 * Gets the #GMainContext with which the source is associated.
984 * Calling this function on a destroyed source is an error.
986 * Return value: the #GMainContext with which the source is associated,
987 * or %NULL if the context has not yet been added
991 g_source_get_context (GSource *source)
993 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
995 return source->context;
1000 * @source:a #GSource
1001 * @fd: a #GPollFD structure holding information about a file
1002 * descriptor to watch.
1004 * Adds a file descriptor to the set of file descriptors polled for
1005 * this source. This is usually combined with g_source_new() to add an
1006 * event source. The event source's check function will typically test
1007 * the @revents field in the #GPollFD struct and return %TRUE if events need
1011 g_source_add_poll (GSource *source,
1014 GMainContext *context;
1016 g_return_if_fail (source != NULL);
1017 g_return_if_fail (fd != NULL);
1018 g_return_if_fail (!SOURCE_DESTROYED (source));
1020 context = source->context;
1023 LOCK_CONTEXT (context);
1025 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1029 if (!SOURCE_BLOCKED (source))
1030 g_main_context_add_poll_unlocked (context, source->priority, fd);
1031 UNLOCK_CONTEXT (context);
1036 * g_source_remove_poll:
1037 * @source:a #GSource
1038 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1040 * Removes a file descriptor from the set of file descriptors polled for
1044 g_source_remove_poll (GSource *source,
1047 GMainContext *context;
1049 g_return_if_fail (source != NULL);
1050 g_return_if_fail (fd != NULL);
1051 g_return_if_fail (!SOURCE_DESTROYED (source));
1053 context = source->context;
1056 LOCK_CONTEXT (context);
1058 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1062 if (!SOURCE_BLOCKED (source))
1063 g_main_context_remove_poll_unlocked (context, fd);
1064 UNLOCK_CONTEXT (context);
1069 * g_source_set_callback_indirect:
1070 * @source: the source
1071 * @callback_data: pointer to callback data "object"
1072 * @callback_funcs: functions for reference counting @callback_data
1073 * and getting the callback and data
1075 * Sets the callback function storing the data as a refcounted callback
1076 * "object". This is used internally. Note that calling
1077 * g_source_set_callback_indirect() assumes
1078 * an initial reference count on @callback_data, and thus
1079 * @callback_funcs->unref will eventually be called once more
1080 * than @callback_funcs->ref.
1083 g_source_set_callback_indirect (GSource *source,
1084 gpointer callback_data,
1085 GSourceCallbackFuncs *callback_funcs)
1087 GMainContext *context;
1088 gpointer old_cb_data;
1089 GSourceCallbackFuncs *old_cb_funcs;
1091 g_return_if_fail (source != NULL);
1092 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1094 context = source->context;
1097 LOCK_CONTEXT (context);
1099 old_cb_data = source->callback_data;
1100 old_cb_funcs = source->callback_funcs;
1102 source->callback_data = callback_data;
1103 source->callback_funcs = callback_funcs;
1106 UNLOCK_CONTEXT (context);
1109 old_cb_funcs->unref (old_cb_data);
1113 g_source_callback_ref (gpointer cb_data)
1115 GSourceCallback *callback = cb_data;
1117 callback->ref_count++;
1122 g_source_callback_unref (gpointer cb_data)
1124 GSourceCallback *callback = cb_data;
1126 callback->ref_count--;
1127 if (callback->ref_count == 0)
1129 if (callback->notify)
1130 callback->notify (callback->data);
1136 g_source_callback_get (gpointer cb_data,
1141 GSourceCallback *callback = cb_data;
1143 *func = callback->func;
1144 *data = callback->data;
1147 static GSourceCallbackFuncs g_source_callback_funcs = {
1148 g_source_callback_ref,
1149 g_source_callback_unref,
1150 g_source_callback_get,
1154 * g_source_set_callback:
1155 * @source: the source
1156 * @func: a callback function
1157 * @data: the data to pass to callback function
1158 * @notify: a function to call when @data is no longer in use, or %NULL.
1160 * Sets the callback function for a source. The callback for a source is
1161 * called from the source's dispatch function.
1163 * The exact type of @func depends on the type of source; ie. you
1164 * should not count on @func being called with @data as its first
1167 * Typically, you won't use this function. Instead use functions specific
1168 * to the type of source you are using.
1171 g_source_set_callback (GSource *source,
1174 GDestroyNotify notify)
1176 GSourceCallback *new_callback;
1178 g_return_if_fail (source != NULL);
1180 new_callback = g_new (GSourceCallback, 1);
1182 new_callback->ref_count = 1;
1183 new_callback->func = func;
1184 new_callback->data = data;
1185 new_callback->notify = notify;
1187 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1192 * g_source_set_funcs:
1193 * @source: a #GSource
1194 * @funcs: the new #GSourceFuncs
1196 * Sets the source functions (can be used to override
1197 * default implementations) of an unattached source.
1202 g_source_set_funcs (GSource *source,
1203 GSourceFuncs *funcs)
1205 g_return_if_fail (source != NULL);
1206 g_return_if_fail (source->context == NULL);
1207 g_return_if_fail (source->ref_count > 0);
1208 g_return_if_fail (funcs != NULL);
1210 source->source_funcs = funcs;
1214 * g_source_set_priority:
1215 * @source: a #GSource
1216 * @priority: the new priority.
1218 * Sets the priority of a source. While the main loop is being
1219 * run, a source will be dispatched if it is ready to be dispatched and no sources
1220 * at a higher (numerically smaller) priority are ready to be dispatched.
1223 g_source_set_priority (GSource *source,
1227 GMainContext *context;
1229 g_return_if_fail (source != NULL);
1231 context = source->context;
1234 LOCK_CONTEXT (context);
1236 source->priority = priority;
1240 /* Remove the source from the context's source and then
1241 * add it back so it is sorted in the correct plcae
1243 g_source_list_remove (source, source->context);
1244 g_source_list_add (source, source->context);
1246 if (!SOURCE_BLOCKED (source))
1248 tmp_list = source->poll_fds;
1251 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1252 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1254 tmp_list = tmp_list->next;
1258 UNLOCK_CONTEXT (source->context);
1263 * g_source_get_priority:
1264 * @source: a #GSource
1266 * Gets the priority of a source.
1268 * Return value: the priority of the source
1271 g_source_get_priority (GSource *source)
1273 g_return_val_if_fail (source != NULL, 0);
1275 return source->priority;
1279 * g_source_set_can_recurse:
1280 * @source: a #GSource
1281 * @can_recurse: whether recursion is allowed for this source
1283 * Sets whether a source can be called recursively. If @can_recurse is
1284 * %TRUE, then while the source is being dispatched then this source
1285 * will be processed normally. Otherwise, all processing of this
1286 * source is blocked until the dispatch function returns.
1289 g_source_set_can_recurse (GSource *source,
1290 gboolean can_recurse)
1292 GMainContext *context;
1294 g_return_if_fail (source != NULL);
1296 context = source->context;
1299 LOCK_CONTEXT (context);
1302 source->flags |= G_SOURCE_CAN_RECURSE;
1304 source->flags &= ~G_SOURCE_CAN_RECURSE;
1307 UNLOCK_CONTEXT (context);
1311 * g_source_get_can_recurse:
1312 * @source: a #GSource
1314 * Checks whether a source is allowed to be called recursively.
1315 * see g_source_set_can_recurse().
1317 * Return value: whether recursion is allowed.
1320 g_source_get_can_recurse (GSource *source)
1322 g_return_val_if_fail (source != NULL, FALSE);
1324 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1329 * @source: a #GSource
1331 * Increases the reference count on a source by one.
1333 * Return value: @source
1336 g_source_ref (GSource *source)
1338 GMainContext *context;
1340 g_return_val_if_fail (source != NULL, NULL);
1342 context = source->context;
1345 LOCK_CONTEXT (context);
1347 source->ref_count++;
1350 UNLOCK_CONTEXT (context);
1355 /* g_source_unref() but possible to call within context lock
1358 g_source_unref_internal (GSource *source,
1359 GMainContext *context,
1362 gpointer old_cb_data = NULL;
1363 GSourceCallbackFuncs *old_cb_funcs = NULL;
1365 g_return_if_fail (source != NULL);
1367 if (!have_lock && context)
1368 LOCK_CONTEXT (context);
1370 source->ref_count--;
1371 if (source->ref_count == 0)
1373 old_cb_data = source->callback_data;
1374 old_cb_funcs = source->callback_funcs;
1376 source->callback_data = NULL;
1377 source->callback_funcs = NULL;
1379 if (context && !SOURCE_DESTROYED (source))
1381 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1382 source->ref_count++;
1385 g_source_list_remove (source, context);
1387 if (source->source_funcs->finalize)
1388 source->source_funcs->finalize (source);
1390 g_slist_free (source->poll_fds);
1391 source->poll_fds = NULL;
1395 if (!have_lock && context)
1396 UNLOCK_CONTEXT (context);
1401 UNLOCK_CONTEXT (context);
1403 old_cb_funcs->unref (old_cb_data);
1406 LOCK_CONTEXT (context);
1412 * @source: a #GSource
1414 * Decreases the reference count of a source by one. If the
1415 * resulting reference count is zero the source and associated
1416 * memory will be destroyed.
1419 g_source_unref (GSource *source)
1421 g_return_if_fail (source != NULL);
1423 g_source_unref_internal (source, source->context, FALSE);
1427 * g_main_context_find_source_by_id:
1428 * @context: a #GMainContext (if %NULL, the default context will be used)
1429 * @source_id: the source ID, as returned by g_source_get_id().
1431 * Finds a #GSource given a pair of context and ID.
1433 * Return value: the #GSource if found, otherwise, %NULL
1436 g_main_context_find_source_by_id (GMainContext *context,
1441 g_return_val_if_fail (source_id > 0, NULL);
1443 if (context == NULL)
1444 context = g_main_context_default ();
1446 LOCK_CONTEXT (context);
1448 source = context->source_list;
1451 if (!SOURCE_DESTROYED (source) &&
1452 source->source_id == source_id)
1454 source = source->next;
1457 UNLOCK_CONTEXT (context);
1463 * g_main_context_find_source_by_funcs_user_data:
1464 * @context: a #GMainContext (if %NULL, the default context will be used).
1465 * @funcs: the @source_funcs passed to g_source_new().
1466 * @user_data: the user data from the callback.
1468 * Finds a source with the given source functions and user data. If
1469 * multiple sources exist with the same source function and user data,
1470 * the first one found will be returned.
1472 * Return value: the source, if one was found, otherwise %NULL
1475 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1476 GSourceFuncs *funcs,
1481 g_return_val_if_fail (funcs != NULL, NULL);
1483 if (context == NULL)
1484 context = g_main_context_default ();
1486 LOCK_CONTEXT (context);
1488 source = context->source_list;
1491 if (!SOURCE_DESTROYED (source) &&
1492 source->source_funcs == funcs &&
1493 source->callback_funcs)
1495 GSourceFunc callback;
1496 gpointer callback_data;
1498 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1500 if (callback_data == user_data)
1503 source = source->next;
1506 UNLOCK_CONTEXT (context);
1512 * g_main_context_find_source_by_user_data:
1513 * @context: a #GMainContext
1514 * @user_data: the user_data for the callback.
1516 * Finds a source with the given user data for the callback. If
1517 * multiple sources exist with the same user data, the first
1518 * one found will be returned.
1520 * Return value: the source, if one was found, otherwise %NULL
1523 g_main_context_find_source_by_user_data (GMainContext *context,
1528 if (context == NULL)
1529 context = g_main_context_default ();
1531 LOCK_CONTEXT (context);
1533 source = context->source_list;
1536 if (!SOURCE_DESTROYED (source) &&
1537 source->callback_funcs)
1539 GSourceFunc callback;
1540 gpointer callback_data = NULL;
1542 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1544 if (callback_data == user_data)
1547 source = source->next;
1550 UNLOCK_CONTEXT (context);
1557 * @tag: the ID of the source to remove.
1559 * Removes the source with the given id from the default main context.
1561 * a #GSource is given by g_source_get_id(), or will be returned by the
1562 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1563 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1564 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1566 * See also g_source_destroy().
1568 * Return value: %TRUE if the source was found and removed.
1571 g_source_remove (guint tag)
1575 g_return_val_if_fail (tag > 0, FALSE);
1577 source = g_main_context_find_source_by_id (NULL, tag);
1579 g_source_destroy (source);
1581 return source != NULL;
1585 * g_source_remove_by_user_data:
1586 * @user_data: the user_data for the callback.
1588 * Removes a source from the default main loop context given the user
1589 * data for the callback. If multiple sources exist with the same user
1590 * data, only one will be destroyed.
1592 * Return value: %TRUE if a source was found and removed.
1595 g_source_remove_by_user_data (gpointer user_data)
1599 source = g_main_context_find_source_by_user_data (NULL, user_data);
1602 g_source_destroy (source);
1610 * g_source_remove_by_funcs_user_data:
1611 * @funcs: The @source_funcs passed to g_source_new()
1612 * @user_data: the user data for the callback
1614 * Removes a source from the default main loop context given the
1615 * source functions and user data. If multiple sources exist with the
1616 * same source functions and user data, only one will be destroyed.
1618 * Return value: %TRUE if a source was found and removed.
1621 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1626 g_return_val_if_fail (funcs != NULL, FALSE);
1628 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1631 g_source_destroy (source);
1639 * g_get_current_time:
1640 * @result: #GTimeVal structure in which to store current time.
1642 * Equivalent to the UNIX gettimeofday() function, but portable.
1645 g_get_current_time (GTimeVal *result)
1650 g_return_if_fail (result != NULL);
1652 /*this is required on alpha, there the timeval structs are int's
1653 not longs and a cast only would fail horribly*/
1654 gettimeofday (&r, NULL);
1655 result->tv_sec = r.tv_sec;
1656 result->tv_usec = r.tv_usec;
1659 guint64 *time64 = (guint64 *) &ft;
1661 GetSystemTimeAsFileTime (&ft);
1663 /* Convert from 100s of nanoseconds since 1601-01-01
1664 * to Unix epoch. Yes, this is Y2038 unsafe.
1666 *time64 -= G_GINT64_CONSTANT (116444736000000000);
1669 result->tv_sec = *time64 / 1000000;
1670 result->tv_usec = *time64 % 1000000;
1675 g_main_dispatch_free (gpointer dispatch)
1677 g_slice_free (GMainDispatch, dispatch);
1680 /* Running the main loop */
1682 static GMainDispatch *
1685 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1686 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1689 dispatch = g_slice_new0 (GMainDispatch);
1690 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1699 * Returns the depth of the stack of calls to
1700 * g_main_context_dispatch() on any #GMainContext in the current thread.
1701 * That is, when called from the toplevel, it gives 0. When
1702 * called from within a callback from g_main_context_iteration()
1703 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1704 * a callback to a recursive call to g_main_context_iterate(),
1705 * it returns 2. And so forth.
1707 * This function is useful in a situation like the following:
1708 * Imagine an extremely simple "garbage collected" system.
1711 * static GList *free_list;
1714 * allocate_memory (gsize size)
1716 * gpointer result = g_malloc (size);
1717 * free_list = g_list_prepend (free_list, result);
1722 * free_allocated_memory (void)
1725 * for (l = free_list; l; l = l->next);
1727 * g_list_free (free_list);
1735 * g_main_context_iteration (NULL, TRUE);
1736 * free_allocated_memory();
1740 * This works from an application, however, if you want to do the same
1741 * thing from a library, it gets more difficult, since you no longer
1742 * control the main loop. You might think you can simply use an idle
1743 * function to make the call to free_allocated_memory(), but that
1744 * doesn't work, since the idle function could be called from a
1745 * recursive callback. This can be fixed by using g_main_depth()
1749 * allocate_memory (gsize size)
1751 * FreeListBlock *block = g_new (FreeListBlock, 1);
1752 * block->mem = g_malloc (size);
1753 * block->depth = g_main_depth ();
1754 * free_list = g_list_prepend (free_list, block);
1755 * return block->mem;
1759 * free_allocated_memory (void)
1763 * int depth = g_main_depth ();
1764 * for (l = free_list; l; );
1766 * GList *next = l->next;
1767 * FreeListBlock *block = l->data;
1768 * if (block->depth > depth)
1770 * g_free (block->mem);
1772 * free_list = g_list_delete_link (free_list, l);
1780 * There is a temptation to use g_main_depth() to solve
1781 * problems with reentrancy. For instance, while waiting for data
1782 * to be received from the network in response to a menu item,
1783 * the menu item might be selected again. It might seem that
1784 * one could make the menu item's callback return immediately
1785 * and do nothing if g_main_depth() returns a value greater than 1.
1786 * However, this should be avoided since the user then sees selecting
1787 * the menu item do nothing. Furthermore, you'll find yourself adding
1788 * these checks all over your code, since there are doubtless many,
1789 * many things that the user could do. Instead, you can use the
1790 * following techniques:
1795 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1796 * the user from interacting with elements while the main
1797 * loop is recursing.
1802 * Avoid main loop recursion in situations where you can't handle
1803 * arbitrary callbacks. Instead, structure your code so that you
1804 * simply return to the main loop and then get called again when
1805 * there is more work to do.
1810 * Return value: The main loop recursion level in the current thread
1815 GMainDispatch *dispatch = get_dispatch ();
1816 return dispatch->depth;
1820 * g_main_current_source:
1822 * Returns the currently firing source for this thread.
1824 * Return value: The currently firing source or %NULL.
1829 g_main_current_source (void)
1831 GMainDispatch *dispatch = get_dispatch ();
1832 return dispatch->source ? dispatch->source->data : NULL;
1836 * g_source_is_destroyed:
1837 * @source: a #GSource
1839 * Returns whether @source has been destroyed.
1841 * This is important when you operate upon your objects
1842 * from within idle handlers, but may have freed the object
1843 * before the dispatch of your idle handler.
1847 * idle_callback (gpointer data)
1849 * SomeWidget *self = data;
1851 * GDK_THREADS_ENTER (<!-- -->);
1852 * /<!-- -->* do stuff with self *<!-- -->/
1853 * self->idle_id = 0;
1854 * GDK_THREADS_LEAVE (<!-- -->);
1860 * some_widget_do_stuff_later (SomeWidget *self)
1862 * self->idle_id = g_idle_add (idle_callback, self);
1866 * some_widget_finalize (GObject *object)
1868 * SomeWidget *self = SOME_WIDGET (object);
1870 * if (self->idle_id)
1871 * g_source_remove (self->idle_id);
1873 * G_OBJECT_CLASS (parent_class)->finalize (object);
1877 * This will fail in a multi-threaded application if the
1878 * widget is destroyed before the idle handler fires due
1879 * to the use after free in the callback. A solution, to
1880 * this particular problem, is to check to if the source
1881 * has already been destroy within the callback.
1885 * idle_callback (gpointer data)
1887 * SomeWidget *self = data;
1889 * GDK_THREADS_ENTER ();
1890 * if (!g_source_is_destroyed (g_main_current_source ()))
1892 * /<!-- -->* do stuff with self *<!-- -->/
1894 * GDK_THREADS_LEAVE ();
1900 * Return value: %TRUE if the source has been destroyed
1905 g_source_is_destroyed (GSource *source)
1907 return SOURCE_DESTROYED (source);
1911 /* Temporarily remove all this source's file descriptors from the
1912 * poll(), so that if data comes available for one of the file descriptors
1913 * we don't continually spin in the poll()
1915 /* HOLDS: source->context's lock */
1917 block_source (GSource *source)
1921 g_return_if_fail (!SOURCE_BLOCKED (source));
1923 tmp_list = source->poll_fds;
1926 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1927 tmp_list = tmp_list->next;
1931 /* HOLDS: source->context's lock */
1933 unblock_source (GSource *source)
1937 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1938 g_return_if_fail (!SOURCE_DESTROYED (source));
1940 tmp_list = source->poll_fds;
1943 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1944 tmp_list = tmp_list->next;
1948 /* HOLDS: context's lock */
1950 g_main_dispatch (GMainContext *context)
1952 GMainDispatch *current = get_dispatch ();
1955 for (i = 0; i < context->pending_dispatches->len; i++)
1957 GSource *source = context->pending_dispatches->pdata[i];
1959 context->pending_dispatches->pdata[i] = NULL;
1962 source->flags &= ~G_SOURCE_READY;
1964 if (!SOURCE_DESTROYED (source))
1966 gboolean was_in_call;
1967 gpointer user_data = NULL;
1968 GSourceFunc callback = NULL;
1969 GSourceCallbackFuncs *cb_funcs;
1971 gboolean need_destroy;
1973 gboolean (*dispatch) (GSource *,
1976 GSList current_source_link;
1978 dispatch = source->source_funcs->dispatch;
1979 cb_funcs = source->callback_funcs;
1980 cb_data = source->callback_data;
1983 cb_funcs->ref (cb_data);
1985 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
1986 block_source (source);
1988 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1989 source->flags |= G_HOOK_FLAG_IN_CALL;
1992 cb_funcs->get (cb_data, source, &callback, &user_data);
1994 UNLOCK_CONTEXT (context);
1997 /* The on-stack allocation of the GSList is unconventional, but
1998 * we know that the lifetime of the link is bounded to this
1999 * function as the link is kept in a thread specific list and
2000 * not manipulated outside of this function and its descendants.
2001 * Avoiding the overhead of a g_slist_alloc() is useful as many
2002 * applications do little more than dispatch events.
2004 * This is a performance hack - do not revert to g_slist_prepend()!
2006 current_source_link.data = source;
2007 current_source_link.next = current->source;
2008 current->source = ¤t_source_link;
2009 need_destroy = ! dispatch (source,
2012 g_assert (current->source == ¤t_source_link);
2013 current->source = current_source_link.next;
2017 cb_funcs->unref (cb_data);
2019 LOCK_CONTEXT (context);
2022 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2024 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2025 !SOURCE_DESTROYED (source))
2026 unblock_source (source);
2028 /* Note: this depends on the fact that we can't switch
2029 * sources from one main context to another
2031 if (need_destroy && !SOURCE_DESTROYED (source))
2033 g_assert (source->context == context);
2034 g_source_destroy_internal (source, context, TRUE);
2038 SOURCE_UNREF (source, context);
2041 g_ptr_array_set_size (context->pending_dispatches, 0);
2044 /* Holds context's lock */
2045 static inline GSource *
2046 next_valid_source (GMainContext *context,
2049 GSource *new_source = source ? source->next : context->source_list;
2053 if (!SOURCE_DESTROYED (new_source))
2055 new_source->ref_count++;
2059 new_source = new_source->next;
2063 SOURCE_UNREF (source, context);
2069 * g_main_context_acquire:
2070 * @context: a #GMainContext
2072 * Tries to become the owner of the specified context.
2073 * If some other thread is the owner of the context,
2074 * returns %FALSE immediately. Ownership is properly
2075 * recursive: the owner can require ownership again
2076 * and will release ownership when g_main_context_release()
2077 * is called as many times as g_main_context_acquire().
2079 * You must be the owner of a context before you
2080 * can call g_main_context_prepare(), g_main_context_query(),
2081 * g_main_context_check(), g_main_context_dispatch().
2083 * Return value: %TRUE if the operation succeeded, and
2084 * this thread is now the owner of @context.
2087 g_main_context_acquire (GMainContext *context)
2089 #ifdef G_THREADS_ENABLED
2090 gboolean result = FALSE;
2091 GThread *self = G_THREAD_SELF;
2093 if (context == NULL)
2094 context = g_main_context_default ();
2096 LOCK_CONTEXT (context);
2098 if (!context->owner)
2100 context->owner = self;
2101 g_assert (context->owner_count == 0);
2104 if (context->owner == self)
2106 context->owner_count++;
2110 UNLOCK_CONTEXT (context);
2113 #else /* !G_THREADS_ENABLED */
2115 #endif /* G_THREADS_ENABLED */
2119 * g_main_context_release:
2120 * @context: a #GMainContext
2122 * Releases ownership of a context previously acquired by this thread
2123 * with g_main_context_acquire(). If the context was acquired multiple
2124 * times, the ownership will be released only when g_main_context_release()
2125 * is called as many times as it was acquired.
2128 g_main_context_release (GMainContext *context)
2130 #ifdef G_THREADS_ENABLED
2131 if (context == NULL)
2132 context = g_main_context_default ();
2134 LOCK_CONTEXT (context);
2136 context->owner_count--;
2137 if (context->owner_count == 0)
2139 context->owner = NULL;
2141 if (context->waiters)
2143 GMainWaiter *waiter = context->waiters->data;
2144 gboolean loop_internal_waiter =
2145 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2146 context->waiters = g_slist_delete_link (context->waiters,
2148 if (!loop_internal_waiter)
2149 g_mutex_lock (waiter->mutex);
2151 g_cond_signal (waiter->cond);
2153 if (!loop_internal_waiter)
2154 g_mutex_unlock (waiter->mutex);
2158 UNLOCK_CONTEXT (context);
2159 #endif /* G_THREADS_ENABLED */
2163 * g_main_context_wait:
2164 * @context: a #GMainContext
2165 * @cond: a condition variable
2166 * @mutex: a mutex, currently held
2168 * Tries to become the owner of the specified context,
2169 * as with g_main_context_acquire(). But if another thread
2170 * is the owner, atomically drop @mutex and wait on @cond until
2171 * that owner releases ownership or until @cond is signaled, then
2172 * try again (once) to become the owner.
2174 * Return value: %TRUE if the operation succeeded, and
2175 * this thread is now the owner of @context.
2178 g_main_context_wait (GMainContext *context,
2182 #ifdef G_THREADS_ENABLED
2183 gboolean result = FALSE;
2184 GThread *self = G_THREAD_SELF;
2185 gboolean loop_internal_waiter;
2187 if (context == NULL)
2188 context = g_main_context_default ();
2190 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2192 if (!loop_internal_waiter)
2193 LOCK_CONTEXT (context);
2195 if (context->owner && context->owner != self)
2200 waiter.mutex = mutex;
2202 context->waiters = g_slist_append (context->waiters, &waiter);
2204 if (!loop_internal_waiter)
2205 UNLOCK_CONTEXT (context);
2206 g_cond_wait (cond, mutex);
2207 if (!loop_internal_waiter)
2208 LOCK_CONTEXT (context);
2210 context->waiters = g_slist_remove (context->waiters, &waiter);
2213 if (!context->owner)
2215 context->owner = self;
2216 g_assert (context->owner_count == 0);
2219 if (context->owner == self)
2221 context->owner_count++;
2225 if (!loop_internal_waiter)
2226 UNLOCK_CONTEXT (context);
2229 #else /* !G_THREADS_ENABLED */
2231 #endif /* G_THREADS_ENABLED */
2235 * g_main_context_prepare:
2236 * @context: a #GMainContext
2237 * @priority: location to store priority of highest priority
2238 * source already ready.
2240 * Prepares to poll sources within a main loop. The resulting information
2241 * for polling is determined by calling g_main_context_query ().
2243 * Return value: %TRUE if some source is ready to be dispatched
2247 g_main_context_prepare (GMainContext *context,
2252 gint current_priority = G_MAXINT;
2255 if (context == NULL)
2256 context = g_main_context_default ();
2258 LOCK_CONTEXT (context);
2260 context->time_is_current = FALSE;
2262 if (context->in_check_or_prepare)
2264 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2265 "prepare() member.");
2266 UNLOCK_CONTEXT (context);
2270 #ifdef G_THREADS_ENABLED
2271 if (context->poll_waiting)
2273 g_warning("g_main_context_prepare(): main loop already active in another thread");
2274 UNLOCK_CONTEXT (context);
2278 context->poll_waiting = TRUE;
2279 #endif /* G_THREADS_ENABLED */
2282 /* If recursing, finish up current dispatch, before starting over */
2283 if (context->pending_dispatches)
2286 g_main_dispatch (context, ¤t_time);
2288 UNLOCK_CONTEXT (context);
2293 /* If recursing, clear list of pending dispatches */
2295 for (i = 0; i < context->pending_dispatches->len; i++)
2297 if (context->pending_dispatches->pdata[i])
2298 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2300 g_ptr_array_set_size (context->pending_dispatches, 0);
2302 /* Prepare all sources */
2304 context->timeout = -1;
2306 source = next_valid_source (context, NULL);
2309 gint source_timeout = -1;
2311 if ((n_ready > 0) && (source->priority > current_priority))
2313 SOURCE_UNREF (source, context);
2316 if (SOURCE_BLOCKED (source))
2319 if (!(source->flags & G_SOURCE_READY))
2322 gboolean (*prepare) (GSource *source,
2325 prepare = source->source_funcs->prepare;
2326 context->in_check_or_prepare++;
2327 UNLOCK_CONTEXT (context);
2329 result = (*prepare) (source, &source_timeout);
2331 LOCK_CONTEXT (context);
2332 context->in_check_or_prepare--;
2335 source->flags |= G_SOURCE_READY;
2338 if (source->flags & G_SOURCE_READY)
2341 current_priority = source->priority;
2342 context->timeout = 0;
2345 if (source_timeout >= 0)
2347 if (context->timeout < 0)
2348 context->timeout = source_timeout;
2350 context->timeout = MIN (context->timeout, source_timeout);
2354 source = next_valid_source (context, source);
2357 UNLOCK_CONTEXT (context);
2360 *priority = current_priority;
2362 return (n_ready > 0);
2366 * g_main_context_query:
2367 * @context: a #GMainContext
2368 * @max_priority: maximum priority source to check
2369 * @timeout_: location to store timeout to be used in polling
2370 * @fds: location to store #GPollFD records that need to be polled.
2371 * @n_fds: length of @fds.
2373 * Determines information necessary to poll this main loop.
2375 * Return value: the number of records actually stored in @fds,
2376 * or, if more than @n_fds records need to be stored, the number
2377 * of records that need to be stored.
2380 g_main_context_query (GMainContext *context,
2389 LOCK_CONTEXT (context);
2391 pollrec = context->poll_records;
2393 while (pollrec && max_priority >= pollrec->priority)
2395 if (pollrec->fd->events)
2399 fds[n_poll].fd = pollrec->fd->fd;
2400 /* In direct contradiction to the Unix98 spec, IRIX runs into
2401 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2402 * flags in the events field of the pollfd while it should
2403 * just ignoring them. So we mask them out here.
2405 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2406 fds[n_poll].revents = 0;
2411 pollrec = pollrec->next;
2414 #ifdef G_THREADS_ENABLED
2415 context->poll_changed = FALSE;
2420 *timeout = context->timeout;
2422 context->time_is_current = FALSE;
2425 UNLOCK_CONTEXT (context);
2431 * g_main_context_check:
2432 * @context: a #GMainContext
2433 * @max_priority: the maximum numerical priority of sources to check
2434 * @fds: array of #GPollFD's that was passed to the last call to
2435 * g_main_context_query()
2436 * @n_fds: return value of g_main_context_query()
2438 * Passes the results of polling back to the main loop.
2440 * Return value: %TRUE if some sources are ready to be dispatched.
2443 g_main_context_check (GMainContext *context,
2453 LOCK_CONTEXT (context);
2455 if (context->in_check_or_prepare)
2457 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2458 "prepare() member.");
2459 UNLOCK_CONTEXT (context);
2463 #ifdef G_THREADS_ENABLED
2464 if (!context->poll_waiting)
2468 read (context->wake_up_pipe[0], &a, 1);
2472 context->poll_waiting = FALSE;
2474 /* If the set of poll file descriptors changed, bail out
2475 * and let the main loop rerun
2477 if (context->poll_changed)
2479 UNLOCK_CONTEXT (context);
2482 #endif /* G_THREADS_ENABLED */
2484 pollrec = context->poll_records;
2488 if (pollrec->fd->events)
2490 pollrec->fd->revents = fds[i].revents;
2493 pollrec = pollrec->next;
2496 source = next_valid_source (context, NULL);
2499 if ((n_ready > 0) && (source->priority > max_priority))
2501 SOURCE_UNREF (source, context);
2504 if (SOURCE_BLOCKED (source))
2507 if (!(source->flags & G_SOURCE_READY))
2510 gboolean (*check) (GSource *source);
2512 check = source->source_funcs->check;
2514 context->in_check_or_prepare++;
2515 UNLOCK_CONTEXT (context);
2517 result = (*check) (source);
2519 LOCK_CONTEXT (context);
2520 context->in_check_or_prepare--;
2523 source->flags |= G_SOURCE_READY;
2526 if (source->flags & G_SOURCE_READY)
2528 source->ref_count++;
2529 g_ptr_array_add (context->pending_dispatches, source);
2533 /* never dispatch sources with less priority than the first
2534 * one we choose to dispatch
2536 max_priority = source->priority;
2540 source = next_valid_source (context, source);
2543 UNLOCK_CONTEXT (context);
2549 * g_main_context_dispatch:
2550 * @context: a #GMainContext
2552 * Dispatches all pending sources.
2555 g_main_context_dispatch (GMainContext *context)
2557 LOCK_CONTEXT (context);
2559 if (context->pending_dispatches->len > 0)
2561 g_main_dispatch (context);
2564 UNLOCK_CONTEXT (context);
2567 /* HOLDS context lock */
2569 g_main_context_iterate (GMainContext *context,
2576 gboolean some_ready;
2577 gint nfds, allocated_nfds;
2578 GPollFD *fds = NULL;
2580 UNLOCK_CONTEXT (context);
2582 #ifdef G_THREADS_ENABLED
2583 if (!g_main_context_acquire (context))
2585 gboolean got_ownership;
2587 g_return_val_if_fail (g_thread_supported (), FALSE);
2592 LOCK_CONTEXT (context);
2595 context->cond = g_cond_new ();
2597 got_ownership = g_main_context_wait (context,
2599 g_static_mutex_get_mutex (&context->mutex));
2603 UNLOCK_CONTEXT (context);
2608 LOCK_CONTEXT (context);
2609 #endif /* G_THREADS_ENABLED */
2611 if (!context->cached_poll_array)
2613 context->cached_poll_array_size = context->n_poll_records;
2614 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2617 allocated_nfds = context->cached_poll_array_size;
2618 fds = context->cached_poll_array;
2620 UNLOCK_CONTEXT (context);
2622 g_main_context_prepare (context, &max_priority);
2624 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2625 allocated_nfds)) > allocated_nfds)
2627 LOCK_CONTEXT (context);
2629 context->cached_poll_array_size = allocated_nfds = nfds;
2630 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2631 UNLOCK_CONTEXT (context);
2637 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2639 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2642 g_main_context_dispatch (context);
2644 #ifdef G_THREADS_ENABLED
2645 g_main_context_release (context);
2646 #endif /* G_THREADS_ENABLED */
2648 LOCK_CONTEXT (context);
2654 * g_main_context_pending:
2655 * @context: a #GMainContext (if %NULL, the default context will be used)
2657 * Checks if any sources have pending events for the given context.
2659 * Return value: %TRUE if events are pending.
2662 g_main_context_pending (GMainContext *context)
2667 context = g_main_context_default();
2669 LOCK_CONTEXT (context);
2670 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2671 UNLOCK_CONTEXT (context);
2677 * g_main_context_iteration:
2678 * @context: a #GMainContext (if %NULL, the default context will be used)
2679 * @may_block: whether the call may block.
2681 * Runs a single iteration for the given main loop. This involves
2682 * checking to see if any event sources are ready to be processed,
2683 * then if no events sources are ready and @may_block is %TRUE, waiting
2684 * for a source to become ready, then dispatching the highest priority
2685 * events sources that are ready. Otherwise, if @may_block is %FALSE
2686 * sources are not waited to become ready, only those highest priority
2687 * events sources will be dispatched (if any), that are ready at this
2688 * given moment without further waiting.
2690 * Note that even when @may_block is %TRUE, it is still possible for
2691 * g_main_context_iteration() to return %FALSE, since the the wait may
2692 * be interrupted for other reasons than an event source becoming ready.
2694 * Return value: %TRUE if events were dispatched.
2697 g_main_context_iteration (GMainContext *context, gboolean may_block)
2702 context = g_main_context_default();
2704 LOCK_CONTEXT (context);
2705 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2706 UNLOCK_CONTEXT (context);
2713 * @context: a #GMainContext (if %NULL, the default context will be used).
2714 * @is_running: set to %TRUE to indicate that the loop is running. This
2715 * is not very important since calling g_main_loop_run() will set this to
2718 * Creates a new #GMainLoop structure.
2720 * Return value: a new #GMainLoop.
2723 g_main_loop_new (GMainContext *context,
2724 gboolean is_running)
2729 context = g_main_context_default();
2731 g_main_context_ref (context);
2733 loop = g_new0 (GMainLoop, 1);
2734 loop->context = context;
2735 loop->is_running = is_running != FALSE;
2736 loop->ref_count = 1;
2743 * @loop: a #GMainLoop
2745 * Increases the reference count on a #GMainLoop object by one.
2747 * Return value: @loop
2750 g_main_loop_ref (GMainLoop *loop)
2752 g_return_val_if_fail (loop != NULL, NULL);
2753 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2755 g_atomic_int_inc (&loop->ref_count);
2761 * g_main_loop_unref:
2762 * @loop: a #GMainLoop
2764 * Decreases the reference count on a #GMainLoop object by one. If
2765 * the result is zero, free the loop and free all associated memory.
2768 g_main_loop_unref (GMainLoop *loop)
2770 g_return_if_fail (loop != NULL);
2771 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2773 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2776 g_main_context_unref (loop->context);
2782 * @loop: a #GMainLoop
2784 * Runs a main loop until g_main_loop_quit() is called on the loop.
2785 * If this is called for the thread of the loop's #GMainContext,
2786 * it will process events from the loop, otherwise it will
2790 g_main_loop_run (GMainLoop *loop)
2792 GThread *self = G_THREAD_SELF;
2794 g_return_if_fail (loop != NULL);
2795 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2797 #ifdef G_THREADS_ENABLED
2798 if (!g_main_context_acquire (loop->context))
2800 gboolean got_ownership = FALSE;
2802 /* Another thread owns this context */
2803 if (!g_thread_supported ())
2805 g_warning ("g_main_loop_run() was called from second thread but "
2806 "g_thread_init() was never called.");
2810 LOCK_CONTEXT (loop->context);
2812 g_atomic_int_inc (&loop->ref_count);
2814 if (!loop->is_running)
2815 loop->is_running = TRUE;
2817 if (!loop->context->cond)
2818 loop->context->cond = g_cond_new ();
2820 while (loop->is_running && !got_ownership)
2821 got_ownership = g_main_context_wait (loop->context,
2822 loop->context->cond,
2823 g_static_mutex_get_mutex (&loop->context->mutex));
2825 if (!loop->is_running)
2827 UNLOCK_CONTEXT (loop->context);
2829 g_main_context_release (loop->context);
2830 g_main_loop_unref (loop);
2834 g_assert (got_ownership);
2837 LOCK_CONTEXT (loop->context);
2838 #endif /* G_THREADS_ENABLED */
2840 if (loop->context->in_check_or_prepare)
2842 g_warning ("g_main_loop_run(): called recursively from within a source's "
2843 "check() or prepare() member, iteration not possible.");
2847 g_atomic_int_inc (&loop->ref_count);
2848 loop->is_running = TRUE;
2849 while (loop->is_running)
2850 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2852 UNLOCK_CONTEXT (loop->context);
2854 #ifdef G_THREADS_ENABLED
2855 g_main_context_release (loop->context);
2856 #endif /* G_THREADS_ENABLED */
2858 g_main_loop_unref (loop);
2863 * @loop: a #GMainLoop
2865 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2866 * for the loop will return.
2868 * Note that sources that have already been dispatched when
2869 * g_main_loop_quit() is called will still be executed.
2872 g_main_loop_quit (GMainLoop *loop)
2874 g_return_if_fail (loop != NULL);
2875 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2877 LOCK_CONTEXT (loop->context);
2878 loop->is_running = FALSE;
2879 g_main_context_wakeup_unlocked (loop->context);
2881 #ifdef G_THREADS_ENABLED
2882 if (loop->context->cond)
2883 g_cond_broadcast (loop->context->cond);
2884 #endif /* G_THREADS_ENABLED */
2886 UNLOCK_CONTEXT (loop->context);
2890 * g_main_loop_is_running:
2891 * @loop: a #GMainLoop.
2893 * Checks to see if the main loop is currently being run via g_main_loop_run().
2895 * Return value: %TRUE if the mainloop is currently being run.
2898 g_main_loop_is_running (GMainLoop *loop)
2900 g_return_val_if_fail (loop != NULL, FALSE);
2901 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2903 return loop->is_running;
2907 * g_main_loop_get_context:
2908 * @loop: a #GMainLoop.
2910 * Returns the #GMainContext of @loop.
2912 * Return value: the #GMainContext of @loop
2915 g_main_loop_get_context (GMainLoop *loop)
2917 g_return_val_if_fail (loop != NULL, NULL);
2918 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2920 return loop->context;
2923 /* HOLDS: context's lock */
2925 g_main_context_poll (GMainContext *context,
2931 #ifdef G_MAIN_POLL_DEBUG
2937 GPollFunc poll_func;
2939 if (n_fds || timeout != 0)
2941 #ifdef G_MAIN_POLL_DEBUG
2942 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2943 poll_timer = g_timer_new ();
2946 LOCK_CONTEXT (context);
2948 poll_func = context->poll_func;
2950 UNLOCK_CONTEXT (context);
2951 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2954 g_warning ("poll(2) failed due to: %s.",
2955 g_strerror (errno));
2957 /* If g_poll () returns -1, it has already called g_warning() */
2961 #ifdef G_MAIN_POLL_DEBUG
2962 LOCK_CONTEXT (context);
2964 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2967 g_timer_elapsed (poll_timer, NULL));
2968 g_timer_destroy (poll_timer);
2969 pollrec = context->poll_records;
2973 if (pollrec->fd->events)
2977 g_print (" [%d:", fds[i].fd);
2978 if (fds[i].revents & G_IO_IN)
2980 if (fds[i].revents & G_IO_OUT)
2982 if (fds[i].revents & G_IO_PRI)
2984 if (fds[i].revents & G_IO_ERR)
2986 if (fds[i].revents & G_IO_HUP)
2988 if (fds[i].revents & G_IO_NVAL)
2994 pollrec = pollrec->next;
2998 UNLOCK_CONTEXT (context);
3000 } /* if (n_fds || timeout != 0) */
3004 * g_main_context_add_poll:
3005 * @context: a #GMainContext (or %NULL for the default context)
3006 * @fd: a #GPollFD structure holding information about a file
3007 * descriptor to watch.
3008 * @priority: the priority for this file descriptor which should be
3009 * the same as the priority used for g_source_attach() to ensure that the
3010 * file descriptor is polled whenever the results may be needed.
3012 * Adds a file descriptor to the set of file descriptors polled for
3013 * this context. This will very seldomly be used directly. Instead
3014 * a typical event source will use g_source_add_poll() instead.
3017 g_main_context_add_poll (GMainContext *context,
3022 context = g_main_context_default ();
3024 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3025 g_return_if_fail (fd);
3027 LOCK_CONTEXT (context);
3028 g_main_context_add_poll_unlocked (context, priority, fd);
3029 UNLOCK_CONTEXT (context);
3032 /* HOLDS: main_loop_lock */
3034 g_main_context_add_poll_unlocked (GMainContext *context,
3038 GPollRec *lastrec, *pollrec;
3039 GPollRec *newrec = g_slice_new (GPollRec);
3041 /* This file descriptor may be checked before we ever poll */
3044 newrec->priority = priority;
3047 pollrec = context->poll_records;
3048 while (pollrec && priority >= pollrec->priority)
3051 pollrec = pollrec->next;
3055 lastrec->next = newrec;
3057 context->poll_records = newrec;
3059 newrec->next = pollrec;
3061 context->n_poll_records++;
3063 #ifdef G_THREADS_ENABLED
3064 context->poll_changed = TRUE;
3066 /* Now wake up the main loop if it is waiting in the poll() */
3067 g_main_context_wakeup_unlocked (context);
3072 * g_main_context_remove_poll:
3073 * @context:a #GMainContext
3074 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3076 * Removes file descriptor from the set of file descriptors to be
3077 * polled for a particular context.
3080 g_main_context_remove_poll (GMainContext *context,
3084 context = g_main_context_default ();
3086 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3087 g_return_if_fail (fd);
3089 LOCK_CONTEXT (context);
3090 g_main_context_remove_poll_unlocked (context, fd);
3091 UNLOCK_CONTEXT (context);
3095 g_main_context_remove_poll_unlocked (GMainContext *context,
3098 GPollRec *pollrec, *lastrec;
3101 pollrec = context->poll_records;
3105 if (pollrec->fd == fd)
3107 if (lastrec != NULL)
3108 lastrec->next = pollrec->next;
3110 context->poll_records = pollrec->next;
3112 g_slice_free (GPollRec, pollrec);
3114 context->n_poll_records--;
3118 pollrec = pollrec->next;
3121 #ifdef G_THREADS_ENABLED
3122 context->poll_changed = TRUE;
3124 /* Now wake up the main loop if it is waiting in the poll() */
3125 g_main_context_wakeup_unlocked (context);
3130 * g_source_get_current_time:
3131 * @source: a #GSource
3132 * @timeval: #GTimeVal structure in which to store current time.
3134 * Gets the "current time" to be used when checking
3135 * this source. The advantage of calling this function over
3136 * calling g_get_current_time() directly is that when
3137 * checking multiple sources, GLib can cache a single value
3138 * instead of having to repeatedly get the system time.
3141 g_source_get_current_time (GSource *source,
3144 GMainContext *context;
3146 g_return_if_fail (source->context != NULL);
3148 context = source->context;
3150 LOCK_CONTEXT (context);
3152 if (!context->time_is_current)
3154 g_get_current_time (&context->current_time);
3155 context->time_is_current = TRUE;
3158 *timeval = context->current_time;
3160 UNLOCK_CONTEXT (context);
3164 * g_main_context_set_poll_func:
3165 * @context: a #GMainContext
3166 * @func: the function to call to poll all file descriptors
3168 * Sets the function to use to handle polling of file descriptors. It
3169 * will be used instead of the poll() system call
3170 * (or GLib's replacement function, which is used where
3171 * poll() isn't available).
3173 * This function could possibly be used to integrate the GLib event
3174 * loop with an external event loop.
3177 g_main_context_set_poll_func (GMainContext *context,
3181 context = g_main_context_default ();
3183 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3185 LOCK_CONTEXT (context);
3188 context->poll_func = func;
3192 context->poll_func = (GPollFunc) poll;
3194 context->poll_func = (GPollFunc) g_poll;
3198 UNLOCK_CONTEXT (context);
3202 * g_main_context_get_poll_func:
3203 * @context: a #GMainContext
3205 * Gets the poll function set by g_main_context_set_poll_func().
3207 * Return value: the poll function
3210 g_main_context_get_poll_func (GMainContext *context)
3215 context = g_main_context_default ();
3217 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3219 LOCK_CONTEXT (context);
3220 result = context->poll_func;
3221 UNLOCK_CONTEXT (context);
3226 /* HOLDS: context's lock */
3227 /* Wake the main loop up from a poll() */
3229 g_main_context_wakeup_unlocked (GMainContext *context)
3231 #ifdef G_THREADS_ENABLED
3232 if (g_thread_supported() && context->poll_waiting)
3234 context->poll_waiting = FALSE;
3236 write (context->wake_up_pipe[1], "A", 1);
3238 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3245 * g_main_context_wakeup:
3246 * @context: a #GMainContext
3248 * If @context is currently waiting in a poll(), interrupt
3249 * the poll(), and continue the iteration process.
3252 g_main_context_wakeup (GMainContext *context)
3255 context = g_main_context_default ();
3257 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3259 LOCK_CONTEXT (context);
3260 g_main_context_wakeup_unlocked (context);
3261 UNLOCK_CONTEXT (context);
3265 * g_main_context_is_owner:
3266 * @context: a #GMainContext
3268 * Determines whether this thread holds the (recursive)
3269 * ownership of this #GMaincontext. This is useful to
3270 * know before waiting on another thread that may be
3271 * blocking to get ownership of @context.
3273 * Returns: %TRUE if current thread is owner of @context.
3278 g_main_context_is_owner (GMainContext *context)
3283 context = g_main_context_default ();
3285 #ifdef G_THREADS_ENABLED
3286 LOCK_CONTEXT (context);
3287 is_owner = context->owner == G_THREAD_SELF;
3288 UNLOCK_CONTEXT (context);
3299 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3300 GTimeVal *current_time)
3302 guint seconds = timeout_source->interval / 1000;
3303 guint msecs = timeout_source->interval - seconds * 1000;
3305 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3306 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3307 if (timeout_source->expiration.tv_usec >= 1000000)
3309 timeout_source->expiration.tv_usec -= 1000000;
3310 timeout_source->expiration.tv_sec++;
3312 if (timer_perturb==-1)
3315 * we want a per machine/session unique 'random' value; try the dbus
3316 * address first, that has a UUID in it. If there is no dbus, use the
3317 * hostname for hashing.
3319 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3320 if (!session_bus_address)
3321 session_bus_address = g_getenv ("HOSTNAME");
3322 if (session_bus_address)
3323 timer_perturb = ABS (g_str_hash (session_bus_address));
3327 if (timeout_source->granularity)
3330 gint gran; /* in usecs */
3333 gran = timeout_source->granularity * 1000;
3334 perturb = timer_perturb % gran;
3336 * We want to give each machine a per machine pertubation;
3337 * shift time back first, and forward later after the rounding
3340 timeout_source->expiration.tv_usec -= perturb;
3341 if (timeout_source->expiration.tv_usec < 0)
3343 timeout_source->expiration.tv_usec += 1000000;
3344 timeout_source->expiration.tv_sec--;
3347 remainder = timeout_source->expiration.tv_usec % gran;
3348 if (remainder >= gran/4) /* round up */
3349 timeout_source->expiration.tv_usec += gran;
3350 timeout_source->expiration.tv_usec -= remainder;
3352 timeout_source->expiration.tv_usec += perturb;
3354 /* the rounding may have overflown tv_usec */
3355 while (timeout_source->expiration.tv_usec > 1000000)
3357 timeout_source->expiration.tv_usec -= 1000000;
3358 timeout_source->expiration.tv_sec++;
3364 g_timeout_prepare (GSource *source,
3369 GTimeVal current_time;
3371 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3373 g_source_get_current_time (source, ¤t_time);
3375 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3376 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3378 /* We do the following in a rather convoluted fashion to deal with
3379 * the fact that we don't have an integral type big enough to hold
3380 * the difference of two timevals in millseconds.
3382 if (sec < 0 || (sec == 0 && msec < 0))
3386 glong interval_sec = timeout_source->interval / 1000;
3387 glong interval_msec = timeout_source->interval % 1000;
3395 if (sec > interval_sec ||
3396 (sec == interval_sec && msec > interval_msec))
3398 /* The system time has been set backwards, so we
3399 * reset the expiration time to now + timeout_source->interval;
3400 * this at least avoids hanging for long periods of time.
3402 g_timeout_set_expiration (timeout_source, ¤t_time);
3403 msec = MIN (G_MAXINT, timeout_source->interval);
3407 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3411 *timeout = (gint)msec;
3417 g_timeout_check (GSource *source)
3419 GTimeVal current_time;
3420 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3422 g_source_get_current_time (source, ¤t_time);
3424 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3425 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3426 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3430 g_timeout_dispatch (GSource *source,
3431 GSourceFunc callback,
3434 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3438 g_warning ("Timeout source dispatched without callback\n"
3439 "You must call g_source_set_callback().");
3443 if (callback (user_data))
3445 GTimeVal current_time;
3447 g_source_get_current_time (source, ¤t_time);
3448 g_timeout_set_expiration (timeout_source, ¤t_time);
3457 * g_timeout_source_new:
3458 * @interval: the timeout interval in milliseconds.
3460 * Creates a new timeout source.
3462 * The source will not initially be associated with any #GMainContext
3463 * and must be added to one with g_source_attach() before it will be
3466 * Return value: the newly-created timeout source
3469 g_timeout_source_new (guint interval)
3471 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3472 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3473 GTimeVal current_time;
3475 timeout_source->interval = interval;
3477 g_get_current_time (¤t_time);
3478 g_timeout_set_expiration (timeout_source, ¤t_time);
3484 * g_timeout_source_new_seconds:
3485 * @interval: the timeout interval in seconds
3487 * Creates a new timeout source.
3489 * The source will not initially be associated with any #GMainContext
3490 * and must be added to one with g_source_attach() before it will be
3493 * The scheduling granularity/accuracy of this timeout source will be
3496 * Return value: the newly-created timeout source
3501 g_timeout_source_new_seconds (guint interval)
3503 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3504 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3505 GTimeVal current_time;
3507 timeout_source->interval = 1000*interval;
3508 timeout_source->granularity = 1000;
3510 g_get_current_time (¤t_time);
3511 g_timeout_set_expiration (timeout_source, ¤t_time);
3518 * g_timeout_add_full:
3519 * @priority: the priority of the timeout source. Typically this will be in
3520 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3521 * @interval: the time between calls to the function, in milliseconds
3522 * (1/1000ths of a second)
3523 * @function: function to call
3524 * @data: data to pass to @function
3525 * @notify: function to call when the timeout is removed, or %NULL
3527 * Sets a function to be called at regular intervals, with the given
3528 * priority. The function is called repeatedly until it returns
3529 * %FALSE, at which point the timeout is automatically destroyed and
3530 * the function will not be called again. The @notify function is
3531 * called when the timeout is destroyed. The first call to the
3532 * function will be at the end of the first @interval.
3534 * Note that timeout functions may be delayed, due to the processing of other
3535 * event sources. Thus they should not be relied on for precise timing.
3536 * After each call to the timeout function, the time of the next
3537 * timeout is recalculated based on the current time and the given interval
3538 * (it does not try to 'catch up' time lost in delays).
3540 * Return value: the ID (greater than 0) of the event source.
3543 g_timeout_add_full (gint priority,
3545 GSourceFunc function,
3547 GDestroyNotify notify)
3552 g_return_val_if_fail (function != NULL, 0);
3554 source = g_timeout_source_new (interval);
3556 if (priority != G_PRIORITY_DEFAULT)
3557 g_source_set_priority (source, priority);
3559 g_source_set_callback (source, function, data, notify);
3560 id = g_source_attach (source, NULL);
3561 g_source_unref (source);
3568 * @interval: the time between calls to the function, in milliseconds
3569 * (1/1000ths of a second)
3570 * @function: function to call
3571 * @data: data to pass to @function
3573 * Sets a function to be called at regular intervals, with the default
3574 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3575 * until it returns %FALSE, at which point the timeout is automatically
3576 * destroyed and the function will not be called again. The first call
3577 * to the function will be at the end of the first @interval.
3579 * Note that timeout functions may be delayed, due to the processing of other
3580 * event sources. Thus they should not be relied on for precise timing.
3581 * After each call to the timeout function, the time of the next
3582 * timeout is recalculated based on the current time and the given interval
3583 * (it does not try to 'catch up' time lost in delays).
3585 * If you want to have a timer in the "seconds" range and do not care
3586 * about the exact time of the first call of the timer, use the
3587 * g_timeout_add_seconds() function; this function allows for more
3588 * optimizations and more efficient system power usage.
3590 * Return value: the ID (greater than 0) of the event source.
3593 g_timeout_add (guint32 interval,
3594 GSourceFunc function,
3597 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3598 interval, function, data, NULL);
3602 * g_timeout_add_seconds_full:
3603 * @priority: the priority of the timeout source. Typically this will be in
3604 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3605 * @interval: the time between calls to the function, in seconds
3606 * @function: function to call
3607 * @data: data to pass to @function
3608 * @notify: function to call when the timeout is removed, or %NULL
3610 * Sets a function to be called at regular intervals, with @priority.
3611 * The function is called repeatedly until it returns %FALSE, at which
3612 * point the timeout is automatically destroyed and the function will
3613 * not be called again.
3615 * Unlike g_timeout_add(), this function operates at whole second granularity.
3616 * The initial starting point of the timer is determined by the implementation
3617 * and the implementation is expected to group multiple timers together so that
3618 * they fire all at the same time.
3619 * To allow this grouping, the @interval to the first timer is rounded
3620 * and can deviate up to one second from the specified interval.
3621 * Subsequent timer iterations will generally run at the specified interval.
3623 * Note that timeout functions may be delayed, due to the processing of other
3624 * event sources. Thus they should not be relied on for precise timing.
3625 * After each call to the timeout function, the time of the next
3626 * timeout is recalculated based on the current time and the given @interval
3628 * If you want timing more precise than whole seconds, use g_timeout_add()
3631 * The grouping of timers to fire at the same time results in a more power
3632 * and CPU efficient behavior so if your timer is in multiples of seconds
3633 * and you don't require the first timer exactly one second from now, the
3634 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3636 * Return value: the ID (greater than 0) of the event source.
3641 g_timeout_add_seconds_full (gint priority,
3643 GSourceFunc function,
3645 GDestroyNotify notify)
3650 g_return_val_if_fail (function != NULL, 0);
3652 source = g_timeout_source_new_seconds (interval);
3654 if (priority != G_PRIORITY_DEFAULT)
3655 g_source_set_priority (source, priority);
3657 g_source_set_callback (source, function, data, notify);
3658 id = g_source_attach (source, NULL);
3659 g_source_unref (source);
3665 * g_timeout_add_seconds:
3666 * @interval: the time between calls to the function, in seconds
3667 * @function: function to call
3668 * @data: data to pass to @function
3670 * Sets a function to be called at regular intervals with the default
3671 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3672 * it returns %FALSE, at which point the timeout is automatically destroyed
3673 * and the function will not be called again.
3675 * See g_timeout_add_seconds_full() for the differences between
3676 * g_timeout_add() and g_timeout_add_seconds().
3678 * Return value: the ID (greater than 0) of the event source.
3683 g_timeout_add_seconds (guint interval,
3684 GSourceFunc function,
3687 g_return_val_if_fail (function != NULL, 0);
3689 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3692 /* Child watch functions */
3697 g_child_watch_prepare (GSource *source,
3706 g_child_watch_check (GSource *source)
3708 GChildWatchSource *child_watch_source;
3709 gboolean child_exited;
3711 child_watch_source = (GChildWatchSource *) source;
3713 child_exited = child_watch_source->poll.revents & G_IO_IN;
3720 * Note: We do _not_ check for the special value of STILL_ACTIVE
3721 * since we know that the process has exited and doing so runs into
3722 * problems if the child process "happens to return STILL_ACTIVE(259)"
3723 * as Microsoft's Platform SDK puts it.
3725 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3727 gchar *emsg = g_win32_error_message (GetLastError ());
3728 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3731 child_watch_source->child_status = -1;
3734 child_watch_source->child_status = child_status;
3737 return child_exited;
3740 #else /* G_OS_WIN32 */
3743 check_for_child_exited (GSource *source)
3745 GChildWatchSource *child_watch_source;
3748 /* protect against another SIGCHLD in the middle of this call */
3749 count = child_watch_count;
3751 child_watch_source = (GChildWatchSource *) source;
3753 if (child_watch_source->child_exited)
3756 if (child_watch_source->count < count)
3760 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3762 child_watch_source->child_status = child_status;
3763 child_watch_source->child_exited = TRUE;
3765 child_watch_source->count = count;
3768 return child_watch_source->child_exited;
3772 g_child_watch_prepare (GSource *source,
3777 return check_for_child_exited (source);
3782 g_child_watch_check (GSource *source)
3784 return check_for_child_exited (source);
3787 #endif /* G_OS_WIN32 */
3790 g_child_watch_dispatch (GSource *source,
3791 GSourceFunc callback,
3794 GChildWatchSource *child_watch_source;
3795 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3797 child_watch_source = (GChildWatchSource *) source;
3801 g_warning ("Child watch source dispatched without callback\n"
3802 "You must call g_source_set_callback().");
3806 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3808 /* We never keep a child watch source around as the child is gone */
3815 g_child_watch_signal_handler (int signum)
3817 child_watch_count ++;
3819 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3821 write (child_watch_wake_up_pipe[1], "B", 1);
3825 /* We count on the signal interrupting the poll in the same thread.
3831 g_child_watch_source_init_single (void)
3833 struct sigaction action;
3835 g_assert (! g_thread_supported());
3836 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3838 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3840 action.sa_handler = g_child_watch_signal_handler;
3841 sigemptyset (&action.sa_mask);
3842 action.sa_flags = SA_NOCLDSTOP;
3843 sigaction (SIGCHLD, &action, NULL);
3847 child_watch_helper_thread (gpointer data)
3854 read (child_watch_wake_up_pipe[0], b, 20);
3856 /* We were woken up. Wake up all other contexts in all other threads */
3857 G_LOCK (main_context_list);
3858 for (list = main_context_list; list; list = list->next)
3860 GMainContext *context;
3862 context = list->data;
3863 if (g_atomic_int_get (&context->ref_count) > 0)
3864 /* Due to racing conditions we can find ref_count == 0, in
3865 * that case, however, the context is still not destroyed
3866 * and no poll can be active, otherwise the ref_count
3868 g_main_context_wakeup (context);
3870 G_UNLOCK (main_context_list);
3877 g_child_watch_source_init_multi_threaded (void)
3879 GError *error = NULL;
3880 struct sigaction action;
3882 g_assert (g_thread_supported());
3884 if (pipe (child_watch_wake_up_pipe) < 0)
3885 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3886 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3888 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3889 /* FIXME: Think this through for races */
3890 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3891 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3892 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3894 action.sa_handler = g_child_watch_signal_handler;
3895 sigemptyset (&action.sa_mask);
3896 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3897 sigaction (SIGCHLD, &action, NULL);
3901 g_child_watch_source_init_promote_single_to_threaded (void)
3903 g_child_watch_source_init_multi_threaded ();
3907 g_child_watch_source_init (void)
3909 if (g_thread_supported())
3911 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3912 g_child_watch_source_init_multi_threaded ();
3913 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3914 g_child_watch_source_init_promote_single_to_threaded ();
3918 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3919 g_child_watch_source_init_single ();
3923 #endif /* !G_OS_WIN32 */
3926 * g_child_watch_source_new:
3927 * @pid: process id of a child process to watch. On Windows, a HANDLE
3928 * for the process to watch (which actually doesn't have to be a child).
3930 * Creates a new child_watch source.
3932 * The source will not initially be associated with any #GMainContext
3933 * and must be added to one with g_source_attach() before it will be
3936 * Note that child watch sources can only be used in conjunction with
3937 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
3940 * Note that on platforms where #GPid must be explicitly closed
3941 * (see g_spawn_close_pid()) @pid must not be closed while the
3942 * source is still active. Typically, you will want to call
3943 * g_spawn_close_pid() in the callback function for the source.
3945 * Note further that using g_child_watch_source_new() is not
3946 * compatible with calling <literal>waitpid(-1)</literal> in
3947 * the application. Calling waitpid() for individual pids will
3950 * Return value: the newly-created child watch source
3955 g_child_watch_source_new (GPid pid)
3957 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
3958 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
3961 child_watch_source->poll.fd = (int)pid;
3962 child_watch_source->poll.events = G_IO_IN;
3964 g_source_add_poll (source, &child_watch_source->poll);
3965 #else /* G_OS_WIN32 */
3966 g_child_watch_source_init ();
3967 #endif /* G_OS_WIN32 */
3969 child_watch_source->pid = pid;
3975 * g_child_watch_add_full:
3976 * @priority: the priority of the idle source. Typically this will be in the
3977 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3978 * @pid: process id of a child process to watch
3979 * @function: function to call
3980 * @data: data to pass to @function
3981 * @notify: function to call when the idle is removed, or %NULL
3983 * Sets a function to be called when the child indicated by @pid
3984 * exits, at the priority @priority.
3986 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
3987 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
3988 * the spawn function for the child watching to work.
3990 * Note that on platforms where #GPid must be explicitly closed
3991 * (see g_spawn_close_pid()) @pid must not be closed while the
3992 * source is still active. Typically, you will want to call
3993 * g_spawn_close_pid() in the callback function for the source.
3995 * GLib supports only a single callback per process id.
3997 * Return value: the ID (greater than 0) of the event source.
4002 g_child_watch_add_full (gint priority,
4004 GChildWatchFunc function,
4006 GDestroyNotify notify)
4011 g_return_val_if_fail (function != NULL, 0);
4013 source = g_child_watch_source_new (pid);
4015 if (priority != G_PRIORITY_DEFAULT)
4016 g_source_set_priority (source, priority);
4018 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4019 id = g_source_attach (source, NULL);
4020 g_source_unref (source);
4026 * g_child_watch_add:
4027 * @pid: process id of a child process to watch
4028 * @function: function to call
4029 * @data: data to pass to @function
4031 * Sets a function to be called when the child indicated by @pid
4032 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4034 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4035 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4036 * the spawn function for the child watching to work.
4038 * Note that on platforms where #GPid must be explicitly closed
4039 * (see g_spawn_close_pid()) @pid must not be closed while the
4040 * source is still active. Typically, you will want to call
4041 * g_spawn_close_pid() in the callback function for the source.
4043 * GLib supports only a single callback per process id.
4045 * Return value: the ID (greater than 0) of the event source.
4050 g_child_watch_add (GPid pid,
4051 GChildWatchFunc function,
4054 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4058 /* Idle functions */
4061 g_idle_prepare (GSource *source,
4070 g_idle_check (GSource *source)
4076 g_idle_dispatch (GSource *source,
4077 GSourceFunc callback,
4082 g_warning ("Idle source dispatched without callback\n"
4083 "You must call g_source_set_callback().");
4087 return callback (user_data);
4091 * g_idle_source_new:
4093 * Creates a new idle source.
4095 * The source will not initially be associated with any #GMainContext
4096 * and must be added to one with g_source_attach() before it will be
4097 * executed. Note that the default priority for idle sources is
4098 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4099 * have a default priority of %G_PRIORITY_DEFAULT.
4101 * Return value: the newly-created idle source
4104 g_idle_source_new (void)
4108 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4109 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4116 * @priority: the priority of the idle source. Typically this will be in the
4117 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4118 * @function: function to call
4119 * @data: data to pass to @function
4120 * @notify: function to call when the idle is removed, or %NULL
4122 * Adds a function to be called whenever there are no higher priority
4123 * events pending. If the function returns %FALSE it is automatically
4124 * removed from the list of event sources and will not be called again.
4126 * Return value: the ID (greater than 0) of the event source.
4129 g_idle_add_full (gint priority,
4130 GSourceFunc function,
4132 GDestroyNotify notify)
4137 g_return_val_if_fail (function != NULL, 0);
4139 source = g_idle_source_new ();
4141 if (priority != G_PRIORITY_DEFAULT_IDLE)
4142 g_source_set_priority (source, priority);
4144 g_source_set_callback (source, function, data, notify);
4145 id = g_source_attach (source, NULL);
4146 g_source_unref (source);
4153 * @function: function to call
4154 * @data: data to pass to @function.
4156 * Adds a function to be called whenever there are no higher priority
4157 * events pending to the default main loop. The function is given the
4158 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4159 * returns %FALSE it is automatically removed from the list of event
4160 * sources and will not be called again.
4162 * Return value: the ID (greater than 0) of the event source.
4165 g_idle_add (GSourceFunc function,
4168 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4172 * g_idle_remove_by_data:
4173 * @data: the data for the idle source's callback.
4175 * Removes the idle function with the given data.
4177 * Return value: %TRUE if an idle source was found and removed.
4180 g_idle_remove_by_data (gpointer data)
4182 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4185 #define __G_MAIN_C__
4186 #include "galiasdef.c"