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;
334 for (f = fds; f < &fds[nfds]; ++f)
337 if (f->fd == G_WIN32_MSG_HANDLE)
339 else if (nhandles == MAXIMUM_WAIT_OBJECTS)
341 g_warning (G_STRLOC ": Too many handles to wait for!\n");
346 #ifdef G_MAIN_POLL_DEBUG
347 g_print ("g_poll: waiting for %#x\n", f->fd);
349 handles[nhandles++] = (HANDLE) f->fd;
358 /* Waiting for messages, and maybe events
359 * -> First PeekMessage
361 #ifdef G_MAIN_POLL_DEBUG
362 g_print ("PeekMessage\n");
364 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
365 ready = WAIT_OBJECT_0 + nhandles;
370 /* Waiting just for messages */
371 if (timeout == INFINITE)
376 #ifdef G_MAIN_POLL_DEBUG
377 g_print ("WaitMessage\n");
381 gchar *emsg = g_win32_error_message (GetLastError ());
382 g_warning (G_STRLOC ": WaitMessage() failed: %s", emsg);
385 ready = WAIT_OBJECT_0 + nhandles;
387 else if (timeout == 0)
389 /* Waiting just for messages, zero timeout.
390 * If we got here, there was no message
392 ready = WAIT_TIMEOUT;
396 /* Waiting just for messages, some timeout
397 * -> Set a timer, wait for message,
398 * kill timer, use PeekMessage
400 timer = SetTimer (NULL, 0, timeout, NULL);
403 gchar *emsg = g_win32_error_message (GetLastError ());
404 g_warning (G_STRLOC ": SetTimer() failed: %s", emsg);
406 ready = WAIT_TIMEOUT;
410 #ifdef G_MAIN_POLL_DEBUG
411 g_print ("WaitMessage\n");
414 KillTimer (NULL, timer);
415 #ifdef G_MAIN_POLL_DEBUG
416 g_print ("PeekMessage\n");
418 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
419 && msg.message != WM_TIMER)
420 ready = WAIT_OBJECT_0;
422 ready = WAIT_TIMEOUT;
428 /* Wait for either message or event
429 * -> Use MsgWaitForMultipleObjects
431 #ifdef G_MAIN_POLL_DEBUG
432 g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
434 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
435 timeout, QS_ALLINPUT);
437 if (ready == WAIT_FAILED)
439 gchar *emsg = g_win32_error_message (GetLastError ());
440 g_warning (G_STRLOC ": MsgWaitForMultipleObjects() failed: %s", emsg);
446 else if (nhandles == 0)
448 /* Wait for nothing (huh?) */
453 /* Wait for just events
454 * -> Use WaitForMultipleObjects
456 #ifdef G_MAIN_POLL_DEBUG
457 g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
459 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
460 if (ready == WAIT_FAILED)
462 gchar *emsg = g_win32_error_message (GetLastError ());
463 g_warning (G_STRLOC ": WaitForMultipleObjects() failed: %s", emsg);
468 #ifdef G_MAIN_POLL_DEBUG
469 g_print ("wait returns %ld%s\n",
471 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
472 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
473 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
475 for (f = fds; f < &fds[nfds]; ++f)
478 if (ready == WAIT_FAILED)
480 else if (ready == WAIT_TIMEOUT)
482 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
484 for (f = fds; f < &fds[nfds]; ++f)
487 if (f->events & G_IO_IN)
488 if (f->fd == G_WIN32_MSG_HANDLE)
489 f->revents |= G_IO_IN;
492 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
493 for (f = fds; f < &fds[nfds]; ++f)
495 if (f->fd == (gint) handles[ready - WAIT_OBJECT_0])
497 f->revents = f->events;
498 #ifdef G_MAIN_POLL_DEBUG
499 g_print ("g_poll: got event %#x\n", f->fd);
507 #else /* !G_OS_WIN32 */
509 /* The following implementation of poll() comes from the GNU C Library.
510 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
513 #include <string.h> /* for bzero on BSD systems */
515 #ifdef HAVE_SYS_SELECT_H
516 #include <sys/select.h>
517 #endif /* HAVE_SYS_SELECT_H */
521 #endif /* G_OS_BEOS */
524 # define SELECT_MASK fd_set
525 #else /* !NO_FD_SET */
527 typedef long fd_mask;
530 # define SELECT_MASK void
532 # define SELECT_MASK int
533 # endif /* !_IBMR2 */
534 #endif /* !NO_FD_SET */
537 g_poll (GPollFD *fds,
542 SELECT_MASK rset, wset, xset;
551 for (f = fds; f < &fds[nfds]; ++f)
554 if (f->events & G_IO_IN)
555 FD_SET (f->fd, &rset);
556 if (f->events & G_IO_OUT)
557 FD_SET (f->fd, &wset);
558 if (f->events & G_IO_PRI)
559 FD_SET (f->fd, &xset);
560 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
564 tv.tv_sec = timeout / 1000;
565 tv.tv_usec = (timeout % 1000) * 1000;
567 ready = select (maxfd + 1, &rset, &wset, &xset,
568 timeout == -1 ? NULL : &tv);
570 for (f = fds; f < &fds[nfds]; ++f)
575 if (FD_ISSET (f->fd, &rset))
576 f->revents |= G_IO_IN;
577 if (FD_ISSET (f->fd, &wset))
578 f->revents |= G_IO_OUT;
579 if (FD_ISSET (f->fd, &xset))
580 f->revents |= G_IO_PRI;
587 #endif /* !G_OS_WIN32 */
589 #endif /* !HAVE_POLL */
592 * g_main_context_ref:
593 * @context: a #GMainContext
595 * Increases the reference count on a #GMainContext object by one.
597 * Returns: the @context that was passed in (since 2.6)
600 g_main_context_ref (GMainContext *context)
602 g_return_val_if_fail (context != NULL, NULL);
603 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
605 g_atomic_int_inc (&context->ref_count);
611 poll_rec_list_free (GMainContext *context,
614 g_slice_free_chain (GPollRec, list, next);
618 * g_main_context_unref:
619 * @context: a #GMainContext
621 * Decreases the reference count on a #GMainContext object by one. If
622 * the result is zero, free the context and free all associated memory.
625 g_main_context_unref (GMainContext *context)
628 g_return_if_fail (context != NULL);
629 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
631 if (!g_atomic_int_dec_and_test (&context->ref_count))
634 G_LOCK (main_context_list);
635 main_context_list = g_slist_remove (main_context_list, context);
636 G_UNLOCK (main_context_list);
638 source = context->source_list;
641 GSource *next = source->next;
642 g_source_destroy_internal (source, context, FALSE);
646 #ifdef G_THREADS_ENABLED
647 g_static_mutex_free (&context->mutex);
650 g_ptr_array_free (context->pending_dispatches, TRUE);
651 g_free (context->cached_poll_array);
653 poll_rec_list_free (context, context->poll_records);
655 #ifdef G_THREADS_ENABLED
656 if (g_thread_supported())
659 close (context->wake_up_pipe[0]);
660 close (context->wake_up_pipe[1]);
662 CloseHandle (context->wake_up_semaphore);
666 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
673 #ifdef G_THREADS_ENABLED
675 g_main_context_init_pipe (GMainContext *context)
678 if (context->wake_up_pipe[0] != -1)
680 if (pipe (context->wake_up_pipe) < 0)
681 g_error ("Cannot create pipe main loop wake-up: %s\n",
684 context->wake_up_rec.fd = context->wake_up_pipe[0];
685 context->wake_up_rec.events = G_IO_IN;
687 if (context->wake_up_semaphore != NULL)
689 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
690 if (context->wake_up_semaphore == NULL)
691 g_error ("Cannot create wake-up semaphore: %s",
692 g_win32_error_message (GetLastError ()));
693 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
694 context->wake_up_rec.events = G_IO_IN;
695 # ifdef G_MAIN_POLL_DEBUG
696 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
699 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
703 _g_main_thread_init (void)
705 GSList *curr = main_contexts_without_pipe;
708 g_main_context_init_pipe ((GMainContext *)curr->data);
711 g_slist_free (main_contexts_without_pipe);
712 main_contexts_without_pipe = NULL;
714 #endif /* G_THREADS_ENABLED */
717 * g_main_context_new:
719 * Creates a new #GMainContext strcuture
721 * Return value: the new #GMainContext
724 g_main_context_new (void)
726 GMainContext *context = g_new0 (GMainContext, 1);
728 #ifdef G_THREADS_ENABLED
729 g_static_mutex_init (&context->mutex);
731 context->owner = NULL;
732 context->waiters = NULL;
735 context->wake_up_pipe[0] = -1;
736 context->wake_up_pipe[1] = -1;
738 context->wake_up_semaphore = NULL;
742 context->ref_count = 1;
744 context->next_id = 1;
746 context->source_list = NULL;
749 context->poll_func = (GPollFunc)poll;
751 context->poll_func = g_poll;
754 context->cached_poll_array = NULL;
755 context->cached_poll_array_size = 0;
757 context->pending_dispatches = g_ptr_array_new ();
759 context->time_is_current = FALSE;
761 #ifdef G_THREADS_ENABLED
762 if (g_thread_supported ())
763 g_main_context_init_pipe (context);
765 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
769 G_LOCK (main_context_list);
770 main_context_list = g_slist_append (main_context_list, context);
771 G_UNLOCK (main_context_list);
777 * g_main_context_default:
779 * Returns the default main context. This is the main context used
780 * for main loop functions when a main loop is not explicitly
783 * Return value: the default main context.
786 g_main_context_default (void)
792 if (!default_main_context)
793 default_main_context = g_main_context_new ();
795 G_UNLOCK (main_loop);
797 return default_main_context;
800 /* Hooks for adding to the main loop */
804 * @source_funcs: structure containing functions that implement
805 * the sources behavior.
806 * @struct_size: size of the #GSource structure to create.
808 * Creates a new #GSource structure. The size is specified to
809 * allow creating structures derived from #GSource that contain
810 * additional data. The size passed in must be at least
811 * <literal>sizeof (GSource)</literal>.
813 * The source will not initially be associated with any #GMainContext
814 * and must be added to one with g_source_attach() before it will be
817 * Return value: the newly-created #GSource.
820 g_source_new (GSourceFuncs *source_funcs,
825 g_return_val_if_fail (source_funcs != NULL, NULL);
826 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
828 source = (GSource*) g_malloc0 (struct_size);
830 source->source_funcs = source_funcs;
831 source->ref_count = 1;
833 source->priority = G_PRIORITY_DEFAULT;
835 source->flags = G_HOOK_FLAG_ACTIVE;
837 /* NULL/0 initialization for all other fields */
842 /* Holds context's lock
845 g_source_list_add (GSource *source,
846 GMainContext *context)
848 GSource *tmp_source, *last_source;
851 tmp_source = context->source_list;
852 while (tmp_source && tmp_source->priority <= source->priority)
854 last_source = tmp_source;
855 tmp_source = tmp_source->next;
858 source->next = tmp_source;
860 tmp_source->prev = source;
862 source->prev = last_source;
864 last_source->next = source;
866 context->source_list = source;
869 /* Holds context's lock
872 g_source_list_remove (GSource *source,
873 GMainContext *context)
876 source->prev->next = source->next;
878 context->source_list = source->next;
881 source->next->prev = source->prev;
889 * @source: a #GSource
890 * @context: a #GMainContext (if %NULL, the default context will be used)
892 * Adds a #GSource to a @context so that it will be executed within
895 * Return value: the ID (greater than 0) for the source within the
899 g_source_attach (GSource *source,
900 GMainContext *context)
905 g_return_val_if_fail (source->context == NULL, 0);
906 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
909 context = g_main_context_default ();
911 LOCK_CONTEXT (context);
913 source->context = context;
914 result = source->source_id = context->next_id++;
917 g_source_list_add (source, context);
919 tmp_list = source->poll_fds;
922 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
923 tmp_list = tmp_list->next;
926 #ifdef G_THREADS_ENABLED
927 /* Now wake up the main loop if it is waiting in the poll() */
928 g_main_context_wakeup_unlocked (context);
931 UNLOCK_CONTEXT (context);
937 g_source_destroy_internal (GSource *source,
938 GMainContext *context,
942 LOCK_CONTEXT (context);
944 if (!SOURCE_DESTROYED (source))
947 gpointer old_cb_data;
948 GSourceCallbackFuncs *old_cb_funcs;
950 source->flags &= ~G_HOOK_FLAG_ACTIVE;
952 old_cb_data = source->callback_data;
953 old_cb_funcs = source->callback_funcs;
955 source->callback_data = NULL;
956 source->callback_funcs = NULL;
960 UNLOCK_CONTEXT (context);
961 old_cb_funcs->unref (old_cb_data);
962 LOCK_CONTEXT (context);
965 if (!SOURCE_BLOCKED (source))
967 tmp_list = source->poll_fds;
970 g_main_context_remove_poll_unlocked (context, tmp_list->data);
971 tmp_list = tmp_list->next;
975 g_source_unref_internal (source, context, TRUE);
979 UNLOCK_CONTEXT (context);
984 * @source: a #GSource
986 * Removes a source from its #GMainContext, if any, and mark it as
987 * destroyed. The source cannot be subsequently added to another
991 g_source_destroy (GSource *source)
993 GMainContext *context;
995 g_return_if_fail (source != NULL);
997 context = source->context;
1000 g_source_destroy_internal (source, context, FALSE);
1002 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1007 * @source: a #GSource
1009 * Returns the numeric ID for a particular source. The ID of a source
1010 * is a positive integer which is unique within a particular main loop
1011 * context. The reverse
1012 * mapping from ID to source is done by g_main_context_find_source_by_id().
1014 * Return value: the ID (greater than 0) for the source
1017 g_source_get_id (GSource *source)
1021 g_return_val_if_fail (source != NULL, 0);
1022 g_return_val_if_fail (source->context != NULL, 0);
1024 LOCK_CONTEXT (source->context);
1025 result = source->source_id;
1026 UNLOCK_CONTEXT (source->context);
1032 * g_source_get_context:
1033 * @source: a #GSource
1035 * Gets the #GMainContext with which the source is associated.
1036 * Calling this function on a destroyed source is an error.
1038 * Return value: the #GMainContext with which the source is associated,
1039 * or %NULL if the context has not yet been added
1043 g_source_get_context (GSource *source)
1045 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
1047 return source->context;
1051 * g_source_add_poll:
1052 * @source:a #GSource
1053 * @fd: a #GPollFD structure holding information about a file
1054 * descriptor to watch.
1056 * Adds a file descriptor to the set of file descriptors polled for
1057 * this source. This is usually combined with g_source_new() to add an
1058 * event source. The event source's check function will typically test
1059 * the @revents field in the #GPollFD struct and return %TRUE if events need
1063 g_source_add_poll (GSource *source,
1066 GMainContext *context;
1068 g_return_if_fail (source != NULL);
1069 g_return_if_fail (fd != NULL);
1070 g_return_if_fail (!SOURCE_DESTROYED (source));
1072 context = source->context;
1075 LOCK_CONTEXT (context);
1077 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1081 if (!SOURCE_BLOCKED (source))
1082 g_main_context_add_poll_unlocked (context, source->priority, fd);
1083 UNLOCK_CONTEXT (context);
1088 * g_source_remove_poll:
1089 * @source:a #GSource
1090 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1092 * Removes a file descriptor from the set of file descriptors polled for
1096 g_source_remove_poll (GSource *source,
1099 GMainContext *context;
1101 g_return_if_fail (source != NULL);
1102 g_return_if_fail (fd != NULL);
1103 g_return_if_fail (!SOURCE_DESTROYED (source));
1105 context = source->context;
1108 LOCK_CONTEXT (context);
1110 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1114 if (!SOURCE_BLOCKED (source))
1115 g_main_context_remove_poll_unlocked (context, fd);
1116 UNLOCK_CONTEXT (context);
1121 * g_source_set_callback_indirect:
1122 * @source: the source
1123 * @callback_data: pointer to callback data "object"
1124 * @callback_funcs: functions for reference counting @callback_data
1125 * and getting the callback and data
1127 * Sets the callback function storing the data as a refcounted callback
1128 * "object". This is used internally. Note that calling
1129 * g_source_set_callback_indirect() assumes
1130 * an initial reference count on @callback_data, and thus
1131 * @callback_funcs->unref will eventually be called once more
1132 * than @callback_funcs->ref.
1135 g_source_set_callback_indirect (GSource *source,
1136 gpointer callback_data,
1137 GSourceCallbackFuncs *callback_funcs)
1139 GMainContext *context;
1140 gpointer old_cb_data;
1141 GSourceCallbackFuncs *old_cb_funcs;
1143 g_return_if_fail (source != NULL);
1144 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1146 context = source->context;
1149 LOCK_CONTEXT (context);
1151 old_cb_data = source->callback_data;
1152 old_cb_funcs = source->callback_funcs;
1154 source->callback_data = callback_data;
1155 source->callback_funcs = callback_funcs;
1158 UNLOCK_CONTEXT (context);
1161 old_cb_funcs->unref (old_cb_data);
1165 g_source_callback_ref (gpointer cb_data)
1167 GSourceCallback *callback = cb_data;
1169 callback->ref_count++;
1174 g_source_callback_unref (gpointer cb_data)
1176 GSourceCallback *callback = cb_data;
1178 callback->ref_count--;
1179 if (callback->ref_count == 0)
1181 if (callback->notify)
1182 callback->notify (callback->data);
1188 g_source_callback_get (gpointer cb_data,
1193 GSourceCallback *callback = cb_data;
1195 *func = callback->func;
1196 *data = callback->data;
1199 static GSourceCallbackFuncs g_source_callback_funcs = {
1200 g_source_callback_ref,
1201 g_source_callback_unref,
1202 g_source_callback_get,
1206 * g_source_set_callback:
1207 * @source: the source
1208 * @func: a callback function
1209 * @data: the data to pass to callback function
1210 * @notify: a function to call when @data is no longer in use, or %NULL.
1212 * Sets the callback function for a source. The callback for a source is
1213 * called from the source's dispatch function.
1215 * The exact type of @func depends on the type of source; ie. you
1216 * should not count on @func being called with @data as its first
1219 * Typically, you won't use this function. Instead use functions specific
1220 * to the type of source you are using.
1223 g_source_set_callback (GSource *source,
1226 GDestroyNotify notify)
1228 GSourceCallback *new_callback;
1230 g_return_if_fail (source != NULL);
1232 new_callback = g_new (GSourceCallback, 1);
1234 new_callback->ref_count = 1;
1235 new_callback->func = func;
1236 new_callback->data = data;
1237 new_callback->notify = notify;
1239 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1244 * g_source_set_funcs:
1245 * @source: a #GSource
1246 * @funcs: the new #GSourceFuncs
1248 * Sets the source functions (can be used to override
1249 * default implementations) of an unattached source.
1254 g_source_set_funcs (GSource *source,
1255 GSourceFuncs *funcs)
1257 g_return_if_fail (source != NULL);
1258 g_return_if_fail (source->context == NULL);
1259 g_return_if_fail (source->ref_count > 0);
1260 g_return_if_fail (funcs != NULL);
1262 source->source_funcs = funcs;
1266 * g_source_set_priority:
1267 * @source: a #GSource
1268 * @priority: the new priority.
1270 * Sets the priority of a source. While the main loop is being
1271 * run, a source will be dispatched if it is ready to be dispatched and no sources
1272 * at a higher (numerically smaller) priority are ready to be dispatched.
1275 g_source_set_priority (GSource *source,
1279 GMainContext *context;
1281 g_return_if_fail (source != NULL);
1283 context = source->context;
1286 LOCK_CONTEXT (context);
1288 source->priority = priority;
1292 /* Remove the source from the context's source and then
1293 * add it back so it is sorted in the correct plcae
1295 g_source_list_remove (source, source->context);
1296 g_source_list_add (source, source->context);
1298 if (!SOURCE_BLOCKED (source))
1300 tmp_list = source->poll_fds;
1303 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1304 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1306 tmp_list = tmp_list->next;
1310 UNLOCK_CONTEXT (source->context);
1315 * g_source_get_priority:
1316 * @source: a #GSource
1318 * Gets the priority of a source.
1320 * Return value: the priority of the source
1323 g_source_get_priority (GSource *source)
1325 g_return_val_if_fail (source != NULL, 0);
1327 return source->priority;
1331 * g_source_set_can_recurse:
1332 * @source: a #GSource
1333 * @can_recurse: whether recursion is allowed for this source
1335 * Sets whether a source can be called recursively. If @can_recurse is
1336 * %TRUE, then while the source is being dispatched then this source
1337 * will be processed normally. Otherwise, all processing of this
1338 * source is blocked until the dispatch function returns.
1341 g_source_set_can_recurse (GSource *source,
1342 gboolean can_recurse)
1344 GMainContext *context;
1346 g_return_if_fail (source != NULL);
1348 context = source->context;
1351 LOCK_CONTEXT (context);
1354 source->flags |= G_SOURCE_CAN_RECURSE;
1356 source->flags &= ~G_SOURCE_CAN_RECURSE;
1359 UNLOCK_CONTEXT (context);
1363 * g_source_get_can_recurse:
1364 * @source: a #GSource
1366 * Checks whether a source is allowed to be called recursively.
1367 * see g_source_set_can_recurse().
1369 * Return value: whether recursion is allowed.
1372 g_source_get_can_recurse (GSource *source)
1374 g_return_val_if_fail (source != NULL, FALSE);
1376 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1381 * @source: a #GSource
1383 * Increases the reference count on a source by one.
1385 * Return value: @source
1388 g_source_ref (GSource *source)
1390 GMainContext *context;
1392 g_return_val_if_fail (source != NULL, NULL);
1394 context = source->context;
1397 LOCK_CONTEXT (context);
1399 source->ref_count++;
1402 UNLOCK_CONTEXT (context);
1407 /* g_source_unref() but possible to call within context lock
1410 g_source_unref_internal (GSource *source,
1411 GMainContext *context,
1414 gpointer old_cb_data = NULL;
1415 GSourceCallbackFuncs *old_cb_funcs = NULL;
1417 g_return_if_fail (source != NULL);
1419 if (!have_lock && context)
1420 LOCK_CONTEXT (context);
1422 source->ref_count--;
1423 if (source->ref_count == 0)
1425 old_cb_data = source->callback_data;
1426 old_cb_funcs = source->callback_funcs;
1428 source->callback_data = NULL;
1429 source->callback_funcs = NULL;
1431 if (context && !SOURCE_DESTROYED (source))
1433 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1434 source->ref_count++;
1437 g_source_list_remove (source, context);
1439 if (source->source_funcs->finalize)
1440 source->source_funcs->finalize (source);
1442 g_slist_free (source->poll_fds);
1443 source->poll_fds = NULL;
1447 if (!have_lock && context)
1448 UNLOCK_CONTEXT (context);
1453 UNLOCK_CONTEXT (context);
1455 old_cb_funcs->unref (old_cb_data);
1458 LOCK_CONTEXT (context);
1464 * @source: a #GSource
1466 * Decreases the reference count of a source by one. If the
1467 * resulting reference count is zero the source and associated
1468 * memory will be destroyed.
1471 g_source_unref (GSource *source)
1473 g_return_if_fail (source != NULL);
1475 g_source_unref_internal (source, source->context, FALSE);
1479 * g_main_context_find_source_by_id:
1480 * @context: a #GMainContext (if %NULL, the default context will be used)
1481 * @source_id: the source ID, as returned by g_source_get_id().
1483 * Finds a #GSource given a pair of context and ID.
1485 * Return value: the #GSource if found, otherwise, %NULL
1488 g_main_context_find_source_by_id (GMainContext *context,
1493 g_return_val_if_fail (source_id > 0, NULL);
1495 if (context == NULL)
1496 context = g_main_context_default ();
1498 LOCK_CONTEXT (context);
1500 source = context->source_list;
1503 if (!SOURCE_DESTROYED (source) &&
1504 source->source_id == source_id)
1506 source = source->next;
1509 UNLOCK_CONTEXT (context);
1515 * g_main_context_find_source_by_funcs_user_data:
1516 * @context: a #GMainContext (if %NULL, the default context will be used).
1517 * @funcs: the @source_funcs passed to g_source_new().
1518 * @user_data: the user data from the callback.
1520 * Finds a source with the given source functions and user data. If
1521 * multiple sources exist with the same source function and user data,
1522 * the first one found will be returned.
1524 * Return value: the source, if one was found, otherwise %NULL
1527 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1528 GSourceFuncs *funcs,
1533 g_return_val_if_fail (funcs != NULL, NULL);
1535 if (context == NULL)
1536 context = g_main_context_default ();
1538 LOCK_CONTEXT (context);
1540 source = context->source_list;
1543 if (!SOURCE_DESTROYED (source) &&
1544 source->source_funcs == funcs &&
1545 source->callback_funcs)
1547 GSourceFunc callback;
1548 gpointer callback_data;
1550 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1552 if (callback_data == user_data)
1555 source = source->next;
1558 UNLOCK_CONTEXT (context);
1564 * g_main_context_find_source_by_user_data:
1565 * @context: a #GMainContext
1566 * @user_data: the user_data for the callback.
1568 * Finds a source with the given user data for the callback. If
1569 * multiple sources exist with the same user data, the first
1570 * one found will be returned.
1572 * Return value: the source, if one was found, otherwise %NULL
1575 g_main_context_find_source_by_user_data (GMainContext *context,
1580 if (context == NULL)
1581 context = g_main_context_default ();
1583 LOCK_CONTEXT (context);
1585 source = context->source_list;
1588 if (!SOURCE_DESTROYED (source) &&
1589 source->callback_funcs)
1591 GSourceFunc callback;
1592 gpointer callback_data = NULL;
1594 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1596 if (callback_data == user_data)
1599 source = source->next;
1602 UNLOCK_CONTEXT (context);
1609 * @tag: the ID of the source to remove.
1611 * Removes the source with the given id from the default main context.
1613 * a #GSource is given by g_source_get_id(), or will be returned by the
1614 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1615 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1616 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1618 * See also g_source_destroy().
1620 * Return value: %TRUE if the source was found and removed.
1623 g_source_remove (guint tag)
1627 g_return_val_if_fail (tag > 0, FALSE);
1629 source = g_main_context_find_source_by_id (NULL, tag);
1631 g_source_destroy (source);
1633 return source != NULL;
1637 * g_source_remove_by_user_data:
1638 * @user_data: the user_data for the callback.
1640 * Removes a source from the default main loop context given the user
1641 * data for the callback. If multiple sources exist with the same user
1642 * data, only one will be destroyed.
1644 * Return value: %TRUE if a source was found and removed.
1647 g_source_remove_by_user_data (gpointer user_data)
1651 source = g_main_context_find_source_by_user_data (NULL, user_data);
1654 g_source_destroy (source);
1662 * g_source_remove_by_funcs_user_data:
1663 * @funcs: The @source_funcs passed to g_source_new()
1664 * @user_data: the user data for the callback
1666 * Removes a source from the default main loop context given the
1667 * source functions and user data. If multiple sources exist with the
1668 * same source functions and user data, only one will be destroyed.
1670 * Return value: %TRUE if a source was found and removed.
1673 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1678 g_return_val_if_fail (funcs != NULL, FALSE);
1680 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1683 g_source_destroy (source);
1691 * g_get_current_time:
1692 * @result: #GTimeVal structure in which to store current time.
1694 * Equivalent to the UNIX gettimeofday() function, but portable.
1697 g_get_current_time (GTimeVal *result)
1702 g_return_if_fail (result != NULL);
1704 /*this is required on alpha, there the timeval structs are int's
1705 not longs and a cast only would fail horribly*/
1706 gettimeofday (&r, NULL);
1707 result->tv_sec = r.tv_sec;
1708 result->tv_usec = r.tv_usec;
1711 guint64 *time64 = (guint64 *) &ft;
1713 GetSystemTimeAsFileTime (&ft);
1715 /* Convert from 100s of nanoseconds since 1601-01-01
1716 * to Unix epoch. Yes, this is Y2038 unsafe.
1718 *time64 -= G_GINT64_CONSTANT (116444736000000000);
1721 result->tv_sec = *time64 / 1000000;
1722 result->tv_usec = *time64 % 1000000;
1727 g_main_dispatch_free (gpointer dispatch)
1729 g_slice_free (GMainDispatch, dispatch);
1732 /* Running the main loop */
1734 static GMainDispatch *
1737 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1738 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1741 dispatch = g_slice_new0 (GMainDispatch);
1742 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1751 * Return value: The main loop recursion level in the current thread
1753 * Returns the depth of the stack of calls to
1754 * g_main_context_dispatch() on any #GMainContext in the current thread.
1755 * That is, when called from the toplevel, it gives 0. When
1756 * called from within a callback from g_main_context_iteration()
1757 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1758 * a callback to a recursive call to g_main_context_iterate(),
1759 * it returns 2. And so forth.
1761 * This function is useful in a situation like the following:
1762 * Imagine an extremely simple "garbage collected" system.
1765 * static GList *free_list;
1768 * allocate_memory (gsize size)
1770 * gpointer result = g_malloc (size);
1771 * free_list = g_list_prepend (free_list, result);
1776 * free_allocated_memory (void)
1779 * for (l = free_list; l; l = l->next);
1781 * g_list_free (free_list);
1789 * g_main_context_iteration (NULL, TRUE);
1790 * free_allocated_memory();
1794 * This works from an application, however, if you want to do the same
1795 * thing from a library, it gets more difficult, since you no longer
1796 * control the main loop. You might think you can simply use an idle
1797 * function to make the call to free_allocated_memory(), but that
1798 * doesn't work, since the idle function could be called from a
1799 * recursive callback. This can be fixed by using g_main_depth()
1803 * allocate_memory (gsize size)
1805 * FreeListBlock *block = g_new (FreeListBlock, 1);\
1806 * block->mem = g_malloc (size);
1807 * block->depth = g_main_depth ();
1808 * free_list = g_list_prepend (free_list, block);
1809 * return block->mem;
1813 * free_allocated_memory (void)
1817 * int depth = g_main_depth ();
1818 * for (l = free_list; l; );
1820 * GList *next = l->next;
1821 * FreeListBlock *block = l->data;
1822 * if (block->depth > depth)
1824 * g_free (block->mem);
1826 * free_list = g_list_delete_link (free_list, l);
1834 * There is a temptation to use g_main_depth() to solve
1835 * problems with reentrancy. For instance, while waiting for data
1836 * to be received from the network in response to a menu item,
1837 * the menu item might be selected again. It might seem that
1838 * one could make the menu item's callback return immediately
1839 * and do nothing if g_main_depth() returns a value greater than 1.
1840 * However, this should be avoided since the user then sees selecting
1841 * the menu item do nothing. Furthermore, you'll find yourself adding
1842 * these checks all over your code, since there are doubtless many,
1843 * many things that the user could do. Instead, you can use the
1844 * following techniques:
1849 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1850 * the user from interacting with elements while the main
1851 * loop is recursing.
1856 * Avoid main loop recursion in situations where you can't handle
1857 * arbitrary callbacks. Instead, structure your code so that you
1858 * simply return to the main loop and then get called again when
1859 * there is more work to do.
1867 GMainDispatch *dispatch = get_dispatch ();
1868 return dispatch->depth;
1872 * g_main_current_source:
1874 * Returns the currently firing source for this thread.
1876 * Return value: The currently firing source or %NULL.
1881 g_main_current_source (void)
1883 GMainDispatch *dispatch = get_dispatch ();
1884 return dispatch->source ? dispatch->source->data : NULL;
1888 * g_source_is_destroyed:
1889 * @source: a #GSource
1891 * Returns whether @source has been destroyed.
1893 * This is important when you operate upon your objects
1894 * from within idle handlers, but may have freed the object
1895 * before the dispatch of your idle handler.
1897 * <informalexample><programlisting>
1899 * idle_callback (gpointer data)
1901 * SomeWidget *self = data;
1903 * GDK_THREADS_ENTER (<!-- -->);
1904 * /<!-- -->* do stuff with self *<!-- -->/
1905 * self->idle_id = 0;
1906 * GDK_THREADS_LEAVE (<!-- -->);
1912 * some_widget_do_stuff_later (SomeWidget *self)
1914 * self->idle_id = g_idle_add (idle_callback, self);
1918 * some_widget_finalize (GObject *object)
1920 * SomeWidget *self = SOME_WIDGET (object);
1922 * if (self->idle_id)
1923 * g_source_remove (self->idle_id);
1925 * G_OBJECT_CLASS (parent_class)->finalize (object);
1927 * </programlisting></informalexample>
1929 * This will fail in a multi-threaded application if the
1930 * widget is destroyed before the idle handler fires due
1931 * to the use after free in the callback. A solution, to
1932 * this particular problem, is to check to if the source
1933 * has already been destroy within the callback.
1935 * <informalexample><programlisting>
1937 * idle_callback (gpointer data)
1939 * SomeWidget *self = data;
1941 * GDK_THREADS_ENTER ();
1942 * if (!g_source_is_destroyed (g_main_current_source ()))
1944 * /<!-- -->* do stuff with self *<!-- -->/
1946 * GDK_THREADS_LEAVE ();
1950 * </programlisting></informalexample>
1952 * Return value: %TRUE if the source has been destroyed
1957 g_source_is_destroyed (GSource *source)
1959 return SOURCE_DESTROYED (source);
1963 /* Temporarily remove all this source's file descriptors from the
1964 * poll(), so that if data comes available for one of the file descriptors
1965 * we don't continually spin in the poll()
1967 /* HOLDS: source->context's lock */
1969 block_source (GSource *source)
1973 g_return_if_fail (!SOURCE_BLOCKED (source));
1975 tmp_list = source->poll_fds;
1978 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1979 tmp_list = tmp_list->next;
1983 /* HOLDS: source->context's lock */
1985 unblock_source (GSource *source)
1989 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1990 g_return_if_fail (!SOURCE_DESTROYED (source));
1992 tmp_list = source->poll_fds;
1995 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1996 tmp_list = tmp_list->next;
2000 /* HOLDS: context's lock */
2002 g_main_dispatch (GMainContext *context)
2004 GMainDispatch *current = get_dispatch ();
2007 for (i = 0; i < context->pending_dispatches->len; i++)
2009 GSource *source = context->pending_dispatches->pdata[i];
2011 context->pending_dispatches->pdata[i] = NULL;
2014 source->flags &= ~G_SOURCE_READY;
2016 if (!SOURCE_DESTROYED (source))
2018 gboolean was_in_call;
2019 gpointer user_data = NULL;
2020 GSourceFunc callback = NULL;
2021 GSourceCallbackFuncs *cb_funcs;
2023 gboolean need_destroy;
2025 gboolean (*dispatch) (GSource *,
2028 GSList current_source_link;
2030 dispatch = source->source_funcs->dispatch;
2031 cb_funcs = source->callback_funcs;
2032 cb_data = source->callback_data;
2035 cb_funcs->ref (cb_data);
2037 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2038 block_source (source);
2040 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2041 source->flags |= G_HOOK_FLAG_IN_CALL;
2044 cb_funcs->get (cb_data, source, &callback, &user_data);
2046 UNLOCK_CONTEXT (context);
2049 /* The on-stack allocation of the GSList is unconventional, but
2050 * we know that the lifetime of the link is bounded to this
2051 * function as the link is kept in a thread specific list and
2052 * not manipulated outside of this function and its descendants.
2053 * Avoiding the overhead of a g_slist_alloc() is useful as many
2054 * applications do little more than dispatch events.
2056 * This is a performance hack - do not revert to g_slist_prepend()!
2058 current_source_link.data = source;
2059 current_source_link.next = current->source;
2060 current->source = ¤t_source_link;
2061 need_destroy = ! dispatch (source,
2064 g_assert (current->source == ¤t_source_link);
2065 current->source = current_source_link.next;
2069 cb_funcs->unref (cb_data);
2071 LOCK_CONTEXT (context);
2074 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2076 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2077 !SOURCE_DESTROYED (source))
2078 unblock_source (source);
2080 /* Note: this depends on the fact that we can't switch
2081 * sources from one main context to another
2083 if (need_destroy && !SOURCE_DESTROYED (source))
2085 g_assert (source->context == context);
2086 g_source_destroy_internal (source, context, TRUE);
2090 SOURCE_UNREF (source, context);
2093 g_ptr_array_set_size (context->pending_dispatches, 0);
2096 /* Holds context's lock */
2097 static inline GSource *
2098 next_valid_source (GMainContext *context,
2101 GSource *new_source = source ? source->next : context->source_list;
2105 if (!SOURCE_DESTROYED (new_source))
2107 new_source->ref_count++;
2111 new_source = new_source->next;
2115 SOURCE_UNREF (source, context);
2121 * g_main_context_acquire:
2122 * @context: a #GMainContext
2124 * Tries to become the owner of the specified context.
2125 * If some other context is the owner of the context,
2126 * returns %FALSE immediately. Ownership is properly
2127 * recursive: the owner can require ownership again
2128 * and will release ownership when g_main_context_release()
2129 * is called as many times as g_main_context_acquire().
2131 * You must be the owner of a context before you
2132 * can call g_main_context_prepare(), g_main_context_query(),
2133 * g_main_context_check(), g_main_context_dispatch().
2135 * Return value: %TRUE if the operation succeeded, and
2136 * this thread is now the owner of @context.
2139 g_main_context_acquire (GMainContext *context)
2141 #ifdef G_THREADS_ENABLED
2142 gboolean result = FALSE;
2143 GThread *self = G_THREAD_SELF;
2145 if (context == NULL)
2146 context = g_main_context_default ();
2148 LOCK_CONTEXT (context);
2150 if (!context->owner)
2152 context->owner = self;
2153 g_assert (context->owner_count == 0);
2156 if (context->owner == self)
2158 context->owner_count++;
2162 UNLOCK_CONTEXT (context);
2165 #else /* !G_THREADS_ENABLED */
2167 #endif /* G_THREADS_ENABLED */
2171 * g_main_context_release:
2172 * @context: a #GMainContext
2174 * Releases ownership of a context previously acquired by this thread
2175 * with g_main_context_acquire(). If the context was acquired multiple
2176 * times, the only release ownership when g_main_context_release()
2177 * is called as many times as it was acquired.
2180 g_main_context_release (GMainContext *context)
2182 #ifdef G_THREADS_ENABLED
2183 if (context == NULL)
2184 context = g_main_context_default ();
2186 LOCK_CONTEXT (context);
2188 context->owner_count--;
2189 if (context->owner_count == 0)
2191 context->owner = NULL;
2193 if (context->waiters)
2195 GMainWaiter *waiter = context->waiters->data;
2196 gboolean loop_internal_waiter =
2197 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2198 context->waiters = g_slist_delete_link (context->waiters,
2200 if (!loop_internal_waiter)
2201 g_mutex_lock (waiter->mutex);
2203 g_cond_signal (waiter->cond);
2205 if (!loop_internal_waiter)
2206 g_mutex_unlock (waiter->mutex);
2210 UNLOCK_CONTEXT (context);
2211 #endif /* G_THREADS_ENABLED */
2215 * g_main_context_wait:
2216 * @context: a #GMainContext
2217 * @cond: a condition variable
2218 * @mutex: a mutex, currently held
2220 * Tries to become the owner of the specified context,
2221 * as with g_main_context_acquire(). But if another thread
2222 * is the owner, atomically drop @mutex and wait on @cond until
2223 * that owner releases ownership or until @cond is signaled, then
2224 * try again (once) to become the owner.
2226 * Return value: %TRUE if the operation succeeded, and
2227 * this thread is now the owner of @context.
2230 g_main_context_wait (GMainContext *context,
2234 #ifdef G_THREADS_ENABLED
2235 gboolean result = FALSE;
2236 GThread *self = G_THREAD_SELF;
2237 gboolean loop_internal_waiter;
2239 if (context == NULL)
2240 context = g_main_context_default ();
2242 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2244 if (!loop_internal_waiter)
2245 LOCK_CONTEXT (context);
2247 if (context->owner && context->owner != self)
2252 waiter.mutex = mutex;
2254 context->waiters = g_slist_append (context->waiters, &waiter);
2256 if (!loop_internal_waiter)
2257 UNLOCK_CONTEXT (context);
2258 g_cond_wait (cond, mutex);
2259 if (!loop_internal_waiter)
2260 LOCK_CONTEXT (context);
2262 context->waiters = g_slist_remove (context->waiters, &waiter);
2265 if (!context->owner)
2267 context->owner = self;
2268 g_assert (context->owner_count == 0);
2271 if (context->owner == self)
2273 context->owner_count++;
2277 if (!loop_internal_waiter)
2278 UNLOCK_CONTEXT (context);
2281 #else /* !G_THREADS_ENABLED */
2283 #endif /* G_THREADS_ENABLED */
2287 * g_main_context_prepare:
2288 * @context: a #GMainContext
2289 * @priority: location to store priority of highest priority
2290 * source already ready.
2292 * Prepares to poll sources within a main loop. The resulting information
2293 * for polling is determined by calling g_main_context_query ().
2295 * Return value: %TRUE if some source is ready to be dispatched
2299 g_main_context_prepare (GMainContext *context,
2304 gint current_priority = G_MAXINT;
2307 if (context == NULL)
2308 context = g_main_context_default ();
2310 LOCK_CONTEXT (context);
2312 context->time_is_current = FALSE;
2314 if (context->in_check_or_prepare)
2316 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2317 "prepare() member.");
2318 UNLOCK_CONTEXT (context);
2322 #ifdef G_THREADS_ENABLED
2323 if (context->poll_waiting)
2325 g_warning("g_main_context_prepare(): main loop already active in another thread");
2326 UNLOCK_CONTEXT (context);
2330 context->poll_waiting = TRUE;
2331 #endif /* G_THREADS_ENABLED */
2334 /* If recursing, finish up current dispatch, before starting over */
2335 if (context->pending_dispatches)
2338 g_main_dispatch (context, ¤t_time);
2340 UNLOCK_CONTEXT (context);
2345 /* If recursing, clear list of pending dispatches */
2347 for (i = 0; i < context->pending_dispatches->len; i++)
2349 if (context->pending_dispatches->pdata[i])
2350 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2352 g_ptr_array_set_size (context->pending_dispatches, 0);
2354 /* Prepare all sources */
2356 context->timeout = -1;
2358 source = next_valid_source (context, NULL);
2361 gint source_timeout = -1;
2363 if ((n_ready > 0) && (source->priority > current_priority))
2365 SOURCE_UNREF (source, context);
2368 if (SOURCE_BLOCKED (source))
2371 if (!(source->flags & G_SOURCE_READY))
2374 gboolean (*prepare) (GSource *source,
2377 prepare = source->source_funcs->prepare;
2378 context->in_check_or_prepare++;
2379 UNLOCK_CONTEXT (context);
2381 result = (*prepare) (source, &source_timeout);
2383 LOCK_CONTEXT (context);
2384 context->in_check_or_prepare--;
2387 source->flags |= G_SOURCE_READY;
2390 if (source->flags & G_SOURCE_READY)
2393 current_priority = source->priority;
2394 context->timeout = 0;
2397 if (source_timeout >= 0)
2399 if (context->timeout < 0)
2400 context->timeout = source_timeout;
2402 context->timeout = MIN (context->timeout, source_timeout);
2406 source = next_valid_source (context, source);
2409 UNLOCK_CONTEXT (context);
2412 *priority = current_priority;
2414 return (n_ready > 0);
2418 * g_main_context_query:
2419 * @context: a #GMainContext
2420 * @max_priority: maximum priority source to check
2421 * @timeout_: location to store timeout to be used in polling
2422 * @fds: location to store #GPollFD records that need to be polled.
2423 * @n_fds: length of @fds.
2425 * Determines information necessary to poll this main loop.
2427 * Return value: the number of records actually stored in @fds,
2428 * or, if more than @n_fds records need to be stored, the number
2429 * of records that need to be stored.
2432 g_main_context_query (GMainContext *context,
2441 LOCK_CONTEXT (context);
2443 pollrec = context->poll_records;
2445 while (pollrec && max_priority >= pollrec->priority)
2447 if (pollrec->fd->events)
2451 fds[n_poll].fd = pollrec->fd->fd;
2452 /* In direct contradiction to the Unix98 spec, IRIX runs into
2453 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2454 * flags in the events field of the pollfd while it should
2455 * just ignoring them. So we mask them out here.
2457 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2458 fds[n_poll].revents = 0;
2463 pollrec = pollrec->next;
2466 #ifdef G_THREADS_ENABLED
2467 context->poll_changed = FALSE;
2472 *timeout = context->timeout;
2474 context->time_is_current = FALSE;
2477 UNLOCK_CONTEXT (context);
2483 * g_main_context_check:
2484 * @context: a #GMainContext
2485 * @max_priority: the maximum numerical priority of sources to check
2486 * @fds: array of #GPollFD's that was passed to the last call to
2487 * g_main_context_query()
2488 * @n_fds: return value of g_main_context_query()
2490 * Passes the results of polling back to the main loop.
2492 * Return value: %TRUE if some sources are ready to be dispatched.
2495 g_main_context_check (GMainContext *context,
2505 LOCK_CONTEXT (context);
2507 if (context->in_check_or_prepare)
2509 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2510 "prepare() member.");
2511 UNLOCK_CONTEXT (context);
2515 #ifdef G_THREADS_ENABLED
2516 if (!context->poll_waiting)
2520 read (context->wake_up_pipe[0], &a, 1);
2524 context->poll_waiting = FALSE;
2526 /* If the set of poll file descriptors changed, bail out
2527 * and let the main loop rerun
2529 if (context->poll_changed)
2531 UNLOCK_CONTEXT (context);
2534 #endif /* G_THREADS_ENABLED */
2536 pollrec = context->poll_records;
2540 if (pollrec->fd->events)
2542 pollrec->fd->revents = fds[i].revents;
2545 pollrec = pollrec->next;
2548 source = next_valid_source (context, NULL);
2551 if ((n_ready > 0) && (source->priority > max_priority))
2553 SOURCE_UNREF (source, context);
2556 if (SOURCE_BLOCKED (source))
2559 if (!(source->flags & G_SOURCE_READY))
2562 gboolean (*check) (GSource *source);
2564 check = source->source_funcs->check;
2566 context->in_check_or_prepare++;
2567 UNLOCK_CONTEXT (context);
2569 result = (*check) (source);
2571 LOCK_CONTEXT (context);
2572 context->in_check_or_prepare--;
2575 source->flags |= G_SOURCE_READY;
2578 if (source->flags & G_SOURCE_READY)
2580 source->ref_count++;
2581 g_ptr_array_add (context->pending_dispatches, source);
2585 /* never dispatch sources with less priority than the first
2586 * one we choose to dispatch
2588 max_priority = source->priority;
2592 source = next_valid_source (context, source);
2595 UNLOCK_CONTEXT (context);
2601 * g_main_context_dispatch:
2602 * @context: a #GMainContext
2604 * Dispatches all pending sources.
2607 g_main_context_dispatch (GMainContext *context)
2609 LOCK_CONTEXT (context);
2611 if (context->pending_dispatches->len > 0)
2613 g_main_dispatch (context);
2616 UNLOCK_CONTEXT (context);
2619 /* HOLDS context lock */
2621 g_main_context_iterate (GMainContext *context,
2628 gboolean some_ready;
2629 gint nfds, allocated_nfds;
2630 GPollFD *fds = NULL;
2632 UNLOCK_CONTEXT (context);
2634 #ifdef G_THREADS_ENABLED
2635 if (!g_main_context_acquire (context))
2637 gboolean got_ownership;
2639 g_return_val_if_fail (g_thread_supported (), FALSE);
2644 LOCK_CONTEXT (context);
2647 context->cond = g_cond_new ();
2649 got_ownership = g_main_context_wait (context,
2651 g_static_mutex_get_mutex (&context->mutex));
2655 UNLOCK_CONTEXT (context);
2660 LOCK_CONTEXT (context);
2661 #endif /* G_THREADS_ENABLED */
2663 if (!context->cached_poll_array)
2665 context->cached_poll_array_size = context->n_poll_records;
2666 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2669 allocated_nfds = context->cached_poll_array_size;
2670 fds = context->cached_poll_array;
2672 UNLOCK_CONTEXT (context);
2674 g_main_context_prepare (context, &max_priority);
2676 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2677 allocated_nfds)) > allocated_nfds)
2679 LOCK_CONTEXT (context);
2681 context->cached_poll_array_size = allocated_nfds = nfds;
2682 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2683 UNLOCK_CONTEXT (context);
2689 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2691 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2694 g_main_context_dispatch (context);
2696 #ifdef G_THREADS_ENABLED
2697 g_main_context_release (context);
2698 #endif /* G_THREADS_ENABLED */
2700 LOCK_CONTEXT (context);
2706 * g_main_context_pending:
2707 * @context: a #GMainContext (if %NULL, the default context will be used)
2709 * Checks if any sources have pending events for the given context.
2711 * Return value: %TRUE if events are pending.
2714 g_main_context_pending (GMainContext *context)
2719 context = g_main_context_default();
2721 LOCK_CONTEXT (context);
2722 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2723 UNLOCK_CONTEXT (context);
2729 * g_main_context_iteration:
2730 * @context: a #GMainContext (if %NULL, the default context will be used)
2731 * @may_block: whether the call may block.
2733 * Runs a single iteration for the given main loop. This involves
2734 * checking to see if any event sources are ready to be processed,
2735 * then if no events sources are ready and @may_block is %TRUE, waiting
2736 * for a source to become ready, then dispatching the highest priority
2737 * events sources that are ready. Note that even when @may_block is %TRUE,
2738 * it is still possible for g_main_context_iteration() to return
2739 * %FALSE, since the the wait may be interrupted for other
2740 * reasons than an event source becoming ready.
2742 * Return value: %TRUE if events were dispatched.
2745 g_main_context_iteration (GMainContext *context, gboolean may_block)
2750 context = g_main_context_default();
2752 LOCK_CONTEXT (context);
2753 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2754 UNLOCK_CONTEXT (context);
2761 * @context: a #GMainContext (if %NULL, the default context will be used).
2762 * @is_running: set to %TRUE to indicate that the loop is running. This
2763 * is not very important since calling g_main_loop_run() will set this to
2766 * Creates a new #GMainLoop structure.
2768 * Return value: a new #GMainLoop.
2771 g_main_loop_new (GMainContext *context,
2772 gboolean is_running)
2777 context = g_main_context_default();
2779 g_main_context_ref (context);
2781 loop = g_new0 (GMainLoop, 1);
2782 loop->context = context;
2783 loop->is_running = is_running != FALSE;
2784 loop->ref_count = 1;
2791 * @loop: a #GMainLoop
2793 * Increases the reference count on a #GMainLoop object by one.
2795 * Return value: @loop
2798 g_main_loop_ref (GMainLoop *loop)
2800 g_return_val_if_fail (loop != NULL, NULL);
2801 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2803 g_atomic_int_inc (&loop->ref_count);
2809 * g_main_loop_unref:
2810 * @loop: a #GMainLoop
2812 * Decreases the reference count on a #GMainLoop object by one. If
2813 * the result is zero, free the loop and free all associated memory.
2816 g_main_loop_unref (GMainLoop *loop)
2818 g_return_if_fail (loop != NULL);
2819 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2821 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2824 g_main_context_unref (loop->context);
2830 * @loop: a #GMainLoop
2832 * Runs a main loop until g_main_loop_quit() is called on the loop.
2833 * If this is called for the thread of the loop's #GMainContext,
2834 * it will process events from the loop, otherwise it will
2838 g_main_loop_run (GMainLoop *loop)
2840 GThread *self = G_THREAD_SELF;
2842 g_return_if_fail (loop != NULL);
2843 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2845 #ifdef G_THREADS_ENABLED
2846 if (!g_main_context_acquire (loop->context))
2848 gboolean got_ownership = FALSE;
2850 /* Another thread owns this context */
2851 if (!g_thread_supported ())
2853 g_warning ("g_main_loop_run() was called from second thread but "
2854 "g_thread_init() was never called.");
2858 LOCK_CONTEXT (loop->context);
2860 g_atomic_int_inc (&loop->ref_count);
2862 if (!loop->is_running)
2863 loop->is_running = TRUE;
2865 if (!loop->context->cond)
2866 loop->context->cond = g_cond_new ();
2868 while (loop->is_running && !got_ownership)
2869 got_ownership = g_main_context_wait (loop->context,
2870 loop->context->cond,
2871 g_static_mutex_get_mutex (&loop->context->mutex));
2873 if (!loop->is_running)
2875 UNLOCK_CONTEXT (loop->context);
2877 g_main_context_release (loop->context);
2878 g_main_loop_unref (loop);
2882 g_assert (got_ownership);
2885 LOCK_CONTEXT (loop->context);
2886 #endif /* G_THREADS_ENABLED */
2888 if (loop->context->in_check_or_prepare)
2890 g_warning ("g_main_loop_run(): called recursively from within a source's "
2891 "check() or prepare() member, iteration not possible.");
2895 g_atomic_int_inc (&loop->ref_count);
2896 loop->is_running = TRUE;
2897 while (loop->is_running)
2898 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2900 UNLOCK_CONTEXT (loop->context);
2902 #ifdef G_THREADS_ENABLED
2903 g_main_context_release (loop->context);
2904 #endif /* G_THREADS_ENABLED */
2906 g_main_loop_unref (loop);
2911 * @loop: a #GMainLoop
2913 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2914 * for the loop will return.
2917 g_main_loop_quit (GMainLoop *loop)
2919 g_return_if_fail (loop != NULL);
2920 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2922 LOCK_CONTEXT (loop->context);
2923 loop->is_running = FALSE;
2924 g_main_context_wakeup_unlocked (loop->context);
2926 #ifdef G_THREADS_ENABLED
2927 if (loop->context->cond)
2928 g_cond_broadcast (loop->context->cond);
2929 #endif /* G_THREADS_ENABLED */
2931 UNLOCK_CONTEXT (loop->context);
2935 * g_main_loop_is_running:
2936 * @loop: a #GMainLoop.
2938 * Checks to see if the main loop is currently being run via g_main_loop_run().
2940 * Return value: %TRUE if the mainloop is currently being run.
2943 g_main_loop_is_running (GMainLoop *loop)
2945 g_return_val_if_fail (loop != NULL, FALSE);
2946 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2948 return loop->is_running;
2952 * g_main_loop_get_context:
2953 * @loop: a #GMainLoop.
2955 * Returns the #GMainContext of @loop.
2957 * Return value: the #GMainContext of @loop
2960 g_main_loop_get_context (GMainLoop *loop)
2962 g_return_val_if_fail (loop != NULL, NULL);
2963 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2965 return loop->context;
2968 /* HOLDS: context's lock */
2970 g_main_context_poll (GMainContext *context,
2976 #ifdef G_MAIN_POLL_DEBUG
2982 GPollFunc poll_func;
2984 if (n_fds || timeout != 0)
2986 #ifdef G_MAIN_POLL_DEBUG
2987 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2988 poll_timer = g_timer_new ();
2991 LOCK_CONTEXT (context);
2993 poll_func = context->poll_func;
2995 UNLOCK_CONTEXT (context);
2996 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2999 g_warning ("poll(2) failed due to: %s.",
3000 g_strerror (errno));
3002 /* If g_poll () returns -1, it has already called g_warning() */
3006 #ifdef G_MAIN_POLL_DEBUG
3007 LOCK_CONTEXT (context);
3009 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3012 g_timer_elapsed (poll_timer, NULL));
3013 g_timer_destroy (poll_timer);
3014 pollrec = context->poll_records;
3018 if (pollrec->fd->events)
3022 g_print (" [%d:", fds[i].fd);
3023 if (fds[i].revents & G_IO_IN)
3025 if (fds[i].revents & G_IO_OUT)
3027 if (fds[i].revents & G_IO_PRI)
3029 if (fds[i].revents & G_IO_ERR)
3031 if (fds[i].revents & G_IO_HUP)
3033 if (fds[i].revents & G_IO_NVAL)
3039 pollrec = pollrec->next;
3043 UNLOCK_CONTEXT (context);
3045 } /* if (n_fds || timeout != 0) */
3049 * g_main_context_add_poll:
3050 * @context: a #GMainContext (or %NULL for the default context)
3051 * @fd: a #GPollFD structure holding information about a file
3052 * descriptor to watch.
3053 * @priority: the priority for this file descriptor which should be
3054 * the same as the priority used for g_source_attach() to ensure that the
3055 * file descriptor is polled whenever the results may be needed.
3057 * Adds a file descriptor to the set of file descriptors polled for
3058 * this context. This will very seldomly be used directly. Instead
3059 * a typical event source will use g_source_add_poll() instead.
3062 g_main_context_add_poll (GMainContext *context,
3067 context = g_main_context_default ();
3069 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3070 g_return_if_fail (fd);
3072 LOCK_CONTEXT (context);
3073 g_main_context_add_poll_unlocked (context, priority, fd);
3074 UNLOCK_CONTEXT (context);
3077 /* HOLDS: main_loop_lock */
3079 g_main_context_add_poll_unlocked (GMainContext *context,
3083 GPollRec *lastrec, *pollrec;
3084 GPollRec *newrec = g_slice_new (GPollRec);
3086 /* This file descriptor may be checked before we ever poll */
3089 newrec->priority = priority;
3092 pollrec = context->poll_records;
3093 while (pollrec && priority >= pollrec->priority)
3096 pollrec = pollrec->next;
3100 lastrec->next = newrec;
3102 context->poll_records = newrec;
3104 newrec->next = pollrec;
3106 context->n_poll_records++;
3108 #ifdef G_THREADS_ENABLED
3109 context->poll_changed = TRUE;
3111 /* Now wake up the main loop if it is waiting in the poll() */
3112 g_main_context_wakeup_unlocked (context);
3117 * g_main_context_remove_poll:
3118 * @context:a #GMainContext
3119 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3121 * Removes file descriptor from the set of file descriptors to be
3122 * polled for a particular context.
3125 g_main_context_remove_poll (GMainContext *context,
3129 context = g_main_context_default ();
3131 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3132 g_return_if_fail (fd);
3134 LOCK_CONTEXT (context);
3135 g_main_context_remove_poll_unlocked (context, fd);
3136 UNLOCK_CONTEXT (context);
3140 g_main_context_remove_poll_unlocked (GMainContext *context,
3143 GPollRec *pollrec, *lastrec;
3146 pollrec = context->poll_records;
3150 if (pollrec->fd == fd)
3152 if (lastrec != NULL)
3153 lastrec->next = pollrec->next;
3155 context->poll_records = pollrec->next;
3157 g_slice_free (GPollRec, pollrec);
3159 context->n_poll_records--;
3163 pollrec = pollrec->next;
3166 #ifdef G_THREADS_ENABLED
3167 context->poll_changed = TRUE;
3169 /* Now wake up the main loop if it is waiting in the poll() */
3170 g_main_context_wakeup_unlocked (context);
3175 * g_source_get_current_time:
3176 * @source: a #GSource
3177 * @timeval: #GTimeVal structure in which to store current time.
3179 * Gets the "current time" to be used when checking
3180 * this source. The advantage of calling this function over
3181 * calling g_get_current_time() directly is that when
3182 * checking multiple sources, GLib can cache a single value
3183 * instead of having to repeatedly get the system time.
3186 g_source_get_current_time (GSource *source,
3189 GMainContext *context;
3191 g_return_if_fail (source->context != NULL);
3193 context = source->context;
3195 LOCK_CONTEXT (context);
3197 if (!context->time_is_current)
3199 g_get_current_time (&context->current_time);
3200 context->time_is_current = TRUE;
3203 *timeval = context->current_time;
3205 UNLOCK_CONTEXT (context);
3209 * g_main_context_set_poll_func:
3210 * @context: a #GMainContext
3211 * @func: the function to call to poll all file descriptors
3213 * Sets the function to use to handle polling of file descriptors. It
3214 * will be used instead of the poll() system call
3215 * (or GLib's replacement function, which is used where
3216 * poll() isn't available).
3218 * This function could possibly be used to integrate the GLib event
3219 * loop with an external event loop.
3222 g_main_context_set_poll_func (GMainContext *context,
3226 context = g_main_context_default ();
3228 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3230 LOCK_CONTEXT (context);
3233 context->poll_func = func;
3237 context->poll_func = (GPollFunc) poll;
3239 context->poll_func = (GPollFunc) g_poll;
3243 UNLOCK_CONTEXT (context);
3247 * g_main_context_get_poll_func:
3248 * @context: a #GMainContext
3250 * Gets the poll function set by g_main_context_set_poll_func().
3252 * Return value: the poll function
3255 g_main_context_get_poll_func (GMainContext *context)
3260 context = g_main_context_default ();
3262 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3264 LOCK_CONTEXT (context);
3265 result = context->poll_func;
3266 UNLOCK_CONTEXT (context);
3271 /* HOLDS: context's lock */
3272 /* Wake the main loop up from a poll() */
3274 g_main_context_wakeup_unlocked (GMainContext *context)
3276 #ifdef G_THREADS_ENABLED
3277 if (g_thread_supported() && context->poll_waiting)
3279 context->poll_waiting = FALSE;
3281 write (context->wake_up_pipe[1], "A", 1);
3283 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3290 * g_main_context_wakeup:
3291 * @context: a #GMainContext
3293 * If @context is currently waiting in a poll(), interrupt
3294 * the poll(), and continue the iteration process.
3297 g_main_context_wakeup (GMainContext *context)
3300 context = g_main_context_default ();
3302 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3304 LOCK_CONTEXT (context);
3305 g_main_context_wakeup_unlocked (context);
3306 UNLOCK_CONTEXT (context);
3310 * g_main_context_is_owner:
3311 * @context: a #GMainContext
3313 * Determines whether this thread holds the (recursive)
3314 * ownership of this #GMaincontext. This is useful to
3315 * know before waiting on another thread that may be
3316 * blocking to get ownership of @context.
3318 * Returns: %TRUE if current thread is owner of @context.
3323 g_main_context_is_owner (GMainContext *context)
3328 context = g_main_context_default ();
3330 #ifdef G_THREADS_ENABLED
3331 LOCK_CONTEXT (context);
3332 is_owner = context->owner == G_THREAD_SELF;
3333 UNLOCK_CONTEXT (context);
3344 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3345 GTimeVal *current_time)
3347 guint seconds = timeout_source->interval / 1000;
3348 guint msecs = timeout_source->interval - seconds * 1000;
3350 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3351 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3352 if (timeout_source->expiration.tv_usec >= 1000000)
3354 timeout_source->expiration.tv_usec -= 1000000;
3355 timeout_source->expiration.tv_sec++;
3357 if (timer_perturb==-1)
3360 * we want a per machine/session unique 'random' value; try the dbus
3361 * address first, that has a UUID in it. If there is no dbus, use the
3362 * hostname for hashing.
3364 const char *session_bus_address = getenv("DBUS_SESSION_BUS_ADDRESS");
3365 if (!session_bus_address)
3366 session_bus_address = getenv("HOSTNAME");
3367 if (session_bus_address)
3368 timer_perturb = g_str_hash(session_bus_address);
3372 if (timeout_source->granularity)
3375 gint gran; /* in usecs */
3378 gran = timeout_source->granularity * 1000;
3379 perturb = timer_perturb % gran;
3381 * We want to give each machine a per machine pertubation;
3382 * shift time back first, and forward later after the rounding
3385 timeout_source->expiration.tv_usec -= perturb;
3386 if (timeout_source->expiration.tv_usec < 0)
3388 timeout_source->expiration.tv_usec += 1000000;
3389 timeout_source->expiration.tv_sec--;
3392 remainder = timeout_source->expiration.tv_usec % gran;
3393 if (remainder >= gran/4) /* round up */
3394 timeout_source->expiration.tv_usec += gran;
3395 timeout_source->expiration.tv_usec -= remainder;
3397 timeout_source->expiration.tv_usec += perturb;
3399 /* the rounding may have overflown tv_usec */
3400 while (timeout_source->expiration.tv_usec > 1000000)
3402 timeout_source->expiration.tv_usec -= 1000000;
3403 timeout_source->expiration.tv_sec++;
3409 g_timeout_prepare (GSource *source,
3414 GTimeVal current_time;
3416 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3418 g_source_get_current_time (source, ¤t_time);
3420 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3421 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3423 /* We do the following in a rather convoluted fashion to deal with
3424 * the fact that we don't have an integral type big enough to hold
3425 * the difference of two timevals in millseconds.
3427 if (sec < 0 || (sec == 0 && msec < 0))
3431 glong interval_sec = timeout_source->interval / 1000;
3432 glong interval_msec = timeout_source->interval % 1000;
3440 if (sec > interval_sec ||
3441 (sec == interval_sec && msec > interval_msec))
3443 /* The system time has been set backwards, so we
3444 * reset the expiration time to now + timeout_source->interval;
3445 * this at least avoids hanging for long periods of time.
3447 g_timeout_set_expiration (timeout_source, ¤t_time);
3448 msec = MIN (G_MAXINT, timeout_source->interval);
3452 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3456 *timeout = (gint)msec;
3462 g_timeout_check (GSource *source)
3464 GTimeVal current_time;
3465 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3467 g_source_get_current_time (source, ¤t_time);
3469 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3470 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3471 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3475 g_timeout_dispatch (GSource *source,
3476 GSourceFunc callback,
3479 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3483 g_warning ("Timeout source dispatched without callback\n"
3484 "You must call g_source_set_callback().");
3488 if (callback (user_data))
3490 GTimeVal current_time;
3492 g_source_get_current_time (source, ¤t_time);
3493 g_timeout_set_expiration (timeout_source, ¤t_time);
3502 * g_timeout_source_new:
3503 * @interval: the timeout interval in milliseconds.
3505 * Creates a new timeout source.
3507 * The source will not initially be associated with any #GMainContext
3508 * and must be added to one with g_source_attach() before it will be
3511 * Return value: the newly-created timeout source
3514 g_timeout_source_new (guint interval)
3516 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3517 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3518 GTimeVal current_time;
3520 timeout_source->interval = interval;
3522 g_get_current_time (¤t_time);
3523 g_timeout_set_expiration (timeout_source, ¤t_time);
3529 * g_timeout_source_new_seconds:
3530 * @interval: the timeout interval in seconds
3532 * Creates a new timeout source.
3534 * The source will not initially be associated with any #GMainContext
3535 * and must be added to one with g_source_attach() before it will be
3537 * The scheduling granularity/accuracy of this timeout source will be
3540 * Return value: the newly-created timeout source
3545 g_timeout_source_new_seconds (guint interval)
3547 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3548 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3549 GTimeVal current_time;
3551 timeout_source->interval = 1000*interval;
3552 timeout_source->granularity = 1000;
3554 g_get_current_time (¤t_time);
3555 g_timeout_set_expiration (timeout_source, ¤t_time);
3562 * g_timeout_add_full:
3563 * @priority: the priority of the idle source. Typically this will be in the
3564 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3565 * @interval: the time between calls to the function, in milliseconds
3566 * (1/1000ths of a second)
3567 * @function: function to call
3568 * @data: data to pass to @function
3569 * @notify: function to call when the idle is removed, or %NULL
3571 * Sets a function to be called at regular intervals, with the given
3572 * priority. The function is called repeatedly until it returns
3573 * %FALSE, at which point the timeout is automatically destroyed and
3574 * the function will not be called again. The @notify function is
3575 * called when the timeout is destroyed. The first call to the
3576 * function will be at the end of the first @interval.
3578 * Note that timeout functions may be delayed, due to the processing of other
3579 * event sources. Thus they should not be relied on for precise timing.
3580 * After each call to the timeout function, the time of the next
3581 * timeout is recalculated based on the current time and the given interval
3582 * (it does not try to 'catch up' time lost in delays).
3584 * Return value: the ID (greater than 0) of the event source.
3587 g_timeout_add_full (gint priority,
3589 GSourceFunc function,
3591 GDestroyNotify notify)
3596 g_return_val_if_fail (function != NULL, 0);
3598 source = g_timeout_source_new (interval);
3600 if (priority != G_PRIORITY_DEFAULT)
3601 g_source_set_priority (source, priority);
3603 g_source_set_callback (source, function, data, notify);
3604 id = g_source_attach (source, NULL);
3605 g_source_unref (source);
3612 * @interval: the time between calls to the function, in milliseconds
3613 * (1/1000ths of a second)
3614 * @function: function to call
3615 * @data: data to pass to @function
3617 * Sets a function to be called at regular intervals, with the default
3618 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3619 * until it returns %FALSE, at which point the timeout is automatically
3620 * destroyed and the function will not be called again. The first call
3621 * to the function will be at the end of the first @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
3627 * (it does not try to 'catch up' time lost in delays).
3629 * If you want to have a timer in the "seconds" range and do not care
3630 * about the exact time of the first call of the timer, use the
3631 * g_timeout_add_seconds() function; this function allows for more
3632 * optimizations and more efficient system power usage.
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:
3647 * @interval: the time between calls to the function, in seconds
3648 * @function: function to call
3649 * @data: data to pass to @function
3651 * Sets a function to be called at regular intervals, with the default
3652 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3653 * until it returns %FALSE, at which point the timeout is automatically
3654 * destroyed and the function will not be called again.
3656 * Unlike g_timeout_add(), this function operates at whole second granularity.
3657 * The initial starting point of the timer is determined by the implementation
3658 * and the implementation is expected to group multiple timers together so that
3659 * they fire all at the same time.
3660 * To allow this grouping, the @interval to the first timer is rounded
3661 * and can deviate up to one second from the specified interval.
3662 * Subsequent timer iterations will generally run at the specified interval.
3664 * Note that timeout functions may be delayed, due to the processing of other
3665 * event sources. Thus they should not be relied on for precise timing.
3666 * After each call to the timeout function, the time of the next
3667 * timeout is recalculated based on the current time and the given @interval
3669 * If you want timing more precise than whole seconds, use g_timeout_add()
3672 * The grouping of timers to fire at the same time results in a more power
3673 * and CPU efficient behavior so if your timer is in multiples of seconds
3674 * and you don't require the first timer exactly 1 second from now, the
3675 * use of g_timeout_add_second() is prefered over g_timeout_add().
3677 * Return value: the ID (greater than 0) of the event source.
3682 g_timeout_add_seconds (guint32 interval,
3683 GSourceFunc function,
3689 g_return_val_if_fail (function != NULL, 0);
3691 source = g_timeout_source_new_seconds (interval);
3693 g_source_set_callback (source, function, data, NULL);
3694 id = g_source_attach (source, NULL);
3695 g_source_unref (source);
3701 /* Child watch functions */
3706 g_child_watch_prepare (GSource *source,
3715 g_child_watch_check (GSource *source)
3717 GChildWatchSource *child_watch_source;
3718 gboolean child_exited;
3720 child_watch_source = (GChildWatchSource *) source;
3722 child_exited = child_watch_source->poll.revents & G_IO_IN;
3729 * Note: We do _not_ check for the special value of STILL_ACTIVE
3730 * since we know that the process has exited and doing so runs into
3731 * problems if the child process "happens to return STILL_ACTIVE(259)"
3732 * as Microsoft's Platform SDK puts it.
3734 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3736 gchar *emsg = g_win32_error_message (GetLastError ());
3737 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3740 child_watch_source->child_status = -1;
3743 child_watch_source->child_status = child_status;
3746 return child_exited;
3749 #else /* G_OS_WIN32 */
3752 check_for_child_exited (GSource *source)
3754 GChildWatchSource *child_watch_source;
3757 /* protect against another SIGCHLD in the middle of this call */
3758 count = child_watch_count;
3760 child_watch_source = (GChildWatchSource *) source;
3762 if (child_watch_source->child_exited)
3765 if (child_watch_source->count < count)
3769 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3771 child_watch_source->child_status = child_status;
3772 child_watch_source->child_exited = TRUE;
3774 child_watch_source->count = count;
3777 return child_watch_source->child_exited;
3781 g_child_watch_prepare (GSource *source,
3786 return check_for_child_exited (source);
3791 g_child_watch_check (GSource *source)
3793 return check_for_child_exited (source);
3796 #endif /* G_OS_WIN32 */
3799 g_child_watch_dispatch (GSource *source,
3800 GSourceFunc callback,
3803 GChildWatchSource *child_watch_source;
3804 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3806 child_watch_source = (GChildWatchSource *) source;
3810 g_warning ("Child watch source dispatched without callback\n"
3811 "You must call g_source_set_callback().");
3815 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3817 /* We never keep a child watch source around as the child is gone */
3824 g_child_watch_signal_handler (int signum)
3826 child_watch_count ++;
3828 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3830 write (child_watch_wake_up_pipe[1], "B", 1);
3834 /* We count on the signal interrupting the poll in the same thread.
3840 g_child_watch_source_init_single (void)
3842 struct sigaction action;
3844 g_assert (! g_thread_supported());
3845 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3847 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3849 action.sa_handler = g_child_watch_signal_handler;
3850 sigemptyset (&action.sa_mask);
3851 action.sa_flags = SA_NOCLDSTOP;
3852 sigaction (SIGCHLD, &action, NULL);
3856 child_watch_helper_thread (gpointer data)
3863 read (child_watch_wake_up_pipe[0], b, 20);
3865 /* We were woken up. Wake up all other contexts in all other threads */
3866 G_LOCK (main_context_list);
3867 for (list = main_context_list; list; list = list->next)
3869 GMainContext *context;
3871 context = list->data;
3872 if (g_atomic_int_get (&context->ref_count) > 0)
3873 /* Due to racing conditions we can find ref_count == 0, in
3874 * that case, however, the context is still not destroyed
3875 * and no poll can be active, otherwise the ref_count
3877 g_main_context_wakeup (context);
3879 G_UNLOCK (main_context_list);
3886 g_child_watch_source_init_multi_threaded (void)
3888 GError *error = NULL;
3889 struct sigaction action;
3891 g_assert (g_thread_supported());
3893 if (pipe (child_watch_wake_up_pipe) < 0)
3894 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3895 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3897 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3898 /* FIXME: Think this through for races */
3899 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3900 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3901 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3903 action.sa_handler = g_child_watch_signal_handler;
3904 sigemptyset (&action.sa_mask);
3905 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3906 sigaction (SIGCHLD, &action, NULL);
3910 g_child_watch_source_init_promote_single_to_threaded (void)
3912 g_child_watch_source_init_multi_threaded ();
3916 g_child_watch_source_init (void)
3918 if (g_thread_supported())
3920 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3921 g_child_watch_source_init_multi_threaded ();
3922 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3923 g_child_watch_source_init_promote_single_to_threaded ();
3927 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3928 g_child_watch_source_init_single ();
3932 #endif /* !G_OS_WIN32 */
3935 * g_child_watch_source_new:
3936 * @pid: process id of a child process to watch. On Windows, a HANDLE
3937 * for the process to watch (which actually doesn't have to be a child).
3939 * Creates a new child_watch source.
3941 * The source will not initially be associated with any #GMainContext
3942 * and must be added to one with g_source_attach() before it will be
3945 * Note that child watch sources can only be used in conjunction with
3946 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
3949 * Note that on platforms where #GPid must be explicitly closed
3950 * (see g_spawn_close_pid()) @pid must not be closed while the
3951 * source is still active. Typically, you will want to call
3952 * g_spawn_close_pid() in the callback function for the source.
3954 * Note further that using g_child_watch_source_new() is not
3955 * compatible with calling <literal>waitpid(-1)</literal> in
3956 * the application. Calling waitpid() for individual pids will
3959 * Return value: the newly-created child watch source
3964 g_child_watch_source_new (GPid pid)
3966 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
3967 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
3970 child_watch_source->poll.fd = (int)pid;
3971 child_watch_source->poll.events = G_IO_IN;
3973 g_source_add_poll (source, &child_watch_source->poll);
3974 #else /* G_OS_WIN32 */
3975 g_child_watch_source_init ();
3976 #endif /* G_OS_WIN32 */
3978 child_watch_source->pid = pid;
3984 * g_child_watch_add_full:
3985 * @priority: the priority of the idle source. Typically this will be in the
3986 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3987 * @pid: process id of a child process to watch
3988 * @function: function to call
3989 * @data: data to pass to @function
3990 * @notify: function to call when the idle is removed, or %NULL
3992 * Sets a function to be called when the child indicated by @pid
3993 * exits, at the priority @priority.
3995 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
3996 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
3997 * the spawn function for the child watching to work.
3999 * Note that on platforms where #GPid must be explicitly closed
4000 * (see g_spawn_close_pid()) @pid must not be closed while the
4001 * source is still active. Typically, you will want to call
4002 * g_spawn_close_pid() in the callback function for the source.
4004 * GLib supports only a single callback per process id.
4006 * Return value: the ID (greater than 0) of the event source.
4011 g_child_watch_add_full (gint priority,
4013 GChildWatchFunc function,
4015 GDestroyNotify notify)
4020 g_return_val_if_fail (function != NULL, 0);
4022 source = g_child_watch_source_new (pid);
4024 if (priority != G_PRIORITY_DEFAULT)
4025 g_source_set_priority (source, priority);
4027 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4028 id = g_source_attach (source, NULL);
4029 g_source_unref (source);
4035 * g_child_watch_add:
4036 * @pid: process id of a child process to watch
4037 * @function: function to call
4038 * @data: data to pass to @function
4040 * Sets a function to be called when the child indicated by @pid
4041 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4043 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4044 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4045 * the spawn function for the child watching to work.
4047 * Note that on platforms where #GPid must be explicitly closed
4048 * (see g_spawn_close_pid()) @pid must not be closed while the
4049 * source is still active. Typically, you will want to call
4050 * g_spawn_close_pid() in the callback function for the source.
4052 * GLib supports only a single callback per process id.
4054 * Return value: the ID (greater than 0) of the event source.
4059 g_child_watch_add (GPid pid,
4060 GChildWatchFunc function,
4063 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4067 /* Idle functions */
4070 g_idle_prepare (GSource *source,
4079 g_idle_check (GSource *source)
4085 g_idle_dispatch (GSource *source,
4086 GSourceFunc callback,
4091 g_warning ("Idle source dispatched without callback\n"
4092 "You must call g_source_set_callback().");
4096 return callback (user_data);
4100 * g_idle_source_new:
4102 * Creates a new idle source.
4104 * The source will not initially be associated with any #GMainContext
4105 * and must be added to one with g_source_attach() before it will be
4106 * executed. Note that the default priority for idle sources is
4107 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4108 * have a default priority of %G_PRIORITY_DEFAULT.
4110 * Return value: the newly-created idle source
4113 g_idle_source_new (void)
4117 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4118 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4125 * @priority: the priority of the idle source. Typically this will be in the
4126 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4127 * @function: function to call
4128 * @data: data to pass to @function
4129 * @notify: function to call when the idle is removed, or %NULL
4131 * Adds a function to be called whenever there are no higher priority
4132 * events pending. If the function returns %FALSE it is automatically
4133 * removed from the list of event sources and will not be called again.
4135 * Return value: the ID (greater than 0) of the event source.
4138 g_idle_add_full (gint priority,
4139 GSourceFunc function,
4141 GDestroyNotify notify)
4146 g_return_val_if_fail (function != NULL, 0);
4148 source = g_idle_source_new ();
4150 if (priority != G_PRIORITY_DEFAULT_IDLE)
4151 g_source_set_priority (source, priority);
4153 g_source_set_callback (source, function, data, notify);
4154 id = g_source_attach (source, NULL);
4155 g_source_unref (source);
4162 * @function: function to call
4163 * @data: data to pass to @function.
4165 * Adds a function to be called whenever there are no higher priority
4166 * events pending to the default main loop. The function is given the
4167 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4168 * returns %FALSE it is automatically removed from the list of event
4169 * sources and will not be called again.
4171 * Return value: the ID (greater than 0) of the event source.
4174 g_idle_add (GSourceFunc function,
4177 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4181 * g_idle_remove_by_data:
4182 * @data: the data for the idle source's callback.
4184 * Removes the idle function with the given data.
4186 * Return value: %TRUE if an idle source was found and removed.
4189 g_idle_remove_by_data (gpointer data)
4191 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4194 #define __G_MAIN_C__
4195 #include "galiasdef.c"