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 enable debugging printouts if the
37 * environment variable G_MAIN_POLL_DEBUG is set to some value.
39 /* #define G_MAIN_POLL_DEBUG */
42 /* Always enable debugging printout on Windows, as it is more often
45 #define G_MAIN_POLL_DEBUG
49 #include "gthreadprivate.h"
51 #include <sys/types.h>
54 #ifdef HAVE_SYS_TIME_H
56 #endif /* HAVE_SYS_TIME_H */
57 #ifdef GLIB_HAVE_SYS_POLL_H
58 # include <sys/poll.h>
59 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
60 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
62 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
63 * so we prefer our own poll emulation.
65 #if defined(_POLL_EMUL_H_) || defined(BROKEN_POLL)
69 #endif /* GLIB_HAVE_SYS_POLL_H */
72 #endif /* HAVE_UNISTD_H */
78 #endif /* G_OS_WIN32 */
81 #include <sys/socket.h>
83 #endif /* G_OS_BEOS */
92 #ifdef G_MAIN_POLL_DEBUG
94 #define GPOLLFD_FORMAT "%#x"
96 #define GPOLLFD_FORMAT "%d"
102 typedef struct _GTimeoutSource GTimeoutSource;
103 typedef struct _GChildWatchSource GChildWatchSource;
104 typedef struct _GPollRec GPollRec;
105 typedef struct _GSourceCallback GSourceCallback;
109 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
110 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
113 #ifdef G_THREADS_ENABLED
114 typedef struct _GMainWaiter GMainWaiter;
123 typedef struct _GMainDispatch GMainDispatch;
125 struct _GMainDispatch
128 GSList *dispatching_sources; /* stack of current sources */
131 #ifdef G_MAIN_POLL_DEBUG
132 static gboolean g_main_poll_debug = FALSE;
137 #ifdef G_THREADS_ENABLED
138 /* The following lock is used for both the list of sources
139 * and the list of poll records
150 GPtrArray *pending_dispatches;
151 gint timeout; /* Timeout for current iteration */
154 GSource *source_list;
155 gint in_check_or_prepare;
157 GPollRec *poll_records;
158 guint n_poll_records;
159 GPollFD *cached_poll_array;
160 guint cached_poll_array_size;
162 #ifdef G_THREADS_ENABLED
164 /* this pipe is used to wake up the main loop when a source is added.
166 gint wake_up_pipe[2];
167 #else /* G_OS_WIN32 */
168 HANDLE wake_up_semaphore;
169 #endif /* G_OS_WIN32 */
172 gboolean poll_waiting;
174 /* Flag indicating whether the set of fd's changed during a poll */
175 gboolean poll_changed;
176 #endif /* G_THREADS_ENABLED */
180 GTimeVal current_time;
181 gboolean time_is_current;
184 struct _GSourceCallback
189 GDestroyNotify notify;
194 GMainContext *context;
199 struct _GTimeoutSource
207 struct _GChildWatchSource
214 #else /* G_OS_WIN32 */
216 gboolean child_exited;
217 #endif /* G_OS_WIN32 */
227 #ifdef G_THREADS_ENABLED
228 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
229 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
230 #define G_THREAD_SELF g_thread_self ()
232 #define LOCK_CONTEXT(context) (void)0
233 #define UNLOCK_CONTEXT(context) (void)0
234 #define G_THREAD_SELF NULL
237 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
238 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
239 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
241 #define SOURCE_UNREF(source, context) \
243 if ((source)->ref_count > 1) \
244 (source)->ref_count--; \
246 g_source_unref_internal ((source), (context), TRUE); \
250 /* Forward declarations */
252 static void g_source_unref_internal (GSource *source,
253 GMainContext *context,
255 static void g_source_destroy_internal (GSource *source,
256 GMainContext *context,
258 static void g_main_context_poll (GMainContext *context,
263 static void g_main_context_add_poll_unlocked (GMainContext *context,
266 static void g_main_context_remove_poll_unlocked (GMainContext *context,
268 static void g_main_context_wakeup_unlocked (GMainContext *context);
270 static gboolean g_timeout_prepare (GSource *source,
272 static gboolean g_timeout_check (GSource *source);
273 static gboolean g_timeout_dispatch (GSource *source,
274 GSourceFunc callback,
276 static gboolean g_child_watch_prepare (GSource *source,
278 static gboolean g_child_watch_check (GSource *source);
279 static gboolean g_child_watch_dispatch (GSource *source,
280 GSourceFunc callback,
282 static gboolean g_idle_prepare (GSource *source,
284 static gboolean g_idle_check (GSource *source);
285 static gboolean g_idle_dispatch (GSource *source,
286 GSourceFunc callback,
289 G_LOCK_DEFINE_STATIC (main_loop);
290 static GMainContext *default_main_context;
291 static GSList *main_contexts_without_pipe = NULL;
294 /* Child status monitoring code */
296 CHILD_WATCH_UNINITIALIZED,
297 CHILD_WATCH_INITIALIZED_SINGLE,
298 CHILD_WATCH_INITIALIZED_THREADED
300 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
301 static gint child_watch_count = 1;
302 static gint child_watch_wake_up_pipe[2] = {0, 0};
303 #endif /* !G_OS_WIN32 */
304 G_LOCK_DEFINE_STATIC (main_context_list);
305 static GSList *main_context_list = NULL;
307 static gint timer_perturb = -1;
309 GSourceFuncs g_timeout_funcs =
317 GSourceFuncs g_child_watch_funcs =
319 g_child_watch_prepare,
321 g_child_watch_dispatch,
325 GSourceFuncs g_idle_funcs =
334 /* SunOS has poll, but doesn't provide a prototype. */
335 # if defined (sun) && !defined (__SVR4)
336 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
338 #else /* !HAVE_POLL */
343 g_poll (GPollFD *fds,
347 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
348 gboolean poll_msgs = FALSE;
354 #ifdef G_MAIN_POLL_DEBUG
355 if (g_main_poll_debug)
356 g_print ("g_poll: waiting for");
358 for (f = fds; f < &fds[nfds]; ++f)
361 if (f->fd == G_WIN32_MSG_HANDLE)
364 #ifdef G_MAIN_POLL_DEBUG
365 if (g_main_poll_debug)
369 else if (nhandles == MAXIMUM_WAIT_OBJECTS)
371 g_warning (G_STRLOC ": Too many handles to wait for!\n");
376 #ifdef G_MAIN_POLL_DEBUG
377 if (g_main_poll_debug)
378 g_print (" %#x", f->fd);
380 handles[nhandles++] = (HANDLE) f->fd;
383 #ifdef G_MAIN_POLL_DEBUG
384 if (g_main_poll_debug)
393 /* Waiting for messages, and maybe events
394 * -> First PeekMessage
396 #ifdef G_MAIN_POLL_DEBUG
397 if (g_main_poll_debug)
398 g_print ("PeekMessage\n");
400 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
401 ready = WAIT_OBJECT_0 + nhandles;
404 /* Wait for either message or event
405 * -> Use MsgWaitForMultipleObjectsEx
407 #ifdef G_MAIN_POLL_DEBUG
408 if (g_main_poll_debug)
409 g_print ("MsgWaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
411 ready = MsgWaitForMultipleObjectsEx (nhandles, handles, timeout,
412 QS_ALLINPUT, MWMO_ALERTABLE);
414 if (ready == WAIT_FAILED)
416 gchar *emsg = g_win32_error_message (GetLastError ());
417 g_warning (G_STRLOC ": MsgWaitForMultipleObjectsEx() failed: %s", emsg);
422 else if (nhandles == 0)
424 /* No handles to wait for, just the timeout */
425 if (timeout == INFINITE)
429 SleepEx (timeout, TRUE);
430 ready = WAIT_TIMEOUT;
435 /* Wait for just events
436 * -> Use WaitForMultipleObjectsEx
438 #ifdef G_MAIN_POLL_DEBUG
439 if (g_main_poll_debug)
440 g_print ("WaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
442 ready = WaitForMultipleObjectsEx (nhandles, handles, FALSE, timeout, TRUE);
443 if (ready == WAIT_FAILED)
445 gchar *emsg = g_win32_error_message (GetLastError ());
446 g_warning (G_STRLOC ": WaitForMultipleObjectsEx() failed: %s", emsg);
451 #ifdef G_MAIN_POLL_DEBUG
452 if (g_main_poll_debug)
453 g_print ("wait returns %ld%s\n",
455 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
456 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
457 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
459 for (f = fds; f < &fds[nfds]; ++f)
462 if (ready == WAIT_FAILED)
464 else if (ready == WAIT_TIMEOUT ||
465 ready == WAIT_IO_COMPLETION)
467 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
469 for (f = fds; f < &fds[nfds]; ++f)
472 if (f->events & G_IO_IN)
473 if (f->fd == G_WIN32_MSG_HANDLE)
474 f->revents |= G_IO_IN;
477 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
478 for (f = fds; f < &fds[nfds]; ++f)
480 if (f->fd == (gint) handles[ready - WAIT_OBJECT_0])
482 f->revents = f->events;
483 #ifdef G_MAIN_POLL_DEBUG
484 if (g_main_poll_debug)
485 g_print ("g_poll: got event %#x\n", f->fd);
493 #else /* !G_OS_WIN32 */
495 /* The following implementation of poll() comes from the GNU C Library.
496 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
499 #include <string.h> /* for bzero on BSD systems */
501 #ifdef HAVE_SYS_SELECT_H
502 #include <sys/select.h>
503 #endif /* HAVE_SYS_SELECT_H */
507 #endif /* G_OS_BEOS */
510 # define SELECT_MASK fd_set
511 #else /* !NO_FD_SET */
513 typedef long fd_mask;
516 # define SELECT_MASK void
518 # define SELECT_MASK int
519 # endif /* !_IBMR2 */
520 #endif /* !NO_FD_SET */
523 g_poll (GPollFD *fds,
528 SELECT_MASK rset, wset, xset;
537 for (f = fds; f < &fds[nfds]; ++f)
540 if (f->events & G_IO_IN)
541 FD_SET (f->fd, &rset);
542 if (f->events & G_IO_OUT)
543 FD_SET (f->fd, &wset);
544 if (f->events & G_IO_PRI)
545 FD_SET (f->fd, &xset);
546 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
550 tv.tv_sec = timeout / 1000;
551 tv.tv_usec = (timeout % 1000) * 1000;
553 ready = select (maxfd + 1, &rset, &wset, &xset,
554 timeout == -1 ? NULL : &tv);
556 for (f = fds; f < &fds[nfds]; ++f)
561 if (FD_ISSET (f->fd, &rset))
562 f->revents |= G_IO_IN;
563 if (FD_ISSET (f->fd, &wset))
564 f->revents |= G_IO_OUT;
565 if (FD_ISSET (f->fd, &xset))
566 f->revents |= G_IO_PRI;
573 #endif /* !G_OS_WIN32 */
575 #endif /* !HAVE_POLL */
578 * g_main_context_ref:
579 * @context: a #GMainContext
581 * Increases the reference count on a #GMainContext object by one.
583 * Returns: the @context that was passed in (since 2.6)
586 g_main_context_ref (GMainContext *context)
588 g_return_val_if_fail (context != NULL, NULL);
589 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
591 g_atomic_int_inc (&context->ref_count);
597 poll_rec_list_free (GMainContext *context,
600 g_slice_free_chain (GPollRec, list, next);
604 * g_main_context_unref:
605 * @context: a #GMainContext
607 * Decreases the reference count on a #GMainContext object by one. If
608 * the result is zero, free the context and free all associated memory.
611 g_main_context_unref (GMainContext *context)
614 g_return_if_fail (context != NULL);
615 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
617 if (!g_atomic_int_dec_and_test (&context->ref_count))
620 G_LOCK (main_context_list);
621 main_context_list = g_slist_remove (main_context_list, context);
622 G_UNLOCK (main_context_list);
624 source = context->source_list;
627 GSource *next = source->next;
628 g_source_destroy_internal (source, context, FALSE);
632 #ifdef G_THREADS_ENABLED
633 g_static_mutex_free (&context->mutex);
636 g_ptr_array_free (context->pending_dispatches, TRUE);
637 g_free (context->cached_poll_array);
639 poll_rec_list_free (context, context->poll_records);
641 #ifdef G_THREADS_ENABLED
642 if (g_thread_supported())
645 close (context->wake_up_pipe[0]);
646 close (context->wake_up_pipe[1]);
648 CloseHandle (context->wake_up_semaphore);
652 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
655 if (context->cond != NULL)
656 g_cond_free (context->cond);
662 #ifdef G_THREADS_ENABLED
664 g_main_context_init_pipe (GMainContext *context)
667 if (context->wake_up_pipe[0] != -1)
669 if (pipe (context->wake_up_pipe) < 0)
670 g_error ("Cannot create pipe main loop wake-up: %s\n",
673 fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC);
674 fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC);
676 context->wake_up_rec.fd = context->wake_up_pipe[0];
677 context->wake_up_rec.events = G_IO_IN;
679 if (context->wake_up_semaphore != NULL)
681 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
682 if (context->wake_up_semaphore == NULL)
683 g_error ("Cannot create wake-up semaphore: %s",
684 g_win32_error_message (GetLastError ()));
685 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
686 context->wake_up_rec.events = G_IO_IN;
687 # ifdef G_MAIN_POLL_DEBUG
688 if (g_main_poll_debug)
689 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
692 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
696 _g_main_thread_init (void)
698 GSList *curr = main_contexts_without_pipe;
701 g_main_context_init_pipe ((GMainContext *)curr->data);
704 g_slist_free (main_contexts_without_pipe);
705 main_contexts_without_pipe = NULL;
707 #endif /* G_THREADS_ENABLED */
710 * g_main_context_new:
712 * Creates a new #GMainContext structure.
714 * Return value: the new #GMainContext
717 g_main_context_new (void)
719 GMainContext *context = g_new0 (GMainContext, 1);
721 #ifdef G_THREADS_ENABLED
722 g_static_mutex_init (&context->mutex);
724 context->owner = NULL;
725 context->waiters = NULL;
728 context->wake_up_pipe[0] = -1;
729 context->wake_up_pipe[1] = -1;
731 context->wake_up_semaphore = NULL;
735 context->ref_count = 1;
737 context->next_id = 1;
739 context->source_list = NULL;
742 context->poll_func = (GPollFunc)poll;
744 context->poll_func = g_poll;
747 context->cached_poll_array = NULL;
748 context->cached_poll_array_size = 0;
750 context->pending_dispatches = g_ptr_array_new ();
752 context->time_is_current = FALSE;
754 #ifdef G_THREADS_ENABLED
755 if (g_thread_supported ())
756 g_main_context_init_pipe (context);
758 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
762 G_LOCK (main_context_list);
763 main_context_list = g_slist_append (main_context_list, context);
765 #ifdef G_MAIN_POLL_DEBUG
766 if (g_main_poll_debug)
767 g_print ("created context=%p\n", context);
770 G_UNLOCK (main_context_list);
776 * g_main_context_default:
778 * Returns the default main context. This is the main context used
779 * for main loop functions when a main loop is not explicitly
782 * Return value: the default main context.
785 g_main_context_default (void)
791 if (!default_main_context)
793 default_main_context = g_main_context_new ();
794 #ifdef G_MAIN_POLL_DEBUG
795 if (g_main_poll_debug)
796 g_print ("default context=%p\n", default_main_context);
800 G_UNLOCK (main_loop);
802 return default_main_context;
805 /* Hooks for adding to the main loop */
809 * @source_funcs: structure containing functions that implement
810 * the sources behavior.
811 * @struct_size: size of the #GSource structure to create.
813 * Creates a new #GSource structure. The size is specified to
814 * allow creating structures derived from #GSource that contain
815 * additional data. The size passed in must be at least
816 * <literal>sizeof (GSource)</literal>.
818 * The source will not initially be associated with any #GMainContext
819 * and must be added to one with g_source_attach() before it will be
822 * Return value: the newly-created #GSource.
825 g_source_new (GSourceFuncs *source_funcs,
830 g_return_val_if_fail (source_funcs != NULL, NULL);
831 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
833 source = (GSource*) g_malloc0 (struct_size);
835 source->source_funcs = source_funcs;
836 source->ref_count = 1;
838 source->priority = G_PRIORITY_DEFAULT;
840 source->flags = G_HOOK_FLAG_ACTIVE;
842 /* NULL/0 initialization for all other fields */
847 /* Holds context's lock
850 g_source_list_add (GSource *source,
851 GMainContext *context)
853 GSource *tmp_source, *last_source;
856 tmp_source = context->source_list;
857 while (tmp_source && tmp_source->priority <= source->priority)
859 last_source = tmp_source;
860 tmp_source = tmp_source->next;
863 source->next = tmp_source;
865 tmp_source->prev = source;
867 source->prev = last_source;
869 last_source->next = source;
871 context->source_list = source;
874 /* Holds context's lock
877 g_source_list_remove (GSource *source,
878 GMainContext *context)
881 source->prev->next = source->next;
883 context->source_list = source->next;
886 source->next->prev = source->prev;
894 * @source: a #GSource
895 * @context: a #GMainContext (if %NULL, the default context will be used)
897 * Adds a #GSource to a @context so that it will be executed within
900 * Return value: the ID (greater than 0) for the source within the
904 g_source_attach (GSource *source,
905 GMainContext *context)
910 g_return_val_if_fail (source->context == NULL, 0);
911 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
914 context = g_main_context_default ();
916 LOCK_CONTEXT (context);
918 source->context = context;
919 result = source->source_id = context->next_id++;
922 g_source_list_add (source, context);
924 tmp_list = source->poll_fds;
927 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
928 tmp_list = tmp_list->next;
931 #ifdef G_THREADS_ENABLED
932 /* Now wake up the main loop if it is waiting in the poll() */
933 g_main_context_wakeup_unlocked (context);
936 UNLOCK_CONTEXT (context);
942 g_source_destroy_internal (GSource *source,
943 GMainContext *context,
947 LOCK_CONTEXT (context);
949 if (!SOURCE_DESTROYED (source))
952 gpointer old_cb_data;
953 GSourceCallbackFuncs *old_cb_funcs;
955 source->flags &= ~G_HOOK_FLAG_ACTIVE;
957 old_cb_data = source->callback_data;
958 old_cb_funcs = source->callback_funcs;
960 source->callback_data = NULL;
961 source->callback_funcs = NULL;
965 UNLOCK_CONTEXT (context);
966 old_cb_funcs->unref (old_cb_data);
967 LOCK_CONTEXT (context);
970 if (!SOURCE_BLOCKED (source))
972 tmp_list = source->poll_fds;
975 g_main_context_remove_poll_unlocked (context, tmp_list->data);
976 tmp_list = tmp_list->next;
980 g_source_unref_internal (source, context, TRUE);
984 UNLOCK_CONTEXT (context);
989 * @source: a #GSource
991 * Removes a source from its #GMainContext, if any, and mark it as
992 * destroyed. The source cannot be subsequently added to another
996 g_source_destroy (GSource *source)
998 GMainContext *context;
1000 g_return_if_fail (source != NULL);
1002 context = source->context;
1005 g_source_destroy_internal (source, context, FALSE);
1007 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1012 * @source: a #GSource
1014 * Returns the numeric ID for a particular source. The ID of a source
1015 * is a positive integer which is unique within a particular main loop
1016 * context. The reverse
1017 * mapping from ID to source is done by g_main_context_find_source_by_id().
1019 * Return value: the ID (greater than 0) for the source
1022 g_source_get_id (GSource *source)
1026 g_return_val_if_fail (source != NULL, 0);
1027 g_return_val_if_fail (source->context != NULL, 0);
1029 LOCK_CONTEXT (source->context);
1030 result = source->source_id;
1031 UNLOCK_CONTEXT (source->context);
1037 * g_source_get_context:
1038 * @source: a #GSource
1040 * Gets the #GMainContext with which the source is associated.
1041 * Calling this function on a destroyed source is an error.
1043 * Return value: the #GMainContext with which the source is associated,
1044 * or %NULL if the context has not yet been added
1048 g_source_get_context (GSource *source)
1050 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
1052 return source->context;
1056 * g_source_add_poll:
1057 * @source:a #GSource
1058 * @fd: a #GPollFD structure holding information about a file
1059 * descriptor to watch.
1061 * Adds a file descriptor to the set of file descriptors polled for
1062 * this source. This is usually combined with g_source_new() to add an
1063 * event source. The event source's check function will typically test
1064 * the @revents field in the #GPollFD struct and return %TRUE if events need
1068 g_source_add_poll (GSource *source,
1071 GMainContext *context;
1073 g_return_if_fail (source != NULL);
1074 g_return_if_fail (fd != NULL);
1075 g_return_if_fail (!SOURCE_DESTROYED (source));
1077 context = source->context;
1080 LOCK_CONTEXT (context);
1082 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1086 if (!SOURCE_BLOCKED (source))
1087 g_main_context_add_poll_unlocked (context, source->priority, fd);
1088 UNLOCK_CONTEXT (context);
1093 * g_source_remove_poll:
1094 * @source:a #GSource
1095 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1097 * Removes a file descriptor from the set of file descriptors polled for
1101 g_source_remove_poll (GSource *source,
1104 GMainContext *context;
1106 g_return_if_fail (source != NULL);
1107 g_return_if_fail (fd != NULL);
1108 g_return_if_fail (!SOURCE_DESTROYED (source));
1110 context = source->context;
1113 LOCK_CONTEXT (context);
1115 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1119 if (!SOURCE_BLOCKED (source))
1120 g_main_context_remove_poll_unlocked (context, fd);
1121 UNLOCK_CONTEXT (context);
1126 * g_source_set_callback_indirect:
1127 * @source: the source
1128 * @callback_data: pointer to callback data "object"
1129 * @callback_funcs: functions for reference counting @callback_data
1130 * and getting the callback and data
1132 * Sets the callback function storing the data as a refcounted callback
1133 * "object". This is used internally. Note that calling
1134 * g_source_set_callback_indirect() assumes
1135 * an initial reference count on @callback_data, and thus
1136 * @callback_funcs->unref will eventually be called once more
1137 * than @callback_funcs->ref.
1140 g_source_set_callback_indirect (GSource *source,
1141 gpointer callback_data,
1142 GSourceCallbackFuncs *callback_funcs)
1144 GMainContext *context;
1145 gpointer old_cb_data;
1146 GSourceCallbackFuncs *old_cb_funcs;
1148 g_return_if_fail (source != NULL);
1149 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1151 context = source->context;
1154 LOCK_CONTEXT (context);
1156 old_cb_data = source->callback_data;
1157 old_cb_funcs = source->callback_funcs;
1159 source->callback_data = callback_data;
1160 source->callback_funcs = callback_funcs;
1163 UNLOCK_CONTEXT (context);
1166 old_cb_funcs->unref (old_cb_data);
1170 g_source_callback_ref (gpointer cb_data)
1172 GSourceCallback *callback = cb_data;
1174 callback->ref_count++;
1179 g_source_callback_unref (gpointer cb_data)
1181 GSourceCallback *callback = cb_data;
1183 callback->ref_count--;
1184 if (callback->ref_count == 0)
1186 if (callback->notify)
1187 callback->notify (callback->data);
1193 g_source_callback_get (gpointer cb_data,
1198 GSourceCallback *callback = cb_data;
1200 *func = callback->func;
1201 *data = callback->data;
1204 static GSourceCallbackFuncs g_source_callback_funcs = {
1205 g_source_callback_ref,
1206 g_source_callback_unref,
1207 g_source_callback_get,
1211 * g_source_set_callback:
1212 * @source: the source
1213 * @func: a callback function
1214 * @data: the data to pass to callback function
1215 * @notify: a function to call when @data is no longer in use, or %NULL.
1217 * Sets the callback function for a source. The callback for a source is
1218 * called from the source's dispatch function.
1220 * The exact type of @func depends on the type of source; ie. you
1221 * should not count on @func being called with @data as its first
1224 * Typically, you won't use this function. Instead use functions specific
1225 * to the type of source you are using.
1228 g_source_set_callback (GSource *source,
1231 GDestroyNotify notify)
1233 GSourceCallback *new_callback;
1235 g_return_if_fail (source != NULL);
1237 new_callback = g_new (GSourceCallback, 1);
1239 new_callback->ref_count = 1;
1240 new_callback->func = func;
1241 new_callback->data = data;
1242 new_callback->notify = notify;
1244 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1249 * g_source_set_funcs:
1250 * @source: a #GSource
1251 * @funcs: the new #GSourceFuncs
1253 * Sets the source functions (can be used to override
1254 * default implementations) of an unattached source.
1259 g_source_set_funcs (GSource *source,
1260 GSourceFuncs *funcs)
1262 g_return_if_fail (source != NULL);
1263 g_return_if_fail (source->context == NULL);
1264 g_return_if_fail (source->ref_count > 0);
1265 g_return_if_fail (funcs != NULL);
1267 source->source_funcs = funcs;
1271 * g_source_set_priority:
1272 * @source: a #GSource
1273 * @priority: the new priority.
1275 * Sets the priority of a source. While the main loop is being
1276 * run, a source will be dispatched if it is ready to be dispatched and no sources
1277 * at a higher (numerically smaller) priority are ready to be dispatched.
1280 g_source_set_priority (GSource *source,
1284 GMainContext *context;
1286 g_return_if_fail (source != NULL);
1288 context = source->context;
1291 LOCK_CONTEXT (context);
1293 source->priority = priority;
1297 /* Remove the source from the context's source and then
1298 * add it back so it is sorted in the correct plcae
1300 g_source_list_remove (source, source->context);
1301 g_source_list_add (source, source->context);
1303 if (!SOURCE_BLOCKED (source))
1305 tmp_list = source->poll_fds;
1308 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1309 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1311 tmp_list = tmp_list->next;
1315 UNLOCK_CONTEXT (source->context);
1320 * g_source_get_priority:
1321 * @source: a #GSource
1323 * Gets the priority of a source.
1325 * Return value: the priority of the source
1328 g_source_get_priority (GSource *source)
1330 g_return_val_if_fail (source != NULL, 0);
1332 return source->priority;
1336 * g_source_set_can_recurse:
1337 * @source: a #GSource
1338 * @can_recurse: whether recursion is allowed for this source
1340 * Sets whether a source can be called recursively. If @can_recurse is
1341 * %TRUE, then while the source is being dispatched then this source
1342 * will be processed normally. Otherwise, all processing of this
1343 * source is blocked until the dispatch function returns.
1346 g_source_set_can_recurse (GSource *source,
1347 gboolean can_recurse)
1349 GMainContext *context;
1351 g_return_if_fail (source != NULL);
1353 context = source->context;
1356 LOCK_CONTEXT (context);
1359 source->flags |= G_SOURCE_CAN_RECURSE;
1361 source->flags &= ~G_SOURCE_CAN_RECURSE;
1364 UNLOCK_CONTEXT (context);
1368 * g_source_get_can_recurse:
1369 * @source: a #GSource
1371 * Checks whether a source is allowed to be called recursively.
1372 * see g_source_set_can_recurse().
1374 * Return value: whether recursion is allowed.
1377 g_source_get_can_recurse (GSource *source)
1379 g_return_val_if_fail (source != NULL, FALSE);
1381 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1386 * @source: a #GSource
1388 * Increases the reference count on a source by one.
1390 * Return value: @source
1393 g_source_ref (GSource *source)
1395 GMainContext *context;
1397 g_return_val_if_fail (source != NULL, NULL);
1399 context = source->context;
1402 LOCK_CONTEXT (context);
1404 source->ref_count++;
1407 UNLOCK_CONTEXT (context);
1412 /* g_source_unref() but possible to call within context lock
1415 g_source_unref_internal (GSource *source,
1416 GMainContext *context,
1419 gpointer old_cb_data = NULL;
1420 GSourceCallbackFuncs *old_cb_funcs = NULL;
1422 g_return_if_fail (source != NULL);
1424 if (!have_lock && context)
1425 LOCK_CONTEXT (context);
1427 source->ref_count--;
1428 if (source->ref_count == 0)
1430 old_cb_data = source->callback_data;
1431 old_cb_funcs = source->callback_funcs;
1433 source->callback_data = NULL;
1434 source->callback_funcs = NULL;
1436 if (context && !SOURCE_DESTROYED (source))
1438 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1439 source->ref_count++;
1442 g_source_list_remove (source, context);
1444 if (source->source_funcs->finalize)
1445 source->source_funcs->finalize (source);
1447 g_slist_free (source->poll_fds);
1448 source->poll_fds = NULL;
1452 if (!have_lock && context)
1453 UNLOCK_CONTEXT (context);
1458 UNLOCK_CONTEXT (context);
1460 old_cb_funcs->unref (old_cb_data);
1463 LOCK_CONTEXT (context);
1469 * @source: a #GSource
1471 * Decreases the reference count of a source by one. If the
1472 * resulting reference count is zero the source and associated
1473 * memory will be destroyed.
1476 g_source_unref (GSource *source)
1478 g_return_if_fail (source != NULL);
1480 g_source_unref_internal (source, source->context, FALSE);
1484 * g_main_context_find_source_by_id:
1485 * @context: a #GMainContext (if %NULL, the default context will be used)
1486 * @source_id: the source ID, as returned by g_source_get_id().
1488 * Finds a #GSource given a pair of context and ID.
1490 * Return value: the #GSource if found, otherwise, %NULL
1493 g_main_context_find_source_by_id (GMainContext *context,
1498 g_return_val_if_fail (source_id > 0, NULL);
1500 if (context == NULL)
1501 context = g_main_context_default ();
1503 LOCK_CONTEXT (context);
1505 source = context->source_list;
1508 if (!SOURCE_DESTROYED (source) &&
1509 source->source_id == source_id)
1511 source = source->next;
1514 UNLOCK_CONTEXT (context);
1520 * g_main_context_find_source_by_funcs_user_data:
1521 * @context: a #GMainContext (if %NULL, the default context will be used).
1522 * @funcs: the @source_funcs passed to g_source_new().
1523 * @user_data: the user data from the callback.
1525 * Finds a source with the given source functions and user data. If
1526 * multiple sources exist with the same source function and user data,
1527 * the first one found will be returned.
1529 * Return value: the source, if one was found, otherwise %NULL
1532 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1533 GSourceFuncs *funcs,
1538 g_return_val_if_fail (funcs != NULL, NULL);
1540 if (context == NULL)
1541 context = g_main_context_default ();
1543 LOCK_CONTEXT (context);
1545 source = context->source_list;
1548 if (!SOURCE_DESTROYED (source) &&
1549 source->source_funcs == funcs &&
1550 source->callback_funcs)
1552 GSourceFunc callback;
1553 gpointer callback_data;
1555 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1557 if (callback_data == user_data)
1560 source = source->next;
1563 UNLOCK_CONTEXT (context);
1569 * g_main_context_find_source_by_user_data:
1570 * @context: a #GMainContext
1571 * @user_data: the user_data for the callback.
1573 * Finds a source with the given user data for the callback. If
1574 * multiple sources exist with the same user data, the first
1575 * one found will be returned.
1577 * Return value: the source, if one was found, otherwise %NULL
1580 g_main_context_find_source_by_user_data (GMainContext *context,
1585 if (context == NULL)
1586 context = g_main_context_default ();
1588 LOCK_CONTEXT (context);
1590 source = context->source_list;
1593 if (!SOURCE_DESTROYED (source) &&
1594 source->callback_funcs)
1596 GSourceFunc callback;
1597 gpointer callback_data = NULL;
1599 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1601 if (callback_data == user_data)
1604 source = source->next;
1607 UNLOCK_CONTEXT (context);
1614 * @tag: the ID of the source to remove.
1616 * Removes the source with the given id from the default main context.
1618 * a #GSource is given by g_source_get_id(), or will be returned by the
1619 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1620 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1621 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1623 * See also g_source_destroy().
1625 * Return value: %TRUE if the source was found and removed.
1628 g_source_remove (guint tag)
1632 g_return_val_if_fail (tag > 0, FALSE);
1634 source = g_main_context_find_source_by_id (NULL, tag);
1636 g_source_destroy (source);
1638 return source != NULL;
1642 * g_source_remove_by_user_data:
1643 * @user_data: the user_data for the callback.
1645 * Removes a source from the default main loop context given the user
1646 * data for the callback. If multiple sources exist with the same user
1647 * data, only one will be destroyed.
1649 * Return value: %TRUE if a source was found and removed.
1652 g_source_remove_by_user_data (gpointer user_data)
1656 source = g_main_context_find_source_by_user_data (NULL, user_data);
1659 g_source_destroy (source);
1667 * g_source_remove_by_funcs_user_data:
1668 * @funcs: The @source_funcs passed to g_source_new()
1669 * @user_data: the user data for the callback
1671 * Removes a source from the default main loop context given the
1672 * source functions and user data. If multiple sources exist with the
1673 * same source functions and user data, only one will be destroyed.
1675 * Return value: %TRUE if a source was found and removed.
1678 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1683 g_return_val_if_fail (funcs != NULL, FALSE);
1685 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1688 g_source_destroy (source);
1696 * g_get_current_time:
1697 * @result: #GTimeVal structure in which to store current time.
1699 * Equivalent to the UNIX gettimeofday() function, but portable.
1702 g_get_current_time (GTimeVal *result)
1707 g_return_if_fail (result != NULL);
1709 /*this is required on alpha, there the timeval structs are int's
1710 not longs and a cast only would fail horribly*/
1711 gettimeofday (&r, NULL);
1712 result->tv_sec = r.tv_sec;
1713 result->tv_usec = r.tv_usec;
1716 guint64 *time64 = (guint64 *) &ft;
1718 GetSystemTimeAsFileTime (&ft);
1720 /* Convert from 100s of nanoseconds since 1601-01-01
1721 * to Unix epoch. Yes, this is Y2038 unsafe.
1723 *time64 -= G_GINT64_CONSTANT (116444736000000000);
1726 result->tv_sec = *time64 / 1000000;
1727 result->tv_usec = *time64 % 1000000;
1732 g_main_dispatch_free (gpointer dispatch)
1734 g_slice_free (GMainDispatch, dispatch);
1737 /* Running the main loop */
1739 static GMainDispatch *
1742 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1743 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1746 dispatch = g_slice_new0 (GMainDispatch);
1747 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1756 * Returns the depth of the stack of calls to
1757 * g_main_context_dispatch() on any #GMainContext in the current thread.
1758 * That is, when called from the toplevel, it gives 0. When
1759 * called from within a callback from g_main_context_iteration()
1760 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1761 * a callback to a recursive call to g_main_context_iterate(),
1762 * it returns 2. And so forth.
1764 * This function is useful in a situation like the following:
1765 * Imagine an extremely simple "garbage collected" system.
1768 * static GList *free_list;
1771 * allocate_memory (gsize size)
1773 * gpointer result = g_malloc (size);
1774 * free_list = g_list_prepend (free_list, result);
1779 * free_allocated_memory (void)
1782 * for (l = free_list; l; l = l->next);
1784 * g_list_free (free_list);
1792 * g_main_context_iteration (NULL, TRUE);
1793 * free_allocated_memory();
1797 * This works from an application, however, if you want to do the same
1798 * thing from a library, it gets more difficult, since you no longer
1799 * control the main loop. You might think you can simply use an idle
1800 * function to make the call to free_allocated_memory(), but that
1801 * doesn't work, since the idle function could be called from a
1802 * recursive callback. This can be fixed by using g_main_depth()
1806 * allocate_memory (gsize size)
1808 * FreeListBlock *block = g_new (FreeListBlock, 1);
1809 * block->mem = g_malloc (size);
1810 * block->depth = g_main_depth ();
1811 * free_list = g_list_prepend (free_list, block);
1812 * return block->mem;
1816 * free_allocated_memory (void)
1820 * int depth = g_main_depth ();
1821 * for (l = free_list; l; );
1823 * GList *next = l->next;
1824 * FreeListBlock *block = l->data;
1825 * if (block->depth > depth)
1827 * g_free (block->mem);
1829 * free_list = g_list_delete_link (free_list, l);
1837 * There is a temptation to use g_main_depth() to solve
1838 * problems with reentrancy. For instance, while waiting for data
1839 * to be received from the network in response to a menu item,
1840 * the menu item might be selected again. It might seem that
1841 * one could make the menu item's callback return immediately
1842 * and do nothing if g_main_depth() returns a value greater than 1.
1843 * However, this should be avoided since the user then sees selecting
1844 * the menu item do nothing. Furthermore, you'll find yourself adding
1845 * these checks all over your code, since there are doubtless many,
1846 * many things that the user could do. Instead, you can use the
1847 * following techniques:
1852 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1853 * the user from interacting with elements while the main
1854 * loop is recursing.
1859 * Avoid main loop recursion in situations where you can't handle
1860 * arbitrary callbacks. Instead, structure your code so that you
1861 * simply return to the main loop and then get called again when
1862 * there is more work to do.
1867 * Return value: The main loop recursion level in the current thread
1872 GMainDispatch *dispatch = get_dispatch ();
1873 return dispatch->depth;
1877 * g_main_current_source:
1879 * Returns the currently firing source for this thread.
1881 * Return value: The currently firing source or %NULL.
1886 g_main_current_source (void)
1888 GMainDispatch *dispatch = get_dispatch ();
1889 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
1893 * g_source_is_destroyed:
1894 * @source: a #GSource
1896 * Returns whether @source has been destroyed.
1898 * This is important when you operate upon your objects
1899 * from within idle handlers, but may have freed the object
1900 * before the dispatch of your idle handler.
1904 * idle_callback (gpointer data)
1906 * SomeWidget *self = data;
1908 * GDK_THREADS_ENTER (<!-- -->);
1909 * /<!-- -->* do stuff with self *<!-- -->/
1910 * self->idle_id = 0;
1911 * GDK_THREADS_LEAVE (<!-- -->);
1917 * some_widget_do_stuff_later (SomeWidget *self)
1919 * self->idle_id = g_idle_add (idle_callback, self);
1923 * some_widget_finalize (GObject *object)
1925 * SomeWidget *self = SOME_WIDGET (object);
1927 * if (self->idle_id)
1928 * g_source_remove (self->idle_id);
1930 * G_OBJECT_CLASS (parent_class)->finalize (object);
1934 * This will fail in a multi-threaded application if the
1935 * widget is destroyed before the idle handler fires due
1936 * to the use after free in the callback. A solution, to
1937 * this particular problem, is to check to if the source
1938 * has already been destroy within the callback.
1942 * idle_callback (gpointer data)
1944 * SomeWidget *self = data;
1946 * GDK_THREADS_ENTER ();
1947 * if (!g_source_is_destroyed (g_main_current_source ()))
1949 * /<!-- -->* do stuff with self *<!-- -->/
1951 * GDK_THREADS_LEAVE ();
1957 * Return value: %TRUE if the source has been destroyed
1962 g_source_is_destroyed (GSource *source)
1964 return SOURCE_DESTROYED (source);
1968 /* Temporarily remove all this source's file descriptors from the
1969 * poll(), so that if data comes available for one of the file descriptors
1970 * we don't continually spin in the poll()
1972 /* HOLDS: source->context's lock */
1974 block_source (GSource *source)
1978 g_return_if_fail (!SOURCE_BLOCKED (source));
1980 tmp_list = source->poll_fds;
1983 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1984 tmp_list = tmp_list->next;
1988 /* HOLDS: source->context's lock */
1990 unblock_source (GSource *source)
1994 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1995 g_return_if_fail (!SOURCE_DESTROYED (source));
1997 tmp_list = source->poll_fds;
2000 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
2001 tmp_list = tmp_list->next;
2005 /* HOLDS: context's lock */
2007 g_main_dispatch (GMainContext *context)
2009 GMainDispatch *current = get_dispatch ();
2012 for (i = 0; i < context->pending_dispatches->len; i++)
2014 GSource *source = context->pending_dispatches->pdata[i];
2016 context->pending_dispatches->pdata[i] = NULL;
2019 source->flags &= ~G_SOURCE_READY;
2021 if (!SOURCE_DESTROYED (source))
2023 gboolean was_in_call;
2024 gpointer user_data = NULL;
2025 GSourceFunc callback = NULL;
2026 GSourceCallbackFuncs *cb_funcs;
2028 gboolean need_destroy;
2030 gboolean (*dispatch) (GSource *,
2033 GSList current_source_link;
2035 dispatch = source->source_funcs->dispatch;
2036 cb_funcs = source->callback_funcs;
2037 cb_data = source->callback_data;
2040 cb_funcs->ref (cb_data);
2042 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2043 block_source (source);
2045 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2046 source->flags |= G_HOOK_FLAG_IN_CALL;
2049 cb_funcs->get (cb_data, source, &callback, &user_data);
2051 UNLOCK_CONTEXT (context);
2054 /* The on-stack allocation of the GSList is unconventional, but
2055 * we know that the lifetime of the link is bounded to this
2056 * function as the link is kept in a thread specific list and
2057 * not manipulated outside of this function and its descendants.
2058 * Avoiding the overhead of a g_slist_alloc() is useful as many
2059 * applications do little more than dispatch events.
2061 * This is a performance hack - do not revert to g_slist_prepend()!
2063 current_source_link.data = source;
2064 current_source_link.next = current->dispatching_sources;
2065 current->dispatching_sources = ¤t_source_link;
2066 need_destroy = ! dispatch (source,
2069 g_assert (current->dispatching_sources == ¤t_source_link);
2070 current->dispatching_sources = current_source_link.next;
2074 cb_funcs->unref (cb_data);
2076 LOCK_CONTEXT (context);
2079 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2081 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2082 !SOURCE_DESTROYED (source))
2083 unblock_source (source);
2085 /* Note: this depends on the fact that we can't switch
2086 * sources from one main context to another
2088 if (need_destroy && !SOURCE_DESTROYED (source))
2090 g_assert (source->context == context);
2091 g_source_destroy_internal (source, context, TRUE);
2095 SOURCE_UNREF (source, context);
2098 g_ptr_array_set_size (context->pending_dispatches, 0);
2101 /* Holds context's lock */
2102 static inline GSource *
2103 next_valid_source (GMainContext *context,
2106 GSource *new_source = source ? source->next : context->source_list;
2110 if (!SOURCE_DESTROYED (new_source))
2112 new_source->ref_count++;
2116 new_source = new_source->next;
2120 SOURCE_UNREF (source, context);
2126 * g_main_context_acquire:
2127 * @context: a #GMainContext
2129 * Tries to become the owner of the specified context.
2130 * If some other thread is the owner of the context,
2131 * returns %FALSE immediately. Ownership is properly
2132 * recursive: the owner can require ownership again
2133 * and will release ownership when g_main_context_release()
2134 * is called as many times as g_main_context_acquire().
2136 * You must be the owner of a context before you
2137 * can call g_main_context_prepare(), g_main_context_query(),
2138 * g_main_context_check(), g_main_context_dispatch().
2140 * Return value: %TRUE if the operation succeeded, and
2141 * this thread is now the owner of @context.
2144 g_main_context_acquire (GMainContext *context)
2146 #ifdef G_THREADS_ENABLED
2147 gboolean result = FALSE;
2148 GThread *self = G_THREAD_SELF;
2150 if (context == NULL)
2151 context = g_main_context_default ();
2153 LOCK_CONTEXT (context);
2155 if (!context->owner)
2157 context->owner = self;
2158 g_assert (context->owner_count == 0);
2161 if (context->owner == self)
2163 context->owner_count++;
2167 UNLOCK_CONTEXT (context);
2170 #else /* !G_THREADS_ENABLED */
2172 #endif /* G_THREADS_ENABLED */
2176 * g_main_context_release:
2177 * @context: a #GMainContext
2179 * Releases ownership of a context previously acquired by this thread
2180 * with g_main_context_acquire(). If the context was acquired multiple
2181 * times, the ownership will be released only when g_main_context_release()
2182 * is called as many times as it was acquired.
2185 g_main_context_release (GMainContext *context)
2187 #ifdef G_THREADS_ENABLED
2188 if (context == NULL)
2189 context = g_main_context_default ();
2191 LOCK_CONTEXT (context);
2193 context->owner_count--;
2194 if (context->owner_count == 0)
2196 context->owner = NULL;
2198 if (context->waiters)
2200 GMainWaiter *waiter = context->waiters->data;
2201 gboolean loop_internal_waiter =
2202 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2203 context->waiters = g_slist_delete_link (context->waiters,
2205 if (!loop_internal_waiter)
2206 g_mutex_lock (waiter->mutex);
2208 g_cond_signal (waiter->cond);
2210 if (!loop_internal_waiter)
2211 g_mutex_unlock (waiter->mutex);
2215 UNLOCK_CONTEXT (context);
2216 #endif /* G_THREADS_ENABLED */
2220 * g_main_context_wait:
2221 * @context: a #GMainContext
2222 * @cond: a condition variable
2223 * @mutex: a mutex, currently held
2225 * Tries to become the owner of the specified context,
2226 * as with g_main_context_acquire(). But if another thread
2227 * is the owner, atomically drop @mutex and wait on @cond until
2228 * that owner releases ownership or until @cond is signaled, then
2229 * try again (once) to become the owner.
2231 * Return value: %TRUE if the operation succeeded, and
2232 * this thread is now the owner of @context.
2235 g_main_context_wait (GMainContext *context,
2239 #ifdef G_THREADS_ENABLED
2240 gboolean result = FALSE;
2241 GThread *self = G_THREAD_SELF;
2242 gboolean loop_internal_waiter;
2244 if (context == NULL)
2245 context = g_main_context_default ();
2247 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2249 if (!loop_internal_waiter)
2250 LOCK_CONTEXT (context);
2252 if (context->owner && context->owner != self)
2257 waiter.mutex = mutex;
2259 context->waiters = g_slist_append (context->waiters, &waiter);
2261 if (!loop_internal_waiter)
2262 UNLOCK_CONTEXT (context);
2263 g_cond_wait (cond, mutex);
2264 if (!loop_internal_waiter)
2265 LOCK_CONTEXT (context);
2267 context->waiters = g_slist_remove (context->waiters, &waiter);
2270 if (!context->owner)
2272 context->owner = self;
2273 g_assert (context->owner_count == 0);
2276 if (context->owner == self)
2278 context->owner_count++;
2282 if (!loop_internal_waiter)
2283 UNLOCK_CONTEXT (context);
2286 #else /* !G_THREADS_ENABLED */
2288 #endif /* G_THREADS_ENABLED */
2292 * g_main_context_prepare:
2293 * @context: a #GMainContext
2294 * @priority: location to store priority of highest priority
2295 * source already ready.
2297 * Prepares to poll sources within a main loop. The resulting information
2298 * for polling is determined by calling g_main_context_query ().
2300 * Return value: %TRUE if some source is ready to be dispatched
2304 g_main_context_prepare (GMainContext *context,
2309 gint current_priority = G_MAXINT;
2312 if (context == NULL)
2313 context = g_main_context_default ();
2315 LOCK_CONTEXT (context);
2317 context->time_is_current = FALSE;
2319 if (context->in_check_or_prepare)
2321 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2322 "prepare() member.");
2323 UNLOCK_CONTEXT (context);
2327 #ifdef G_THREADS_ENABLED
2328 if (context->poll_waiting)
2330 g_warning("g_main_context_prepare(): main loop already active in another thread");
2331 UNLOCK_CONTEXT (context);
2335 context->poll_waiting = TRUE;
2336 #endif /* G_THREADS_ENABLED */
2339 /* If recursing, finish up current dispatch, before starting over */
2340 if (context->pending_dispatches)
2343 g_main_dispatch (context, ¤t_time);
2345 UNLOCK_CONTEXT (context);
2350 /* If recursing, clear list of pending dispatches */
2352 for (i = 0; i < context->pending_dispatches->len; i++)
2354 if (context->pending_dispatches->pdata[i])
2355 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2357 g_ptr_array_set_size (context->pending_dispatches, 0);
2359 /* Prepare all sources */
2361 context->timeout = -1;
2363 source = next_valid_source (context, NULL);
2366 gint source_timeout = -1;
2368 if ((n_ready > 0) && (source->priority > current_priority))
2370 SOURCE_UNREF (source, context);
2373 if (SOURCE_BLOCKED (source))
2376 if (!(source->flags & G_SOURCE_READY))
2379 gboolean (*prepare) (GSource *source,
2382 prepare = source->source_funcs->prepare;
2383 context->in_check_or_prepare++;
2384 UNLOCK_CONTEXT (context);
2386 result = (*prepare) (source, &source_timeout);
2388 LOCK_CONTEXT (context);
2389 context->in_check_or_prepare--;
2392 source->flags |= G_SOURCE_READY;
2395 if (source->flags & G_SOURCE_READY)
2398 current_priority = source->priority;
2399 context->timeout = 0;
2402 if (source_timeout >= 0)
2404 if (context->timeout < 0)
2405 context->timeout = source_timeout;
2407 context->timeout = MIN (context->timeout, source_timeout);
2411 source = next_valid_source (context, source);
2414 UNLOCK_CONTEXT (context);
2417 *priority = current_priority;
2419 return (n_ready > 0);
2423 * g_main_context_query:
2424 * @context: a #GMainContext
2425 * @max_priority: maximum priority source to check
2426 * @timeout_: location to store timeout to be used in polling
2427 * @fds: location to store #GPollFD records that need to be polled.
2428 * @n_fds: length of @fds.
2430 * Determines information necessary to poll this main loop.
2432 * Return value: the number of records actually stored in @fds,
2433 * or, if more than @n_fds records need to be stored, the number
2434 * of records that need to be stored.
2437 g_main_context_query (GMainContext *context,
2446 LOCK_CONTEXT (context);
2448 pollrec = context->poll_records;
2450 while (pollrec && max_priority >= pollrec->priority)
2452 if (pollrec->fd->events)
2456 fds[n_poll].fd = pollrec->fd->fd;
2457 /* In direct contradiction to the Unix98 spec, IRIX runs into
2458 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2459 * flags in the events field of the pollfd while it should
2460 * just ignoring them. So we mask them out here.
2462 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2463 fds[n_poll].revents = 0;
2468 pollrec = pollrec->next;
2471 #ifdef G_THREADS_ENABLED
2472 context->poll_changed = FALSE;
2477 *timeout = context->timeout;
2479 context->time_is_current = FALSE;
2482 UNLOCK_CONTEXT (context);
2488 * g_main_context_check:
2489 * @context: a #GMainContext
2490 * @max_priority: the maximum numerical priority of sources to check
2491 * @fds: array of #GPollFD's that was passed to the last call to
2492 * g_main_context_query()
2493 * @n_fds: return value of g_main_context_query()
2495 * Passes the results of polling back to the main loop.
2497 * Return value: %TRUE if some sources are ready to be dispatched.
2500 g_main_context_check (GMainContext *context,
2510 LOCK_CONTEXT (context);
2512 if (context->in_check_or_prepare)
2514 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2515 "prepare() member.");
2516 UNLOCK_CONTEXT (context);
2520 #ifdef G_THREADS_ENABLED
2521 if (!context->poll_waiting)
2525 read (context->wake_up_pipe[0], &a, 1);
2529 context->poll_waiting = FALSE;
2531 /* If the set of poll file descriptors changed, bail out
2532 * and let the main loop rerun
2534 if (context->poll_changed)
2536 UNLOCK_CONTEXT (context);
2539 #endif /* G_THREADS_ENABLED */
2541 pollrec = context->poll_records;
2545 if (pollrec->fd->events)
2547 pollrec->fd->revents = fds[i].revents;
2550 pollrec = pollrec->next;
2553 source = next_valid_source (context, NULL);
2556 if ((n_ready > 0) && (source->priority > max_priority))
2558 SOURCE_UNREF (source, context);
2561 if (SOURCE_BLOCKED (source))
2564 if (!(source->flags & G_SOURCE_READY))
2567 gboolean (*check) (GSource *source);
2569 check = source->source_funcs->check;
2571 context->in_check_or_prepare++;
2572 UNLOCK_CONTEXT (context);
2574 result = (*check) (source);
2576 LOCK_CONTEXT (context);
2577 context->in_check_or_prepare--;
2580 source->flags |= G_SOURCE_READY;
2583 if (source->flags & G_SOURCE_READY)
2585 source->ref_count++;
2586 g_ptr_array_add (context->pending_dispatches, source);
2590 /* never dispatch sources with less priority than the first
2591 * one we choose to dispatch
2593 max_priority = source->priority;
2597 source = next_valid_source (context, source);
2600 UNLOCK_CONTEXT (context);
2606 * g_main_context_dispatch:
2607 * @context: a #GMainContext
2609 * Dispatches all pending sources.
2612 g_main_context_dispatch (GMainContext *context)
2614 LOCK_CONTEXT (context);
2616 if (context->pending_dispatches->len > 0)
2618 g_main_dispatch (context);
2621 UNLOCK_CONTEXT (context);
2624 /* HOLDS context lock */
2626 g_main_context_iterate (GMainContext *context,
2633 gboolean some_ready;
2634 gint nfds, allocated_nfds;
2635 GPollFD *fds = NULL;
2637 UNLOCK_CONTEXT (context);
2639 #ifdef G_THREADS_ENABLED
2640 if (!g_main_context_acquire (context))
2642 gboolean got_ownership;
2644 g_return_val_if_fail (g_thread_supported (), FALSE);
2649 LOCK_CONTEXT (context);
2652 context->cond = g_cond_new ();
2654 got_ownership = g_main_context_wait (context,
2656 g_static_mutex_get_mutex (&context->mutex));
2660 UNLOCK_CONTEXT (context);
2665 LOCK_CONTEXT (context);
2666 #endif /* G_THREADS_ENABLED */
2668 if (!context->cached_poll_array)
2670 context->cached_poll_array_size = context->n_poll_records;
2671 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2674 allocated_nfds = context->cached_poll_array_size;
2675 fds = context->cached_poll_array;
2677 UNLOCK_CONTEXT (context);
2679 g_main_context_prepare (context, &max_priority);
2681 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2682 allocated_nfds)) > allocated_nfds)
2684 LOCK_CONTEXT (context);
2686 context->cached_poll_array_size = allocated_nfds = nfds;
2687 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2688 UNLOCK_CONTEXT (context);
2694 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2696 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2699 g_main_context_dispatch (context);
2701 #ifdef G_THREADS_ENABLED
2702 g_main_context_release (context);
2703 #endif /* G_THREADS_ENABLED */
2705 LOCK_CONTEXT (context);
2711 * g_main_context_pending:
2712 * @context: a #GMainContext (if %NULL, the default context will be used)
2714 * Checks if any sources have pending events for the given context.
2716 * Return value: %TRUE if events are pending.
2719 g_main_context_pending (GMainContext *context)
2724 context = g_main_context_default();
2726 LOCK_CONTEXT (context);
2727 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2728 UNLOCK_CONTEXT (context);
2734 * g_main_context_iteration:
2735 * @context: a #GMainContext (if %NULL, the default context will be used)
2736 * @may_block: whether the call may block.
2738 * Runs a single iteration for the given main loop. This involves
2739 * checking to see if any event sources are ready to be processed,
2740 * then if no events sources are ready and @may_block is %TRUE, waiting
2741 * for a source to become ready, then dispatching the highest priority
2742 * events sources that are ready. Otherwise, if @may_block is %FALSE
2743 * sources are not waited to become ready, only those highest priority
2744 * events sources will be dispatched (if any), that are ready at this
2745 * given moment without further waiting.
2747 * Note that even when @may_block is %TRUE, it is still possible for
2748 * g_main_context_iteration() to return %FALSE, since the the wait may
2749 * be interrupted for other reasons than an event source becoming ready.
2751 * Return value: %TRUE if events were dispatched.
2754 g_main_context_iteration (GMainContext *context, gboolean may_block)
2759 context = g_main_context_default();
2761 LOCK_CONTEXT (context);
2762 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2763 UNLOCK_CONTEXT (context);
2770 * @context: a #GMainContext (if %NULL, the default context will be used).
2771 * @is_running: set to %TRUE to indicate that the loop is running. This
2772 * is not very important since calling g_main_loop_run() will set this to
2775 * Creates a new #GMainLoop structure.
2777 * Return value: a new #GMainLoop.
2780 g_main_loop_new (GMainContext *context,
2781 gboolean is_running)
2784 #ifdef G_MAIN_POLL_DEBUG
2785 static gboolean beenhere = FALSE;
2788 #ifdef G_MAIN_POLL_DEBUG
2794 if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
2795 g_main_poll_debug = TRUE;
2797 G_UNLOCK (main_loop);
2801 context = g_main_context_default();
2803 g_main_context_ref (context);
2805 loop = g_new0 (GMainLoop, 1);
2806 loop->context = context;
2807 loop->is_running = is_running != FALSE;
2808 loop->ref_count = 1;
2815 * @loop: a #GMainLoop
2817 * Increases the reference count on a #GMainLoop object by one.
2819 * Return value: @loop
2822 g_main_loop_ref (GMainLoop *loop)
2824 g_return_val_if_fail (loop != NULL, NULL);
2825 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2827 g_atomic_int_inc (&loop->ref_count);
2833 * g_main_loop_unref:
2834 * @loop: a #GMainLoop
2836 * Decreases the reference count on a #GMainLoop object by one. If
2837 * the result is zero, free the loop and free all associated memory.
2840 g_main_loop_unref (GMainLoop *loop)
2842 g_return_if_fail (loop != NULL);
2843 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2845 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2848 g_main_context_unref (loop->context);
2854 * @loop: a #GMainLoop
2856 * Runs a main loop until g_main_loop_quit() is called on the loop.
2857 * If this is called for the thread of the loop's #GMainContext,
2858 * it will process events from the loop, otherwise it will
2862 g_main_loop_run (GMainLoop *loop)
2864 GThread *self = G_THREAD_SELF;
2866 g_return_if_fail (loop != NULL);
2867 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2869 #ifdef G_THREADS_ENABLED
2870 if (!g_main_context_acquire (loop->context))
2872 gboolean got_ownership = FALSE;
2874 /* Another thread owns this context */
2875 if (!g_thread_supported ())
2877 g_warning ("g_main_loop_run() was called from second thread but "
2878 "g_thread_init() was never called.");
2882 LOCK_CONTEXT (loop->context);
2884 g_atomic_int_inc (&loop->ref_count);
2886 if (!loop->is_running)
2887 loop->is_running = TRUE;
2889 if (!loop->context->cond)
2890 loop->context->cond = g_cond_new ();
2892 while (loop->is_running && !got_ownership)
2893 got_ownership = g_main_context_wait (loop->context,
2894 loop->context->cond,
2895 g_static_mutex_get_mutex (&loop->context->mutex));
2897 if (!loop->is_running)
2899 UNLOCK_CONTEXT (loop->context);
2901 g_main_context_release (loop->context);
2902 g_main_loop_unref (loop);
2906 g_assert (got_ownership);
2909 LOCK_CONTEXT (loop->context);
2910 #endif /* G_THREADS_ENABLED */
2912 if (loop->context->in_check_or_prepare)
2914 g_warning ("g_main_loop_run(): called recursively from within a source's "
2915 "check() or prepare() member, iteration not possible.");
2919 g_atomic_int_inc (&loop->ref_count);
2920 loop->is_running = TRUE;
2921 while (loop->is_running)
2922 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2924 UNLOCK_CONTEXT (loop->context);
2926 #ifdef G_THREADS_ENABLED
2927 g_main_context_release (loop->context);
2928 #endif /* G_THREADS_ENABLED */
2930 g_main_loop_unref (loop);
2935 * @loop: a #GMainLoop
2937 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2938 * for the loop will return.
2940 * Note that sources that have already been dispatched when
2941 * g_main_loop_quit() is called will still be executed.
2944 g_main_loop_quit (GMainLoop *loop)
2946 g_return_if_fail (loop != NULL);
2947 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2949 LOCK_CONTEXT (loop->context);
2950 loop->is_running = FALSE;
2951 g_main_context_wakeup_unlocked (loop->context);
2953 #ifdef G_THREADS_ENABLED
2954 if (loop->context->cond)
2955 g_cond_broadcast (loop->context->cond);
2956 #endif /* G_THREADS_ENABLED */
2958 UNLOCK_CONTEXT (loop->context);
2962 * g_main_loop_is_running:
2963 * @loop: a #GMainLoop.
2965 * Checks to see if the main loop is currently being run via g_main_loop_run().
2967 * Return value: %TRUE if the mainloop is currently being run.
2970 g_main_loop_is_running (GMainLoop *loop)
2972 g_return_val_if_fail (loop != NULL, FALSE);
2973 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2975 return loop->is_running;
2979 * g_main_loop_get_context:
2980 * @loop: a #GMainLoop.
2982 * Returns the #GMainContext of @loop.
2984 * Return value: the #GMainContext of @loop
2987 g_main_loop_get_context (GMainLoop *loop)
2989 g_return_val_if_fail (loop != NULL, NULL);
2990 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2992 return loop->context;
2995 /* HOLDS: context's lock */
2997 g_main_context_poll (GMainContext *context,
3003 #ifdef G_MAIN_POLL_DEBUG
3009 GPollFunc poll_func;
3011 if (n_fds || timeout != 0)
3013 #ifdef G_MAIN_POLL_DEBUG
3014 if (g_main_poll_debug)
3016 g_print ("polling context=%p n=%d timeout=%d\n",
3017 context, n_fds, timeout);
3018 poll_timer = g_timer_new ();
3022 LOCK_CONTEXT (context);
3024 poll_func = context->poll_func;
3026 UNLOCK_CONTEXT (context);
3027 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
3030 g_warning ("poll(2) failed due to: %s.",
3031 g_strerror (errno));
3033 /* If g_poll () returns -1, it has already called g_warning() */
3037 #ifdef G_MAIN_POLL_DEBUG
3038 if (g_main_poll_debug)
3040 LOCK_CONTEXT (context);
3042 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3045 g_timer_elapsed (poll_timer, NULL));
3046 g_timer_destroy (poll_timer);
3047 pollrec = context->poll_records;
3049 while (pollrec != NULL)
3054 if (fds[i].fd == pollrec->fd->fd &&
3055 pollrec->fd->events &&
3058 g_print (" [" GPOLLFD_FORMAT " :", fds[i].fd);
3059 if (fds[i].revents & G_IO_IN)
3061 if (fds[i].revents & G_IO_OUT)
3063 if (fds[i].revents & G_IO_PRI)
3065 if (fds[i].revents & G_IO_ERR)
3067 if (fds[i].revents & G_IO_HUP)
3069 if (fds[i].revents & G_IO_NVAL)
3075 pollrec = pollrec->next;
3079 UNLOCK_CONTEXT (context);
3082 } /* if (n_fds || timeout != 0) */
3086 * g_main_context_add_poll:
3087 * @context: a #GMainContext (or %NULL for the default context)
3088 * @fd: a #GPollFD structure holding information about a file
3089 * descriptor to watch.
3090 * @priority: the priority for this file descriptor which should be
3091 * the same as the priority used for g_source_attach() to ensure that the
3092 * file descriptor is polled whenever the results may be needed.
3094 * Adds a file descriptor to the set of file descriptors polled for
3095 * this context. This will very seldomly be used directly. Instead
3096 * a typical event source will use g_source_add_poll() instead.
3099 g_main_context_add_poll (GMainContext *context,
3104 context = g_main_context_default ();
3106 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3107 g_return_if_fail (fd);
3109 LOCK_CONTEXT (context);
3110 g_main_context_add_poll_unlocked (context, priority, fd);
3111 UNLOCK_CONTEXT (context);
3114 /* HOLDS: main_loop_lock */
3116 g_main_context_add_poll_unlocked (GMainContext *context,
3120 GPollRec *lastrec, *pollrec;
3121 GPollRec *newrec = g_slice_new (GPollRec);
3123 /* This file descriptor may be checked before we ever poll */
3126 newrec->priority = priority;
3129 pollrec = context->poll_records;
3130 while (pollrec && priority >= pollrec->priority)
3133 pollrec = pollrec->next;
3137 lastrec->next = newrec;
3139 context->poll_records = newrec;
3141 newrec->next = pollrec;
3143 context->n_poll_records++;
3145 #ifdef G_THREADS_ENABLED
3146 context->poll_changed = TRUE;
3148 /* Now wake up the main loop if it is waiting in the poll() */
3149 g_main_context_wakeup_unlocked (context);
3154 * g_main_context_remove_poll:
3155 * @context:a #GMainContext
3156 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3158 * Removes file descriptor from the set of file descriptors to be
3159 * polled for a particular context.
3162 g_main_context_remove_poll (GMainContext *context,
3166 context = g_main_context_default ();
3168 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3169 g_return_if_fail (fd);
3171 LOCK_CONTEXT (context);
3172 g_main_context_remove_poll_unlocked (context, fd);
3173 UNLOCK_CONTEXT (context);
3177 g_main_context_remove_poll_unlocked (GMainContext *context,
3180 GPollRec *pollrec, *lastrec;
3183 pollrec = context->poll_records;
3187 if (pollrec->fd == fd)
3189 if (lastrec != NULL)
3190 lastrec->next = pollrec->next;
3192 context->poll_records = pollrec->next;
3194 g_slice_free (GPollRec, pollrec);
3196 context->n_poll_records--;
3200 pollrec = pollrec->next;
3203 #ifdef G_THREADS_ENABLED
3204 context->poll_changed = TRUE;
3206 /* Now wake up the main loop if it is waiting in the poll() */
3207 g_main_context_wakeup_unlocked (context);
3212 * g_source_get_current_time:
3213 * @source: a #GSource
3214 * @timeval: #GTimeVal structure in which to store current time.
3216 * Gets the "current time" to be used when checking
3217 * this source. The advantage of calling this function over
3218 * calling g_get_current_time() directly is that when
3219 * checking multiple sources, GLib can cache a single value
3220 * instead of having to repeatedly get the system time.
3223 g_source_get_current_time (GSource *source,
3226 GMainContext *context;
3228 g_return_if_fail (source->context != NULL);
3230 context = source->context;
3232 LOCK_CONTEXT (context);
3234 if (!context->time_is_current)
3236 g_get_current_time (&context->current_time);
3237 context->time_is_current = TRUE;
3240 *timeval = context->current_time;
3242 UNLOCK_CONTEXT (context);
3246 * g_main_context_set_poll_func:
3247 * @context: a #GMainContext
3248 * @func: the function to call to poll all file descriptors
3250 * Sets the function to use to handle polling of file descriptors. It
3251 * will be used instead of the poll() system call
3252 * (or GLib's replacement function, which is used where
3253 * poll() isn't available).
3255 * This function could possibly be used to integrate the GLib event
3256 * loop with an external event loop.
3259 g_main_context_set_poll_func (GMainContext *context,
3263 context = g_main_context_default ();
3265 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3267 LOCK_CONTEXT (context);
3270 context->poll_func = func;
3274 context->poll_func = (GPollFunc) poll;
3276 context->poll_func = (GPollFunc) g_poll;
3280 UNLOCK_CONTEXT (context);
3284 * g_main_context_get_poll_func:
3285 * @context: a #GMainContext
3287 * Gets the poll function set by g_main_context_set_poll_func().
3289 * Return value: the poll function
3292 g_main_context_get_poll_func (GMainContext *context)
3297 context = g_main_context_default ();
3299 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3301 LOCK_CONTEXT (context);
3302 result = context->poll_func;
3303 UNLOCK_CONTEXT (context);
3308 /* HOLDS: context's lock */
3309 /* Wake the main loop up from a poll() */
3311 g_main_context_wakeup_unlocked (GMainContext *context)
3313 #ifdef G_THREADS_ENABLED
3314 if (g_thread_supported() && context->poll_waiting)
3316 context->poll_waiting = FALSE;
3318 write (context->wake_up_pipe[1], "A", 1);
3320 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3327 * g_main_context_wakeup:
3328 * @context: a #GMainContext
3330 * If @context is currently waiting in a poll(), interrupt
3331 * the poll(), and continue the iteration process.
3334 g_main_context_wakeup (GMainContext *context)
3337 context = g_main_context_default ();
3339 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3341 LOCK_CONTEXT (context);
3342 g_main_context_wakeup_unlocked (context);
3343 UNLOCK_CONTEXT (context);
3347 * g_main_context_is_owner:
3348 * @context: a #GMainContext
3350 * Determines whether this thread holds the (recursive)
3351 * ownership of this #GMaincontext. This is useful to
3352 * know before waiting on another thread that may be
3353 * blocking to get ownership of @context.
3355 * Returns: %TRUE if current thread is owner of @context.
3360 g_main_context_is_owner (GMainContext *context)
3365 context = g_main_context_default ();
3367 #ifdef G_THREADS_ENABLED
3368 LOCK_CONTEXT (context);
3369 is_owner = context->owner == G_THREAD_SELF;
3370 UNLOCK_CONTEXT (context);
3381 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3382 GTimeVal *current_time)
3384 guint seconds = timeout_source->interval / 1000;
3385 guint msecs = timeout_source->interval - seconds * 1000;
3387 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3388 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3389 if (timeout_source->expiration.tv_usec >= 1000000)
3391 timeout_source->expiration.tv_usec -= 1000000;
3392 timeout_source->expiration.tv_sec++;
3394 if (timer_perturb==-1)
3397 * we want a per machine/session unique 'random' value; try the dbus
3398 * address first, that has a UUID in it. If there is no dbus, use the
3399 * hostname for hashing.
3401 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3402 if (!session_bus_address)
3403 session_bus_address = g_getenv ("HOSTNAME");
3404 if (session_bus_address)
3405 timer_perturb = ABS ((gint) g_str_hash (session_bus_address));
3409 if (timeout_source->granularity)
3412 gint gran; /* in usecs */
3415 gran = timeout_source->granularity * 1000;
3416 perturb = timer_perturb % gran;
3418 * We want to give each machine a per machine pertubation;
3419 * shift time back first, and forward later after the rounding
3422 timeout_source->expiration.tv_usec -= perturb;
3423 if (timeout_source->expiration.tv_usec < 0)
3425 timeout_source->expiration.tv_usec += 1000000;
3426 timeout_source->expiration.tv_sec--;
3429 remainder = timeout_source->expiration.tv_usec % gran;
3430 if (remainder >= gran/4) /* round up */
3431 timeout_source->expiration.tv_usec += gran;
3432 timeout_source->expiration.tv_usec -= remainder;
3434 timeout_source->expiration.tv_usec += perturb;
3436 /* the rounding may have overflown tv_usec */
3437 while (timeout_source->expiration.tv_usec > 1000000)
3439 timeout_source->expiration.tv_usec -= 1000000;
3440 timeout_source->expiration.tv_sec++;
3446 g_timeout_prepare (GSource *source,
3451 GTimeVal current_time;
3453 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3455 g_source_get_current_time (source, ¤t_time);
3457 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3458 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3460 /* We do the following in a rather convoluted fashion to deal with
3461 * the fact that we don't have an integral type big enough to hold
3462 * the difference of two timevals in millseconds.
3464 if (sec < 0 || (sec == 0 && msec < 0))
3468 glong interval_sec = timeout_source->interval / 1000;
3469 glong interval_msec = timeout_source->interval % 1000;
3477 if (sec > interval_sec ||
3478 (sec == interval_sec && msec > interval_msec))
3480 /* The system time has been set backwards, so we
3481 * reset the expiration time to now + timeout_source->interval;
3482 * this at least avoids hanging for long periods of time.
3484 g_timeout_set_expiration (timeout_source, ¤t_time);
3485 msec = MIN (G_MAXINT, timeout_source->interval);
3489 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3493 *timeout = (gint)msec;
3499 g_timeout_check (GSource *source)
3501 GTimeVal current_time;
3502 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3504 g_source_get_current_time (source, ¤t_time);
3506 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3507 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3508 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3512 g_timeout_dispatch (GSource *source,
3513 GSourceFunc callback,
3516 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3520 g_warning ("Timeout source dispatched without callback\n"
3521 "You must call g_source_set_callback().");
3525 if (callback (user_data))
3527 GTimeVal current_time;
3529 g_source_get_current_time (source, ¤t_time);
3530 g_timeout_set_expiration (timeout_source, ¤t_time);
3539 * g_timeout_source_new:
3540 * @interval: the timeout interval in milliseconds.
3542 * Creates a new timeout source.
3544 * The source will not initially be associated with any #GMainContext
3545 * and must be added to one with g_source_attach() before it will be
3548 * Return value: the newly-created timeout source
3551 g_timeout_source_new (guint interval)
3553 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3554 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3555 GTimeVal current_time;
3557 timeout_source->interval = interval;
3559 g_get_current_time (¤t_time);
3560 g_timeout_set_expiration (timeout_source, ¤t_time);
3566 * g_timeout_source_new_seconds:
3567 * @interval: the timeout interval in seconds
3569 * Creates a new timeout source.
3571 * The source will not initially be associated with any #GMainContext
3572 * and must be added to one with g_source_attach() before it will be
3575 * The scheduling granularity/accuracy of this timeout source will be
3578 * Return value: the newly-created timeout source
3583 g_timeout_source_new_seconds (guint interval)
3585 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3586 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3587 GTimeVal current_time;
3589 timeout_source->interval = 1000*interval;
3590 timeout_source->granularity = 1000;
3592 g_get_current_time (¤t_time);
3593 g_timeout_set_expiration (timeout_source, ¤t_time);
3600 * g_timeout_add_full:
3601 * @priority: the priority of the timeout source. Typically this will be in
3602 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3603 * @interval: the time between calls to the function, in milliseconds
3604 * (1/1000ths of a second)
3605 * @function: function to call
3606 * @data: data to pass to @function
3607 * @notify: function to call when the timeout is removed, or %NULL
3609 * Sets a function to be called at regular intervals, with the given
3610 * priority. The function is called repeatedly until it returns
3611 * %FALSE, at which point the timeout is automatically destroyed and
3612 * the function will not be called again. The @notify function is
3613 * called when the timeout is destroyed. The first call to the
3614 * function will be at the end of the first @interval.
3616 * Note that timeout functions may be delayed, due to the processing of other
3617 * event sources. Thus they should not be relied on for precise timing.
3618 * After each call to the timeout function, the time of the next
3619 * timeout is recalculated based on the current time and the given interval
3620 * (it does not try to 'catch up' time lost in delays).
3622 * Return value: the ID (greater than 0) of the event source.
3625 g_timeout_add_full (gint priority,
3627 GSourceFunc function,
3629 GDestroyNotify notify)
3634 g_return_val_if_fail (function != NULL, 0);
3636 source = g_timeout_source_new (interval);
3638 if (priority != G_PRIORITY_DEFAULT)
3639 g_source_set_priority (source, priority);
3641 g_source_set_callback (source, function, data, notify);
3642 id = g_source_attach (source, NULL);
3643 g_source_unref (source);
3650 * @interval: the time between calls to the function, in milliseconds
3651 * (1/1000ths of a second)
3652 * @function: function to call
3653 * @data: data to pass to @function
3655 * Sets a function to be called at regular intervals, with the default
3656 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3657 * until it returns %FALSE, at which point the timeout is automatically
3658 * destroyed and the function will not be called again. The first call
3659 * to the function will be at the end of the first @interval.
3661 * Note that timeout functions may be delayed, due to the processing of other
3662 * event sources. Thus they should not be relied on for precise timing.
3663 * After each call to the timeout function, the time of the next
3664 * timeout is recalculated based on the current time and the given interval
3665 * (it does not try to 'catch up' time lost in delays).
3667 * If you want to have a timer in the "seconds" range and do not care
3668 * about the exact time of the first call of the timer, use the
3669 * g_timeout_add_seconds() function; this function allows for more
3670 * optimizations and more efficient system power usage.
3672 * Return value: the ID (greater than 0) of the event source.
3675 g_timeout_add (guint32 interval,
3676 GSourceFunc function,
3679 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3680 interval, function, data, NULL);
3684 * g_timeout_add_seconds_full:
3685 * @priority: the priority of the timeout source. Typically this will be in
3686 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3687 * @interval: the time between calls to the function, in seconds
3688 * @function: function to call
3689 * @data: data to pass to @function
3690 * @notify: function to call when the timeout is removed, or %NULL
3692 * Sets a function to be called at regular intervals, with @priority.
3693 * The function is called repeatedly until it returns %FALSE, at which
3694 * point the timeout is automatically destroyed and the function will
3695 * not be called again.
3697 * Unlike g_timeout_add(), this function operates at whole second granularity.
3698 * The initial starting point of the timer is determined by the implementation
3699 * and the implementation is expected to group multiple timers together so that
3700 * they fire all at the same time.
3701 * To allow this grouping, the @interval to the first timer is rounded
3702 * and can deviate up to one second from the specified interval.
3703 * Subsequent timer iterations will generally run at the specified interval.
3705 * Note that timeout functions may be delayed, due to the processing of other
3706 * event sources. Thus they should not be relied on for precise timing.
3707 * After each call to the timeout function, the time of the next
3708 * timeout is recalculated based on the current time and the given @interval
3710 * If you want timing more precise than whole seconds, use g_timeout_add()
3713 * The grouping of timers to fire at the same time results in a more power
3714 * and CPU efficient behavior so if your timer is in multiples of seconds
3715 * and you don't require the first timer exactly one second from now, the
3716 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3718 * Return value: the ID (greater than 0) of the event source.
3723 g_timeout_add_seconds_full (gint priority,
3725 GSourceFunc function,
3727 GDestroyNotify notify)
3732 g_return_val_if_fail (function != NULL, 0);
3734 source = g_timeout_source_new_seconds (interval);
3736 if (priority != G_PRIORITY_DEFAULT)
3737 g_source_set_priority (source, priority);
3739 g_source_set_callback (source, function, data, notify);
3740 id = g_source_attach (source, NULL);
3741 g_source_unref (source);
3747 * g_timeout_add_seconds:
3748 * @interval: the time between calls to the function, in seconds
3749 * @function: function to call
3750 * @data: data to pass to @function
3752 * Sets a function to be called at regular intervals with the default
3753 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3754 * it returns %FALSE, at which point the timeout is automatically destroyed
3755 * and the function will not be called again.
3757 * See g_timeout_add_seconds_full() for the differences between
3758 * g_timeout_add() and g_timeout_add_seconds().
3760 * Return value: the ID (greater than 0) of the event source.
3765 g_timeout_add_seconds (guint interval,
3766 GSourceFunc function,
3769 g_return_val_if_fail (function != NULL, 0);
3771 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3774 /* Child watch functions */
3779 g_child_watch_prepare (GSource *source,
3788 g_child_watch_check (GSource *source)
3790 GChildWatchSource *child_watch_source;
3791 gboolean child_exited;
3793 child_watch_source = (GChildWatchSource *) source;
3795 child_exited = child_watch_source->poll.revents & G_IO_IN;
3802 * Note: We do _not_ check for the special value of STILL_ACTIVE
3803 * since we know that the process has exited and doing so runs into
3804 * problems if the child process "happens to return STILL_ACTIVE(259)"
3805 * as Microsoft's Platform SDK puts it.
3807 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3809 gchar *emsg = g_win32_error_message (GetLastError ());
3810 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3813 child_watch_source->child_status = -1;
3816 child_watch_source->child_status = child_status;
3819 return child_exited;
3822 #else /* G_OS_WIN32 */
3825 check_for_child_exited (GSource *source)
3827 GChildWatchSource *child_watch_source;
3830 /* protect against another SIGCHLD in the middle of this call */
3831 count = child_watch_count;
3833 child_watch_source = (GChildWatchSource *) source;
3835 if (child_watch_source->child_exited)
3838 if (child_watch_source->count < count)
3842 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3844 child_watch_source->child_status = child_status;
3845 child_watch_source->child_exited = TRUE;
3847 child_watch_source->count = count;
3850 return child_watch_source->child_exited;
3854 g_child_watch_prepare (GSource *source,
3859 return check_for_child_exited (source);
3864 g_child_watch_check (GSource *source)
3866 return check_for_child_exited (source);
3869 #endif /* G_OS_WIN32 */
3872 g_child_watch_dispatch (GSource *source,
3873 GSourceFunc callback,
3876 GChildWatchSource *child_watch_source;
3877 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3879 child_watch_source = (GChildWatchSource *) source;
3883 g_warning ("Child watch source dispatched without callback\n"
3884 "You must call g_source_set_callback().");
3888 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3890 /* We never keep a child watch source around as the child is gone */
3897 g_child_watch_signal_handler (int signum)
3899 child_watch_count ++;
3901 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3903 write (child_watch_wake_up_pipe[1], "B", 1);
3907 /* We count on the signal interrupting the poll in the same thread.
3913 g_child_watch_source_init_single (void)
3915 struct sigaction action;
3917 g_assert (! g_thread_supported());
3918 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3920 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3922 action.sa_handler = g_child_watch_signal_handler;
3923 sigemptyset (&action.sa_mask);
3924 action.sa_flags = SA_NOCLDSTOP;
3925 sigaction (SIGCHLD, &action, NULL);
3929 child_watch_helper_thread (gpointer data)
3936 read (child_watch_wake_up_pipe[0], b, 20);
3938 /* We were woken up. Wake up all other contexts in all other threads */
3939 G_LOCK (main_context_list);
3940 for (list = main_context_list; list; list = list->next)
3942 GMainContext *context;
3944 context = list->data;
3945 if (g_atomic_int_get (&context->ref_count) > 0)
3946 /* Due to racing conditions we can find ref_count == 0, in
3947 * that case, however, the context is still not destroyed
3948 * and no poll can be active, otherwise the ref_count
3950 g_main_context_wakeup (context);
3952 G_UNLOCK (main_context_list);
3959 g_child_watch_source_init_multi_threaded (void)
3961 GError *error = NULL;
3962 struct sigaction action;
3964 g_assert (g_thread_supported());
3966 if (pipe (child_watch_wake_up_pipe) < 0)
3967 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3968 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3970 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3971 /* FIXME: Think this through for races */
3972 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3973 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3974 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3976 action.sa_handler = g_child_watch_signal_handler;
3977 sigemptyset (&action.sa_mask);
3978 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3979 sigaction (SIGCHLD, &action, NULL);
3983 g_child_watch_source_init_promote_single_to_threaded (void)
3985 g_child_watch_source_init_multi_threaded ();
3989 g_child_watch_source_init (void)
3991 if (g_thread_supported())
3993 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3994 g_child_watch_source_init_multi_threaded ();
3995 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3996 g_child_watch_source_init_promote_single_to_threaded ();
4000 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
4001 g_child_watch_source_init_single ();
4005 #endif /* !G_OS_WIN32 */
4008 * g_child_watch_source_new:
4009 * @pid: process to watch. On POSIX the pid of a child process. On
4010 * Windows a handle for a process (which doesn't have to be a child).
4012 * Creates a new child_watch source.
4014 * The source will not initially be associated with any #GMainContext
4015 * and must be added to one with g_source_attach() before it will be
4018 * Note that child watch sources can only be used in conjunction with
4019 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
4022 * Note that on platforms where #GPid must be explicitly closed
4023 * (see g_spawn_close_pid()) @pid must not be closed while the
4024 * source is still active. Typically, you will want to call
4025 * g_spawn_close_pid() in the callback function for the source.
4027 * Note further that using g_child_watch_source_new() is not
4028 * compatible with calling <literal>waitpid(-1)</literal> in
4029 * the application. Calling waitpid() for individual pids will
4032 * Return value: the newly-created child watch source
4037 g_child_watch_source_new (GPid pid)
4039 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
4040 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
4043 child_watch_source->poll.fd = (int)pid;
4044 child_watch_source->poll.events = G_IO_IN;
4046 g_source_add_poll (source, &child_watch_source->poll);
4047 #else /* G_OS_WIN32 */
4048 g_child_watch_source_init ();
4049 #endif /* G_OS_WIN32 */
4051 child_watch_source->pid = pid;
4057 * g_child_watch_add_full:
4058 * @priority: the priority of the idle source. Typically this will be in the
4059 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4060 * @pid: process to watch. On POSIX the pid of a child process. On
4061 * Windows a handle for a process (which doesn't have to be a child).
4062 * @function: function to call
4063 * @data: data to pass to @function
4064 * @notify: function to call when the idle is removed, or %NULL
4066 * Sets a function to be called when the child indicated by @pid
4067 * exits, at the priority @priority.
4069 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4070 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4071 * the spawn function for the child watching to work.
4073 * Note that on platforms where #GPid must be explicitly closed
4074 * (see g_spawn_close_pid()) @pid must not be closed while the
4075 * source is still active. Typically, you will want to call
4076 * g_spawn_close_pid() in the callback function for the source.
4078 * GLib supports only a single callback per process id.
4080 * Return value: the ID (greater than 0) of the event source.
4085 g_child_watch_add_full (gint priority,
4087 GChildWatchFunc function,
4089 GDestroyNotify notify)
4094 g_return_val_if_fail (function != NULL, 0);
4096 source = g_child_watch_source_new (pid);
4098 if (priority != G_PRIORITY_DEFAULT)
4099 g_source_set_priority (source, priority);
4101 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4102 id = g_source_attach (source, NULL);
4103 g_source_unref (source);
4109 * g_child_watch_add:
4110 * @pid: process id to watch. On POSIX the pid of a child process. On
4111 * Windows a handle for a process (which doesn't have to be a child).
4112 * @function: function to call
4113 * @data: data to pass to @function
4115 * Sets a function to be called when the child indicated by @pid
4116 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4118 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4119 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4120 * the spawn function for the child watching to work.
4122 * Note that on platforms where #GPid must be explicitly closed
4123 * (see g_spawn_close_pid()) @pid must not be closed while the
4124 * source is still active. Typically, you will want to call
4125 * g_spawn_close_pid() in the callback function for the source.
4127 * GLib supports only a single callback per process id.
4129 * Return value: the ID (greater than 0) of the event source.
4134 g_child_watch_add (GPid pid,
4135 GChildWatchFunc function,
4138 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4142 /* Idle functions */
4145 g_idle_prepare (GSource *source,
4154 g_idle_check (GSource *source)
4160 g_idle_dispatch (GSource *source,
4161 GSourceFunc callback,
4166 g_warning ("Idle source dispatched without callback\n"
4167 "You must call g_source_set_callback().");
4171 return callback (user_data);
4175 * g_idle_source_new:
4177 * Creates a new idle source.
4179 * The source will not initially be associated with any #GMainContext
4180 * and must be added to one with g_source_attach() before it will be
4181 * executed. Note that the default priority for idle sources is
4182 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4183 * have a default priority of %G_PRIORITY_DEFAULT.
4185 * Return value: the newly-created idle source
4188 g_idle_source_new (void)
4192 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4193 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4200 * @priority: the priority of the idle source. Typically this will be in the
4201 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4202 * @function: function to call
4203 * @data: data to pass to @function
4204 * @notify: function to call when the idle is removed, or %NULL
4206 * Adds a function to be called whenever there are no higher priority
4207 * events pending. If the function returns %FALSE it is automatically
4208 * removed from the list of event sources and will not be called again.
4210 * Return value: the ID (greater than 0) of the event source.
4213 g_idle_add_full (gint priority,
4214 GSourceFunc function,
4216 GDestroyNotify notify)
4221 g_return_val_if_fail (function != NULL, 0);
4223 source = g_idle_source_new ();
4225 if (priority != G_PRIORITY_DEFAULT_IDLE)
4226 g_source_set_priority (source, priority);
4228 g_source_set_callback (source, function, data, notify);
4229 id = g_source_attach (source, NULL);
4230 g_source_unref (source);
4237 * @function: function to call
4238 * @data: data to pass to @function.
4240 * Adds a function to be called whenever there are no higher priority
4241 * events pending to the default main loop. The function is given the
4242 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4243 * returns %FALSE it is automatically removed from the list of event
4244 * sources and will not be called again.
4246 * Return value: the ID (greater than 0) of the event source.
4249 g_idle_add (GSourceFunc function,
4252 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4256 * g_idle_remove_by_data:
4257 * @data: the data for the idle source's callback.
4259 * Removes the idle function with the given data.
4261 * Return value: %TRUE if an idle source was found and removed.
4264 g_idle_remove_by_data (gpointer data)
4266 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4269 #define __G_MAIN_C__
4270 #include "galiasdef.c"