1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 /* uncomment the next line to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
40 #include "gthreadinit.h"
41 #include <sys/types.h>
43 #ifdef HAVE_SYS_TIME_H
45 #endif /* HAVE_SYS_TIME_H */
46 #ifdef GLIB_HAVE_SYS_POLL_H
47 # include <sys/poll.h>
48 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
49 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
50 #endif /* GLIB_HAVE_SYS_POLL_H */
53 #endif /* HAVE_UNISTD_H */
59 #endif /* G_OS_WIN32 */
62 #include <net/socket.h>
63 #endif /* G_OS_BEOS */
67 typedef struct _GTimeoutSource GTimeoutSource;
68 typedef struct _GPollRec GPollRec;
69 typedef struct _GSourceCallback GSourceCallback;
73 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
74 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
77 #ifdef G_THREADS_ENABLED
78 typedef struct _GMainWaiter GMainWaiter;
89 #ifdef G_THREADS_ENABLED
90 /* The following lock is used for both the list of sources
91 * and the list of poll records
102 GPtrArray *pending_dispatches;
103 gint timeout; /* Timeout for current iteration */
106 GSource *source_list;
107 gint in_check_or_prepare;
109 GPollRec *poll_records;
110 GPollRec *poll_free_list;
111 GMemChunk *poll_chunk;
112 guint n_poll_records;
113 GPollFD *cached_poll_array;
114 guint cached_poll_array_size;
116 #ifdef G_THREADS_ENABLED
118 /* this pipe is used to wake up the main loop when a source is added.
120 gint wake_up_pipe[2];
121 #else /* G_OS_WIN32 */
122 HANDLE wake_up_semaphore;
123 #endif /* G_OS_WIN32 */
126 gboolean poll_waiting;
128 /* Flag indicating whether the set of fd's changed during a poll */
129 gboolean poll_changed;
130 #endif /* G_THREADS_ENABLED */
134 GTimeVal current_time;
135 gboolean time_is_current;
138 struct _GSourceCallback
143 GDestroyNotify notify;
148 GMainContext *context;
153 struct _GTimeoutSource
167 #ifdef G_THREADS_ENABLED
168 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
169 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
170 #define G_THREAD_SELF g_thread_self ()
172 #define LOCK_CONTEXT(context) (void)0
173 #define UNLOCK_CONTEXT(context) (void)0
174 #define G_THREAD_SELF NULL
177 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
179 #define SOURCE_UNREF(source, context) \
181 if ((source)->ref_count > 1) \
182 (source)->ref_count--; \
184 g_source_unref_internal ((source), (context), TRUE); \
188 /* Forward declarations */
190 static void g_source_unref_internal (GSource *source,
191 GMainContext *context,
193 static void g_source_destroy_internal (GSource *source,
194 GMainContext *context,
196 static void g_main_context_poll (GMainContext *context,
201 static void g_main_context_add_poll_unlocked (GMainContext *context,
204 static void g_main_context_remove_poll_unlocked (GMainContext *context,
206 static void g_main_context_wakeup_unlocked (GMainContext *context);
208 static gboolean g_timeout_prepare (GSource *source,
210 static gboolean g_timeout_check (GSource *source);
211 static gboolean g_timeout_dispatch (GSource *source,
212 GSourceFunc callback,
214 static gboolean g_idle_prepare (GSource *source,
216 static gboolean g_idle_check (GSource *source);
217 static gboolean g_idle_dispatch (GSource *source,
218 GSourceFunc callback,
221 G_LOCK_DEFINE_STATIC (main_loop);
222 static GMainContext *default_main_context;
223 static GSList *main_contexts_without_pipe = NULL;
225 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
226 __declspec(dllexport)
228 GSourceFuncs g_timeout_funcs =
236 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
237 __declspec(dllexport)
239 GSourceFuncs g_idle_funcs =
248 /* SunOS has poll, but doesn't provide a prototype. */
249 # if defined (sun) && !defined (__SVR4)
250 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
252 #else /* !HAVE_POLL */
257 g_poll (GPollFD *fds,
261 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
262 gboolean poll_msgs = FALSE;
269 for (f = fds; f < &fds[nfds]; ++f)
272 if (f->fd == G_WIN32_MSG_HANDLE)
276 #ifdef G_MAIN_POLL_DEBUG
277 g_print ("g_poll: waiting for %#x\n", f->fd);
279 handles[nhandles++] = (HANDLE) f->fd;
288 /* Waiting for messages, and maybe events
289 * -> First PeekMessage
291 #ifdef G_MAIN_POLL_DEBUG
292 g_print ("PeekMessage\n");
294 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
295 ready = WAIT_OBJECT_0 + nhandles;
300 /* Waiting just for messages */
301 if (timeout == INFINITE)
306 #ifdef G_MAIN_POLL_DEBUG
307 g_print ("WaitMessage\n");
311 gchar *emsg = g_win32_error_message (GetLastError ());
312 g_warning (G_STRLOC ": WaitMessage() failed: %s", emsg);
315 ready = WAIT_OBJECT_0 + nhandles;
317 else if (timeout == 0)
319 /* Waiting just for messages, zero timeout.
320 * If we got here, there was no message
322 ready = WAIT_TIMEOUT;
326 /* Waiting just for messages, some timeout
327 * -> Set a timer, wait for message,
328 * kill timer, use PeekMessage
330 timer = SetTimer (NULL, 0, timeout, NULL);
333 gchar *emsg = g_win32_error_message (GetLastError ());
334 g_warning (G_STRLOC ": SetTimer() failed: %s", emsg);
336 ready = WAIT_TIMEOUT;
340 #ifdef G_MAIN_POLL_DEBUG
341 g_print ("WaitMessage\n");
344 KillTimer (NULL, timer);
345 #ifdef G_MAIN_POLL_DEBUG
346 g_print ("PeekMessage\n");
348 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
349 && msg.message != WM_TIMER)
350 ready = WAIT_OBJECT_0;
352 ready = WAIT_TIMEOUT;
358 /* Wait for either message or event
359 * -> Use MsgWaitForMultipleObjects
361 #ifdef G_MAIN_POLL_DEBUG
362 g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
364 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
365 timeout, QS_ALLINPUT);
367 if (ready == WAIT_FAILED)
369 gchar *emsg = g_win32_error_message (GetLastError ());
370 g_warning (G_STRLOC ": MsgWaitForMultipleObjects() failed: %s", emsg);
376 else if (nhandles == 0)
378 /* Wait for nothing (huh?) */
383 /* Wait for just events
384 * -> Use WaitForMultipleObjects
386 #ifdef G_MAIN_POLL_DEBUG
387 g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
389 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
390 if (ready == WAIT_FAILED)
392 gchar *emsg = g_win32_error_message (GetLastError ());
393 g_warning (G_STRLOC ": WaitForMultipleObjects() failed: %s", emsg);
398 #ifdef G_MAIN_POLL_DEBUG
399 g_print ("wait returns %ld%s\n",
401 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
402 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
403 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
405 for (f = fds; f < &fds[nfds]; ++f)
408 if (ready == WAIT_FAILED)
410 else if (ready == WAIT_TIMEOUT)
412 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
414 for (f = fds; f < &fds[nfds]; ++f)
417 if (f->events & G_IO_IN)
418 if (f->fd == G_WIN32_MSG_HANDLE)
419 f->revents |= G_IO_IN;
422 #if 1 /* TEST_WITHOUT_THIS */
423 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
424 for (f = fds; f < &fds[nfds]; ++f)
426 if ((f->events & (G_IO_IN | G_IO_OUT))
427 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
429 if (f->events & G_IO_IN)
430 f->revents |= G_IO_IN;
432 f->revents |= G_IO_OUT;
433 #ifdef G_MAIN_POLL_DEBUG
434 g_print ("g_poll: got event %#x\n", f->fd);
437 ResetEvent ((HANDLE) f->fd);
446 #else /* !G_OS_WIN32 */
448 /* The following implementation of poll() comes from the GNU C Library.
449 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
452 #include <string.h> /* for bzero on BSD systems */
454 #ifdef HAVE_SYS_SELECT_H
455 #include <sys/select.h>
456 #endif /* HAVE_SYS_SELECT_H */
460 #endif /* G_OS_BEOS */
463 # define SELECT_MASK fd_set
464 #else /* !NO_FD_SET */
466 typedef long fd_mask;
469 # define SELECT_MASK void
471 # define SELECT_MASK int
472 # endif /* !_IBMR2 */
473 #endif /* !NO_FD_SET */
476 g_poll (GPollFD *fds,
481 SELECT_MASK rset, wset, xset;
490 for (f = fds; f < &fds[nfds]; ++f)
493 if (f->events & G_IO_IN)
494 FD_SET (f->fd, &rset);
495 if (f->events & G_IO_OUT)
496 FD_SET (f->fd, &wset);
497 if (f->events & G_IO_PRI)
498 FD_SET (f->fd, &xset);
499 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
503 tv.tv_sec = timeout / 1000;
504 tv.tv_usec = (timeout % 1000) * 1000;
506 ready = select (maxfd + 1, &rset, &wset, &xset,
507 timeout == -1 ? NULL : &tv);
509 for (f = fds; f < &fds[nfds]; ++f)
514 if (FD_ISSET (f->fd, &rset))
515 f->revents |= G_IO_IN;
516 if (FD_ISSET (f->fd, &wset))
517 f->revents |= G_IO_OUT;
518 if (FD_ISSET (f->fd, &xset))
519 f->revents |= G_IO_PRI;
526 #endif /* !G_OS_WIN32 */
528 #endif /* !HAVE_POLL */
531 * g_main_context_ref:
532 * @context: a #GMainContext
534 * Increases the reference count on a #GMainContext object by one.
537 g_main_context_ref (GMainContext *context)
539 g_return_if_fail (context != NULL);
540 g_return_if_fail (context->ref_count > 0);
542 LOCK_CONTEXT (context);
544 context->ref_count++;
546 UNLOCK_CONTEXT (context);
550 g_main_context_unref_and_unlock (GMainContext *context)
554 context->ref_count--;
556 if (context->ref_count != 0)
558 UNLOCK_CONTEXT (context);
562 source = context->source_list;
565 GSource *next = source->next;
566 g_source_destroy_internal (source, context, TRUE);
569 UNLOCK_CONTEXT (context);
571 #ifdef G_THREADS_ENABLED
572 g_static_mutex_free (&context->mutex);
575 g_ptr_array_free (context->pending_dispatches, TRUE);
576 g_free (context->cached_poll_array);
578 g_mem_chunk_destroy (context->poll_chunk);
580 #ifdef G_THREADS_ENABLED
581 if (g_thread_supported())
584 close (context->wake_up_pipe[0]);
585 close (context->wake_up_pipe[1]);
587 CloseHandle (context->wake_up_semaphore);
591 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
599 * g_main_context_unref:
600 * @context: a #GMainContext
602 * Decreases the reference count on a #GMainContext object by one. If
603 * the result is zero, free the context and free all associated memory.
606 g_main_context_unref (GMainContext *context)
608 g_return_if_fail (context != NULL);
609 g_return_if_fail (context->ref_count > 0);
611 LOCK_CONTEXT (context);
612 g_main_context_unref_and_unlock (context);
615 #ifdef G_THREADS_ENABLED
617 g_main_context_init_pipe (GMainContext *context)
620 if (pipe (context->wake_up_pipe) < 0)
621 g_error ("Cannot create pipe main loop wake-up: %s\n",
624 context->wake_up_rec.fd = context->wake_up_pipe[0];
625 context->wake_up_rec.events = G_IO_IN;
627 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
628 if (context->wake_up_semaphore == NULL)
629 g_error ("Cannot create wake-up semaphore: %s",
630 g_win32_error_message (GetLastError ()));
631 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
632 context->wake_up_rec.events = G_IO_IN;
633 # ifdef G_MAIN_POLL_DEBUG
634 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
637 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
641 _g_main_thread_init ()
643 GSList *curr = main_contexts_without_pipe;
646 g_main_context_init_pipe ((GMainContext *)curr->data);
649 g_slist_free (main_contexts_without_pipe);
650 main_contexts_without_pipe = NULL;
652 #endif /* G_THREADS_ENABLED */
655 * g_main_context_new:
657 * Creates a new #GMainContext strcuture
659 * Return value: the new #GMainContext
662 g_main_context_new ()
664 GMainContext *context = g_new0 (GMainContext, 1);
666 #ifdef G_THREADS_ENABLED
667 g_static_mutex_init (&context->mutex);
669 context->owner = NULL;
670 context->waiters = NULL;
673 context->ref_count = 1;
675 context->next_id = 1;
677 context->source_list = NULL;
680 context->poll_func = (GPollFunc)poll;
682 context->poll_func = g_poll;
685 context->cached_poll_array = NULL;
686 context->cached_poll_array_size = 0;
688 context->pending_dispatches = g_ptr_array_new ();
690 context->time_is_current = FALSE;
692 #ifdef G_THREADS_ENABLED
693 if (g_thread_supported ())
694 g_main_context_init_pipe (context);
696 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
704 * g_main_context_default:
706 * Returns the default main context. This is the main context used
707 * for main loop functions when a main loop is not explicitly
710 * Return value: the default main context.
713 g_main_context_default (void)
719 if (!default_main_context)
720 default_main_context = g_main_context_new ();
722 G_UNLOCK (main_loop);
724 return default_main_context;
727 /* Hooks for adding to the main loop */
731 * @source_funcs: structure containing functions that implement
732 * the sources behavior.
733 * @struct_size: size of the #GSource structure to create.
735 * Creates a new #GSource structure. The size is specified to
736 * allow creating structures derived from #GSource that contain
737 * additional data. The size passed in must be at least
738 * <literal>sizeof (GSource)</literal>.
740 * The source will not initially be associated with any #GMainContext
741 * and must be added to one with g_source_attach() before it will be
744 * Return value: the newly-created #GSource.
747 g_source_new (GSourceFuncs *source_funcs,
752 g_return_val_if_fail (source_funcs != NULL, NULL);
753 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
755 source = (GSource*) g_malloc0 (struct_size);
757 source->source_funcs = source_funcs;
758 source->ref_count = 1;
760 source->priority = G_PRIORITY_DEFAULT;
762 source->flags = G_HOOK_FLAG_ACTIVE;
764 /* NULL/0 initialization for all other fields */
769 /* Holds context's lock
772 g_source_list_add (GSource *source,
773 GMainContext *context)
775 GSource *tmp_source, *last_source;
778 tmp_source = context->source_list;
779 while (tmp_source && tmp_source->priority <= source->priority)
781 last_source = tmp_source;
782 tmp_source = tmp_source->next;
785 source->next = tmp_source;
787 tmp_source->prev = source;
789 source->prev = last_source;
791 last_source->next = source;
793 context->source_list = source;
796 /* Holds context's lock
799 g_source_list_remove (GSource *source,
800 GMainContext *context)
803 source->prev->next = source->next;
805 context->source_list = source->next;
808 source->next->prev = source->prev;
816 * @source: a #GSource
817 * @context: a #GMainContext (if %NULL, the default context will be used)
819 * Adds a #GSource to a @context so that it will be executed within
822 * Return value: the ID for the source within the #GMainContext
825 g_source_attach (GSource *source,
826 GMainContext *context)
831 g_return_val_if_fail (source->context == NULL, 0);
832 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
835 context = g_main_context_default ();
837 LOCK_CONTEXT (context);
839 source->context = context;
840 result = source->source_id = context->next_id++;
843 g_source_list_add (source, context);
845 tmp_list = source->poll_fds;
848 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
849 tmp_list = tmp_list->next;
852 #ifdef G_THREADS_ENABLED
853 /* Now wake up the main loop if it is waiting in the poll() */
854 g_main_context_wakeup_unlocked (context);
857 UNLOCK_CONTEXT (context);
863 g_source_destroy_internal (GSource *source,
864 GMainContext *context,
868 LOCK_CONTEXT (context);
870 if (!SOURCE_DESTROYED (source))
873 gpointer old_cb_data;
874 GSourceCallbackFuncs *old_cb_funcs;
876 source->flags &= ~G_HOOK_FLAG_ACTIVE;
878 old_cb_data = source->callback_data;
879 old_cb_funcs = source->callback_funcs;
881 source->callback_data = NULL;
882 source->callback_funcs = NULL;
886 UNLOCK_CONTEXT (context);
887 old_cb_funcs->unref (old_cb_data);
888 LOCK_CONTEXT (context);
891 tmp_list = source->poll_fds;
894 g_main_context_remove_poll_unlocked (context, tmp_list->data);
895 tmp_list = tmp_list->next;
898 g_source_unref_internal (source, context, TRUE);
902 UNLOCK_CONTEXT (context);
907 * @source: a #GSource
909 * Removes a source from its #GMainContext, if any, and mark it as
910 * destroyed. The source cannot be subsequently added to another
914 g_source_destroy (GSource *source)
916 GMainContext *context;
918 g_return_if_fail (source != NULL);
920 context = source->context;
923 g_source_destroy_internal (source, context, FALSE);
925 source->flags &= ~G_HOOK_FLAG_ACTIVE;
930 * @source: a #GSource
932 * Returns the numeric ID for a particular source. The ID of a source
933 * is unique within a particular main loop context. The reverse
934 * mapping from ID to source is done by g_main_context_find_source_by_id().
936 * Return value: the ID for the source
939 g_source_get_id (GSource *source)
943 g_return_val_if_fail (source != NULL, 0);
944 g_return_val_if_fail (source->context != NULL, 0);
946 LOCK_CONTEXT (source->context);
947 result = source->source_id;
948 UNLOCK_CONTEXT (source->context);
954 * g_source_get_context:
955 * @source: a #GSource
957 * Gets the #GMainContext with which the source is associated.
958 * Calling this function on a destroyed source is an error.
960 * Return value: the #GMainContext with which the source is associated,
961 * or %NULL if the context has not yet been added
965 g_source_get_context (GSource *source)
967 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
969 return source->context;
975 * @fd: a #GPollFD structure holding information about a file
976 * descriptor to watch.
978 * Adds a file descriptor to the set of file descriptors polled for
979 * this source. This is usually combined with g_source_new() to add an
980 * event source. The event source's check function will typically test
981 * the @revents field in the #GPollFD struct and return %TRUE if events need
985 g_source_add_poll (GSource *source,
988 GMainContext *context;
990 g_return_if_fail (source != NULL);
991 g_return_if_fail (fd != NULL);
992 g_return_if_fail (!SOURCE_DESTROYED (source));
994 context = source->context;
997 LOCK_CONTEXT (context);
999 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1003 g_main_context_add_poll_unlocked (context, source->priority, fd);
1004 UNLOCK_CONTEXT (context);
1009 * g_source_remove_poll:
1010 * @source:a #GSource
1011 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1013 * Removes a file descriptor from the set of file descriptors polled for
1017 g_source_remove_poll (GSource *source,
1020 GMainContext *context;
1022 g_return_if_fail (source != NULL);
1023 g_return_if_fail (fd != NULL);
1024 g_return_if_fail (!SOURCE_DESTROYED (source));
1026 context = source->context;
1029 LOCK_CONTEXT (context);
1031 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1035 g_main_context_remove_poll_unlocked (context, fd);
1036 UNLOCK_CONTEXT (context);
1041 * g_source_set_callback_indirect:
1042 * @source: the source
1043 * @callback_data: pointer to callback data "object"
1044 * @callback_funcs: functions for reference counting @callback_data
1045 * and getting the callback and data
1047 * Sets the callback function storing the data as a refcounted callback
1048 * "object". This is used internally. Note that calling
1049 * g_source_set_callback_indirect() assumes
1050 * an initial reference count on @callback_data, and thus
1051 * @callback_funcs->unref will eventually be called once more
1052 * than @callback_funcs->ref.
1055 g_source_set_callback_indirect (GSource *source,
1056 gpointer callback_data,
1057 GSourceCallbackFuncs *callback_funcs)
1059 GMainContext *context;
1060 gpointer old_cb_data;
1061 GSourceCallbackFuncs *old_cb_funcs;
1063 g_return_if_fail (source != NULL);
1064 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1066 context = source->context;
1069 LOCK_CONTEXT (context);
1071 old_cb_data = source->callback_data;
1072 old_cb_funcs = source->callback_funcs;
1074 source->callback_data = callback_data;
1075 source->callback_funcs = callback_funcs;
1078 UNLOCK_CONTEXT (context);
1081 old_cb_funcs->unref (old_cb_data);
1085 g_source_callback_ref (gpointer cb_data)
1087 GSourceCallback *callback = cb_data;
1089 callback->ref_count++;
1094 g_source_callback_unref (gpointer cb_data)
1096 GSourceCallback *callback = cb_data;
1098 callback->ref_count--;
1099 if (callback->ref_count == 0)
1101 if (callback->notify)
1102 callback->notify (callback->data);
1108 g_source_callback_get (gpointer cb_data,
1113 GSourceCallback *callback = cb_data;
1115 *func = callback->func;
1116 *data = callback->data;
1119 static GSourceCallbackFuncs g_source_callback_funcs = {
1120 g_source_callback_ref,
1121 g_source_callback_unref,
1122 g_source_callback_get,
1126 * g_source_set_callback:
1127 * @source: the source
1128 * @func: a callback function
1129 * @data: the data to pass to callback function
1130 * @notify: a function to call when @data is no longer in use, or %NULL.
1132 * Sets the callback function for a source. The callback for a source is
1133 * called from the source's dispatch function.
1135 * The exact type of @func depends on the type of source; ie. you
1136 * should not count on @func being called with @data as its first
1139 * Typically, you won't use this function. Instead use functions specific
1140 * to the type of source you are using.
1143 g_source_set_callback (GSource *source,
1146 GDestroyNotify notify)
1148 GSourceCallback *new_callback;
1150 g_return_if_fail (source != NULL);
1152 new_callback = g_new (GSourceCallback, 1);
1154 new_callback->ref_count = 1;
1155 new_callback->func = func;
1156 new_callback->data = data;
1157 new_callback->notify = notify;
1159 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1163 * g_source_set_priority:
1164 * @source: a #GSource
1165 * @priority: the new priority.
1167 * Sets the priority of a source. While the main loop is being
1168 * run, a source will be dispatched if it is ready to be dispatched and no sources
1169 * at a higher (numerically smaller) priority are ready to be dispatched.
1172 g_source_set_priority (GSource *source,
1176 GMainContext *context;
1178 g_return_if_fail (source != NULL);
1180 context = source->context;
1183 LOCK_CONTEXT (context);
1185 source->priority = priority;
1189 source->next = NULL;
1190 source->prev = NULL;
1192 tmp_list = source->poll_fds;
1195 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1196 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1198 tmp_list = tmp_list->next;
1201 UNLOCK_CONTEXT (source->context);
1206 * g_source_get_priority:
1207 * @source: a #GSource
1209 * Gets the priority of a source.
1211 * Return value: the priority of the source
1214 g_source_get_priority (GSource *source)
1216 g_return_val_if_fail (source != NULL, 0);
1218 return source->priority;
1222 * g_source_set_can_recurse:
1223 * @source: a #GSource
1224 * @can_recurse: whether recursion is allowed for this source
1226 * Sets whether a source can be called recursively. If @can_recurse is
1227 * %TRUE, then while the source is being dispatched then this source
1228 * will be processed normally. Otherwise, all processing of this
1229 * source is blocked until the dispatch function returns.
1232 g_source_set_can_recurse (GSource *source,
1233 gboolean can_recurse)
1235 GMainContext *context;
1237 g_return_if_fail (source != NULL);
1239 context = source->context;
1242 LOCK_CONTEXT (context);
1245 source->flags |= G_SOURCE_CAN_RECURSE;
1247 source->flags &= ~G_SOURCE_CAN_RECURSE;
1250 UNLOCK_CONTEXT (context);
1254 * g_source_get_can_recurse:
1255 * @source: a #GSource
1257 * Checks whether a source is allowed to be called recursively.
1258 * see g_source_set_can_recurse().
1260 * Return value: whether recursion is allowed.
1263 g_source_get_can_recurse (GSource *source)
1265 g_return_val_if_fail (source != NULL, FALSE);
1267 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1272 * @source: a #GSource
1274 * Increases the reference count on a source by one.
1276 * Return value: @source
1279 g_source_ref (GSource *source)
1281 GMainContext *context;
1283 g_return_val_if_fail (source != NULL, NULL);
1285 context = source->context;
1288 LOCK_CONTEXT (context);
1290 source->ref_count++;
1293 UNLOCK_CONTEXT (context);
1298 /* g_source_unref() but possible to call within context lock
1301 g_source_unref_internal (GSource *source,
1302 GMainContext *context,
1305 gpointer old_cb_data = NULL;
1306 GSourceCallbackFuncs *old_cb_funcs = NULL;
1308 g_return_if_fail (source != NULL);
1310 if (!have_lock && context)
1311 LOCK_CONTEXT (context);
1313 source->ref_count--;
1314 if (source->ref_count == 0)
1316 old_cb_data = source->callback_data;
1317 old_cb_funcs = source->callback_funcs;
1319 source->callback_data = NULL;
1320 source->callback_funcs = NULL;
1322 if (context && !SOURCE_DESTROYED (source))
1324 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1325 source->ref_count++;
1328 g_source_list_remove (source, context);
1330 if (source->source_funcs->finalize)
1331 source->source_funcs->finalize (source);
1333 g_slist_free (source->poll_fds);
1334 source->poll_fds = NULL;
1338 if (!have_lock && context)
1339 UNLOCK_CONTEXT (context);
1344 UNLOCK_CONTEXT (context);
1346 old_cb_funcs->unref (old_cb_data);
1349 LOCK_CONTEXT (context);
1355 * @source: a #GSource
1357 * Decreases the reference count of a source by one. If the
1358 * resulting reference count is zero the source and associated
1359 * memory will be destroyed.
1362 g_source_unref (GSource *source)
1364 g_return_if_fail (source != NULL);
1366 g_source_unref_internal (source, source->context, FALSE);
1370 * g_main_context_find_source_by_id:
1371 * @context: a #GMainContext (if %NULL, the default context will be used)
1372 * @source_id: the source ID, as returned by g_source_get_id()
1374 * Finds a #GSource given a pair of context and ID
1376 * Return value: the #GSource if found, otherwise, %NULL
1379 g_main_context_find_source_by_id (GMainContext *context,
1384 g_return_val_if_fail (source_id > 0, NULL);
1386 if (context == NULL)
1387 context = g_main_context_default ();
1389 LOCK_CONTEXT (context);
1391 source = context->source_list;
1394 if (!SOURCE_DESTROYED (source) &&
1395 source->source_id == source_id)
1397 source = source->next;
1400 UNLOCK_CONTEXT (context);
1406 * g_main_context_find_source_by_funcs_user_data:
1407 * @context: a #GMainContext (if %NULL, the default context will be used).
1408 * @funcs: the @source_funcs passed to g_source_new().
1409 * @user_data: the user data from the callback.
1411 * Finds a source with the given source functions and user data. If
1412 * multiple sources exist with the same source function and user data,
1413 * the first one found will be returned.
1415 * Return value: the source, if one was found, otherwise %NULL
1418 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1419 GSourceFuncs *funcs,
1424 g_return_val_if_fail (funcs != NULL, NULL);
1426 if (context == NULL)
1427 context = g_main_context_default ();
1429 LOCK_CONTEXT (context);
1431 source = context->source_list;
1434 if (!SOURCE_DESTROYED (source) &&
1435 source->source_funcs == funcs &&
1436 source->callback_funcs)
1438 GSourceFunc callback;
1439 gpointer callback_data;
1441 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1443 if (callback_data == user_data)
1446 source = source->next;
1449 UNLOCK_CONTEXT (context);
1455 * g_main_context_find_source_by_user_data:
1456 * @context: a #GMainContext
1457 * @user_data: the user_data for the callback.
1459 * Finds a source with the given user data for the callback. If
1460 * multiple sources exist with the same user data, the first
1461 * one found will be returned.
1463 * Return value: the source, if one was found, otherwise %NULL
1466 g_main_context_find_source_by_user_data (GMainContext *context,
1471 if (context == NULL)
1472 context = g_main_context_default ();
1474 LOCK_CONTEXT (context);
1476 source = context->source_list;
1479 if (!SOURCE_DESTROYED (source) &&
1480 source->callback_funcs)
1482 GSourceFunc callback;
1483 gpointer callback_data = NULL;
1485 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1487 if (callback_data == user_data)
1490 source = source->next;
1493 UNLOCK_CONTEXT (context);
1500 * @tag: the id of the source to remove.
1502 * Removes the source with the given id from the default main
1503 * context. The id of a #GSource is given by g_source_get_id(),
1504 * or will be returned by the functions g_source_attach(),
1505 * g_idle_add(), g_idle_add_full(), g_timeout_add(),
1506 * g_timeout_add_full(), g_io_add_watch, and g_io_add_watch_full().
1508 * See also g_source_destroy().
1510 * Return value: %TRUE if the source was found and removed.
1513 g_source_remove (guint tag)
1517 g_return_val_if_fail (tag > 0, FALSE);
1519 source = g_main_context_find_source_by_id (NULL, tag);
1521 g_source_destroy (source);
1523 return source != NULL;
1527 * g_source_remove_by_user_data:
1528 * @user_data: the user_data for the callback.
1530 * Removes a source from the default main loop context given the user
1531 * data for the callback. If multiple sources exist with the same user
1532 * data, only one will be destroyed.
1534 * Return value: %TRUE if a source was found and removed.
1537 g_source_remove_by_user_data (gpointer user_data)
1541 source = g_main_context_find_source_by_user_data (NULL, user_data);
1544 g_source_destroy (source);
1552 * g_source_remove_by_funcs_user_data:
1553 * @funcs: The @source_funcs passed to g_source_new()
1554 * @user_data: the user data for the callback
1556 * Removes a source from the default main loop context given the
1557 * source functions and user data. If multiple sources exist with the
1558 * same source functions and user data, only one will be destroyed.
1560 * Return value: %TRUE if a source was found and removed.
1563 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1568 g_return_val_if_fail (funcs != NULL, FALSE);
1570 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1573 g_source_destroy (source);
1581 * g_get_current_time:
1582 * @result: #GTimeVal structure in which to store current time.
1584 * Equivalent to the UNIX <function>gettimeofday()</function> function, but portable.
1587 g_get_current_time (GTimeVal *result)
1592 g_return_if_fail (result != NULL);
1594 /*this is required on alpha, there the timeval structs are int's
1595 not longs and a cast only would fail horribly*/
1596 gettimeofday (&r, NULL);
1597 result->tv_sec = r.tv_sec;
1598 result->tv_usec = r.tv_usec;
1600 /* Avoid calling time() except for the first time.
1601 * GetTickCount() should be pretty fast and low-level?
1602 * I could also use ftime() but it seems unnecessarily overheady.
1604 static DWORD start_tick = 0;
1605 static time_t start_time;
1608 g_return_if_fail (result != NULL);
1610 if (start_tick == 0)
1612 start_tick = GetTickCount ();
1616 tick = GetTickCount ();
1618 result->tv_sec = (tick - start_tick) / 1000 + start_time;
1619 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
1623 /* Running the main loop */
1625 /* HOLDS: context's lock */
1627 g_main_dispatch (GMainContext *context)
1631 for (i = 0; i < context->pending_dispatches->len; i++)
1633 GSource *source = context->pending_dispatches->pdata[i];
1635 context->pending_dispatches->pdata[i] = NULL;
1638 source->flags &= ~G_SOURCE_READY;
1640 if (!SOURCE_DESTROYED (source))
1642 gboolean was_in_call;
1643 gpointer user_data = NULL;
1644 GSourceFunc callback = NULL;
1645 GSourceCallbackFuncs *cb_funcs;
1647 gboolean need_destroy;
1649 gboolean (*dispatch) (GSource *,
1653 dispatch = source->source_funcs->dispatch;
1654 cb_funcs = source->callback_funcs;
1655 cb_data = source->callback_data;
1658 cb_funcs->ref (cb_data);
1660 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1661 source->flags |= G_HOOK_FLAG_IN_CALL;
1664 cb_funcs->get (cb_data, source, &callback, &user_data);
1666 UNLOCK_CONTEXT (context);
1668 need_destroy = ! dispatch (source,
1671 LOCK_CONTEXT (context);
1674 cb_funcs->unref (cb_data);
1677 source->flags &= ~G_HOOK_FLAG_IN_CALL;
1679 /* Note: this depends on the fact that we can't switch
1680 * sources from one main context to another
1682 if (need_destroy && !SOURCE_DESTROYED (source))
1684 g_assert (source->context == context);
1685 g_source_destroy_internal (source, context, TRUE);
1689 SOURCE_UNREF (source, context);
1692 g_ptr_array_set_size (context->pending_dispatches, 0);
1695 /* Holds context's lock */
1696 static inline GSource *
1697 next_valid_source (GMainContext *context,
1700 GSource *new_source = source ? source->next : context->source_list;
1704 if (!SOURCE_DESTROYED (new_source))
1706 new_source->ref_count++;
1710 new_source = new_source->next;
1714 SOURCE_UNREF (source, context);
1720 * g_main_context_acquire:
1721 * @context: a #GMainContext
1723 * Tries to become the owner of the specified context.
1724 * If some other context is the owner of the context,
1725 * returns %FALSE immediately. Ownership is properly
1726 * recursive: the owner can require ownership again
1727 * and will release ownership when g_main_context_release()
1728 * is called as many times as g_main_context_acquire().
1730 * You must be the owner of a context before you
1731 * can call g_main_context_prepare(), g_main_context_query(),
1732 * g_main_context_check(), g_main_context_dispatch().
1734 * Return value: %TRUE if the operation succeeded, and
1735 * this thread is now the owner of @context.
1738 g_main_context_acquire (GMainContext *context)
1740 #ifdef G_THREADS_ENABLED
1741 gboolean result = FALSE;
1742 GThread *self = G_THREAD_SELF;
1744 if (context == NULL)
1745 context = g_main_context_default ();
1747 LOCK_CONTEXT (context);
1749 if (!context->owner)
1751 context->owner = self;
1752 g_assert (context->owner_count == 0);
1755 if (context->owner == self)
1757 context->owner_count++;
1761 UNLOCK_CONTEXT (context);
1764 #else /* !G_THREADS_ENABLED */
1766 #endif /* G_THREADS_ENABLED */
1770 * g_main_context_release:
1771 * @context: a #GMainContext
1773 * Releases ownership of a context previously acquired by this thread
1774 * with g_main_context_acquire(). If the context was acquired multiple
1775 * times, the only release ownership when g_main_context_release()
1776 * is called as many times as it was acquired.
1779 g_main_context_release (GMainContext *context)
1781 #ifdef G_THREADS_ENABLED
1782 if (context == NULL)
1783 context = g_main_context_default ();
1785 LOCK_CONTEXT (context);
1787 context->owner_count--;
1788 if (context->owner_count == 0)
1790 context->owner = NULL;
1792 if (context->waiters)
1794 GMainWaiter *waiter = context->waiters->data;
1795 gboolean loop_internal_waiter =
1796 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
1797 context->waiters = g_slist_delete_link (context->waiters,
1799 if (!loop_internal_waiter)
1800 g_mutex_lock (waiter->mutex);
1802 g_cond_signal (waiter->cond);
1804 if (!loop_internal_waiter)
1805 g_mutex_unlock (waiter->mutex);
1809 UNLOCK_CONTEXT (context);
1810 #endif /* G_THREADS_ENABLED */
1814 * g_main_context_wait:
1815 * @context: a #GMainContext
1816 * @cond: a condition variable
1817 * @mutex: a mutex, currently held
1819 * Tries to become the owner of the specified context,
1820 * as with g_main_context_acquire(). But if another thread
1821 * is the owner, atomically drop @mutex and wait on @cond until
1822 * that owner releases ownership or until @cond is signaled, then
1823 * try again (once) to become the owner.
1825 * Return value: %TRUE if the operation succeeded, and
1826 * this thread is now the owner of @context.
1829 g_main_context_wait (GMainContext *context,
1833 #ifdef G_THREADS_ENABLED
1834 gboolean result = FALSE;
1835 GThread *self = G_THREAD_SELF;
1836 gboolean loop_internal_waiter;
1838 if (context == NULL)
1839 context = g_main_context_default ();
1841 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
1843 if (!loop_internal_waiter)
1844 LOCK_CONTEXT (context);
1846 if (context->owner && context->owner != self)
1851 waiter.mutex = mutex;
1853 context->waiters = g_slist_append (context->waiters, &waiter);
1855 if (!loop_internal_waiter)
1856 UNLOCK_CONTEXT (context);
1857 g_cond_wait (cond, mutex);
1858 if (!loop_internal_waiter)
1859 LOCK_CONTEXT (context);
1861 context->waiters = g_slist_remove (context->waiters, &waiter);
1864 if (!context->owner)
1866 context->owner = self;
1867 g_assert (context->owner_count == 0);
1870 if (context->owner == self)
1872 context->owner_count++;
1876 if (!loop_internal_waiter)
1877 UNLOCK_CONTEXT (context);
1880 #else /* !G_THREADS_ENABLED */
1882 #endif /* G_THREADS_ENABLED */
1886 * g_main_context_prepare:
1887 * @context: a #GMainContext
1888 * @priority: location to store priority of highest priority
1889 * source already ready.
1891 * Prepares to poll sources within a main loop. The resulting information
1892 * for polling is determined by calling g_main_context_query ().
1894 * Return value: %TRUE if some source is ready to be dispatched
1898 g_main_context_prepare (GMainContext *context,
1903 gint current_priority = G_MAXINT;
1906 if (context == NULL)
1907 context = g_main_context_default ();
1909 LOCK_CONTEXT (context);
1911 context->time_is_current = FALSE;
1913 if (context->in_check_or_prepare)
1915 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
1916 "prepare() member.");
1917 UNLOCK_CONTEXT (context);
1921 #ifdef G_THREADS_ENABLED
1922 if (context->poll_waiting)
1924 g_warning("g_main_context_prepare(): main loop already active in another thread");
1925 UNLOCK_CONTEXT (context);
1929 context->poll_waiting = TRUE;
1930 #endif /* G_THREADS_ENABLED */
1933 /* If recursing, finish up current dispatch, before starting over */
1934 if (context->pending_dispatches)
1937 g_main_dispatch (context, ¤t_time);
1939 UNLOCK_CONTEXT (context);
1944 /* If recursing, clear list of pending dispatches */
1946 for (i = 0; i < context->pending_dispatches->len; i++)
1948 if (context->pending_dispatches->pdata[i])
1949 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
1951 g_ptr_array_set_size (context->pending_dispatches, 0);
1953 /* Prepare all sources */
1955 context->timeout = -1;
1957 source = next_valid_source (context, NULL);
1960 gint source_timeout = -1;
1962 if ((n_ready > 0) && (source->priority > current_priority))
1964 SOURCE_UNREF (source, context);
1967 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
1970 if (!(source->flags & G_SOURCE_READY))
1973 gboolean (*prepare) (GSource *source,
1976 prepare = source->source_funcs->prepare;
1977 context->in_check_or_prepare++;
1978 UNLOCK_CONTEXT (context);
1980 result = (*prepare) (source, &source_timeout);
1982 LOCK_CONTEXT (context);
1983 context->in_check_or_prepare--;
1986 source->flags |= G_SOURCE_READY;
1989 if (source->flags & G_SOURCE_READY)
1992 current_priority = source->priority;
1993 context->timeout = 0;
1996 if (source_timeout >= 0)
1998 if (context->timeout < 0)
1999 context->timeout = source_timeout;
2001 context->timeout = MIN (context->timeout, source_timeout);
2005 source = next_valid_source (context, source);
2008 UNLOCK_CONTEXT (context);
2011 *priority = current_priority;
2013 return (n_ready > 0);
2017 * g_main_context_query:
2018 * @context: a #GMainContext
2019 * @max_priority: maximum priority source to check
2020 * @timeout_: location to store timeout to be used in polling
2021 * @fds: location to store #GPollFD records that need to be polled.
2022 * @n_fds: length of @fds.
2024 * Determines information necessary to poll this main loop.
2026 * Return value: the number of records actually stored in @fds,
2027 * or, if more than @n_fds records need to be stored, the number
2028 * of records that need to be stored.
2031 g_main_context_query (GMainContext *context,
2040 LOCK_CONTEXT (context);
2042 pollrec = context->poll_records;
2044 while (pollrec && max_priority >= pollrec->priority)
2046 if (pollrec->fd->events)
2050 fds[n_poll].fd = pollrec->fd->fd;
2051 /* In direct contradiction to the Unix98 spec, IRIX runs into
2052 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2053 * flags in the events field of the pollfd while it should
2054 * just ignoring them. So we mask them out here.
2056 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2057 fds[n_poll].revents = 0;
2062 pollrec = pollrec->next;
2065 #ifdef G_THREADS_ENABLED
2066 context->poll_changed = FALSE;
2071 *timeout = context->timeout;
2073 context->time_is_current = FALSE;
2076 UNLOCK_CONTEXT (context);
2082 * g_main_context_check:
2083 * @context: a #GMainContext
2084 * @max_priority: the maximum numerical priority of sources to check
2085 * @fds: array of #GPollFD's that was passed to the last call to
2086 * g_main_context_query()
2087 * @n_fds: return value of g_main_context_query()
2089 * Passes the results of polling back to the main loop.
2091 * Return value: %TRUE if some sources are ready to be dispatched.
2094 g_main_context_check (GMainContext *context,
2104 LOCK_CONTEXT (context);
2106 if (context->in_check_or_prepare)
2108 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2109 "prepare() member.");
2110 UNLOCK_CONTEXT (context);
2114 #ifdef G_THREADS_ENABLED
2115 if (!context->poll_waiting)
2119 read (context->wake_up_pipe[0], &c, 1);
2123 context->poll_waiting = FALSE;
2125 /* If the set of poll file descriptors changed, bail out
2126 * and let the main loop rerun
2128 if (context->poll_changed)
2130 UNLOCK_CONTEXT (context);
2133 #endif /* G_THREADS_ENABLED */
2135 pollrec = context->poll_records;
2139 if (pollrec->fd->events)
2141 pollrec->fd->revents = fds[i].revents;
2144 pollrec = pollrec->next;
2147 source = next_valid_source (context, NULL);
2150 if ((n_ready > 0) && (source->priority > max_priority))
2152 SOURCE_UNREF (source, context);
2155 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
2158 if (!(source->flags & G_SOURCE_READY))
2161 gboolean (*check) (GSource *source);
2163 check = source->source_funcs->check;
2165 context->in_check_or_prepare++;
2166 UNLOCK_CONTEXT (context);
2168 result = (*check) (source);
2170 LOCK_CONTEXT (context);
2171 context->in_check_or_prepare--;
2174 source->flags |= G_SOURCE_READY;
2177 if (source->flags & G_SOURCE_READY)
2179 source->ref_count++;
2180 g_ptr_array_add (context->pending_dispatches, source);
2184 /* never dispatch sources with less priority than the first
2185 * one we choose to dispatch
2187 max_priority = source->priority;
2191 source = next_valid_source (context, source);
2194 UNLOCK_CONTEXT (context);
2200 * g_main_context_dispatch:
2201 * @context: a #GMainContext
2203 * Dispatches all pending sources.
2206 g_main_context_dispatch (GMainContext *context)
2208 LOCK_CONTEXT (context);
2210 if (context->pending_dispatches->len > 0)
2212 g_main_dispatch (context);
2215 UNLOCK_CONTEXT (context);
2218 /* HOLDS context lock */
2220 g_main_context_iterate (GMainContext *context,
2227 gboolean some_ready;
2228 gint nfds, allocated_nfds;
2229 GPollFD *fds = NULL;
2231 UNLOCK_CONTEXT (context);
2233 #ifdef G_THREADS_ENABLED
2234 if (!g_main_context_acquire (context))
2236 gboolean got_ownership;
2238 g_return_val_if_fail (g_thread_supported (), FALSE);
2243 LOCK_CONTEXT (context);
2246 context->cond = g_cond_new ();
2248 got_ownership = g_main_context_wait (context,
2250 g_static_mutex_get_mutex (&context->mutex));
2254 UNLOCK_CONTEXT (context);
2259 LOCK_CONTEXT (context);
2260 #endif /* G_THREADS_ENABLED */
2262 if (!context->cached_poll_array)
2264 context->cached_poll_array_size = context->n_poll_records;
2265 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2268 allocated_nfds = context->cached_poll_array_size;
2269 fds = context->cached_poll_array;
2271 UNLOCK_CONTEXT (context);
2273 some_ready = g_main_context_prepare (context, &max_priority);
2275 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2276 allocated_nfds)) > allocated_nfds)
2278 LOCK_CONTEXT (context);
2280 context->cached_poll_array_size = allocated_nfds = nfds;
2281 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2282 UNLOCK_CONTEXT (context);
2288 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2290 g_main_context_check (context, max_priority, fds, nfds);
2293 g_main_context_dispatch (context);
2295 #ifdef G_THREADS_ENABLED
2296 g_main_context_release (context);
2297 #endif /* G_THREADS_ENABLED */
2299 LOCK_CONTEXT (context);
2305 * g_main_context_pending:
2306 * @context: a #GMainContext (if %NULL, the default context will be used)
2308 * Checks if any sources have pending events for the given context.
2310 * Return value: %TRUE if events are pending.
2313 g_main_context_pending (GMainContext *context)
2318 context = g_main_context_default();
2320 LOCK_CONTEXT (context);
2321 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2322 UNLOCK_CONTEXT (context);
2328 * g_main_context_iteration:
2329 * @context: a #GMainContext (if %NULL, the default context will be used)
2330 * @may_block: whether the call may block.
2332 * Runs a single iteration for the given main loop. This involves
2333 * checking to see if any event sources are ready to be processed,
2334 * then if no events sources are ready and @may_block is %TRUE, waiting
2335 * for a source to become ready, then dispatching the highest priority
2336 * events sources that are ready. Note that even when @may_block is %TRUE,
2337 * it is still possible for g_main_context_iteration() to return
2338 * %FALSE, since the the wait may be interrupted for other
2339 * reasons than an event source becoming ready.
2341 * Return value: %TRUE if events were dispatched.
2344 g_main_context_iteration (GMainContext *context, gboolean may_block)
2349 context = g_main_context_default();
2351 LOCK_CONTEXT (context);
2352 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2353 UNLOCK_CONTEXT (context);
2360 * @context: a #GMainContext (if %NULL, the default context will be used).
2361 * @is_running: set to %TRUE to indicate that the loop is running. This
2362 * is not very important since calling g_main_loop_run() will set this to
2365 * Creates a new #GMainLoop structure.
2367 * Return value: a new #GMainLoop.
2370 g_main_loop_new (GMainContext *context,
2371 gboolean is_running)
2376 context = g_main_context_default();
2378 g_main_context_ref (context);
2380 loop = g_new0 (GMainLoop, 1);
2381 loop->context = context;
2382 loop->is_running = is_running != FALSE;
2383 loop->ref_count = 1;
2390 * @loop: a #GMainLoop
2392 * Increases the reference count on a #GMainLoop object by one.
2394 * Return value: @loop
2397 g_main_loop_ref (GMainLoop *loop)
2399 g_return_val_if_fail (loop != NULL, NULL);
2400 g_return_val_if_fail (loop->ref_count > 0, NULL);
2402 LOCK_CONTEXT (loop->context);
2404 UNLOCK_CONTEXT (loop->context);
2410 g_main_loop_unref_and_unlock (GMainLoop *loop)
2413 if (loop->ref_count == 0)
2415 /* When the ref_count is 0, there can be nobody else using the
2416 * loop, so it is safe to unlock before destroying.
2418 g_main_context_unref_and_unlock (loop->context);
2422 UNLOCK_CONTEXT (loop->context);
2426 * g_main_loop_unref:
2427 * @loop: a #GMainLoop
2429 * Decreases the reference count on a #GMainLoop object by one. If
2430 * the result is zero, free the loop and free all associated memory.
2433 g_main_loop_unref (GMainLoop *loop)
2435 g_return_if_fail (loop != NULL);
2436 g_return_if_fail (loop->ref_count > 0);
2438 LOCK_CONTEXT (loop->context);
2440 g_main_loop_unref_and_unlock (loop);
2445 * @loop: a #GMainLoop
2447 * Runs a main loop until g_main_loop_quit() is called on the loop.
2448 * If this is called for the thread of the loop's #GMainContext,
2449 * it will process events from the loop, otherwise it will
2453 g_main_loop_run (GMainLoop *loop)
2455 GThread *self = G_THREAD_SELF;
2457 g_return_if_fail (loop != NULL);
2458 g_return_if_fail (loop->ref_count > 0);
2460 #ifdef G_THREADS_ENABLED
2461 if (!g_main_context_acquire (loop->context))
2463 gboolean got_ownership = FALSE;
2465 /* Another thread owns this context */
2466 if (!g_thread_supported ())
2468 g_warning ("g_main_loop_run() was called from second thread but "
2469 "g_thread_init() was never called.");
2473 LOCK_CONTEXT (loop->context);
2477 if (!loop->is_running)
2478 loop->is_running = TRUE;
2480 if (!loop->context->cond)
2481 loop->context->cond = g_cond_new ();
2483 while (loop->is_running && !got_ownership)
2484 got_ownership = g_main_context_wait (loop->context,
2485 loop->context->cond,
2486 g_static_mutex_get_mutex (&loop->context->mutex));
2488 if (!loop->is_running)
2490 UNLOCK_CONTEXT (loop->context);
2492 g_main_context_release (loop->context);
2493 g_main_loop_unref (loop);
2497 g_assert (got_ownership);
2500 LOCK_CONTEXT (loop->context);
2501 #endif /* G_THREADS_ENABLED */
2503 if (loop->context->in_check_or_prepare)
2505 g_warning ("g_main_loop_run(): called recursively from within a source's "
2506 "check() or prepare() member, iteration not possible.");
2511 loop->is_running = TRUE;
2512 while (loop->is_running)
2513 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2515 UNLOCK_CONTEXT (loop->context);
2517 #ifdef G_THREADS_ENABLED
2518 g_main_context_release (loop->context);
2519 #endif /* G_THREADS_ENABLED */
2521 g_main_loop_unref (loop);
2526 * @loop: a #GMainLoop
2528 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2529 * for the loop will return.
2532 g_main_loop_quit (GMainLoop *loop)
2534 g_return_if_fail (loop != NULL);
2535 g_return_if_fail (loop->ref_count > 0);
2537 LOCK_CONTEXT (loop->context);
2538 loop->is_running = FALSE;
2539 g_main_context_wakeup_unlocked (loop->context);
2541 #ifdef G_THREADS_ENABLED
2542 if (loop->context->cond)
2543 g_cond_broadcast (loop->context->cond);
2544 #endif /* G_THREADS_ENABLED */
2546 UNLOCK_CONTEXT (loop->context);
2550 * g_main_loop_is_running:
2551 * @loop: a #GMainLoop.
2553 * Checks to see if the main loop is currently being run via g_main_loop_run().
2555 * Return value: %TRUE if the mainloop is currently being run.
2558 g_main_loop_is_running (GMainLoop *loop)
2560 g_return_val_if_fail (loop != NULL, FALSE);
2561 g_return_val_if_fail (loop->ref_count > 0, FALSE);
2563 return loop->is_running;
2567 * g_main_loop_get_context:
2568 * @loop: a #GMainLoop.
2570 * Returns the #GMainContext of @loop.
2572 * Return value: the #GMainContext of @loop
2575 g_main_loop_get_context (GMainLoop *loop)
2577 g_return_val_if_fail (loop != NULL, NULL);
2578 g_return_val_if_fail (loop->ref_count > 0, NULL);
2580 return loop->context;
2583 /* HOLDS: context's lock */
2585 g_main_context_poll (GMainContext *context,
2591 #ifdef G_MAIN_POLL_DEBUG
2597 GPollFunc poll_func;
2599 if (n_fds || timeout != 0)
2601 #ifdef G_MAIN_POLL_DEBUG
2602 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2603 poll_timer = g_timer_new ();
2606 LOCK_CONTEXT (context);
2608 poll_func = context->poll_func;
2610 UNLOCK_CONTEXT (context);
2611 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2614 g_warning ("poll(2) failed due to: %s.",
2615 g_strerror (errno));
2617 /* If g_poll () returns -1, it has already called g_warning() */
2621 #ifdef G_MAIN_POLL_DEBUG
2622 LOCK_CONTEXT (context);
2624 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2627 g_timer_elapsed (poll_timer, NULL));
2628 g_timer_destroy (poll_timer);
2629 pollrec = context->poll_records;
2633 if (pollrec->fd->events)
2637 g_print (" [%d:", fds[i].fd);
2638 if (fds[i].revents & G_IO_IN)
2640 if (fds[i].revents & G_IO_OUT)
2642 if (fds[i].revents & G_IO_PRI)
2644 if (fds[i].revents & G_IO_ERR)
2646 if (fds[i].revents & G_IO_HUP)
2648 if (fds[i].revents & G_IO_NVAL)
2654 pollrec = pollrec->next;
2658 UNLOCK_CONTEXT (context);
2660 } /* if (n_fds || timeout != 0) */
2664 * g_main_context_add_poll:
2665 * @context: a #GMainContext (or %NULL for the default context)
2666 * @fd: a #GPollFD structure holding information about a file
2667 * descriptor to watch.
2668 * @priority: the priority for this file descriptor which should be
2669 * the same as the priority used for g_source_attach() to ensure that the
2670 * file descriptor is polled whenever the results may be needed.
2672 * Adds a file descriptor to the set of file descriptors polled for
2673 * this context. This will very seldomly be used directly. Instead
2674 * a typical event source will use g_source_add_poll() instead.
2677 g_main_context_add_poll (GMainContext *context,
2682 context = g_main_context_default ();
2684 g_return_if_fail (context->ref_count > 0);
2685 g_return_if_fail (fd);
2687 LOCK_CONTEXT (context);
2688 g_main_context_add_poll_unlocked (context, priority, fd);
2689 UNLOCK_CONTEXT (context);
2692 /* HOLDS: main_loop_lock */
2694 g_main_context_add_poll_unlocked (GMainContext *context,
2698 GPollRec *lastrec, *pollrec, *newrec;
2700 if (!context->poll_chunk)
2701 context->poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
2703 if (context->poll_free_list)
2705 newrec = context->poll_free_list;
2706 context->poll_free_list = newrec->next;
2709 newrec = g_chunk_new (GPollRec, context->poll_chunk);
2711 /* This file descriptor may be checked before we ever poll */
2714 newrec->priority = priority;
2717 pollrec = context->poll_records;
2718 while (pollrec && priority >= pollrec->priority)
2721 pollrec = pollrec->next;
2725 lastrec->next = newrec;
2727 context->poll_records = newrec;
2729 newrec->next = pollrec;
2731 context->n_poll_records++;
2733 #ifdef G_THREADS_ENABLED
2734 context->poll_changed = TRUE;
2736 /* Now wake up the main loop if it is waiting in the poll() */
2737 g_main_context_wakeup_unlocked (context);
2742 * g_main_context_remove_poll:
2743 * @context:a #GMainContext
2744 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
2746 * Removes file descriptor from the set of file descriptors to be
2747 * polled for a particular context.
2750 g_main_context_remove_poll (GMainContext *context,
2754 context = g_main_context_default ();
2756 g_return_if_fail (context->ref_count > 0);
2757 g_return_if_fail (fd);
2759 LOCK_CONTEXT (context);
2760 g_main_context_remove_poll_unlocked (context, fd);
2761 UNLOCK_CONTEXT (context);
2765 g_main_context_remove_poll_unlocked (GMainContext *context,
2768 GPollRec *pollrec, *lastrec;
2771 pollrec = context->poll_records;
2775 if (pollrec->fd == fd)
2777 if (lastrec != NULL)
2778 lastrec->next = pollrec->next;
2780 context->poll_records = pollrec->next;
2782 #ifdef ENABLE_GC_FRIENDLY
2784 #endif /* ENABLE_GC_FRIENDLY */
2786 pollrec->next = context->poll_free_list;
2787 context->poll_free_list = pollrec;
2789 context->n_poll_records--;
2793 pollrec = pollrec->next;
2796 #ifdef G_THREADS_ENABLED
2797 context->poll_changed = TRUE;
2799 /* Now wake up the main loop if it is waiting in the poll() */
2800 g_main_context_wakeup_unlocked (context);
2805 * g_source_get_current_time:
2806 * @source: a #GSource
2807 * @timeval: #GTimeVal structure in which to store current time.
2809 * Gets the "current time" to be used when checking
2810 * this source. The advantage of calling this function over
2811 * calling g_get_current_time() directly is that when
2812 * checking multiple sources, GLib can cache a single value
2813 * instead of having to repeatedly get the system time.
2816 g_source_get_current_time (GSource *source,
2819 GMainContext *context;
2821 g_return_if_fail (source->context != NULL);
2823 context = source->context;
2825 LOCK_CONTEXT (context);
2827 if (!context->time_is_current)
2829 g_get_current_time (&context->current_time);
2830 context->time_is_current = TRUE;
2833 *timeval = context->current_time;
2835 UNLOCK_CONTEXT (context);
2839 * g_main_context_set_poll_func:
2840 * @context: a #GMainContext
2841 * @func: the function to call to poll all file descriptors
2843 * Sets the function to use to handle polling of file descriptors. It
2844 * will be used instead of the <function>poll()</function> system call
2845 * (or GLib's replacement function, which is used where
2846 * <function>poll()</function> isn't available).
2848 * This function could possibly be used to integrate the GLib event
2849 * loop with an external event loop.
2852 g_main_context_set_poll_func (GMainContext *context,
2856 context = g_main_context_default ();
2858 g_return_if_fail (context->ref_count > 0);
2860 LOCK_CONTEXT (context);
2863 context->poll_func = func;
2867 context->poll_func = (GPollFunc) poll;
2869 context->poll_func = (GPollFunc) g_poll;
2873 UNLOCK_CONTEXT (context);
2877 * g_main_context_get_poll_func:
2878 * @context: a #GMainContext
2880 * Gets the poll function set by g_main_context_set_poll_func().
2882 * Return value: the poll function
2885 g_main_context_get_poll_func (GMainContext *context)
2890 context = g_main_context_default ();
2892 g_return_val_if_fail (context->ref_count > 0, NULL);
2894 LOCK_CONTEXT (context);
2895 result = context->poll_func;
2896 UNLOCK_CONTEXT (context);
2901 /* HOLDS: context's lock */
2902 /* Wake the main loop up from a poll() */
2904 g_main_context_wakeup_unlocked (GMainContext *context)
2906 #ifdef G_THREADS_ENABLED
2907 if (g_thread_supported() && context->poll_waiting)
2909 context->poll_waiting = FALSE;
2911 write (context->wake_up_pipe[1], "A", 1);
2913 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
2920 * g_main_context_wakeup:
2921 * @context: a #GMainContext
2923 * If @context is currently waiting in a <function>poll()</function>, interrupt
2924 * the <function>poll()</function>, and continue the iteration process.
2927 g_main_context_wakeup (GMainContext *context)
2930 context = g_main_context_default ();
2932 g_return_if_fail (context->ref_count > 0);
2934 LOCK_CONTEXT (context);
2935 g_main_context_wakeup_unlocked (context);
2936 UNLOCK_CONTEXT (context);
2942 g_timeout_set_expiration (GTimeoutSource *timeout_source,
2943 GTimeVal *current_time)
2945 guint seconds = timeout_source->interval / 1000;
2946 guint msecs = timeout_source->interval - seconds * 1000;
2948 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
2949 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
2950 if (timeout_source->expiration.tv_usec >= 1000000)
2952 timeout_source->expiration.tv_usec -= 1000000;
2953 timeout_source->expiration.tv_sec++;
2958 g_timeout_prepare (GSource *source,
2963 GTimeVal current_time;
2965 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2967 g_source_get_current_time (source, ¤t_time);
2969 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
2970 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
2972 /* We do the following in a rather convoluted fashion to deal with
2973 * the fact that we don't have an integral type big enough to hold
2974 * the difference of two timevals in millseconds.
2976 if (sec < 0 || (sec == 0 && msec < 0))
2980 glong interval_sec = timeout_source->interval / 1000;
2981 glong interval_msec = timeout_source->interval % 1000;
2989 if (sec > interval_sec ||
2990 (sec == interval_sec && msec > interval_msec))
2992 /* The system time has been set backwards, so we
2993 * reset the expiration time to now + timeout_source->interval;
2994 * this at least avoids hanging for long periods of time.
2996 g_timeout_set_expiration (timeout_source, ¤t_time);
2997 msec = MIN (G_MAXINT, timeout_source->interval);
3001 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3005 *timeout = (gint)msec;
3011 g_timeout_check (GSource *source)
3013 GTimeVal current_time;
3014 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3016 g_source_get_current_time (source, ¤t_time);
3018 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3019 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3020 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3024 g_timeout_dispatch (GSource *source,
3025 GSourceFunc callback,
3028 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3032 g_warning ("Timeout source dispatched without callback\n"
3033 "You must call g_source_set_callback().");
3037 if (callback (user_data))
3039 GTimeVal current_time;
3041 g_source_get_current_time (source, ¤t_time);
3042 g_timeout_set_expiration (timeout_source, ¤t_time);
3051 * g_timeout_source_new:
3052 * @interval: the timeout interval in milliseconds.
3054 * Creates a new timeout source.
3056 * The source will not initially be associated with any #GMainContext
3057 * and must be added to one with g_source_attach() before it will be
3060 * Return value: the newly-created timeout source
3063 g_timeout_source_new (guint interval)
3065 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3066 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3067 GTimeVal current_time;
3069 timeout_source->interval = interval;
3071 g_get_current_time (¤t_time);
3072 g_timeout_set_expiration (timeout_source, ¤t_time);
3078 * g_timeout_add_full:
3079 * @priority: the priority of the idle source. Typically this will be in the
3080 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3081 * @interval: the time between calls to the function, in milliseconds
3082 * (1/1000ths of a second)
3083 * @function: function to call
3084 * @data: data to pass to @function
3085 * @notify: function to call when the idle is removed, or %NULL
3087 * Sets a function to be called at regular intervals, with the given
3088 * priority. The function is called repeatedly until it returns
3089 * %FALSE, at which point the timeout is automatically destroyed and
3090 * the function will not be called again. The @notify function is
3091 * called when the timeout is destroyed. The first call to the
3092 * function will be at the end of the first @interval.
3094 * Note that timeout functions may be delayed, due to the processing of other
3095 * event sources. Thus they should not be relied on for precise timing.
3096 * After each call to the timeout function, the time of the next
3097 * timeout is recalculated based on the current time and the given interval
3098 * (it does not try to 'catch up' time lost in delays).
3100 * Return value: the id of event source.
3103 g_timeout_add_full (gint priority,
3105 GSourceFunc function,
3107 GDestroyNotify notify)
3112 g_return_val_if_fail (function != NULL, 0);
3114 source = g_timeout_source_new (interval);
3116 if (priority != G_PRIORITY_DEFAULT)
3117 g_source_set_priority (source, priority);
3119 g_source_set_callback (source, function, data, notify);
3120 id = g_source_attach (source, NULL);
3121 g_source_unref (source);
3128 * @interval: the time between calls to the function, in milliseconds
3129 * (1/1000ths of a second)
3130 * @function: function to call
3131 * @data: data to pass to @function
3133 * Sets a function to be called at regular intervals, with the default
3134 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3135 * until it returns %FALSE, at which point the timeout is automatically
3136 * destroyed and the function will not be called again. The first call
3137 * to the function will be at the end of the first @interval.
3139 * Note that timeout functions may be delayed, due to the processing of other
3140 * event sources. Thus they should not be relied on for precise timing.
3141 * After each call to the timeout function, the time of the next
3142 * timeout is recalculated based on the current time and the given interval
3143 * (it does not try to 'catch up' time lost in delays).
3145 * Return value: the id of event source.
3148 g_timeout_add (guint32 interval,
3149 GSourceFunc function,
3152 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3153 interval, function, data, NULL);
3156 /* Idle functions */
3159 g_idle_prepare (GSource *source,
3168 g_idle_check (GSource *source)
3174 g_idle_dispatch (GSource *source,
3175 GSourceFunc callback,
3180 g_warning ("Idle source dispatched without callback\n"
3181 "You must call g_source_set_callback().");
3185 return callback (user_data);
3189 * g_idle_source_new:
3191 * Creates a new idle source.
3193 * The source will not initially be associated with any #GMainContext
3194 * and must be added to one with g_source_attach() before it will be
3197 * Return value: the newly-created idle source
3200 g_idle_source_new (void)
3202 return g_source_new (&g_idle_funcs, sizeof (GSource));
3207 * @priority: the priority of the idle source. Typically this will be in the
3208 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3209 * @function: function to call
3210 * @data: data to pass to @function
3211 * @notify: function to call when the idle is removed, or %NULL
3213 * Adds a function to be called whenever there are no higher priority
3214 * events pending. If the function returns %FALSE it is automatically
3215 * removed from the list of event sources and will not be called again.
3217 * Return value: the id of the event source.
3220 g_idle_add_full (gint priority,
3221 GSourceFunc function,
3223 GDestroyNotify notify)
3228 g_return_val_if_fail (function != NULL, 0);
3230 source = g_idle_source_new ();
3232 if (priority != G_PRIORITY_DEFAULT)
3233 g_source_set_priority (source, priority);
3235 g_source_set_callback (source, function, data, notify);
3236 id = g_source_attach (source, NULL);
3237 g_source_unref (source);
3244 * @function: function to call
3245 * @data: data to pass to @function.
3247 * Adds a function to be called whenever there are no higher priority
3248 * events pending to the default main loop. The function is given the
3249 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
3250 * returns %FALSE it is automatically removed from the list of event
3251 * sources and will not be called again.
3253 * Return value: the id of the event source.
3256 g_idle_add (GSourceFunc function,
3259 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
3263 * g_idle_remove_by_data:
3264 * @data: the data for the idle source's callback.
3266 * Removes the idle function with the given data.
3268 * Return value: %TRUE if an idle source was found and removed.
3271 g_idle_remove_by_data (gpointer data)
3273 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);