1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 /* uncomment the next line to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
40 #include "gthreadprivate.h"
42 #include <sys/types.h>
45 #ifdef HAVE_SYS_TIME_H
47 #endif /* HAVE_SYS_TIME_H */
48 #ifdef GLIB_HAVE_SYS_POLL_H
49 # include <sys/poll.h>
50 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
51 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
53 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
54 * so we prefer our own poll emulation.
56 #if defined(_POLL_EMUL_H_) || defined(BROKEN_POLL)
60 #endif /* GLIB_HAVE_SYS_POLL_H */
63 #endif /* HAVE_UNISTD_H */
69 #endif /* G_OS_WIN32 */
72 #include <sys/socket.h>
74 #endif /* G_OS_BEOS */
85 typedef struct _GTimeoutSource GTimeoutSource;
86 typedef struct _GChildWatchSource GChildWatchSource;
87 typedef struct _GPollRec GPollRec;
88 typedef struct _GSourceCallback GSourceCallback;
92 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
93 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
96 #ifdef G_THREADS_ENABLED
97 typedef struct _GMainWaiter GMainWaiter;
106 typedef struct _GMainDispatch GMainDispatch;
108 struct _GMainDispatch
111 GSList *source; /* stack of current sources */
116 #ifdef G_THREADS_ENABLED
117 /* The following lock is used for both the list of sources
118 * and the list of poll records
129 GPtrArray *pending_dispatches;
130 gint timeout; /* Timeout for current iteration */
133 GSource *source_list;
134 gint in_check_or_prepare;
136 GPollRec *poll_records;
137 guint n_poll_records;
138 GPollFD *cached_poll_array;
139 guint cached_poll_array_size;
141 #ifdef G_THREADS_ENABLED
143 /* this pipe is used to wake up the main loop when a source is added.
145 gint wake_up_pipe[2];
146 #else /* G_OS_WIN32 */
147 HANDLE wake_up_semaphore;
148 #endif /* G_OS_WIN32 */
151 gboolean poll_waiting;
153 /* Flag indicating whether the set of fd's changed during a poll */
154 gboolean poll_changed;
155 #endif /* G_THREADS_ENABLED */
159 GTimeVal current_time;
160 gboolean time_is_current;
163 struct _GSourceCallback
168 GDestroyNotify notify;
173 GMainContext *context;
178 struct _GTimeoutSource
186 struct _GChildWatchSource
193 #else /* G_OS_WIN32 */
195 gboolean child_exited;
196 #endif /* G_OS_WIN32 */
206 #ifdef G_THREADS_ENABLED
207 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
208 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
209 #define G_THREAD_SELF g_thread_self ()
211 #define LOCK_CONTEXT(context) (void)0
212 #define UNLOCK_CONTEXT(context) (void)0
213 #define G_THREAD_SELF NULL
216 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
217 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
218 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
220 #define SOURCE_UNREF(source, context) \
222 if ((source)->ref_count > 1) \
223 (source)->ref_count--; \
225 g_source_unref_internal ((source), (context), TRUE); \
229 /* Forward declarations */
231 static void g_source_unref_internal (GSource *source,
232 GMainContext *context,
234 static void g_source_destroy_internal (GSource *source,
235 GMainContext *context,
237 static void g_main_context_poll (GMainContext *context,
242 static void g_main_context_add_poll_unlocked (GMainContext *context,
245 static void g_main_context_remove_poll_unlocked (GMainContext *context,
247 static void g_main_context_wakeup_unlocked (GMainContext *context);
249 static gboolean g_timeout_prepare (GSource *source,
251 static gboolean g_timeout_check (GSource *source);
252 static gboolean g_timeout_dispatch (GSource *source,
253 GSourceFunc callback,
255 static gboolean g_child_watch_prepare (GSource *source,
257 static gboolean g_child_watch_check (GSource *source);
258 static gboolean g_child_watch_dispatch (GSource *source,
259 GSourceFunc callback,
261 static gboolean g_idle_prepare (GSource *source,
263 static gboolean g_idle_check (GSource *source);
264 static gboolean g_idle_dispatch (GSource *source,
265 GSourceFunc callback,
268 G_LOCK_DEFINE_STATIC (main_loop);
269 static GMainContext *default_main_context;
270 static GSList *main_contexts_without_pipe = NULL;
273 /* Child status monitoring code */
275 CHILD_WATCH_UNINITIALIZED,
276 CHILD_WATCH_INITIALIZED_SINGLE,
277 CHILD_WATCH_INITIALIZED_THREADED
279 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
280 static gint child_watch_count = 1;
281 static gint child_watch_wake_up_pipe[2] = {0, 0};
282 #endif /* !G_OS_WIN32 */
283 G_LOCK_DEFINE_STATIC (main_context_list);
284 static GSList *main_context_list = NULL;
286 static gint timer_perturb = -1;
288 GSourceFuncs g_timeout_funcs =
296 GSourceFuncs g_child_watch_funcs =
298 g_child_watch_prepare,
300 g_child_watch_dispatch,
304 GSourceFuncs g_idle_funcs =
313 /* SunOS has poll, but doesn't provide a prototype. */
314 # if defined (sun) && !defined (__SVR4)
315 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
317 #else /* !HAVE_POLL */
322 g_poll (GPollFD *fds,
326 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
327 gboolean poll_msgs = FALSE;
334 for (f = fds; f < &fds[nfds]; ++f)
337 if (f->fd == G_WIN32_MSG_HANDLE)
339 else if (nhandles == MAXIMUM_WAIT_OBJECTS)
341 g_warning (G_STRLOC ": Too many handles to wait for!\n");
346 #ifdef G_MAIN_POLL_DEBUG
347 g_print ("g_poll: waiting for %#x\n", f->fd);
349 handles[nhandles++] = (HANDLE) f->fd;
358 /* Waiting for messages, and maybe events
359 * -> First PeekMessage
361 #ifdef G_MAIN_POLL_DEBUG
362 g_print ("PeekMessage\n");
364 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
365 ready = WAIT_OBJECT_0 + nhandles;
370 /* Waiting just for messages */
371 if (timeout == INFINITE)
376 #ifdef G_MAIN_POLL_DEBUG
377 g_print ("WaitMessage\n");
381 gchar *emsg = g_win32_error_message (GetLastError ());
382 g_warning (G_STRLOC ": WaitMessage() failed: %s", emsg);
385 ready = WAIT_OBJECT_0 + nhandles;
387 else if (timeout == 0)
389 /* Waiting just for messages, zero timeout.
390 * If we got here, there was no message
392 ready = WAIT_TIMEOUT;
396 /* Waiting just for messages, some timeout
397 * -> Set a timer, wait for message,
398 * kill timer, use PeekMessage
400 timer = SetTimer (NULL, 0, timeout, NULL);
403 gchar *emsg = g_win32_error_message (GetLastError ());
404 g_warning (G_STRLOC ": SetTimer() failed: %s", emsg);
406 ready = WAIT_TIMEOUT;
410 #ifdef G_MAIN_POLL_DEBUG
411 g_print ("WaitMessage\n");
414 KillTimer (NULL, timer);
415 #ifdef G_MAIN_POLL_DEBUG
416 g_print ("PeekMessage\n");
418 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
419 && msg.message != WM_TIMER)
420 ready = WAIT_OBJECT_0;
422 ready = WAIT_TIMEOUT;
428 /* Wait for either message or event
429 * -> Use MsgWaitForMultipleObjects
431 #ifdef G_MAIN_POLL_DEBUG
432 g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
434 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
435 timeout, QS_ALLINPUT);
437 if (ready == WAIT_FAILED)
439 gchar *emsg = g_win32_error_message (GetLastError ());
440 g_warning (G_STRLOC ": MsgWaitForMultipleObjects() failed: %s", emsg);
446 else if (nhandles == 0)
448 /* Wait for nothing (huh?) */
453 /* Wait for just events
454 * -> Use WaitForMultipleObjects
456 #ifdef G_MAIN_POLL_DEBUG
457 g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
459 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
460 if (ready == WAIT_FAILED)
462 gchar *emsg = g_win32_error_message (GetLastError ());
463 g_warning (G_STRLOC ": WaitForMultipleObjects() failed: %s", emsg);
468 #ifdef G_MAIN_POLL_DEBUG
469 g_print ("wait returns %ld%s\n",
471 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
472 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
473 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
475 for (f = fds; f < &fds[nfds]; ++f)
478 if (ready == WAIT_FAILED)
480 else if (ready == WAIT_TIMEOUT)
482 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
484 for (f = fds; f < &fds[nfds]; ++f)
487 if (f->events & G_IO_IN)
488 if (f->fd == G_WIN32_MSG_HANDLE)
489 f->revents |= G_IO_IN;
492 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
493 for (f = fds; f < &fds[nfds]; ++f)
495 if (f->fd == (gint) handles[ready - WAIT_OBJECT_0])
497 f->revents = f->events;
498 #ifdef G_MAIN_POLL_DEBUG
499 g_print ("g_poll: got event %#x\n", f->fd);
507 #else /* !G_OS_WIN32 */
509 /* The following implementation of poll() comes from the GNU C Library.
510 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
513 #include <string.h> /* for bzero on BSD systems */
515 #ifdef HAVE_SYS_SELECT_H
516 #include <sys/select.h>
517 #endif /* HAVE_SYS_SELECT_H */
521 #endif /* G_OS_BEOS */
524 # define SELECT_MASK fd_set
525 #else /* !NO_FD_SET */
527 typedef long fd_mask;
530 # define SELECT_MASK void
532 # define SELECT_MASK int
533 # endif /* !_IBMR2 */
534 #endif /* !NO_FD_SET */
537 g_poll (GPollFD *fds,
542 SELECT_MASK rset, wset, xset;
551 for (f = fds; f < &fds[nfds]; ++f)
554 if (f->events & G_IO_IN)
555 FD_SET (f->fd, &rset);
556 if (f->events & G_IO_OUT)
557 FD_SET (f->fd, &wset);
558 if (f->events & G_IO_PRI)
559 FD_SET (f->fd, &xset);
560 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
564 tv.tv_sec = timeout / 1000;
565 tv.tv_usec = (timeout % 1000) * 1000;
567 ready = select (maxfd + 1, &rset, &wset, &xset,
568 timeout == -1 ? NULL : &tv);
570 for (f = fds; f < &fds[nfds]; ++f)
575 if (FD_ISSET (f->fd, &rset))
576 f->revents |= G_IO_IN;
577 if (FD_ISSET (f->fd, &wset))
578 f->revents |= G_IO_OUT;
579 if (FD_ISSET (f->fd, &xset))
580 f->revents |= G_IO_PRI;
587 #endif /* !G_OS_WIN32 */
589 #endif /* !HAVE_POLL */
592 * g_main_context_ref:
593 * @context: a #GMainContext
595 * Increases the reference count on a #GMainContext object by one.
597 * Returns: the @context that was passed in (since 2.6)
600 g_main_context_ref (GMainContext *context)
602 g_return_val_if_fail (context != NULL, NULL);
603 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
605 g_atomic_int_inc (&context->ref_count);
611 poll_rec_list_free (GMainContext *context,
614 g_slice_free_chain (GPollRec, list, next);
618 * g_main_context_unref:
619 * @context: a #GMainContext
621 * Decreases the reference count on a #GMainContext object by one. If
622 * the result is zero, free the context and free all associated memory.
625 g_main_context_unref (GMainContext *context)
628 g_return_if_fail (context != NULL);
629 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
631 if (!g_atomic_int_dec_and_test (&context->ref_count))
634 G_LOCK (main_context_list);
635 main_context_list = g_slist_remove (main_context_list, context);
636 G_UNLOCK (main_context_list);
638 source = context->source_list;
641 GSource *next = source->next;
642 g_source_destroy_internal (source, context, FALSE);
646 #ifdef G_THREADS_ENABLED
647 g_static_mutex_free (&context->mutex);
650 g_ptr_array_free (context->pending_dispatches, TRUE);
651 g_free (context->cached_poll_array);
653 poll_rec_list_free (context, context->poll_records);
655 #ifdef G_THREADS_ENABLED
656 if (g_thread_supported())
659 close (context->wake_up_pipe[0]);
660 close (context->wake_up_pipe[1]);
662 CloseHandle (context->wake_up_semaphore);
666 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
669 if (context->cond != NULL)
670 g_cond_free (context->cond);
676 #ifdef G_THREADS_ENABLED
678 g_main_context_init_pipe (GMainContext *context)
681 if (context->wake_up_pipe[0] != -1)
683 if (pipe (context->wake_up_pipe) < 0)
684 g_error ("Cannot create pipe main loop wake-up: %s\n",
687 context->wake_up_rec.fd = context->wake_up_pipe[0];
688 context->wake_up_rec.events = G_IO_IN;
690 if (context->wake_up_semaphore != NULL)
692 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
693 if (context->wake_up_semaphore == NULL)
694 g_error ("Cannot create wake-up semaphore: %s",
695 g_win32_error_message (GetLastError ()));
696 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
697 context->wake_up_rec.events = G_IO_IN;
698 # ifdef G_MAIN_POLL_DEBUG
699 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
702 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
706 _g_main_thread_init (void)
708 GSList *curr = main_contexts_without_pipe;
711 g_main_context_init_pipe ((GMainContext *)curr->data);
714 g_slist_free (main_contexts_without_pipe);
715 main_contexts_without_pipe = NULL;
717 #endif /* G_THREADS_ENABLED */
720 * g_main_context_new:
722 * Creates a new #GMainContext structure.
724 * Return value: the new #GMainContext
727 g_main_context_new (void)
729 GMainContext *context = g_new0 (GMainContext, 1);
731 #ifdef G_THREADS_ENABLED
732 g_static_mutex_init (&context->mutex);
734 context->owner = NULL;
735 context->waiters = NULL;
738 context->wake_up_pipe[0] = -1;
739 context->wake_up_pipe[1] = -1;
741 context->wake_up_semaphore = NULL;
745 context->ref_count = 1;
747 context->next_id = 1;
749 context->source_list = NULL;
752 context->poll_func = (GPollFunc)poll;
754 context->poll_func = g_poll;
757 context->cached_poll_array = NULL;
758 context->cached_poll_array_size = 0;
760 context->pending_dispatches = g_ptr_array_new ();
762 context->time_is_current = FALSE;
764 #ifdef G_THREADS_ENABLED
765 if (g_thread_supported ())
766 g_main_context_init_pipe (context);
768 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
772 G_LOCK (main_context_list);
773 main_context_list = g_slist_append (main_context_list, context);
774 G_UNLOCK (main_context_list);
780 * g_main_context_default:
782 * Returns the default main context. This is the main context used
783 * for main loop functions when a main loop is not explicitly
786 * Return value: the default main context.
789 g_main_context_default (void)
795 if (!default_main_context)
796 default_main_context = g_main_context_new ();
798 G_UNLOCK (main_loop);
800 return default_main_context;
803 /* Hooks for adding to the main loop */
807 * @source_funcs: structure containing functions that implement
808 * the sources behavior.
809 * @struct_size: size of the #GSource structure to create.
811 * Creates a new #GSource structure. The size is specified to
812 * allow creating structures derived from #GSource that contain
813 * additional data. The size passed in must be at least
814 * <literal>sizeof (GSource)</literal>.
816 * The source will not initially be associated with any #GMainContext
817 * and must be added to one with g_source_attach() before it will be
820 * Return value: the newly-created #GSource.
823 g_source_new (GSourceFuncs *source_funcs,
828 g_return_val_if_fail (source_funcs != NULL, NULL);
829 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
831 source = (GSource*) g_malloc0 (struct_size);
833 source->source_funcs = source_funcs;
834 source->ref_count = 1;
836 source->priority = G_PRIORITY_DEFAULT;
838 source->flags = G_HOOK_FLAG_ACTIVE;
840 /* NULL/0 initialization for all other fields */
845 /* Holds context's lock
848 g_source_list_add (GSource *source,
849 GMainContext *context)
851 GSource *tmp_source, *last_source;
854 tmp_source = context->source_list;
855 while (tmp_source && tmp_source->priority <= source->priority)
857 last_source = tmp_source;
858 tmp_source = tmp_source->next;
861 source->next = tmp_source;
863 tmp_source->prev = source;
865 source->prev = last_source;
867 last_source->next = source;
869 context->source_list = source;
872 /* Holds context's lock
875 g_source_list_remove (GSource *source,
876 GMainContext *context)
879 source->prev->next = source->next;
881 context->source_list = source->next;
884 source->next->prev = source->prev;
892 * @source: a #GSource
893 * @context: a #GMainContext (if %NULL, the default context will be used)
895 * Adds a #GSource to a @context so that it will be executed within
898 * Return value: the ID (greater than 0) for the source within the
902 g_source_attach (GSource *source,
903 GMainContext *context)
908 g_return_val_if_fail (source->context == NULL, 0);
909 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
912 context = g_main_context_default ();
914 LOCK_CONTEXT (context);
916 source->context = context;
917 result = source->source_id = context->next_id++;
920 g_source_list_add (source, context);
922 tmp_list = source->poll_fds;
925 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
926 tmp_list = tmp_list->next;
929 #ifdef G_THREADS_ENABLED
930 /* Now wake up the main loop if it is waiting in the poll() */
931 g_main_context_wakeup_unlocked (context);
934 UNLOCK_CONTEXT (context);
940 g_source_destroy_internal (GSource *source,
941 GMainContext *context,
945 LOCK_CONTEXT (context);
947 if (!SOURCE_DESTROYED (source))
950 gpointer old_cb_data;
951 GSourceCallbackFuncs *old_cb_funcs;
953 source->flags &= ~G_HOOK_FLAG_ACTIVE;
955 old_cb_data = source->callback_data;
956 old_cb_funcs = source->callback_funcs;
958 source->callback_data = NULL;
959 source->callback_funcs = NULL;
963 UNLOCK_CONTEXT (context);
964 old_cb_funcs->unref (old_cb_data);
965 LOCK_CONTEXT (context);
968 if (!SOURCE_BLOCKED (source))
970 tmp_list = source->poll_fds;
973 g_main_context_remove_poll_unlocked (context, tmp_list->data);
974 tmp_list = tmp_list->next;
978 g_source_unref_internal (source, context, TRUE);
982 UNLOCK_CONTEXT (context);
987 * @source: a #GSource
989 * Removes a source from its #GMainContext, if any, and mark it as
990 * destroyed. The source cannot be subsequently added to another
994 g_source_destroy (GSource *source)
996 GMainContext *context;
998 g_return_if_fail (source != NULL);
1000 context = source->context;
1003 g_source_destroy_internal (source, context, FALSE);
1005 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1010 * @source: a #GSource
1012 * Returns the numeric ID for a particular source. The ID of a source
1013 * is a positive integer which is unique within a particular main loop
1014 * context. The reverse
1015 * mapping from ID to source is done by g_main_context_find_source_by_id().
1017 * Return value: the ID (greater than 0) for the source
1020 g_source_get_id (GSource *source)
1024 g_return_val_if_fail (source != NULL, 0);
1025 g_return_val_if_fail (source->context != NULL, 0);
1027 LOCK_CONTEXT (source->context);
1028 result = source->source_id;
1029 UNLOCK_CONTEXT (source->context);
1035 * g_source_get_context:
1036 * @source: a #GSource
1038 * Gets the #GMainContext with which the source is associated.
1039 * Calling this function on a destroyed source is an error.
1041 * Return value: the #GMainContext with which the source is associated,
1042 * or %NULL if the context has not yet been added
1046 g_source_get_context (GSource *source)
1048 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
1050 return source->context;
1054 * g_source_add_poll:
1055 * @source:a #GSource
1056 * @fd: a #GPollFD structure holding information about a file
1057 * descriptor to watch.
1059 * Adds a file descriptor to the set of file descriptors polled for
1060 * this source. This is usually combined with g_source_new() to add an
1061 * event source. The event source's check function will typically test
1062 * the @revents field in the #GPollFD struct and return %TRUE if events need
1066 g_source_add_poll (GSource *source,
1069 GMainContext *context;
1071 g_return_if_fail (source != NULL);
1072 g_return_if_fail (fd != NULL);
1073 g_return_if_fail (!SOURCE_DESTROYED (source));
1075 context = source->context;
1078 LOCK_CONTEXT (context);
1080 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1084 if (!SOURCE_BLOCKED (source))
1085 g_main_context_add_poll_unlocked (context, source->priority, fd);
1086 UNLOCK_CONTEXT (context);
1091 * g_source_remove_poll:
1092 * @source:a #GSource
1093 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1095 * Removes a file descriptor from the set of file descriptors polled for
1099 g_source_remove_poll (GSource *source,
1102 GMainContext *context;
1104 g_return_if_fail (source != NULL);
1105 g_return_if_fail (fd != NULL);
1106 g_return_if_fail (!SOURCE_DESTROYED (source));
1108 context = source->context;
1111 LOCK_CONTEXT (context);
1113 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1117 if (!SOURCE_BLOCKED (source))
1118 g_main_context_remove_poll_unlocked (context, fd);
1119 UNLOCK_CONTEXT (context);
1124 * g_source_set_callback_indirect:
1125 * @source: the source
1126 * @callback_data: pointer to callback data "object"
1127 * @callback_funcs: functions for reference counting @callback_data
1128 * and getting the callback and data
1130 * Sets the callback function storing the data as a refcounted callback
1131 * "object". This is used internally. Note that calling
1132 * g_source_set_callback_indirect() assumes
1133 * an initial reference count on @callback_data, and thus
1134 * @callback_funcs->unref will eventually be called once more
1135 * than @callback_funcs->ref.
1138 g_source_set_callback_indirect (GSource *source,
1139 gpointer callback_data,
1140 GSourceCallbackFuncs *callback_funcs)
1142 GMainContext *context;
1143 gpointer old_cb_data;
1144 GSourceCallbackFuncs *old_cb_funcs;
1146 g_return_if_fail (source != NULL);
1147 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1149 context = source->context;
1152 LOCK_CONTEXT (context);
1154 old_cb_data = source->callback_data;
1155 old_cb_funcs = source->callback_funcs;
1157 source->callback_data = callback_data;
1158 source->callback_funcs = callback_funcs;
1161 UNLOCK_CONTEXT (context);
1164 old_cb_funcs->unref (old_cb_data);
1168 g_source_callback_ref (gpointer cb_data)
1170 GSourceCallback *callback = cb_data;
1172 callback->ref_count++;
1177 g_source_callback_unref (gpointer cb_data)
1179 GSourceCallback *callback = cb_data;
1181 callback->ref_count--;
1182 if (callback->ref_count == 0)
1184 if (callback->notify)
1185 callback->notify (callback->data);
1191 g_source_callback_get (gpointer cb_data,
1196 GSourceCallback *callback = cb_data;
1198 *func = callback->func;
1199 *data = callback->data;
1202 static GSourceCallbackFuncs g_source_callback_funcs = {
1203 g_source_callback_ref,
1204 g_source_callback_unref,
1205 g_source_callback_get,
1209 * g_source_set_callback:
1210 * @source: the source
1211 * @func: a callback function
1212 * @data: the data to pass to callback function
1213 * @notify: a function to call when @data is no longer in use, or %NULL.
1215 * Sets the callback function for a source. The callback for a source is
1216 * called from the source's dispatch function.
1218 * The exact type of @func depends on the type of source; ie. you
1219 * should not count on @func being called with @data as its first
1222 * Typically, you won't use this function. Instead use functions specific
1223 * to the type of source you are using.
1226 g_source_set_callback (GSource *source,
1229 GDestroyNotify notify)
1231 GSourceCallback *new_callback;
1233 g_return_if_fail (source != NULL);
1235 new_callback = g_new (GSourceCallback, 1);
1237 new_callback->ref_count = 1;
1238 new_callback->func = func;
1239 new_callback->data = data;
1240 new_callback->notify = notify;
1242 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1247 * g_source_set_funcs:
1248 * @source: a #GSource
1249 * @funcs: the new #GSourceFuncs
1251 * Sets the source functions (can be used to override
1252 * default implementations) of an unattached source.
1257 g_source_set_funcs (GSource *source,
1258 GSourceFuncs *funcs)
1260 g_return_if_fail (source != NULL);
1261 g_return_if_fail (source->context == NULL);
1262 g_return_if_fail (source->ref_count > 0);
1263 g_return_if_fail (funcs != NULL);
1265 source->source_funcs = funcs;
1269 * g_source_set_priority:
1270 * @source: a #GSource
1271 * @priority: the new priority.
1273 * Sets the priority of a source. While the main loop is being
1274 * run, a source will be dispatched if it is ready to be dispatched and no sources
1275 * at a higher (numerically smaller) priority are ready to be dispatched.
1278 g_source_set_priority (GSource *source,
1282 GMainContext *context;
1284 g_return_if_fail (source != NULL);
1286 context = source->context;
1289 LOCK_CONTEXT (context);
1291 source->priority = priority;
1295 /* Remove the source from the context's source and then
1296 * add it back so it is sorted in the correct plcae
1298 g_source_list_remove (source, source->context);
1299 g_source_list_add (source, source->context);
1301 if (!SOURCE_BLOCKED (source))
1303 tmp_list = source->poll_fds;
1306 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1307 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1309 tmp_list = tmp_list->next;
1313 UNLOCK_CONTEXT (source->context);
1318 * g_source_get_priority:
1319 * @source: a #GSource
1321 * Gets the priority of a source.
1323 * Return value: the priority of the source
1326 g_source_get_priority (GSource *source)
1328 g_return_val_if_fail (source != NULL, 0);
1330 return source->priority;
1334 * g_source_set_can_recurse:
1335 * @source: a #GSource
1336 * @can_recurse: whether recursion is allowed for this source
1338 * Sets whether a source can be called recursively. If @can_recurse is
1339 * %TRUE, then while the source is being dispatched then this source
1340 * will be processed normally. Otherwise, all processing of this
1341 * source is blocked until the dispatch function returns.
1344 g_source_set_can_recurse (GSource *source,
1345 gboolean can_recurse)
1347 GMainContext *context;
1349 g_return_if_fail (source != NULL);
1351 context = source->context;
1354 LOCK_CONTEXT (context);
1357 source->flags |= G_SOURCE_CAN_RECURSE;
1359 source->flags &= ~G_SOURCE_CAN_RECURSE;
1362 UNLOCK_CONTEXT (context);
1366 * g_source_get_can_recurse:
1367 * @source: a #GSource
1369 * Checks whether a source is allowed to be called recursively.
1370 * see g_source_set_can_recurse().
1372 * Return value: whether recursion is allowed.
1375 g_source_get_can_recurse (GSource *source)
1377 g_return_val_if_fail (source != NULL, FALSE);
1379 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1384 * @source: a #GSource
1386 * Increases the reference count on a source by one.
1388 * Return value: @source
1391 g_source_ref (GSource *source)
1393 GMainContext *context;
1395 g_return_val_if_fail (source != NULL, NULL);
1397 context = source->context;
1400 LOCK_CONTEXT (context);
1402 source->ref_count++;
1405 UNLOCK_CONTEXT (context);
1410 /* g_source_unref() but possible to call within context lock
1413 g_source_unref_internal (GSource *source,
1414 GMainContext *context,
1417 gpointer old_cb_data = NULL;
1418 GSourceCallbackFuncs *old_cb_funcs = NULL;
1420 g_return_if_fail (source != NULL);
1422 if (!have_lock && context)
1423 LOCK_CONTEXT (context);
1425 source->ref_count--;
1426 if (source->ref_count == 0)
1428 old_cb_data = source->callback_data;
1429 old_cb_funcs = source->callback_funcs;
1431 source->callback_data = NULL;
1432 source->callback_funcs = NULL;
1434 if (context && !SOURCE_DESTROYED (source))
1436 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1437 source->ref_count++;
1440 g_source_list_remove (source, context);
1442 if (source->source_funcs->finalize)
1443 source->source_funcs->finalize (source);
1445 g_slist_free (source->poll_fds);
1446 source->poll_fds = NULL;
1450 if (!have_lock && context)
1451 UNLOCK_CONTEXT (context);
1456 UNLOCK_CONTEXT (context);
1458 old_cb_funcs->unref (old_cb_data);
1461 LOCK_CONTEXT (context);
1467 * @source: a #GSource
1469 * Decreases the reference count of a source by one. If the
1470 * resulting reference count is zero the source and associated
1471 * memory will be destroyed.
1474 g_source_unref (GSource *source)
1476 g_return_if_fail (source != NULL);
1478 g_source_unref_internal (source, source->context, FALSE);
1482 * g_main_context_find_source_by_id:
1483 * @context: a #GMainContext (if %NULL, the default context will be used)
1484 * @source_id: the source ID, as returned by g_source_get_id().
1486 * Finds a #GSource given a pair of context and ID.
1488 * Return value: the #GSource if found, otherwise, %NULL
1491 g_main_context_find_source_by_id (GMainContext *context,
1496 g_return_val_if_fail (source_id > 0, NULL);
1498 if (context == NULL)
1499 context = g_main_context_default ();
1501 LOCK_CONTEXT (context);
1503 source = context->source_list;
1506 if (!SOURCE_DESTROYED (source) &&
1507 source->source_id == source_id)
1509 source = source->next;
1512 UNLOCK_CONTEXT (context);
1518 * g_main_context_find_source_by_funcs_user_data:
1519 * @context: a #GMainContext (if %NULL, the default context will be used).
1520 * @funcs: the @source_funcs passed to g_source_new().
1521 * @user_data: the user data from the callback.
1523 * Finds a source with the given source functions and user data. If
1524 * multiple sources exist with the same source function and user data,
1525 * the first one found will be returned.
1527 * Return value: the source, if one was found, otherwise %NULL
1530 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1531 GSourceFuncs *funcs,
1536 g_return_val_if_fail (funcs != NULL, NULL);
1538 if (context == NULL)
1539 context = g_main_context_default ();
1541 LOCK_CONTEXT (context);
1543 source = context->source_list;
1546 if (!SOURCE_DESTROYED (source) &&
1547 source->source_funcs == funcs &&
1548 source->callback_funcs)
1550 GSourceFunc callback;
1551 gpointer callback_data;
1553 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1555 if (callback_data == user_data)
1558 source = source->next;
1561 UNLOCK_CONTEXT (context);
1567 * g_main_context_find_source_by_user_data:
1568 * @context: a #GMainContext
1569 * @user_data: the user_data for the callback.
1571 * Finds a source with the given user data for the callback. If
1572 * multiple sources exist with the same user data, the first
1573 * one found will be returned.
1575 * Return value: the source, if one was found, otherwise %NULL
1578 g_main_context_find_source_by_user_data (GMainContext *context,
1583 if (context == NULL)
1584 context = g_main_context_default ();
1586 LOCK_CONTEXT (context);
1588 source = context->source_list;
1591 if (!SOURCE_DESTROYED (source) &&
1592 source->callback_funcs)
1594 GSourceFunc callback;
1595 gpointer callback_data = NULL;
1597 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1599 if (callback_data == user_data)
1602 source = source->next;
1605 UNLOCK_CONTEXT (context);
1612 * @tag: the ID of the source to remove.
1614 * Removes the source with the given id from the default main context.
1616 * a #GSource is given by g_source_get_id(), or will be returned by the
1617 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1618 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1619 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1621 * See also g_source_destroy().
1623 * Return value: %TRUE if the source was found and removed.
1626 g_source_remove (guint tag)
1630 g_return_val_if_fail (tag > 0, FALSE);
1632 source = g_main_context_find_source_by_id (NULL, tag);
1634 g_source_destroy (source);
1636 return source != NULL;
1640 * g_source_remove_by_user_data:
1641 * @user_data: the user_data for the callback.
1643 * Removes a source from the default main loop context given the user
1644 * data for the callback. If multiple sources exist with the same user
1645 * data, only one will be destroyed.
1647 * Return value: %TRUE if a source was found and removed.
1650 g_source_remove_by_user_data (gpointer user_data)
1654 source = g_main_context_find_source_by_user_data (NULL, user_data);
1657 g_source_destroy (source);
1665 * g_source_remove_by_funcs_user_data:
1666 * @funcs: The @source_funcs passed to g_source_new()
1667 * @user_data: the user data for the callback
1669 * Removes a source from the default main loop context given the
1670 * source functions and user data. If multiple sources exist with the
1671 * same source functions and user data, only one will be destroyed.
1673 * Return value: %TRUE if a source was found and removed.
1676 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1681 g_return_val_if_fail (funcs != NULL, FALSE);
1683 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1686 g_source_destroy (source);
1694 * g_get_current_time:
1695 * @result: #GTimeVal structure in which to store current time.
1697 * Equivalent to the UNIX gettimeofday() function, but portable.
1700 g_get_current_time (GTimeVal *result)
1705 g_return_if_fail (result != NULL);
1707 /*this is required on alpha, there the timeval structs are int's
1708 not longs and a cast only would fail horribly*/
1709 gettimeofday (&r, NULL);
1710 result->tv_sec = r.tv_sec;
1711 result->tv_usec = r.tv_usec;
1714 guint64 *time64 = (guint64 *) &ft;
1716 GetSystemTimeAsFileTime (&ft);
1718 /* Convert from 100s of nanoseconds since 1601-01-01
1719 * to Unix epoch. Yes, this is Y2038 unsafe.
1721 *time64 -= G_GINT64_CONSTANT (116444736000000000);
1724 result->tv_sec = *time64 / 1000000;
1725 result->tv_usec = *time64 % 1000000;
1730 g_main_dispatch_free (gpointer dispatch)
1732 g_slice_free (GMainDispatch, dispatch);
1735 /* Running the main loop */
1737 static GMainDispatch *
1740 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1741 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1744 dispatch = g_slice_new0 (GMainDispatch);
1745 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1754 * Returns the depth of the stack of calls to
1755 * g_main_context_dispatch() on any #GMainContext in the current thread.
1756 * That is, when called from the toplevel, it gives 0. When
1757 * called from within a callback from g_main_context_iteration()
1758 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1759 * a callback to a recursive call to g_main_context_iterate(),
1760 * it returns 2. And so forth.
1762 * This function is useful in a situation like the following:
1763 * Imagine an extremely simple "garbage collected" system.
1766 * static GList *free_list;
1769 * allocate_memory (gsize size)
1771 * gpointer result = g_malloc (size);
1772 * free_list = g_list_prepend (free_list, result);
1777 * free_allocated_memory (void)
1780 * for (l = free_list; l; l = l->next);
1782 * g_list_free (free_list);
1790 * g_main_context_iteration (NULL, TRUE);
1791 * free_allocated_memory();
1795 * This works from an application, however, if you want to do the same
1796 * thing from a library, it gets more difficult, since you no longer
1797 * control the main loop. You might think you can simply use an idle
1798 * function to make the call to free_allocated_memory(), but that
1799 * doesn't work, since the idle function could be called from a
1800 * recursive callback. This can be fixed by using g_main_depth()
1804 * allocate_memory (gsize size)
1806 * FreeListBlock *block = g_new (FreeListBlock, 1);
1807 * block->mem = g_malloc (size);
1808 * block->depth = g_main_depth ();
1809 * free_list = g_list_prepend (free_list, block);
1810 * return block->mem;
1814 * free_allocated_memory (void)
1818 * int depth = g_main_depth ();
1819 * for (l = free_list; l; );
1821 * GList *next = l->next;
1822 * FreeListBlock *block = l->data;
1823 * if (block->depth > depth)
1825 * g_free (block->mem);
1827 * free_list = g_list_delete_link (free_list, l);
1835 * There is a temptation to use g_main_depth() to solve
1836 * problems with reentrancy. For instance, while waiting for data
1837 * to be received from the network in response to a menu item,
1838 * the menu item might be selected again. It might seem that
1839 * one could make the menu item's callback return immediately
1840 * and do nothing if g_main_depth() returns a value greater than 1.
1841 * However, this should be avoided since the user then sees selecting
1842 * the menu item do nothing. Furthermore, you'll find yourself adding
1843 * these checks all over your code, since there are doubtless many,
1844 * many things that the user could do. Instead, you can use the
1845 * following techniques:
1850 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1851 * the user from interacting with elements while the main
1852 * loop is recursing.
1857 * Avoid main loop recursion in situations where you can't handle
1858 * arbitrary callbacks. Instead, structure your code so that you
1859 * simply return to the main loop and then get called again when
1860 * there is more work to do.
1865 * Return value: The main loop recursion level in the current thread
1870 GMainDispatch *dispatch = get_dispatch ();
1871 return dispatch->depth;
1875 * g_main_current_source:
1877 * Returns the currently firing source for this thread.
1879 * Return value: The currently firing source or %NULL.
1884 g_main_current_source (void)
1886 GMainDispatch *dispatch = get_dispatch ();
1887 return dispatch->source ? dispatch->source->data : NULL;
1891 * g_source_is_destroyed:
1892 * @source: a #GSource
1894 * Returns whether @source has been destroyed.
1896 * This is important when you operate upon your objects
1897 * from within idle handlers, but may have freed the object
1898 * before the dispatch of your idle handler.
1900 * <informalexample><programlisting>
1902 * idle_callback (gpointer data)
1904 * SomeWidget *self = data;
1906 * GDK_THREADS_ENTER (<!-- -->);
1907 * /<!-- -->* do stuff with self *<!-- -->/
1908 * self->idle_id = 0;
1909 * GDK_THREADS_LEAVE (<!-- -->);
1915 * some_widget_do_stuff_later (SomeWidget *self)
1917 * self->idle_id = g_idle_add (idle_callback, self);
1921 * some_widget_finalize (GObject *object)
1923 * SomeWidget *self = SOME_WIDGET (object);
1925 * if (self->idle_id)
1926 * g_source_remove (self->idle_id);
1928 * G_OBJECT_CLASS (parent_class)->finalize (object);
1930 * </programlisting></informalexample>
1932 * This will fail in a multi-threaded application if the
1933 * widget is destroyed before the idle handler fires due
1934 * to the use after free in the callback. A solution, to
1935 * this particular problem, is to check to if the source
1936 * has already been destroy within the callback.
1938 * <informalexample><programlisting>
1940 * idle_callback (gpointer data)
1942 * SomeWidget *self = data;
1944 * GDK_THREADS_ENTER ();
1945 * if (!g_source_is_destroyed (g_main_current_source ()))
1947 * /<!-- -->* do stuff with self *<!-- -->/
1949 * GDK_THREADS_LEAVE ();
1953 * </programlisting></informalexample>
1955 * Return value: %TRUE if the source has been destroyed
1960 g_source_is_destroyed (GSource *source)
1962 return SOURCE_DESTROYED (source);
1966 /* Temporarily remove all this source's file descriptors from the
1967 * poll(), so that if data comes available for one of the file descriptors
1968 * we don't continually spin in the poll()
1970 /* HOLDS: source->context's lock */
1972 block_source (GSource *source)
1976 g_return_if_fail (!SOURCE_BLOCKED (source));
1978 tmp_list = source->poll_fds;
1981 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1982 tmp_list = tmp_list->next;
1986 /* HOLDS: source->context's lock */
1988 unblock_source (GSource *source)
1992 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1993 g_return_if_fail (!SOURCE_DESTROYED (source));
1995 tmp_list = source->poll_fds;
1998 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1999 tmp_list = tmp_list->next;
2003 /* HOLDS: context's lock */
2005 g_main_dispatch (GMainContext *context)
2007 GMainDispatch *current = get_dispatch ();
2010 for (i = 0; i < context->pending_dispatches->len; i++)
2012 GSource *source = context->pending_dispatches->pdata[i];
2014 context->pending_dispatches->pdata[i] = NULL;
2017 source->flags &= ~G_SOURCE_READY;
2019 if (!SOURCE_DESTROYED (source))
2021 gboolean was_in_call;
2022 gpointer user_data = NULL;
2023 GSourceFunc callback = NULL;
2024 GSourceCallbackFuncs *cb_funcs;
2026 gboolean need_destroy;
2028 gboolean (*dispatch) (GSource *,
2031 GSList current_source_link;
2033 dispatch = source->source_funcs->dispatch;
2034 cb_funcs = source->callback_funcs;
2035 cb_data = source->callback_data;
2038 cb_funcs->ref (cb_data);
2040 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2041 block_source (source);
2043 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2044 source->flags |= G_HOOK_FLAG_IN_CALL;
2047 cb_funcs->get (cb_data, source, &callback, &user_data);
2049 UNLOCK_CONTEXT (context);
2052 /* The on-stack allocation of the GSList is unconventional, but
2053 * we know that the lifetime of the link is bounded to this
2054 * function as the link is kept in a thread specific list and
2055 * not manipulated outside of this function and its descendants.
2056 * Avoiding the overhead of a g_slist_alloc() is useful as many
2057 * applications do little more than dispatch events.
2059 * This is a performance hack - do not revert to g_slist_prepend()!
2061 current_source_link.data = source;
2062 current_source_link.next = current->source;
2063 current->source = ¤t_source_link;
2064 need_destroy = ! dispatch (source,
2067 g_assert (current->source == ¤t_source_link);
2068 current->source = current_source_link.next;
2072 cb_funcs->unref (cb_data);
2074 LOCK_CONTEXT (context);
2077 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2079 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2080 !SOURCE_DESTROYED (source))
2081 unblock_source (source);
2083 /* Note: this depends on the fact that we can't switch
2084 * sources from one main context to another
2086 if (need_destroy && !SOURCE_DESTROYED (source))
2088 g_assert (source->context == context);
2089 g_source_destroy_internal (source, context, TRUE);
2093 SOURCE_UNREF (source, context);
2096 g_ptr_array_set_size (context->pending_dispatches, 0);
2099 /* Holds context's lock */
2100 static inline GSource *
2101 next_valid_source (GMainContext *context,
2104 GSource *new_source = source ? source->next : context->source_list;
2108 if (!SOURCE_DESTROYED (new_source))
2110 new_source->ref_count++;
2114 new_source = new_source->next;
2118 SOURCE_UNREF (source, context);
2124 * g_main_context_acquire:
2125 * @context: a #GMainContext
2127 * Tries to become the owner of the specified context.
2128 * If some other thread is the owner of the context,
2129 * returns %FALSE immediately. Ownership is properly
2130 * recursive: the owner can require ownership again
2131 * and will release ownership when g_main_context_release()
2132 * is called as many times as g_main_context_acquire().
2134 * You must be the owner of a context before you
2135 * can call g_main_context_prepare(), g_main_context_query(),
2136 * g_main_context_check(), g_main_context_dispatch().
2138 * Return value: %TRUE if the operation succeeded, and
2139 * this thread is now the owner of @context.
2142 g_main_context_acquire (GMainContext *context)
2144 #ifdef G_THREADS_ENABLED
2145 gboolean result = FALSE;
2146 GThread *self = G_THREAD_SELF;
2148 if (context == NULL)
2149 context = g_main_context_default ();
2151 LOCK_CONTEXT (context);
2153 if (!context->owner)
2155 context->owner = self;
2156 g_assert (context->owner_count == 0);
2159 if (context->owner == self)
2161 context->owner_count++;
2165 UNLOCK_CONTEXT (context);
2168 #else /* !G_THREADS_ENABLED */
2170 #endif /* G_THREADS_ENABLED */
2174 * g_main_context_release:
2175 * @context: a #GMainContext
2177 * Releases ownership of a context previously acquired by this thread
2178 * with g_main_context_acquire(). If the context was acquired multiple
2179 * times, the ownership will be released only when g_main_context_release()
2180 * is called as many times as it was acquired.
2183 g_main_context_release (GMainContext *context)
2185 #ifdef G_THREADS_ENABLED
2186 if (context == NULL)
2187 context = g_main_context_default ();
2189 LOCK_CONTEXT (context);
2191 context->owner_count--;
2192 if (context->owner_count == 0)
2194 context->owner = NULL;
2196 if (context->waiters)
2198 GMainWaiter *waiter = context->waiters->data;
2199 gboolean loop_internal_waiter =
2200 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2201 context->waiters = g_slist_delete_link (context->waiters,
2203 if (!loop_internal_waiter)
2204 g_mutex_lock (waiter->mutex);
2206 g_cond_signal (waiter->cond);
2208 if (!loop_internal_waiter)
2209 g_mutex_unlock (waiter->mutex);
2213 UNLOCK_CONTEXT (context);
2214 #endif /* G_THREADS_ENABLED */
2218 * g_main_context_wait:
2219 * @context: a #GMainContext
2220 * @cond: a condition variable
2221 * @mutex: a mutex, currently held
2223 * Tries to become the owner of the specified context,
2224 * as with g_main_context_acquire(). But if another thread
2225 * is the owner, atomically drop @mutex and wait on @cond until
2226 * that owner releases ownership or until @cond is signaled, then
2227 * try again (once) to become the owner.
2229 * Return value: %TRUE if the operation succeeded, and
2230 * this thread is now the owner of @context.
2233 g_main_context_wait (GMainContext *context,
2237 #ifdef G_THREADS_ENABLED
2238 gboolean result = FALSE;
2239 GThread *self = G_THREAD_SELF;
2240 gboolean loop_internal_waiter;
2242 if (context == NULL)
2243 context = g_main_context_default ();
2245 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2247 if (!loop_internal_waiter)
2248 LOCK_CONTEXT (context);
2250 if (context->owner && context->owner != self)
2255 waiter.mutex = mutex;
2257 context->waiters = g_slist_append (context->waiters, &waiter);
2259 if (!loop_internal_waiter)
2260 UNLOCK_CONTEXT (context);
2261 g_cond_wait (cond, mutex);
2262 if (!loop_internal_waiter)
2263 LOCK_CONTEXT (context);
2265 context->waiters = g_slist_remove (context->waiters, &waiter);
2268 if (!context->owner)
2270 context->owner = self;
2271 g_assert (context->owner_count == 0);
2274 if (context->owner == self)
2276 context->owner_count++;
2280 if (!loop_internal_waiter)
2281 UNLOCK_CONTEXT (context);
2284 #else /* !G_THREADS_ENABLED */
2286 #endif /* G_THREADS_ENABLED */
2290 * g_main_context_prepare:
2291 * @context: a #GMainContext
2292 * @priority: location to store priority of highest priority
2293 * source already ready.
2295 * Prepares to poll sources within a main loop. The resulting information
2296 * for polling is determined by calling g_main_context_query ().
2298 * Return value: %TRUE if some source is ready to be dispatched
2302 g_main_context_prepare (GMainContext *context,
2307 gint current_priority = G_MAXINT;
2310 if (context == NULL)
2311 context = g_main_context_default ();
2313 LOCK_CONTEXT (context);
2315 context->time_is_current = FALSE;
2317 if (context->in_check_or_prepare)
2319 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2320 "prepare() member.");
2321 UNLOCK_CONTEXT (context);
2325 #ifdef G_THREADS_ENABLED
2326 if (context->poll_waiting)
2328 g_warning("g_main_context_prepare(): main loop already active in another thread");
2329 UNLOCK_CONTEXT (context);
2333 context->poll_waiting = TRUE;
2334 #endif /* G_THREADS_ENABLED */
2337 /* If recursing, finish up current dispatch, before starting over */
2338 if (context->pending_dispatches)
2341 g_main_dispatch (context, ¤t_time);
2343 UNLOCK_CONTEXT (context);
2348 /* If recursing, clear list of pending dispatches */
2350 for (i = 0; i < context->pending_dispatches->len; i++)
2352 if (context->pending_dispatches->pdata[i])
2353 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2355 g_ptr_array_set_size (context->pending_dispatches, 0);
2357 /* Prepare all sources */
2359 context->timeout = -1;
2361 source = next_valid_source (context, NULL);
2364 gint source_timeout = -1;
2366 if ((n_ready > 0) && (source->priority > current_priority))
2368 SOURCE_UNREF (source, context);
2371 if (SOURCE_BLOCKED (source))
2374 if (!(source->flags & G_SOURCE_READY))
2377 gboolean (*prepare) (GSource *source,
2380 prepare = source->source_funcs->prepare;
2381 context->in_check_or_prepare++;
2382 UNLOCK_CONTEXT (context);
2384 result = (*prepare) (source, &source_timeout);
2386 LOCK_CONTEXT (context);
2387 context->in_check_or_prepare--;
2390 source->flags |= G_SOURCE_READY;
2393 if (source->flags & G_SOURCE_READY)
2396 current_priority = source->priority;
2397 context->timeout = 0;
2400 if (source_timeout >= 0)
2402 if (context->timeout < 0)
2403 context->timeout = source_timeout;
2405 context->timeout = MIN (context->timeout, source_timeout);
2409 source = next_valid_source (context, source);
2412 UNLOCK_CONTEXT (context);
2415 *priority = current_priority;
2417 return (n_ready > 0);
2421 * g_main_context_query:
2422 * @context: a #GMainContext
2423 * @max_priority: maximum priority source to check
2424 * @timeout_: location to store timeout to be used in polling
2425 * @fds: location to store #GPollFD records that need to be polled.
2426 * @n_fds: length of @fds.
2428 * Determines information necessary to poll this main loop.
2430 * Return value: the number of records actually stored in @fds,
2431 * or, if more than @n_fds records need to be stored, the number
2432 * of records that need to be stored.
2435 g_main_context_query (GMainContext *context,
2444 LOCK_CONTEXT (context);
2446 pollrec = context->poll_records;
2448 while (pollrec && max_priority >= pollrec->priority)
2450 if (pollrec->fd->events)
2454 fds[n_poll].fd = pollrec->fd->fd;
2455 /* In direct contradiction to the Unix98 spec, IRIX runs into
2456 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2457 * flags in the events field of the pollfd while it should
2458 * just ignoring them. So we mask them out here.
2460 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2461 fds[n_poll].revents = 0;
2466 pollrec = pollrec->next;
2469 #ifdef G_THREADS_ENABLED
2470 context->poll_changed = FALSE;
2475 *timeout = context->timeout;
2477 context->time_is_current = FALSE;
2480 UNLOCK_CONTEXT (context);
2486 * g_main_context_check:
2487 * @context: a #GMainContext
2488 * @max_priority: the maximum numerical priority of sources to check
2489 * @fds: array of #GPollFD's that was passed to the last call to
2490 * g_main_context_query()
2491 * @n_fds: return value of g_main_context_query()
2493 * Passes the results of polling back to the main loop.
2495 * Return value: %TRUE if some sources are ready to be dispatched.
2498 g_main_context_check (GMainContext *context,
2508 LOCK_CONTEXT (context);
2510 if (context->in_check_or_prepare)
2512 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2513 "prepare() member.");
2514 UNLOCK_CONTEXT (context);
2518 #ifdef G_THREADS_ENABLED
2519 if (!context->poll_waiting)
2523 read (context->wake_up_pipe[0], &a, 1);
2527 context->poll_waiting = FALSE;
2529 /* If the set of poll file descriptors changed, bail out
2530 * and let the main loop rerun
2532 if (context->poll_changed)
2534 UNLOCK_CONTEXT (context);
2537 #endif /* G_THREADS_ENABLED */
2539 pollrec = context->poll_records;
2543 if (pollrec->fd->events)
2545 pollrec->fd->revents = fds[i].revents;
2548 pollrec = pollrec->next;
2551 source = next_valid_source (context, NULL);
2554 if ((n_ready > 0) && (source->priority > max_priority))
2556 SOURCE_UNREF (source, context);
2559 if (SOURCE_BLOCKED (source))
2562 if (!(source->flags & G_SOURCE_READY))
2565 gboolean (*check) (GSource *source);
2567 check = source->source_funcs->check;
2569 context->in_check_or_prepare++;
2570 UNLOCK_CONTEXT (context);
2572 result = (*check) (source);
2574 LOCK_CONTEXT (context);
2575 context->in_check_or_prepare--;
2578 source->flags |= G_SOURCE_READY;
2581 if (source->flags & G_SOURCE_READY)
2583 source->ref_count++;
2584 g_ptr_array_add (context->pending_dispatches, source);
2588 /* never dispatch sources with less priority than the first
2589 * one we choose to dispatch
2591 max_priority = source->priority;
2595 source = next_valid_source (context, source);
2598 UNLOCK_CONTEXT (context);
2604 * g_main_context_dispatch:
2605 * @context: a #GMainContext
2607 * Dispatches all pending sources.
2610 g_main_context_dispatch (GMainContext *context)
2612 LOCK_CONTEXT (context);
2614 if (context->pending_dispatches->len > 0)
2616 g_main_dispatch (context);
2619 UNLOCK_CONTEXT (context);
2622 /* HOLDS context lock */
2624 g_main_context_iterate (GMainContext *context,
2631 gboolean some_ready;
2632 gint nfds, allocated_nfds;
2633 GPollFD *fds = NULL;
2635 UNLOCK_CONTEXT (context);
2637 #ifdef G_THREADS_ENABLED
2638 if (!g_main_context_acquire (context))
2640 gboolean got_ownership;
2642 g_return_val_if_fail (g_thread_supported (), FALSE);
2647 LOCK_CONTEXT (context);
2650 context->cond = g_cond_new ();
2652 got_ownership = g_main_context_wait (context,
2654 g_static_mutex_get_mutex (&context->mutex));
2658 UNLOCK_CONTEXT (context);
2663 LOCK_CONTEXT (context);
2664 #endif /* G_THREADS_ENABLED */
2666 if (!context->cached_poll_array)
2668 context->cached_poll_array_size = context->n_poll_records;
2669 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2672 allocated_nfds = context->cached_poll_array_size;
2673 fds = context->cached_poll_array;
2675 UNLOCK_CONTEXT (context);
2677 g_main_context_prepare (context, &max_priority);
2679 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2680 allocated_nfds)) > allocated_nfds)
2682 LOCK_CONTEXT (context);
2684 context->cached_poll_array_size = allocated_nfds = nfds;
2685 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2686 UNLOCK_CONTEXT (context);
2692 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2694 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2697 g_main_context_dispatch (context);
2699 #ifdef G_THREADS_ENABLED
2700 g_main_context_release (context);
2701 #endif /* G_THREADS_ENABLED */
2703 LOCK_CONTEXT (context);
2709 * g_main_context_pending:
2710 * @context: a #GMainContext (if %NULL, the default context will be used)
2712 * Checks if any sources have pending events for the given context.
2714 * Return value: %TRUE if events are pending.
2717 g_main_context_pending (GMainContext *context)
2722 context = g_main_context_default();
2724 LOCK_CONTEXT (context);
2725 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2726 UNLOCK_CONTEXT (context);
2732 * g_main_context_iteration:
2733 * @context: a #GMainContext (if %NULL, the default context will be used)
2734 * @may_block: whether the call may block.
2736 * Runs a single iteration for the given main loop. This involves
2737 * checking to see if any event sources are ready to be processed,
2738 * then if no events sources are ready and @may_block is %TRUE, waiting
2739 * for a source to become ready, then dispatching the highest priority
2740 * events sources that are ready. Otherwise, if @may_block is %FALSE
2741 * sources are not waited to become ready, only those highest priority
2742 * events sources will be dispatched (if any), that are ready at this
2743 * given moment without further waiting.
2745 * Note that even when @may_block is %TRUE, it is still possible for
2746 * g_main_context_iteration() to return %FALSE, since the the wait may
2747 * be interrupted for other reasons than an event source becoming ready.
2749 * Return value: %TRUE if events were dispatched.
2752 g_main_context_iteration (GMainContext *context, gboolean may_block)
2757 context = g_main_context_default();
2759 LOCK_CONTEXT (context);
2760 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2761 UNLOCK_CONTEXT (context);
2768 * @context: a #GMainContext (if %NULL, the default context will be used).
2769 * @is_running: set to %TRUE to indicate that the loop is running. This
2770 * is not very important since calling g_main_loop_run() will set this to
2773 * Creates a new #GMainLoop structure.
2775 * Return value: a new #GMainLoop.
2778 g_main_loop_new (GMainContext *context,
2779 gboolean is_running)
2784 context = g_main_context_default();
2786 g_main_context_ref (context);
2788 loop = g_new0 (GMainLoop, 1);
2789 loop->context = context;
2790 loop->is_running = is_running != FALSE;
2791 loop->ref_count = 1;
2798 * @loop: a #GMainLoop
2800 * Increases the reference count on a #GMainLoop object by one.
2802 * Return value: @loop
2805 g_main_loop_ref (GMainLoop *loop)
2807 g_return_val_if_fail (loop != NULL, NULL);
2808 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2810 g_atomic_int_inc (&loop->ref_count);
2816 * g_main_loop_unref:
2817 * @loop: a #GMainLoop
2819 * Decreases the reference count on a #GMainLoop object by one. If
2820 * the result is zero, free the loop and free all associated memory.
2823 g_main_loop_unref (GMainLoop *loop)
2825 g_return_if_fail (loop != NULL);
2826 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2828 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2831 g_main_context_unref (loop->context);
2837 * @loop: a #GMainLoop
2839 * Runs a main loop until g_main_loop_quit() is called on the loop.
2840 * If this is called for the thread of the loop's #GMainContext,
2841 * it will process events from the loop, otherwise it will
2845 g_main_loop_run (GMainLoop *loop)
2847 GThread *self = G_THREAD_SELF;
2849 g_return_if_fail (loop != NULL);
2850 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2852 #ifdef G_THREADS_ENABLED
2853 if (!g_main_context_acquire (loop->context))
2855 gboolean got_ownership = FALSE;
2857 /* Another thread owns this context */
2858 if (!g_thread_supported ())
2860 g_warning ("g_main_loop_run() was called from second thread but "
2861 "g_thread_init() was never called.");
2865 LOCK_CONTEXT (loop->context);
2867 g_atomic_int_inc (&loop->ref_count);
2869 if (!loop->is_running)
2870 loop->is_running = TRUE;
2872 if (!loop->context->cond)
2873 loop->context->cond = g_cond_new ();
2875 while (loop->is_running && !got_ownership)
2876 got_ownership = g_main_context_wait (loop->context,
2877 loop->context->cond,
2878 g_static_mutex_get_mutex (&loop->context->mutex));
2880 if (!loop->is_running)
2882 UNLOCK_CONTEXT (loop->context);
2884 g_main_context_release (loop->context);
2885 g_main_loop_unref (loop);
2889 g_assert (got_ownership);
2892 LOCK_CONTEXT (loop->context);
2893 #endif /* G_THREADS_ENABLED */
2895 if (loop->context->in_check_or_prepare)
2897 g_warning ("g_main_loop_run(): called recursively from within a source's "
2898 "check() or prepare() member, iteration not possible.");
2902 g_atomic_int_inc (&loop->ref_count);
2903 loop->is_running = TRUE;
2904 while (loop->is_running)
2905 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2907 UNLOCK_CONTEXT (loop->context);
2909 #ifdef G_THREADS_ENABLED
2910 g_main_context_release (loop->context);
2911 #endif /* G_THREADS_ENABLED */
2913 g_main_loop_unref (loop);
2918 * @loop: a #GMainLoop
2920 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2921 * for the loop will return.
2923 * Note that sources that have already been dispatched when
2924 * g_main_loop_quit() is called will still be executed.
2927 g_main_loop_quit (GMainLoop *loop)
2929 g_return_if_fail (loop != NULL);
2930 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2932 LOCK_CONTEXT (loop->context);
2933 loop->is_running = FALSE;
2934 g_main_context_wakeup_unlocked (loop->context);
2936 #ifdef G_THREADS_ENABLED
2937 if (loop->context->cond)
2938 g_cond_broadcast (loop->context->cond);
2939 #endif /* G_THREADS_ENABLED */
2941 UNLOCK_CONTEXT (loop->context);
2945 * g_main_loop_is_running:
2946 * @loop: a #GMainLoop.
2948 * Checks to see if the main loop is currently being run via g_main_loop_run().
2950 * Return value: %TRUE if the mainloop is currently being run.
2953 g_main_loop_is_running (GMainLoop *loop)
2955 g_return_val_if_fail (loop != NULL, FALSE);
2956 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2958 return loop->is_running;
2962 * g_main_loop_get_context:
2963 * @loop: a #GMainLoop.
2965 * Returns the #GMainContext of @loop.
2967 * Return value: the #GMainContext of @loop
2970 g_main_loop_get_context (GMainLoop *loop)
2972 g_return_val_if_fail (loop != NULL, NULL);
2973 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2975 return loop->context;
2978 /* HOLDS: context's lock */
2980 g_main_context_poll (GMainContext *context,
2986 #ifdef G_MAIN_POLL_DEBUG
2992 GPollFunc poll_func;
2994 if (n_fds || timeout != 0)
2996 #ifdef G_MAIN_POLL_DEBUG
2997 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2998 poll_timer = g_timer_new ();
3001 LOCK_CONTEXT (context);
3003 poll_func = context->poll_func;
3005 UNLOCK_CONTEXT (context);
3006 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
3009 g_warning ("poll(2) failed due to: %s.",
3010 g_strerror (errno));
3012 /* If g_poll () returns -1, it has already called g_warning() */
3016 #ifdef G_MAIN_POLL_DEBUG
3017 LOCK_CONTEXT (context);
3019 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3022 g_timer_elapsed (poll_timer, NULL));
3023 g_timer_destroy (poll_timer);
3024 pollrec = context->poll_records;
3028 if (pollrec->fd->events)
3032 g_print (" [%d:", fds[i].fd);
3033 if (fds[i].revents & G_IO_IN)
3035 if (fds[i].revents & G_IO_OUT)
3037 if (fds[i].revents & G_IO_PRI)
3039 if (fds[i].revents & G_IO_ERR)
3041 if (fds[i].revents & G_IO_HUP)
3043 if (fds[i].revents & G_IO_NVAL)
3049 pollrec = pollrec->next;
3053 UNLOCK_CONTEXT (context);
3055 } /* if (n_fds || timeout != 0) */
3059 * g_main_context_add_poll:
3060 * @context: a #GMainContext (or %NULL for the default context)
3061 * @fd: a #GPollFD structure holding information about a file
3062 * descriptor to watch.
3063 * @priority: the priority for this file descriptor which should be
3064 * the same as the priority used for g_source_attach() to ensure that the
3065 * file descriptor is polled whenever the results may be needed.
3067 * Adds a file descriptor to the set of file descriptors polled for
3068 * this context. This will very seldomly be used directly. Instead
3069 * a typical event source will use g_source_add_poll() instead.
3072 g_main_context_add_poll (GMainContext *context,
3077 context = g_main_context_default ();
3079 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3080 g_return_if_fail (fd);
3082 LOCK_CONTEXT (context);
3083 g_main_context_add_poll_unlocked (context, priority, fd);
3084 UNLOCK_CONTEXT (context);
3087 /* HOLDS: main_loop_lock */
3089 g_main_context_add_poll_unlocked (GMainContext *context,
3093 GPollRec *lastrec, *pollrec;
3094 GPollRec *newrec = g_slice_new (GPollRec);
3096 /* This file descriptor may be checked before we ever poll */
3099 newrec->priority = priority;
3102 pollrec = context->poll_records;
3103 while (pollrec && priority >= pollrec->priority)
3106 pollrec = pollrec->next;
3110 lastrec->next = newrec;
3112 context->poll_records = newrec;
3114 newrec->next = pollrec;
3116 context->n_poll_records++;
3118 #ifdef G_THREADS_ENABLED
3119 context->poll_changed = TRUE;
3121 /* Now wake up the main loop if it is waiting in the poll() */
3122 g_main_context_wakeup_unlocked (context);
3127 * g_main_context_remove_poll:
3128 * @context:a #GMainContext
3129 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3131 * Removes file descriptor from the set of file descriptors to be
3132 * polled for a particular context.
3135 g_main_context_remove_poll (GMainContext *context,
3139 context = g_main_context_default ();
3141 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3142 g_return_if_fail (fd);
3144 LOCK_CONTEXT (context);
3145 g_main_context_remove_poll_unlocked (context, fd);
3146 UNLOCK_CONTEXT (context);
3150 g_main_context_remove_poll_unlocked (GMainContext *context,
3153 GPollRec *pollrec, *lastrec;
3156 pollrec = context->poll_records;
3160 if (pollrec->fd == fd)
3162 if (lastrec != NULL)
3163 lastrec->next = pollrec->next;
3165 context->poll_records = pollrec->next;
3167 g_slice_free (GPollRec, pollrec);
3169 context->n_poll_records--;
3173 pollrec = pollrec->next;
3176 #ifdef G_THREADS_ENABLED
3177 context->poll_changed = TRUE;
3179 /* Now wake up the main loop if it is waiting in the poll() */
3180 g_main_context_wakeup_unlocked (context);
3185 * g_source_get_current_time:
3186 * @source: a #GSource
3187 * @timeval: #GTimeVal structure in which to store current time.
3189 * Gets the "current time" to be used when checking
3190 * this source. The advantage of calling this function over
3191 * calling g_get_current_time() directly is that when
3192 * checking multiple sources, GLib can cache a single value
3193 * instead of having to repeatedly get the system time.
3196 g_source_get_current_time (GSource *source,
3199 GMainContext *context;
3201 g_return_if_fail (source->context != NULL);
3203 context = source->context;
3205 LOCK_CONTEXT (context);
3207 if (!context->time_is_current)
3209 g_get_current_time (&context->current_time);
3210 context->time_is_current = TRUE;
3213 *timeval = context->current_time;
3215 UNLOCK_CONTEXT (context);
3219 * g_main_context_set_poll_func:
3220 * @context: a #GMainContext
3221 * @func: the function to call to poll all file descriptors
3223 * Sets the function to use to handle polling of file descriptors. It
3224 * will be used instead of the poll() system call
3225 * (or GLib's replacement function, which is used where
3226 * poll() isn't available).
3228 * This function could possibly be used to integrate the GLib event
3229 * loop with an external event loop.
3232 g_main_context_set_poll_func (GMainContext *context,
3236 context = g_main_context_default ();
3238 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3240 LOCK_CONTEXT (context);
3243 context->poll_func = func;
3247 context->poll_func = (GPollFunc) poll;
3249 context->poll_func = (GPollFunc) g_poll;
3253 UNLOCK_CONTEXT (context);
3257 * g_main_context_get_poll_func:
3258 * @context: a #GMainContext
3260 * Gets the poll function set by g_main_context_set_poll_func().
3262 * Return value: the poll function
3265 g_main_context_get_poll_func (GMainContext *context)
3270 context = g_main_context_default ();
3272 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3274 LOCK_CONTEXT (context);
3275 result = context->poll_func;
3276 UNLOCK_CONTEXT (context);
3281 /* HOLDS: context's lock */
3282 /* Wake the main loop up from a poll() */
3284 g_main_context_wakeup_unlocked (GMainContext *context)
3286 #ifdef G_THREADS_ENABLED
3287 if (g_thread_supported() && context->poll_waiting)
3289 context->poll_waiting = FALSE;
3291 write (context->wake_up_pipe[1], "A", 1);
3293 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3300 * g_main_context_wakeup:
3301 * @context: a #GMainContext
3303 * If @context is currently waiting in a poll(), interrupt
3304 * the poll(), and continue the iteration process.
3307 g_main_context_wakeup (GMainContext *context)
3310 context = g_main_context_default ();
3312 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3314 LOCK_CONTEXT (context);
3315 g_main_context_wakeup_unlocked (context);
3316 UNLOCK_CONTEXT (context);
3320 * g_main_context_is_owner:
3321 * @context: a #GMainContext
3323 * Determines whether this thread holds the (recursive)
3324 * ownership of this #GMaincontext. This is useful to
3325 * know before waiting on another thread that may be
3326 * blocking to get ownership of @context.
3328 * Returns: %TRUE if current thread is owner of @context.
3333 g_main_context_is_owner (GMainContext *context)
3338 context = g_main_context_default ();
3340 #ifdef G_THREADS_ENABLED
3341 LOCK_CONTEXT (context);
3342 is_owner = context->owner == G_THREAD_SELF;
3343 UNLOCK_CONTEXT (context);
3354 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3355 GTimeVal *current_time)
3357 guint seconds = timeout_source->interval / 1000;
3358 guint msecs = timeout_source->interval - seconds * 1000;
3360 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3361 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3362 if (timeout_source->expiration.tv_usec >= 1000000)
3364 timeout_source->expiration.tv_usec -= 1000000;
3365 timeout_source->expiration.tv_sec++;
3367 if (timer_perturb==-1)
3370 * we want a per machine/session unique 'random' value; try the dbus
3371 * address first, that has a UUID in it. If there is no dbus, use the
3372 * hostname for hashing.
3374 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3375 if (!session_bus_address)
3376 session_bus_address = g_getenv ("HOSTNAME");
3377 if (session_bus_address)
3378 timer_perturb = g_str_hash (session_bus_address);
3382 if (timeout_source->granularity)
3385 gint gran; /* in usecs */
3388 gran = timeout_source->granularity * 1000;
3389 perturb = timer_perturb % gran;
3391 * We want to give each machine a per machine pertubation;
3392 * shift time back first, and forward later after the rounding
3395 timeout_source->expiration.tv_usec -= perturb;
3396 if (timeout_source->expiration.tv_usec < 0)
3398 timeout_source->expiration.tv_usec += 1000000;
3399 timeout_source->expiration.tv_sec--;
3402 remainder = timeout_source->expiration.tv_usec % gran;
3403 if (remainder >= gran/4) /* round up */
3404 timeout_source->expiration.tv_usec += gran;
3405 timeout_source->expiration.tv_usec -= remainder;
3407 timeout_source->expiration.tv_usec += perturb;
3409 /* the rounding may have overflown tv_usec */
3410 while (timeout_source->expiration.tv_usec > 1000000)
3412 timeout_source->expiration.tv_usec -= 1000000;
3413 timeout_source->expiration.tv_sec++;
3419 g_timeout_prepare (GSource *source,
3424 GTimeVal current_time;
3426 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3428 g_source_get_current_time (source, ¤t_time);
3430 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3431 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3433 /* We do the following in a rather convoluted fashion to deal with
3434 * the fact that we don't have an integral type big enough to hold
3435 * the difference of two timevals in millseconds.
3437 if (sec < 0 || (sec == 0 && msec < 0))
3441 glong interval_sec = timeout_source->interval / 1000;
3442 glong interval_msec = timeout_source->interval % 1000;
3450 if (sec > interval_sec ||
3451 (sec == interval_sec && msec > interval_msec))
3453 /* The system time has been set backwards, so we
3454 * reset the expiration time to now + timeout_source->interval;
3455 * this at least avoids hanging for long periods of time.
3457 g_timeout_set_expiration (timeout_source, ¤t_time);
3458 msec = MIN (G_MAXINT, timeout_source->interval);
3462 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3466 *timeout = (gint)msec;
3472 g_timeout_check (GSource *source)
3474 GTimeVal current_time;
3475 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3477 g_source_get_current_time (source, ¤t_time);
3479 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3480 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3481 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3485 g_timeout_dispatch (GSource *source,
3486 GSourceFunc callback,
3489 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3493 g_warning ("Timeout source dispatched without callback\n"
3494 "You must call g_source_set_callback().");
3498 if (callback (user_data))
3500 GTimeVal current_time;
3502 g_source_get_current_time (source, ¤t_time);
3503 g_timeout_set_expiration (timeout_source, ¤t_time);
3512 * g_timeout_source_new:
3513 * @interval: the timeout interval in milliseconds.
3515 * Creates a new timeout source.
3517 * The source will not initially be associated with any #GMainContext
3518 * and must be added to one with g_source_attach() before it will be
3521 * Return value: the newly-created timeout source
3524 g_timeout_source_new (guint interval)
3526 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3527 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3528 GTimeVal current_time;
3530 timeout_source->interval = interval;
3532 g_get_current_time (¤t_time);
3533 g_timeout_set_expiration (timeout_source, ¤t_time);
3539 * g_timeout_source_new_seconds:
3540 * @interval: the timeout interval in seconds
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 * The scheduling granularity/accuracy of this timeout source will be
3551 * Return value: the newly-created timeout source
3556 g_timeout_source_new_seconds (guint interval)
3558 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3559 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3560 GTimeVal current_time;
3562 timeout_source->interval = 1000*interval;
3563 timeout_source->granularity = 1000;
3565 g_get_current_time (¤t_time);
3566 g_timeout_set_expiration (timeout_source, ¤t_time);
3573 * g_timeout_add_full:
3574 * @priority: the priority of the timeout source. Typically this will be in
3575 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3576 * @interval: the time between calls to the function, in milliseconds
3577 * (1/1000ths of a second)
3578 * @function: function to call
3579 * @data: data to pass to @function
3580 * @notify: function to call when the timeout is removed, or %NULL
3582 * Sets a function to be called at regular intervals, with the given
3583 * priority. The function is called repeatedly until it returns
3584 * %FALSE, at which point the timeout is automatically destroyed and
3585 * the function will not be called again. The @notify function is
3586 * called when the timeout is destroyed. The first call to the
3587 * function will be at the end of the first @interval.
3589 * Note that timeout functions may be delayed, due to the processing of other
3590 * event sources. Thus they should not be relied on for precise timing.
3591 * After each call to the timeout function, the time of the next
3592 * timeout is recalculated based on the current time and the given interval
3593 * (it does not try to 'catch up' time lost in delays).
3595 * Return value: the ID (greater than 0) of the event source.
3598 g_timeout_add_full (gint priority,
3600 GSourceFunc function,
3602 GDestroyNotify notify)
3607 g_return_val_if_fail (function != NULL, 0);
3609 source = g_timeout_source_new (interval);
3611 if (priority != G_PRIORITY_DEFAULT)
3612 g_source_set_priority (source, priority);
3614 g_source_set_callback (source, function, data, notify);
3615 id = g_source_attach (source, NULL);
3616 g_source_unref (source);
3623 * @interval: the time between calls to the function, in milliseconds
3624 * (1/1000ths of a second)
3625 * @function: function to call
3626 * @data: data to pass to @function
3628 * Sets a function to be called at regular intervals, with the default
3629 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3630 * until it returns %FALSE, at which point the timeout is automatically
3631 * destroyed and the function will not be called again. The first call
3632 * to the function will be at the end of the first @interval.
3634 * Note that timeout functions may be delayed, due to the processing of other
3635 * event sources. Thus they should not be relied on for precise timing.
3636 * After each call to the timeout function, the time of the next
3637 * timeout is recalculated based on the current time and the given interval
3638 * (it does not try to 'catch up' time lost in delays).
3640 * If you want to have a timer in the "seconds" range and do not care
3641 * about the exact time of the first call of the timer, use the
3642 * g_timeout_add_seconds() function; this function allows for more
3643 * optimizations and more efficient system power usage.
3645 * Return value: the ID (greater than 0) of the event source.
3648 g_timeout_add (guint32 interval,
3649 GSourceFunc function,
3652 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3653 interval, function, data, NULL);
3657 * g_timeout_add_seconds_full:
3658 * @priority: the priority of the timeout source. Typically this will be in
3659 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3660 * @interval: the time between calls to the function, in seconds
3661 * @function: function to call
3662 * @data: data to pass to @function
3663 * @notify: function to call when the timeout is removed, or %NULL
3665 * Sets a function to be called at regular intervals, with @priority.
3666 * The function is called repeatedly until it returns %FALSE, at which
3667 * point the timeout is automatically destroyed and the function will
3668 * not be called again.
3670 * Unlike g_timeout_add(), this function operates at whole second granularity.
3671 * The initial starting point of the timer is determined by the implementation
3672 * and the implementation is expected to group multiple timers together so that
3673 * they fire all at the same time.
3674 * To allow this grouping, the @interval to the first timer is rounded
3675 * and can deviate up to one second from the specified interval.
3676 * Subsequent timer iterations will generally run at the specified interval.
3678 * Note that timeout functions may be delayed, due to the processing of other
3679 * event sources. Thus they should not be relied on for precise timing.
3680 * After each call to the timeout function, the time of the next
3681 * timeout is recalculated based on the current time and the given @interval
3683 * If you want timing more precise than whole seconds, use g_timeout_add()
3686 * The grouping of timers to fire at the same time results in a more power
3687 * and CPU efficient behavior so if your timer is in multiples of seconds
3688 * and you don't require the first timer exactly one second from now, the
3689 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3691 * Return value: the ID (greater than 0) of the event source.
3696 g_timeout_add_seconds_full (gint priority,
3698 GSourceFunc function,
3700 GDestroyNotify notify)
3705 g_return_val_if_fail (function != NULL, 0);
3707 source = g_timeout_source_new_seconds (interval);
3709 if (priority != G_PRIORITY_DEFAULT)
3710 g_source_set_priority (source, priority);
3712 g_source_set_callback (source, function, data, notify);
3713 id = g_source_attach (source, NULL);
3714 g_source_unref (source);
3720 * g_timeout_add_seconds:
3721 * @interval: the time between calls to the function, in seconds
3722 * @function: function to call
3723 * @data: data to pass to @function
3725 * Sets a function to be called at regular intervals with the default
3726 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3727 * it returns %FALSE, at which point the timeout is automatically destroyed
3728 * and the function will not be called again.
3730 * See g_timeout_add_seconds_full() for the differences between
3731 * g_timeout_add() and g_timeout_add_seconds().
3733 * Return value: the ID (greater than 0) of the event source.
3738 g_timeout_add_seconds (guint interval,
3739 GSourceFunc function,
3742 g_return_val_if_fail (function != NULL, 0);
3744 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3747 /* Child watch functions */
3752 g_child_watch_prepare (GSource *source,
3761 g_child_watch_check (GSource *source)
3763 GChildWatchSource *child_watch_source;
3764 gboolean child_exited;
3766 child_watch_source = (GChildWatchSource *) source;
3768 child_exited = child_watch_source->poll.revents & G_IO_IN;
3775 * Note: We do _not_ check for the special value of STILL_ACTIVE
3776 * since we know that the process has exited and doing so runs into
3777 * problems if the child process "happens to return STILL_ACTIVE(259)"
3778 * as Microsoft's Platform SDK puts it.
3780 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3782 gchar *emsg = g_win32_error_message (GetLastError ());
3783 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3786 child_watch_source->child_status = -1;
3789 child_watch_source->child_status = child_status;
3792 return child_exited;
3795 #else /* G_OS_WIN32 */
3798 check_for_child_exited (GSource *source)
3800 GChildWatchSource *child_watch_source;
3803 /* protect against another SIGCHLD in the middle of this call */
3804 count = child_watch_count;
3806 child_watch_source = (GChildWatchSource *) source;
3808 if (child_watch_source->child_exited)
3811 if (child_watch_source->count < count)
3815 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3817 child_watch_source->child_status = child_status;
3818 child_watch_source->child_exited = TRUE;
3820 child_watch_source->count = count;
3823 return child_watch_source->child_exited;
3827 g_child_watch_prepare (GSource *source,
3832 return check_for_child_exited (source);
3837 g_child_watch_check (GSource *source)
3839 return check_for_child_exited (source);
3842 #endif /* G_OS_WIN32 */
3845 g_child_watch_dispatch (GSource *source,
3846 GSourceFunc callback,
3849 GChildWatchSource *child_watch_source;
3850 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3852 child_watch_source = (GChildWatchSource *) source;
3856 g_warning ("Child watch source dispatched without callback\n"
3857 "You must call g_source_set_callback().");
3861 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3863 /* We never keep a child watch source around as the child is gone */
3870 g_child_watch_signal_handler (int signum)
3872 child_watch_count ++;
3874 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3876 write (child_watch_wake_up_pipe[1], "B", 1);
3880 /* We count on the signal interrupting the poll in the same thread.
3886 g_child_watch_source_init_single (void)
3888 struct sigaction action;
3890 g_assert (! g_thread_supported());
3891 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3893 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3895 action.sa_handler = g_child_watch_signal_handler;
3896 sigemptyset (&action.sa_mask);
3897 action.sa_flags = SA_NOCLDSTOP;
3898 sigaction (SIGCHLD, &action, NULL);
3902 child_watch_helper_thread (gpointer data)
3909 read (child_watch_wake_up_pipe[0], b, 20);
3911 /* We were woken up. Wake up all other contexts in all other threads */
3912 G_LOCK (main_context_list);
3913 for (list = main_context_list; list; list = list->next)
3915 GMainContext *context;
3917 context = list->data;
3918 if (g_atomic_int_get (&context->ref_count) > 0)
3919 /* Due to racing conditions we can find ref_count == 0, in
3920 * that case, however, the context is still not destroyed
3921 * and no poll can be active, otherwise the ref_count
3923 g_main_context_wakeup (context);
3925 G_UNLOCK (main_context_list);
3932 g_child_watch_source_init_multi_threaded (void)
3934 GError *error = NULL;
3935 struct sigaction action;
3937 g_assert (g_thread_supported());
3939 if (pipe (child_watch_wake_up_pipe) < 0)
3940 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3941 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3943 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3944 /* FIXME: Think this through for races */
3945 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3946 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3947 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3949 action.sa_handler = g_child_watch_signal_handler;
3950 sigemptyset (&action.sa_mask);
3951 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3952 sigaction (SIGCHLD, &action, NULL);
3956 g_child_watch_source_init_promote_single_to_threaded (void)
3958 g_child_watch_source_init_multi_threaded ();
3962 g_child_watch_source_init (void)
3964 if (g_thread_supported())
3966 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3967 g_child_watch_source_init_multi_threaded ();
3968 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3969 g_child_watch_source_init_promote_single_to_threaded ();
3973 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3974 g_child_watch_source_init_single ();
3978 #endif /* !G_OS_WIN32 */
3981 * g_child_watch_source_new:
3982 * @pid: process id of a child process to watch. On Windows, a HANDLE
3983 * for the process to watch (which actually doesn't have to be a child).
3985 * Creates a new child_watch source.
3987 * The source will not initially be associated with any #GMainContext
3988 * and must be added to one with g_source_attach() before it will be
3991 * Note that child watch sources can only be used in conjunction with
3992 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
3995 * Note that on platforms where #GPid must be explicitly closed
3996 * (see g_spawn_close_pid()) @pid must not be closed while the
3997 * source is still active. Typically, you will want to call
3998 * g_spawn_close_pid() in the callback function for the source.
4000 * Note further that using g_child_watch_source_new() is not
4001 * compatible with calling <literal>waitpid(-1)</literal> in
4002 * the application. Calling waitpid() for individual pids will
4005 * Return value: the newly-created child watch source
4010 g_child_watch_source_new (GPid pid)
4012 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
4013 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
4016 child_watch_source->poll.fd = (int)pid;
4017 child_watch_source->poll.events = G_IO_IN;
4019 g_source_add_poll (source, &child_watch_source->poll);
4020 #else /* G_OS_WIN32 */
4021 g_child_watch_source_init ();
4022 #endif /* G_OS_WIN32 */
4024 child_watch_source->pid = pid;
4030 * g_child_watch_add_full:
4031 * @priority: the priority of the idle source. Typically this will be in the
4032 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4033 * @pid: process id of a child process to watch
4034 * @function: function to call
4035 * @data: data to pass to @function
4036 * @notify: function to call when the idle is removed, or %NULL
4038 * Sets a function to be called when the child indicated by @pid
4039 * exits, at the priority @priority.
4041 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4042 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4043 * the spawn function for the child watching to work.
4045 * Note that on platforms where #GPid must be explicitly closed
4046 * (see g_spawn_close_pid()) @pid must not be closed while the
4047 * source is still active. Typically, you will want to call
4048 * g_spawn_close_pid() in the callback function for the source.
4050 * GLib supports only a single callback per process id.
4052 * Return value: the ID (greater than 0) of the event source.
4057 g_child_watch_add_full (gint priority,
4059 GChildWatchFunc function,
4061 GDestroyNotify notify)
4066 g_return_val_if_fail (function != NULL, 0);
4068 source = g_child_watch_source_new (pid);
4070 if (priority != G_PRIORITY_DEFAULT)
4071 g_source_set_priority (source, priority);
4073 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4074 id = g_source_attach (source, NULL);
4075 g_source_unref (source);
4081 * g_child_watch_add:
4082 * @pid: process id of a child process to watch
4083 * @function: function to call
4084 * @data: data to pass to @function
4086 * Sets a function to be called when the child indicated by @pid
4087 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4089 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4090 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4091 * the spawn function for the child watching to work.
4093 * Note that on platforms where #GPid must be explicitly closed
4094 * (see g_spawn_close_pid()) @pid must not be closed while the
4095 * source is still active. Typically, you will want to call
4096 * g_spawn_close_pid() in the callback function for the source.
4098 * GLib supports only a single callback per process id.
4100 * Return value: the ID (greater than 0) of the event source.
4105 g_child_watch_add (GPid pid,
4106 GChildWatchFunc function,
4109 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4113 /* Idle functions */
4116 g_idle_prepare (GSource *source,
4125 g_idle_check (GSource *source)
4131 g_idle_dispatch (GSource *source,
4132 GSourceFunc callback,
4137 g_warning ("Idle source dispatched without callback\n"
4138 "You must call g_source_set_callback().");
4142 return callback (user_data);
4146 * g_idle_source_new:
4148 * Creates a new idle source.
4150 * The source will not initially be associated with any #GMainContext
4151 * and must be added to one with g_source_attach() before it will be
4152 * executed. Note that the default priority for idle sources is
4153 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4154 * have a default priority of %G_PRIORITY_DEFAULT.
4156 * Return value: the newly-created idle source
4159 g_idle_source_new (void)
4163 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4164 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4171 * @priority: the priority of the idle source. Typically this will be in the
4172 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4173 * @function: function to call
4174 * @data: data to pass to @function
4175 * @notify: function to call when the idle is removed, or %NULL
4177 * Adds a function to be called whenever there are no higher priority
4178 * events pending. If the function returns %FALSE it is automatically
4179 * removed from the list of event sources and will not be called again.
4181 * Return value: the ID (greater than 0) of the event source.
4184 g_idle_add_full (gint priority,
4185 GSourceFunc function,
4187 GDestroyNotify notify)
4192 g_return_val_if_fail (function != NULL, 0);
4194 source = g_idle_source_new ();
4196 if (priority != G_PRIORITY_DEFAULT_IDLE)
4197 g_source_set_priority (source, priority);
4199 g_source_set_callback (source, function, data, notify);
4200 id = g_source_attach (source, NULL);
4201 g_source_unref (source);
4208 * @function: function to call
4209 * @data: data to pass to @function.
4211 * Adds a function to be called whenever there are no higher priority
4212 * events pending to the default main loop. The function is given the
4213 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4214 * returns %FALSE it is automatically removed from the list of event
4215 * sources and will not be called again.
4217 * Return value: the ID (greater than 0) of the event source.
4220 g_idle_add (GSourceFunc function,
4223 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4227 * g_idle_remove_by_data:
4228 * @data: the data for the idle source's callback.
4230 * Removes the idle function with the given data.
4232 * Return value: %TRUE if an idle source was found and removed.
4235 g_idle_remove_by_data (gpointer data)
4237 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4240 #define __G_MAIN_C__
4241 #include "galiasdef.c"