1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 /* Uncomment the next line to enable debugging printouts if the
37 * environment variable G_MAIN_POLL_DEBUG is set to some value.
39 /* #define G_MAIN_POLL_DEBUG */
42 /* Always enable debugging printout on Windows, as it is more often
45 #define G_MAIN_POLL_DEBUG
49 #include "gthreadprivate.h"
51 #include <sys/types.h>
54 #ifdef HAVE_SYS_TIME_H
56 #endif /* HAVE_SYS_TIME_H */
57 #ifdef GLIB_HAVE_SYS_POLL_H
58 # include <sys/poll.h>
59 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
60 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
62 /* The poll() emulation on OS/X doesn't handle fds=NULL, nfds=0,
63 * so we prefer our own poll emulation.
65 #if defined(_POLL_EMUL_H_) || defined(BROKEN_POLL)
69 #endif /* GLIB_HAVE_SYS_POLL_H */
72 #endif /* HAVE_UNISTD_H */
78 #endif /* G_OS_WIN32 */
81 #include <sys/socket.h>
83 #endif /* G_OS_BEOS */
92 #ifdef G_MAIN_POLL_DEBUG
94 #define GPOLLFD_FORMAT "%#x"
96 #define GPOLLFD_FORMAT "%d"
102 typedef struct _GTimeoutSource GTimeoutSource;
103 typedef struct _GChildWatchSource GChildWatchSource;
104 typedef struct _GPollRec GPollRec;
105 typedef struct _GSourceCallback GSourceCallback;
109 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
110 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
113 #ifdef G_THREADS_ENABLED
114 typedef struct _GMainWaiter GMainWaiter;
123 typedef struct _GMainDispatch GMainDispatch;
125 struct _GMainDispatch
128 GSList *dispatching_sources; /* stack of current sources */
131 #ifdef G_MAIN_POLL_DEBUG
132 static gboolean g_main_poll_debug = FALSE;
137 #ifdef G_THREADS_ENABLED
138 /* The following lock is used for both the list of sources
139 * and the list of poll records
150 GPtrArray *pending_dispatches;
151 gint timeout; /* Timeout for current iteration */
154 GSource *source_list;
155 gint in_check_or_prepare;
157 GPollRec *poll_records;
158 guint n_poll_records;
159 GPollFD *cached_poll_array;
160 guint cached_poll_array_size;
162 #ifdef G_THREADS_ENABLED
164 /* this pipe is used to wake up the main loop when a source is added.
166 gint wake_up_pipe[2];
167 #else /* G_OS_WIN32 */
168 HANDLE wake_up_semaphore;
169 #endif /* G_OS_WIN32 */
172 gboolean poll_waiting;
174 /* Flag indicating whether the set of fd's changed during a poll */
175 gboolean poll_changed;
176 #endif /* G_THREADS_ENABLED */
180 GTimeVal current_time;
181 gboolean time_is_current;
184 struct _GSourceCallback
189 GDestroyNotify notify;
194 GMainContext *context;
199 struct _GTimeoutSource
207 struct _GChildWatchSource
214 #else /* G_OS_WIN32 */
216 gboolean child_exited;
217 #endif /* G_OS_WIN32 */
227 #ifdef G_THREADS_ENABLED
228 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
229 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
230 #define G_THREAD_SELF g_thread_self ()
232 #define LOCK_CONTEXT(context) (void)0
233 #define UNLOCK_CONTEXT(context) (void)0
234 #define G_THREAD_SELF NULL
237 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
238 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
239 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
241 #define SOURCE_UNREF(source, context) \
243 if ((source)->ref_count > 1) \
244 (source)->ref_count--; \
246 g_source_unref_internal ((source), (context), TRUE); \
250 /* Forward declarations */
252 static void g_source_unref_internal (GSource *source,
253 GMainContext *context,
255 static void g_source_destroy_internal (GSource *source,
256 GMainContext *context,
258 static void g_main_context_poll (GMainContext *context,
263 static void g_main_context_add_poll_unlocked (GMainContext *context,
266 static void g_main_context_remove_poll_unlocked (GMainContext *context,
268 static void g_main_context_wakeup_unlocked (GMainContext *context);
270 static gboolean g_timeout_prepare (GSource *source,
272 static gboolean g_timeout_check (GSource *source);
273 static gboolean g_timeout_dispatch (GSource *source,
274 GSourceFunc callback,
276 static gboolean g_child_watch_prepare (GSource *source,
278 static gboolean g_child_watch_check (GSource *source);
279 static gboolean g_child_watch_dispatch (GSource *source,
280 GSourceFunc callback,
282 static gboolean g_idle_prepare (GSource *source,
284 static gboolean g_idle_check (GSource *source);
285 static gboolean g_idle_dispatch (GSource *source,
286 GSourceFunc callback,
289 G_LOCK_DEFINE_STATIC (main_loop);
290 static GMainContext *default_main_context;
291 static GSList *main_contexts_without_pipe = NULL;
294 /* Child status monitoring code */
296 CHILD_WATCH_UNINITIALIZED,
297 CHILD_WATCH_INITIALIZED_SINGLE,
298 CHILD_WATCH_INITIALIZED_THREADED
300 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
301 static gint child_watch_count = 1;
302 static gint child_watch_wake_up_pipe[2] = {0, 0};
303 #endif /* !G_OS_WIN32 */
304 G_LOCK_DEFINE_STATIC (main_context_list);
305 static GSList *main_context_list = NULL;
307 static gint timer_perturb = -1;
309 GSourceFuncs g_timeout_funcs =
317 GSourceFuncs g_child_watch_funcs =
319 g_child_watch_prepare,
321 g_child_watch_dispatch,
325 GSourceFuncs g_idle_funcs =
334 /* SunOS has poll, but doesn't provide a prototype. */
335 # if defined (sun) && !defined (__SVR4)
336 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
338 #else /* !HAVE_POLL */
343 g_poll (GPollFD *fds,
347 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
348 gboolean poll_msgs = FALSE;
354 #ifdef G_MAIN_POLL_DEBUG
355 if (g_main_poll_debug)
356 g_print ("g_poll: waiting for");
358 for (f = fds; f < &fds[nfds]; ++f)
361 if (f->fd == G_WIN32_MSG_HANDLE)
364 #ifdef G_MAIN_POLL_DEBUG
365 if (g_main_poll_debug)
369 else if (nhandles == MAXIMUM_WAIT_OBJECTS)
371 g_warning (G_STRLOC ": Too many handles to wait for!\n");
376 #ifdef G_MAIN_POLL_DEBUG
377 if (g_main_poll_debug)
378 g_print (" %#x", f->fd);
380 handles[nhandles++] = (HANDLE) f->fd;
383 #ifdef G_MAIN_POLL_DEBUG
384 if (g_main_poll_debug)
393 /* Waiting for messages, and maybe events
394 * -> First PeekMessage
396 #ifdef G_MAIN_POLL_DEBUG
397 if (g_main_poll_debug)
398 g_print ("PeekMessage\n");
400 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
401 ready = WAIT_OBJECT_0 + nhandles;
404 /* Wait for either message or event
405 * -> Use MsgWaitForMultipleObjectsEx
407 #ifdef G_MAIN_POLL_DEBUG
408 if (g_main_poll_debug)
409 g_print ("MsgWaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
411 ready = MsgWaitForMultipleObjectsEx (nhandles, handles, timeout,
412 QS_ALLINPUT, MWMO_ALERTABLE);
414 if (ready == WAIT_FAILED)
416 gchar *emsg = g_win32_error_message (GetLastError ());
417 g_warning (G_STRLOC ": MsgWaitForMultipleObjectsEx() failed: %s", emsg);
422 else if (nhandles == 0)
424 /* No handles to wait for, just the timeout */
425 if (timeout == INFINITE)
429 SleepEx (timeout, TRUE);
430 ready = WAIT_TIMEOUT;
435 /* Wait for just events
436 * -> Use WaitForMultipleObjectsEx
438 #ifdef G_MAIN_POLL_DEBUG
439 if (g_main_poll_debug)
440 g_print ("WaitForMultipleObjectsEx(%d, %d)\n", nhandles, timeout);
442 ready = WaitForMultipleObjectsEx (nhandles, handles, FALSE, timeout, TRUE);
443 if (ready == WAIT_FAILED)
445 gchar *emsg = g_win32_error_message (GetLastError ());
446 g_warning (G_STRLOC ": WaitForMultipleObjectsEx() failed: %s", emsg);
451 #ifdef G_MAIN_POLL_DEBUG
452 if (g_main_poll_debug)
453 g_print ("wait returns %ld%s\n",
455 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
456 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
457 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
459 for (f = fds; f < &fds[nfds]; ++f)
462 if (ready == WAIT_FAILED)
464 else if (ready == WAIT_TIMEOUT ||
465 ready == WAIT_IO_COMPLETION)
467 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
469 for (f = fds; f < &fds[nfds]; ++f)
472 if (f->events & G_IO_IN)
473 if (f->fd == G_WIN32_MSG_HANDLE)
474 f->revents |= G_IO_IN;
477 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
478 for (f = fds; f < &fds[nfds]; ++f)
480 if (f->fd == (gint) handles[ready - WAIT_OBJECT_0])
482 f->revents = f->events;
483 #ifdef G_MAIN_POLL_DEBUG
484 if (g_main_poll_debug)
485 g_print ("g_poll: got event %#x\n", f->fd);
493 #else /* !G_OS_WIN32 */
495 /* The following implementation of poll() comes from the GNU C Library.
496 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
499 #include <string.h> /* for bzero on BSD systems */
501 #ifdef HAVE_SYS_SELECT_H
502 #include <sys/select.h>
503 #endif /* HAVE_SYS_SELECT_H */
507 #endif /* G_OS_BEOS */
510 # define SELECT_MASK fd_set
511 #else /* !NO_FD_SET */
513 typedef long fd_mask;
516 # define SELECT_MASK void
518 # define SELECT_MASK int
519 # endif /* !_IBMR2 */
520 #endif /* !NO_FD_SET */
523 g_poll (GPollFD *fds,
528 SELECT_MASK rset, wset, xset;
537 for (f = fds; f < &fds[nfds]; ++f)
540 if (f->events & G_IO_IN)
541 FD_SET (f->fd, &rset);
542 if (f->events & G_IO_OUT)
543 FD_SET (f->fd, &wset);
544 if (f->events & G_IO_PRI)
545 FD_SET (f->fd, &xset);
546 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
550 tv.tv_sec = timeout / 1000;
551 tv.tv_usec = (timeout % 1000) * 1000;
553 ready = select (maxfd + 1, &rset, &wset, &xset,
554 timeout == -1 ? NULL : &tv);
556 for (f = fds; f < &fds[nfds]; ++f)
561 if (FD_ISSET (f->fd, &rset))
562 f->revents |= G_IO_IN;
563 if (FD_ISSET (f->fd, &wset))
564 f->revents |= G_IO_OUT;
565 if (FD_ISSET (f->fd, &xset))
566 f->revents |= G_IO_PRI;
573 #endif /* !G_OS_WIN32 */
575 #endif /* !HAVE_POLL */
578 * g_main_context_ref:
579 * @context: a #GMainContext
581 * Increases the reference count on a #GMainContext object by one.
583 * Returns: the @context that was passed in (since 2.6)
586 g_main_context_ref (GMainContext *context)
588 g_return_val_if_fail (context != NULL, NULL);
589 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
591 g_atomic_int_inc (&context->ref_count);
597 poll_rec_list_free (GMainContext *context,
600 g_slice_free_chain (GPollRec, list, next);
604 * g_main_context_unref:
605 * @context: a #GMainContext
607 * Decreases the reference count on a #GMainContext object by one. If
608 * the result is zero, free the context and free all associated memory.
611 g_main_context_unref (GMainContext *context)
614 g_return_if_fail (context != NULL);
615 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
617 if (!g_atomic_int_dec_and_test (&context->ref_count))
620 G_LOCK (main_context_list);
621 main_context_list = g_slist_remove (main_context_list, context);
622 G_UNLOCK (main_context_list);
624 source = context->source_list;
627 GSource *next = source->next;
628 g_source_destroy_internal (source, context, FALSE);
632 #ifdef G_THREADS_ENABLED
633 g_static_mutex_free (&context->mutex);
636 g_ptr_array_free (context->pending_dispatches, TRUE);
637 g_free (context->cached_poll_array);
639 poll_rec_list_free (context, context->poll_records);
641 #ifdef G_THREADS_ENABLED
642 if (g_thread_supported())
645 close (context->wake_up_pipe[0]);
646 close (context->wake_up_pipe[1]);
648 CloseHandle (context->wake_up_semaphore);
652 main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe,
655 if (context->cond != NULL)
656 g_cond_free (context->cond);
662 #ifdef G_THREADS_ENABLED
664 g_main_context_init_pipe (GMainContext *context)
667 if (context->wake_up_pipe[0] != -1)
669 if (pipe (context->wake_up_pipe) < 0)
670 g_error ("Cannot create pipe main loop wake-up: %s\n",
673 context->wake_up_rec.fd = context->wake_up_pipe[0];
674 context->wake_up_rec.events = G_IO_IN;
676 if (context->wake_up_semaphore != NULL)
678 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
679 if (context->wake_up_semaphore == NULL)
680 g_error ("Cannot create wake-up semaphore: %s",
681 g_win32_error_message (GetLastError ()));
682 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
683 context->wake_up_rec.events = G_IO_IN;
684 # ifdef G_MAIN_POLL_DEBUG
685 if (g_main_poll_debug)
686 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
689 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
693 _g_main_thread_init (void)
695 GSList *curr = main_contexts_without_pipe;
698 g_main_context_init_pipe ((GMainContext *)curr->data);
701 g_slist_free (main_contexts_without_pipe);
702 main_contexts_without_pipe = NULL;
704 #endif /* G_THREADS_ENABLED */
707 * g_main_context_new:
709 * Creates a new #GMainContext structure.
711 * Return value: the new #GMainContext
714 g_main_context_new (void)
716 GMainContext *context = g_new0 (GMainContext, 1);
718 #ifdef G_THREADS_ENABLED
719 g_static_mutex_init (&context->mutex);
721 context->owner = NULL;
722 context->waiters = NULL;
725 context->wake_up_pipe[0] = -1;
726 context->wake_up_pipe[1] = -1;
728 context->wake_up_semaphore = NULL;
732 context->ref_count = 1;
734 context->next_id = 1;
736 context->source_list = NULL;
739 context->poll_func = (GPollFunc)poll;
741 context->poll_func = g_poll;
744 context->cached_poll_array = NULL;
745 context->cached_poll_array_size = 0;
747 context->pending_dispatches = g_ptr_array_new ();
749 context->time_is_current = FALSE;
751 #ifdef G_THREADS_ENABLED
752 if (g_thread_supported ())
753 g_main_context_init_pipe (context);
755 main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe,
759 G_LOCK (main_context_list);
760 main_context_list = g_slist_append (main_context_list, context);
762 #ifdef G_MAIN_POLL_DEBUG
763 if (g_main_poll_debug)
764 g_print ("created context=%p\n", context);
767 G_UNLOCK (main_context_list);
773 * g_main_context_default:
775 * Returns the default main context. This is the main context used
776 * for main loop functions when a main loop is not explicitly
779 * Return value: the default main context.
782 g_main_context_default (void)
788 if (!default_main_context)
790 default_main_context = g_main_context_new ();
791 #ifdef G_MAIN_POLL_DEBUG
792 if (g_main_poll_debug)
793 g_print ("default context=%p\n", default_main_context);
797 G_UNLOCK (main_loop);
799 return default_main_context;
802 /* Hooks for adding to the main loop */
806 * @source_funcs: structure containing functions that implement
807 * the sources behavior.
808 * @struct_size: size of the #GSource structure to create.
810 * Creates a new #GSource structure. The size is specified to
811 * allow creating structures derived from #GSource that contain
812 * additional data. The size passed in must be at least
813 * <literal>sizeof (GSource)</literal>.
815 * The source will not initially be associated with any #GMainContext
816 * and must be added to one with g_source_attach() before it will be
819 * Return value: the newly-created #GSource.
822 g_source_new (GSourceFuncs *source_funcs,
827 g_return_val_if_fail (source_funcs != NULL, NULL);
828 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
830 source = (GSource*) g_malloc0 (struct_size);
832 source->source_funcs = source_funcs;
833 source->ref_count = 1;
835 source->priority = G_PRIORITY_DEFAULT;
837 source->flags = G_HOOK_FLAG_ACTIVE;
839 /* NULL/0 initialization for all other fields */
844 /* Holds context's lock
847 g_source_list_add (GSource *source,
848 GMainContext *context)
850 GSource *tmp_source, *last_source;
853 tmp_source = context->source_list;
854 while (tmp_source && tmp_source->priority <= source->priority)
856 last_source = tmp_source;
857 tmp_source = tmp_source->next;
860 source->next = tmp_source;
862 tmp_source->prev = source;
864 source->prev = last_source;
866 last_source->next = source;
868 context->source_list = source;
871 /* Holds context's lock
874 g_source_list_remove (GSource *source,
875 GMainContext *context)
878 source->prev->next = source->next;
880 context->source_list = source->next;
883 source->next->prev = source->prev;
891 * @source: a #GSource
892 * @context: a #GMainContext (if %NULL, the default context will be used)
894 * Adds a #GSource to a @context so that it will be executed within
897 * Return value: the ID (greater than 0) for the source within the
901 g_source_attach (GSource *source,
902 GMainContext *context)
907 g_return_val_if_fail (source->context == NULL, 0);
908 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
911 context = g_main_context_default ();
913 LOCK_CONTEXT (context);
915 source->context = context;
916 result = source->source_id = context->next_id++;
919 g_source_list_add (source, context);
921 tmp_list = source->poll_fds;
924 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
925 tmp_list = tmp_list->next;
928 #ifdef G_THREADS_ENABLED
929 /* Now wake up the main loop if it is waiting in the poll() */
930 g_main_context_wakeup_unlocked (context);
933 UNLOCK_CONTEXT (context);
939 g_source_destroy_internal (GSource *source,
940 GMainContext *context,
944 LOCK_CONTEXT (context);
946 if (!SOURCE_DESTROYED (source))
949 gpointer old_cb_data;
950 GSourceCallbackFuncs *old_cb_funcs;
952 source->flags &= ~G_HOOK_FLAG_ACTIVE;
954 old_cb_data = source->callback_data;
955 old_cb_funcs = source->callback_funcs;
957 source->callback_data = NULL;
958 source->callback_funcs = NULL;
962 UNLOCK_CONTEXT (context);
963 old_cb_funcs->unref (old_cb_data);
964 LOCK_CONTEXT (context);
967 if (!SOURCE_BLOCKED (source))
969 tmp_list = source->poll_fds;
972 g_main_context_remove_poll_unlocked (context, tmp_list->data);
973 tmp_list = tmp_list->next;
977 g_source_unref_internal (source, context, TRUE);
981 UNLOCK_CONTEXT (context);
986 * @source: a #GSource
988 * Removes a source from its #GMainContext, if any, and mark it as
989 * destroyed. The source cannot be subsequently added to another
993 g_source_destroy (GSource *source)
995 GMainContext *context;
997 g_return_if_fail (source != NULL);
999 context = source->context;
1002 g_source_destroy_internal (source, context, FALSE);
1004 source->flags &= ~G_HOOK_FLAG_ACTIVE;
1009 * @source: a #GSource
1011 * Returns the numeric ID for a particular source. The ID of a source
1012 * is a positive integer which is unique within a particular main loop
1013 * context. The reverse
1014 * mapping from ID to source is done by g_main_context_find_source_by_id().
1016 * Return value: the ID (greater than 0) for the source
1019 g_source_get_id (GSource *source)
1023 g_return_val_if_fail (source != NULL, 0);
1024 g_return_val_if_fail (source->context != NULL, 0);
1026 LOCK_CONTEXT (source->context);
1027 result = source->source_id;
1028 UNLOCK_CONTEXT (source->context);
1034 * g_source_get_context:
1035 * @source: a #GSource
1037 * Gets the #GMainContext with which the source is associated.
1038 * Calling this function on a destroyed source is an error.
1040 * Return value: the #GMainContext with which the source is associated,
1041 * or %NULL if the context has not yet been added
1045 g_source_get_context (GSource *source)
1047 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
1049 return source->context;
1053 * g_source_add_poll:
1054 * @source:a #GSource
1055 * @fd: a #GPollFD structure holding information about a file
1056 * descriptor to watch.
1058 * Adds a file descriptor to the set of file descriptors polled for
1059 * this source. This is usually combined with g_source_new() to add an
1060 * event source. The event source's check function will typically test
1061 * the @revents field in the #GPollFD struct and return %TRUE if events need
1065 g_source_add_poll (GSource *source,
1068 GMainContext *context;
1070 g_return_if_fail (source != NULL);
1071 g_return_if_fail (fd != NULL);
1072 g_return_if_fail (!SOURCE_DESTROYED (source));
1074 context = source->context;
1077 LOCK_CONTEXT (context);
1079 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
1083 if (!SOURCE_BLOCKED (source))
1084 g_main_context_add_poll_unlocked (context, source->priority, fd);
1085 UNLOCK_CONTEXT (context);
1090 * g_source_remove_poll:
1091 * @source:a #GSource
1092 * @fd: a #GPollFD structure previously passed to g_source_add_poll().
1094 * Removes a file descriptor from the set of file descriptors polled for
1098 g_source_remove_poll (GSource *source,
1101 GMainContext *context;
1103 g_return_if_fail (source != NULL);
1104 g_return_if_fail (fd != NULL);
1105 g_return_if_fail (!SOURCE_DESTROYED (source));
1107 context = source->context;
1110 LOCK_CONTEXT (context);
1112 source->poll_fds = g_slist_remove (source->poll_fds, fd);
1116 if (!SOURCE_BLOCKED (source))
1117 g_main_context_remove_poll_unlocked (context, fd);
1118 UNLOCK_CONTEXT (context);
1123 * g_source_set_callback_indirect:
1124 * @source: the source
1125 * @callback_data: pointer to callback data "object"
1126 * @callback_funcs: functions for reference counting @callback_data
1127 * and getting the callback and data
1129 * Sets the callback function storing the data as a refcounted callback
1130 * "object". This is used internally. Note that calling
1131 * g_source_set_callback_indirect() assumes
1132 * an initial reference count on @callback_data, and thus
1133 * @callback_funcs->unref will eventually be called once more
1134 * than @callback_funcs->ref.
1137 g_source_set_callback_indirect (GSource *source,
1138 gpointer callback_data,
1139 GSourceCallbackFuncs *callback_funcs)
1141 GMainContext *context;
1142 gpointer old_cb_data;
1143 GSourceCallbackFuncs *old_cb_funcs;
1145 g_return_if_fail (source != NULL);
1146 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
1148 context = source->context;
1151 LOCK_CONTEXT (context);
1153 old_cb_data = source->callback_data;
1154 old_cb_funcs = source->callback_funcs;
1156 source->callback_data = callback_data;
1157 source->callback_funcs = callback_funcs;
1160 UNLOCK_CONTEXT (context);
1163 old_cb_funcs->unref (old_cb_data);
1167 g_source_callback_ref (gpointer cb_data)
1169 GSourceCallback *callback = cb_data;
1171 callback->ref_count++;
1176 g_source_callback_unref (gpointer cb_data)
1178 GSourceCallback *callback = cb_data;
1180 callback->ref_count--;
1181 if (callback->ref_count == 0)
1183 if (callback->notify)
1184 callback->notify (callback->data);
1190 g_source_callback_get (gpointer cb_data,
1195 GSourceCallback *callback = cb_data;
1197 *func = callback->func;
1198 *data = callback->data;
1201 static GSourceCallbackFuncs g_source_callback_funcs = {
1202 g_source_callback_ref,
1203 g_source_callback_unref,
1204 g_source_callback_get,
1208 * g_source_set_callback:
1209 * @source: the source
1210 * @func: a callback function
1211 * @data: the data to pass to callback function
1212 * @notify: a function to call when @data is no longer in use, or %NULL.
1214 * Sets the callback function for a source. The callback for a source is
1215 * called from the source's dispatch function.
1217 * The exact type of @func depends on the type of source; ie. you
1218 * should not count on @func being called with @data as its first
1221 * Typically, you won't use this function. Instead use functions specific
1222 * to the type of source you are using.
1225 g_source_set_callback (GSource *source,
1228 GDestroyNotify notify)
1230 GSourceCallback *new_callback;
1232 g_return_if_fail (source != NULL);
1234 new_callback = g_new (GSourceCallback, 1);
1236 new_callback->ref_count = 1;
1237 new_callback->func = func;
1238 new_callback->data = data;
1239 new_callback->notify = notify;
1241 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1246 * g_source_set_funcs:
1247 * @source: a #GSource
1248 * @funcs: the new #GSourceFuncs
1250 * Sets the source functions (can be used to override
1251 * default implementations) of an unattached source.
1256 g_source_set_funcs (GSource *source,
1257 GSourceFuncs *funcs)
1259 g_return_if_fail (source != NULL);
1260 g_return_if_fail (source->context == NULL);
1261 g_return_if_fail (source->ref_count > 0);
1262 g_return_if_fail (funcs != NULL);
1264 source->source_funcs = funcs;
1268 * g_source_set_priority:
1269 * @source: a #GSource
1270 * @priority: the new priority.
1272 * Sets the priority of a source. While the main loop is being
1273 * run, a source will be dispatched if it is ready to be dispatched and no sources
1274 * at a higher (numerically smaller) priority are ready to be dispatched.
1277 g_source_set_priority (GSource *source,
1281 GMainContext *context;
1283 g_return_if_fail (source != NULL);
1285 context = source->context;
1288 LOCK_CONTEXT (context);
1290 source->priority = priority;
1294 /* Remove the source from the context's source and then
1295 * add it back so it is sorted in the correct plcae
1297 g_source_list_remove (source, source->context);
1298 g_source_list_add (source, source->context);
1300 if (!SOURCE_BLOCKED (source))
1302 tmp_list = source->poll_fds;
1305 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1306 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1308 tmp_list = tmp_list->next;
1312 UNLOCK_CONTEXT (source->context);
1317 * g_source_get_priority:
1318 * @source: a #GSource
1320 * Gets the priority of a source.
1322 * Return value: the priority of the source
1325 g_source_get_priority (GSource *source)
1327 g_return_val_if_fail (source != NULL, 0);
1329 return source->priority;
1333 * g_source_set_can_recurse:
1334 * @source: a #GSource
1335 * @can_recurse: whether recursion is allowed for this source
1337 * Sets whether a source can be called recursively. If @can_recurse is
1338 * %TRUE, then while the source is being dispatched then this source
1339 * will be processed normally. Otherwise, all processing of this
1340 * source is blocked until the dispatch function returns.
1343 g_source_set_can_recurse (GSource *source,
1344 gboolean can_recurse)
1346 GMainContext *context;
1348 g_return_if_fail (source != NULL);
1350 context = source->context;
1353 LOCK_CONTEXT (context);
1356 source->flags |= G_SOURCE_CAN_RECURSE;
1358 source->flags &= ~G_SOURCE_CAN_RECURSE;
1361 UNLOCK_CONTEXT (context);
1365 * g_source_get_can_recurse:
1366 * @source: a #GSource
1368 * Checks whether a source is allowed to be called recursively.
1369 * see g_source_set_can_recurse().
1371 * Return value: whether recursion is allowed.
1374 g_source_get_can_recurse (GSource *source)
1376 g_return_val_if_fail (source != NULL, FALSE);
1378 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1383 * @source: a #GSource
1385 * Increases the reference count on a source by one.
1387 * Return value: @source
1390 g_source_ref (GSource *source)
1392 GMainContext *context;
1394 g_return_val_if_fail (source != NULL, NULL);
1396 context = source->context;
1399 LOCK_CONTEXT (context);
1401 source->ref_count++;
1404 UNLOCK_CONTEXT (context);
1409 /* g_source_unref() but possible to call within context lock
1412 g_source_unref_internal (GSource *source,
1413 GMainContext *context,
1416 gpointer old_cb_data = NULL;
1417 GSourceCallbackFuncs *old_cb_funcs = NULL;
1419 g_return_if_fail (source != NULL);
1421 if (!have_lock && context)
1422 LOCK_CONTEXT (context);
1424 source->ref_count--;
1425 if (source->ref_count == 0)
1427 old_cb_data = source->callback_data;
1428 old_cb_funcs = source->callback_funcs;
1430 source->callback_data = NULL;
1431 source->callback_funcs = NULL;
1433 if (context && !SOURCE_DESTROYED (source))
1435 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1436 source->ref_count++;
1439 g_source_list_remove (source, context);
1441 if (source->source_funcs->finalize)
1442 source->source_funcs->finalize (source);
1444 g_slist_free (source->poll_fds);
1445 source->poll_fds = NULL;
1449 if (!have_lock && context)
1450 UNLOCK_CONTEXT (context);
1455 UNLOCK_CONTEXT (context);
1457 old_cb_funcs->unref (old_cb_data);
1460 LOCK_CONTEXT (context);
1466 * @source: a #GSource
1468 * Decreases the reference count of a source by one. If the
1469 * resulting reference count is zero the source and associated
1470 * memory will be destroyed.
1473 g_source_unref (GSource *source)
1475 g_return_if_fail (source != NULL);
1477 g_source_unref_internal (source, source->context, FALSE);
1481 * g_main_context_find_source_by_id:
1482 * @context: a #GMainContext (if %NULL, the default context will be used)
1483 * @source_id: the source ID, as returned by g_source_get_id().
1485 * Finds a #GSource given a pair of context and ID.
1487 * Return value: the #GSource if found, otherwise, %NULL
1490 g_main_context_find_source_by_id (GMainContext *context,
1495 g_return_val_if_fail (source_id > 0, NULL);
1497 if (context == NULL)
1498 context = g_main_context_default ();
1500 LOCK_CONTEXT (context);
1502 source = context->source_list;
1505 if (!SOURCE_DESTROYED (source) &&
1506 source->source_id == source_id)
1508 source = source->next;
1511 UNLOCK_CONTEXT (context);
1517 * g_main_context_find_source_by_funcs_user_data:
1518 * @context: a #GMainContext (if %NULL, the default context will be used).
1519 * @funcs: the @source_funcs passed to g_source_new().
1520 * @user_data: the user data from the callback.
1522 * Finds a source with the given source functions and user data. If
1523 * multiple sources exist with the same source function and user data,
1524 * the first one found will be returned.
1526 * Return value: the source, if one was found, otherwise %NULL
1529 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1530 GSourceFuncs *funcs,
1535 g_return_val_if_fail (funcs != NULL, NULL);
1537 if (context == NULL)
1538 context = g_main_context_default ();
1540 LOCK_CONTEXT (context);
1542 source = context->source_list;
1545 if (!SOURCE_DESTROYED (source) &&
1546 source->source_funcs == funcs &&
1547 source->callback_funcs)
1549 GSourceFunc callback;
1550 gpointer callback_data;
1552 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1554 if (callback_data == user_data)
1557 source = source->next;
1560 UNLOCK_CONTEXT (context);
1566 * g_main_context_find_source_by_user_data:
1567 * @context: a #GMainContext
1568 * @user_data: the user_data for the callback.
1570 * Finds a source with the given user data for the callback. If
1571 * multiple sources exist with the same user data, the first
1572 * one found will be returned.
1574 * Return value: the source, if one was found, otherwise %NULL
1577 g_main_context_find_source_by_user_data (GMainContext *context,
1582 if (context == NULL)
1583 context = g_main_context_default ();
1585 LOCK_CONTEXT (context);
1587 source = context->source_list;
1590 if (!SOURCE_DESTROYED (source) &&
1591 source->callback_funcs)
1593 GSourceFunc callback;
1594 gpointer callback_data = NULL;
1596 source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1598 if (callback_data == user_data)
1601 source = source->next;
1604 UNLOCK_CONTEXT (context);
1611 * @tag: the ID of the source to remove.
1613 * Removes the source with the given id from the default main context.
1615 * a #GSource is given by g_source_get_id(), or will be returned by the
1616 * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1617 * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1618 * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1620 * See also g_source_destroy().
1622 * Return value: %TRUE if the source was found and removed.
1625 g_source_remove (guint tag)
1629 g_return_val_if_fail (tag > 0, FALSE);
1631 source = g_main_context_find_source_by_id (NULL, tag);
1633 g_source_destroy (source);
1635 return source != NULL;
1639 * g_source_remove_by_user_data:
1640 * @user_data: the user_data for the callback.
1642 * Removes a source from the default main loop context given the user
1643 * data for the callback. If multiple sources exist with the same user
1644 * data, only one will be destroyed.
1646 * Return value: %TRUE if a source was found and removed.
1649 g_source_remove_by_user_data (gpointer user_data)
1653 source = g_main_context_find_source_by_user_data (NULL, user_data);
1656 g_source_destroy (source);
1664 * g_source_remove_by_funcs_user_data:
1665 * @funcs: The @source_funcs passed to g_source_new()
1666 * @user_data: the user data for the callback
1668 * Removes a source from the default main loop context given the
1669 * source functions and user data. If multiple sources exist with the
1670 * same source functions and user data, only one will be destroyed.
1672 * Return value: %TRUE if a source was found and removed.
1675 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1680 g_return_val_if_fail (funcs != NULL, FALSE);
1682 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1685 g_source_destroy (source);
1693 * g_get_current_time:
1694 * @result: #GTimeVal structure in which to store current time.
1696 * Equivalent to the UNIX gettimeofday() function, but portable.
1699 g_get_current_time (GTimeVal *result)
1704 g_return_if_fail (result != NULL);
1706 /*this is required on alpha, there the timeval structs are int's
1707 not longs and a cast only would fail horribly*/
1708 gettimeofday (&r, NULL);
1709 result->tv_sec = r.tv_sec;
1710 result->tv_usec = r.tv_usec;
1713 guint64 *time64 = (guint64 *) &ft;
1715 GetSystemTimeAsFileTime (&ft);
1717 /* Convert from 100s of nanoseconds since 1601-01-01
1718 * to Unix epoch. Yes, this is Y2038 unsafe.
1720 *time64 -= G_GINT64_CONSTANT (116444736000000000);
1723 result->tv_sec = *time64 / 1000000;
1724 result->tv_usec = *time64 % 1000000;
1729 g_main_dispatch_free (gpointer dispatch)
1731 g_slice_free (GMainDispatch, dispatch);
1734 /* Running the main loop */
1736 static GMainDispatch *
1739 static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1740 GMainDispatch *dispatch = g_static_private_get (&depth_private);
1743 dispatch = g_slice_new0 (GMainDispatch);
1744 g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1753 * Returns the depth of the stack of calls to
1754 * g_main_context_dispatch() on any #GMainContext in the current thread.
1755 * That is, when called from the toplevel, it gives 0. When
1756 * called from within a callback from g_main_context_iteration()
1757 * (or g_main_loop_run(), etc.) it returns 1. When called from within
1758 * a callback to a recursive call to g_main_context_iterate(),
1759 * it returns 2. And so forth.
1761 * This function is useful in a situation like the following:
1762 * Imagine an extremely simple "garbage collected" system.
1765 * static GList *free_list;
1768 * allocate_memory (gsize size)
1770 * gpointer result = g_malloc (size);
1771 * free_list = g_list_prepend (free_list, result);
1776 * free_allocated_memory (void)
1779 * for (l = free_list; l; l = l->next);
1781 * g_list_free (free_list);
1789 * g_main_context_iteration (NULL, TRUE);
1790 * free_allocated_memory();
1794 * This works from an application, however, if you want to do the same
1795 * thing from a library, it gets more difficult, since you no longer
1796 * control the main loop. You might think you can simply use an idle
1797 * function to make the call to free_allocated_memory(), but that
1798 * doesn't work, since the idle function could be called from a
1799 * recursive callback. This can be fixed by using g_main_depth()
1803 * allocate_memory (gsize size)
1805 * FreeListBlock *block = g_new (FreeListBlock, 1);
1806 * block->mem = g_malloc (size);
1807 * block->depth = g_main_depth ();
1808 * free_list = g_list_prepend (free_list, block);
1809 * return block->mem;
1813 * free_allocated_memory (void)
1817 * int depth = g_main_depth ();
1818 * for (l = free_list; l; );
1820 * GList *next = l->next;
1821 * FreeListBlock *block = l->data;
1822 * if (block->depth > depth)
1824 * g_free (block->mem);
1826 * free_list = g_list_delete_link (free_list, l);
1834 * There is a temptation to use g_main_depth() to solve
1835 * problems with reentrancy. For instance, while waiting for data
1836 * to be received from the network in response to a menu item,
1837 * the menu item might be selected again. It might seem that
1838 * one could make the menu item's callback return immediately
1839 * and do nothing if g_main_depth() returns a value greater than 1.
1840 * However, this should be avoided since the user then sees selecting
1841 * the menu item do nothing. Furthermore, you'll find yourself adding
1842 * these checks all over your code, since there are doubtless many,
1843 * many things that the user could do. Instead, you can use the
1844 * following techniques:
1849 * Use gtk_widget_set_sensitive() or modal dialogs to prevent
1850 * the user from interacting with elements while the main
1851 * loop is recursing.
1856 * Avoid main loop recursion in situations where you can't handle
1857 * arbitrary callbacks. Instead, structure your code so that you
1858 * simply return to the main loop and then get called again when
1859 * there is more work to do.
1864 * Return value: The main loop recursion level in the current thread
1869 GMainDispatch *dispatch = get_dispatch ();
1870 return dispatch->depth;
1874 * g_main_current_source:
1876 * Returns the currently firing source for this thread.
1878 * Return value: The currently firing source or %NULL.
1883 g_main_current_source (void)
1885 GMainDispatch *dispatch = get_dispatch ();
1886 return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
1890 * g_source_is_destroyed:
1891 * @source: a #GSource
1893 * Returns whether @source has been destroyed.
1895 * This is important when you operate upon your objects
1896 * from within idle handlers, but may have freed the object
1897 * before the dispatch of your idle handler.
1901 * idle_callback (gpointer data)
1903 * SomeWidget *self = data;
1905 * GDK_THREADS_ENTER (<!-- -->);
1906 * /<!-- -->* do stuff with self *<!-- -->/
1907 * self->idle_id = 0;
1908 * GDK_THREADS_LEAVE (<!-- -->);
1914 * some_widget_do_stuff_later (SomeWidget *self)
1916 * self->idle_id = g_idle_add (idle_callback, self);
1920 * some_widget_finalize (GObject *object)
1922 * SomeWidget *self = SOME_WIDGET (object);
1924 * if (self->idle_id)
1925 * g_source_remove (self->idle_id);
1927 * G_OBJECT_CLASS (parent_class)->finalize (object);
1931 * This will fail in a multi-threaded application if the
1932 * widget is destroyed before the idle handler fires due
1933 * to the use after free in the callback. A solution, to
1934 * this particular problem, is to check to if the source
1935 * has already been destroy within the callback.
1939 * idle_callback (gpointer data)
1941 * SomeWidget *self = data;
1943 * GDK_THREADS_ENTER ();
1944 * if (!g_source_is_destroyed (g_main_current_source ()))
1946 * /<!-- -->* do stuff with self *<!-- -->/
1948 * GDK_THREADS_LEAVE ();
1954 * Return value: %TRUE if the source has been destroyed
1959 g_source_is_destroyed (GSource *source)
1961 return SOURCE_DESTROYED (source);
1965 /* Temporarily remove all this source's file descriptors from the
1966 * poll(), so that if data comes available for one of the file descriptors
1967 * we don't continually spin in the poll()
1969 /* HOLDS: source->context's lock */
1971 block_source (GSource *source)
1975 g_return_if_fail (!SOURCE_BLOCKED (source));
1977 tmp_list = source->poll_fds;
1980 g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1981 tmp_list = tmp_list->next;
1985 /* HOLDS: source->context's lock */
1987 unblock_source (GSource *source)
1991 g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1992 g_return_if_fail (!SOURCE_DESTROYED (source));
1994 tmp_list = source->poll_fds;
1997 g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1998 tmp_list = tmp_list->next;
2002 /* HOLDS: context's lock */
2004 g_main_dispatch (GMainContext *context)
2006 GMainDispatch *current = get_dispatch ();
2009 for (i = 0; i < context->pending_dispatches->len; i++)
2011 GSource *source = context->pending_dispatches->pdata[i];
2013 context->pending_dispatches->pdata[i] = NULL;
2016 source->flags &= ~G_SOURCE_READY;
2018 if (!SOURCE_DESTROYED (source))
2020 gboolean was_in_call;
2021 gpointer user_data = NULL;
2022 GSourceFunc callback = NULL;
2023 GSourceCallbackFuncs *cb_funcs;
2025 gboolean need_destroy;
2027 gboolean (*dispatch) (GSource *,
2030 GSList current_source_link;
2032 dispatch = source->source_funcs->dispatch;
2033 cb_funcs = source->callback_funcs;
2034 cb_data = source->callback_data;
2037 cb_funcs->ref (cb_data);
2039 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
2040 block_source (source);
2042 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
2043 source->flags |= G_HOOK_FLAG_IN_CALL;
2046 cb_funcs->get (cb_data, source, &callback, &user_data);
2048 UNLOCK_CONTEXT (context);
2051 /* The on-stack allocation of the GSList is unconventional, but
2052 * we know that the lifetime of the link is bounded to this
2053 * function as the link is kept in a thread specific list and
2054 * not manipulated outside of this function and its descendants.
2055 * Avoiding the overhead of a g_slist_alloc() is useful as many
2056 * applications do little more than dispatch events.
2058 * This is a performance hack - do not revert to g_slist_prepend()!
2060 current_source_link.data = source;
2061 current_source_link.next = current->dispatching_sources;
2062 current->dispatching_sources = ¤t_source_link;
2063 need_destroy = ! dispatch (source,
2066 g_assert (current->dispatching_sources == ¤t_source_link);
2067 current->dispatching_sources = current_source_link.next;
2071 cb_funcs->unref (cb_data);
2073 LOCK_CONTEXT (context);
2076 source->flags &= ~G_HOOK_FLAG_IN_CALL;
2078 if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
2079 !SOURCE_DESTROYED (source))
2080 unblock_source (source);
2082 /* Note: this depends on the fact that we can't switch
2083 * sources from one main context to another
2085 if (need_destroy && !SOURCE_DESTROYED (source))
2087 g_assert (source->context == context);
2088 g_source_destroy_internal (source, context, TRUE);
2092 SOURCE_UNREF (source, context);
2095 g_ptr_array_set_size (context->pending_dispatches, 0);
2098 /* Holds context's lock */
2099 static inline GSource *
2100 next_valid_source (GMainContext *context,
2103 GSource *new_source = source ? source->next : context->source_list;
2107 if (!SOURCE_DESTROYED (new_source))
2109 new_source->ref_count++;
2113 new_source = new_source->next;
2117 SOURCE_UNREF (source, context);
2123 * g_main_context_acquire:
2124 * @context: a #GMainContext
2126 * Tries to become the owner of the specified context.
2127 * If some other thread is the owner of the context,
2128 * returns %FALSE immediately. Ownership is properly
2129 * recursive: the owner can require ownership again
2130 * and will release ownership when g_main_context_release()
2131 * is called as many times as g_main_context_acquire().
2133 * You must be the owner of a context before you
2134 * can call g_main_context_prepare(), g_main_context_query(),
2135 * g_main_context_check(), g_main_context_dispatch().
2137 * Return value: %TRUE if the operation succeeded, and
2138 * this thread is now the owner of @context.
2141 g_main_context_acquire (GMainContext *context)
2143 #ifdef G_THREADS_ENABLED
2144 gboolean result = FALSE;
2145 GThread *self = G_THREAD_SELF;
2147 if (context == NULL)
2148 context = g_main_context_default ();
2150 LOCK_CONTEXT (context);
2152 if (!context->owner)
2154 context->owner = self;
2155 g_assert (context->owner_count == 0);
2158 if (context->owner == self)
2160 context->owner_count++;
2164 UNLOCK_CONTEXT (context);
2167 #else /* !G_THREADS_ENABLED */
2169 #endif /* G_THREADS_ENABLED */
2173 * g_main_context_release:
2174 * @context: a #GMainContext
2176 * Releases ownership of a context previously acquired by this thread
2177 * with g_main_context_acquire(). If the context was acquired multiple
2178 * times, the ownership will be released only when g_main_context_release()
2179 * is called as many times as it was acquired.
2182 g_main_context_release (GMainContext *context)
2184 #ifdef G_THREADS_ENABLED
2185 if (context == NULL)
2186 context = g_main_context_default ();
2188 LOCK_CONTEXT (context);
2190 context->owner_count--;
2191 if (context->owner_count == 0)
2193 context->owner = NULL;
2195 if (context->waiters)
2197 GMainWaiter *waiter = context->waiters->data;
2198 gboolean loop_internal_waiter =
2199 (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
2200 context->waiters = g_slist_delete_link (context->waiters,
2202 if (!loop_internal_waiter)
2203 g_mutex_lock (waiter->mutex);
2205 g_cond_signal (waiter->cond);
2207 if (!loop_internal_waiter)
2208 g_mutex_unlock (waiter->mutex);
2212 UNLOCK_CONTEXT (context);
2213 #endif /* G_THREADS_ENABLED */
2217 * g_main_context_wait:
2218 * @context: a #GMainContext
2219 * @cond: a condition variable
2220 * @mutex: a mutex, currently held
2222 * Tries to become the owner of the specified context,
2223 * as with g_main_context_acquire(). But if another thread
2224 * is the owner, atomically drop @mutex and wait on @cond until
2225 * that owner releases ownership or until @cond is signaled, then
2226 * try again (once) to become the owner.
2228 * Return value: %TRUE if the operation succeeded, and
2229 * this thread is now the owner of @context.
2232 g_main_context_wait (GMainContext *context,
2236 #ifdef G_THREADS_ENABLED
2237 gboolean result = FALSE;
2238 GThread *self = G_THREAD_SELF;
2239 gboolean loop_internal_waiter;
2241 if (context == NULL)
2242 context = g_main_context_default ();
2244 loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
2246 if (!loop_internal_waiter)
2247 LOCK_CONTEXT (context);
2249 if (context->owner && context->owner != self)
2254 waiter.mutex = mutex;
2256 context->waiters = g_slist_append (context->waiters, &waiter);
2258 if (!loop_internal_waiter)
2259 UNLOCK_CONTEXT (context);
2260 g_cond_wait (cond, mutex);
2261 if (!loop_internal_waiter)
2262 LOCK_CONTEXT (context);
2264 context->waiters = g_slist_remove (context->waiters, &waiter);
2267 if (!context->owner)
2269 context->owner = self;
2270 g_assert (context->owner_count == 0);
2273 if (context->owner == self)
2275 context->owner_count++;
2279 if (!loop_internal_waiter)
2280 UNLOCK_CONTEXT (context);
2283 #else /* !G_THREADS_ENABLED */
2285 #endif /* G_THREADS_ENABLED */
2289 * g_main_context_prepare:
2290 * @context: a #GMainContext
2291 * @priority: location to store priority of highest priority
2292 * source already ready.
2294 * Prepares to poll sources within a main loop. The resulting information
2295 * for polling is determined by calling g_main_context_query ().
2297 * Return value: %TRUE if some source is ready to be dispatched
2301 g_main_context_prepare (GMainContext *context,
2306 gint current_priority = G_MAXINT;
2309 if (context == NULL)
2310 context = g_main_context_default ();
2312 LOCK_CONTEXT (context);
2314 context->time_is_current = FALSE;
2316 if (context->in_check_or_prepare)
2318 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2319 "prepare() member.");
2320 UNLOCK_CONTEXT (context);
2324 #ifdef G_THREADS_ENABLED
2325 if (context->poll_waiting)
2327 g_warning("g_main_context_prepare(): main loop already active in another thread");
2328 UNLOCK_CONTEXT (context);
2332 context->poll_waiting = TRUE;
2333 #endif /* G_THREADS_ENABLED */
2336 /* If recursing, finish up current dispatch, before starting over */
2337 if (context->pending_dispatches)
2340 g_main_dispatch (context, ¤t_time);
2342 UNLOCK_CONTEXT (context);
2347 /* If recursing, clear list of pending dispatches */
2349 for (i = 0; i < context->pending_dispatches->len; i++)
2351 if (context->pending_dispatches->pdata[i])
2352 SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2354 g_ptr_array_set_size (context->pending_dispatches, 0);
2356 /* Prepare all sources */
2358 context->timeout = -1;
2360 source = next_valid_source (context, NULL);
2363 gint source_timeout = -1;
2365 if ((n_ready > 0) && (source->priority > current_priority))
2367 SOURCE_UNREF (source, context);
2370 if (SOURCE_BLOCKED (source))
2373 if (!(source->flags & G_SOURCE_READY))
2376 gboolean (*prepare) (GSource *source,
2379 prepare = source->source_funcs->prepare;
2380 context->in_check_or_prepare++;
2381 UNLOCK_CONTEXT (context);
2383 result = (*prepare) (source, &source_timeout);
2385 LOCK_CONTEXT (context);
2386 context->in_check_or_prepare--;
2389 source->flags |= G_SOURCE_READY;
2392 if (source->flags & G_SOURCE_READY)
2395 current_priority = source->priority;
2396 context->timeout = 0;
2399 if (source_timeout >= 0)
2401 if (context->timeout < 0)
2402 context->timeout = source_timeout;
2404 context->timeout = MIN (context->timeout, source_timeout);
2408 source = next_valid_source (context, source);
2411 UNLOCK_CONTEXT (context);
2414 *priority = current_priority;
2416 return (n_ready > 0);
2420 * g_main_context_query:
2421 * @context: a #GMainContext
2422 * @max_priority: maximum priority source to check
2423 * @timeout_: location to store timeout to be used in polling
2424 * @fds: location to store #GPollFD records that need to be polled.
2425 * @n_fds: length of @fds.
2427 * Determines information necessary to poll this main loop.
2429 * Return value: the number of records actually stored in @fds,
2430 * or, if more than @n_fds records need to be stored, the number
2431 * of records that need to be stored.
2434 g_main_context_query (GMainContext *context,
2443 LOCK_CONTEXT (context);
2445 pollrec = context->poll_records;
2447 while (pollrec && max_priority >= pollrec->priority)
2449 if (pollrec->fd->events)
2453 fds[n_poll].fd = pollrec->fd->fd;
2454 /* In direct contradiction to the Unix98 spec, IRIX runs into
2455 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2456 * flags in the events field of the pollfd while it should
2457 * just ignoring them. So we mask them out here.
2459 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2460 fds[n_poll].revents = 0;
2465 pollrec = pollrec->next;
2468 #ifdef G_THREADS_ENABLED
2469 context->poll_changed = FALSE;
2474 *timeout = context->timeout;
2476 context->time_is_current = FALSE;
2479 UNLOCK_CONTEXT (context);
2485 * g_main_context_check:
2486 * @context: a #GMainContext
2487 * @max_priority: the maximum numerical priority of sources to check
2488 * @fds: array of #GPollFD's that was passed to the last call to
2489 * g_main_context_query()
2490 * @n_fds: return value of g_main_context_query()
2492 * Passes the results of polling back to the main loop.
2494 * Return value: %TRUE if some sources are ready to be dispatched.
2497 g_main_context_check (GMainContext *context,
2507 LOCK_CONTEXT (context);
2509 if (context->in_check_or_prepare)
2511 g_warning ("g_main_context_check() called recursively from within a source's check() or "
2512 "prepare() member.");
2513 UNLOCK_CONTEXT (context);
2517 #ifdef G_THREADS_ENABLED
2518 if (!context->poll_waiting)
2522 read (context->wake_up_pipe[0], &a, 1);
2526 context->poll_waiting = FALSE;
2528 /* If the set of poll file descriptors changed, bail out
2529 * and let the main loop rerun
2531 if (context->poll_changed)
2533 UNLOCK_CONTEXT (context);
2536 #endif /* G_THREADS_ENABLED */
2538 pollrec = context->poll_records;
2542 if (pollrec->fd->events)
2544 pollrec->fd->revents = fds[i].revents;
2547 pollrec = pollrec->next;
2550 source = next_valid_source (context, NULL);
2553 if ((n_ready > 0) && (source->priority > max_priority))
2555 SOURCE_UNREF (source, context);
2558 if (SOURCE_BLOCKED (source))
2561 if (!(source->flags & G_SOURCE_READY))
2564 gboolean (*check) (GSource *source);
2566 check = source->source_funcs->check;
2568 context->in_check_or_prepare++;
2569 UNLOCK_CONTEXT (context);
2571 result = (*check) (source);
2573 LOCK_CONTEXT (context);
2574 context->in_check_or_prepare--;
2577 source->flags |= G_SOURCE_READY;
2580 if (source->flags & G_SOURCE_READY)
2582 source->ref_count++;
2583 g_ptr_array_add (context->pending_dispatches, source);
2587 /* never dispatch sources with less priority than the first
2588 * one we choose to dispatch
2590 max_priority = source->priority;
2594 source = next_valid_source (context, source);
2597 UNLOCK_CONTEXT (context);
2603 * g_main_context_dispatch:
2604 * @context: a #GMainContext
2606 * Dispatches all pending sources.
2609 g_main_context_dispatch (GMainContext *context)
2611 LOCK_CONTEXT (context);
2613 if (context->pending_dispatches->len > 0)
2615 g_main_dispatch (context);
2618 UNLOCK_CONTEXT (context);
2621 /* HOLDS context lock */
2623 g_main_context_iterate (GMainContext *context,
2630 gboolean some_ready;
2631 gint nfds, allocated_nfds;
2632 GPollFD *fds = NULL;
2634 UNLOCK_CONTEXT (context);
2636 #ifdef G_THREADS_ENABLED
2637 if (!g_main_context_acquire (context))
2639 gboolean got_ownership;
2641 g_return_val_if_fail (g_thread_supported (), FALSE);
2646 LOCK_CONTEXT (context);
2649 context->cond = g_cond_new ();
2651 got_ownership = g_main_context_wait (context,
2653 g_static_mutex_get_mutex (&context->mutex));
2657 UNLOCK_CONTEXT (context);
2662 LOCK_CONTEXT (context);
2663 #endif /* G_THREADS_ENABLED */
2665 if (!context->cached_poll_array)
2667 context->cached_poll_array_size = context->n_poll_records;
2668 context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2671 allocated_nfds = context->cached_poll_array_size;
2672 fds = context->cached_poll_array;
2674 UNLOCK_CONTEXT (context);
2676 g_main_context_prepare (context, &max_priority);
2678 while ((nfds = g_main_context_query (context, max_priority, &timeout, fds,
2679 allocated_nfds)) > allocated_nfds)
2681 LOCK_CONTEXT (context);
2683 context->cached_poll_array_size = allocated_nfds = nfds;
2684 context->cached_poll_array = fds = g_new (GPollFD, nfds);
2685 UNLOCK_CONTEXT (context);
2691 g_main_context_poll (context, timeout, max_priority, fds, nfds);
2693 some_ready = g_main_context_check (context, max_priority, fds, nfds);
2696 g_main_context_dispatch (context);
2698 #ifdef G_THREADS_ENABLED
2699 g_main_context_release (context);
2700 #endif /* G_THREADS_ENABLED */
2702 LOCK_CONTEXT (context);
2708 * g_main_context_pending:
2709 * @context: a #GMainContext (if %NULL, the default context will be used)
2711 * Checks if any sources have pending events for the given context.
2713 * Return value: %TRUE if events are pending.
2716 g_main_context_pending (GMainContext *context)
2721 context = g_main_context_default();
2723 LOCK_CONTEXT (context);
2724 retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2725 UNLOCK_CONTEXT (context);
2731 * g_main_context_iteration:
2732 * @context: a #GMainContext (if %NULL, the default context will be used)
2733 * @may_block: whether the call may block.
2735 * Runs a single iteration for the given main loop. This involves
2736 * checking to see if any event sources are ready to be processed,
2737 * then if no events sources are ready and @may_block is %TRUE, waiting
2738 * for a source to become ready, then dispatching the highest priority
2739 * events sources that are ready. Otherwise, if @may_block is %FALSE
2740 * sources are not waited to become ready, only those highest priority
2741 * events sources will be dispatched (if any), that are ready at this
2742 * given moment without further waiting.
2744 * Note that even when @may_block is %TRUE, it is still possible for
2745 * g_main_context_iteration() to return %FALSE, since the the wait may
2746 * be interrupted for other reasons than an event source becoming ready.
2748 * Return value: %TRUE if events were dispatched.
2751 g_main_context_iteration (GMainContext *context, gboolean may_block)
2756 context = g_main_context_default();
2758 LOCK_CONTEXT (context);
2759 retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2760 UNLOCK_CONTEXT (context);
2767 * @context: a #GMainContext (if %NULL, the default context will be used).
2768 * @is_running: set to %TRUE to indicate that the loop is running. This
2769 * is not very important since calling g_main_loop_run() will set this to
2772 * Creates a new #GMainLoop structure.
2774 * Return value: a new #GMainLoop.
2777 g_main_loop_new (GMainContext *context,
2778 gboolean is_running)
2781 #ifdef G_MAIN_POLL_DEBUG
2782 static gboolean beenhere = FALSE;
2785 #ifdef G_MAIN_POLL_DEBUG
2791 if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
2792 g_main_poll_debug = TRUE;
2794 G_UNLOCK (main_loop);
2798 context = g_main_context_default();
2800 g_main_context_ref (context);
2802 loop = g_new0 (GMainLoop, 1);
2803 loop->context = context;
2804 loop->is_running = is_running != FALSE;
2805 loop->ref_count = 1;
2812 * @loop: a #GMainLoop
2814 * Increases the reference count on a #GMainLoop object by one.
2816 * Return value: @loop
2819 g_main_loop_ref (GMainLoop *loop)
2821 g_return_val_if_fail (loop != NULL, NULL);
2822 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2824 g_atomic_int_inc (&loop->ref_count);
2830 * g_main_loop_unref:
2831 * @loop: a #GMainLoop
2833 * Decreases the reference count on a #GMainLoop object by one. If
2834 * the result is zero, free the loop and free all associated memory.
2837 g_main_loop_unref (GMainLoop *loop)
2839 g_return_if_fail (loop != NULL);
2840 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2842 if (!g_atomic_int_dec_and_test (&loop->ref_count))
2845 g_main_context_unref (loop->context);
2851 * @loop: a #GMainLoop
2853 * Runs a main loop until g_main_loop_quit() is called on the loop.
2854 * If this is called for the thread of the loop's #GMainContext,
2855 * it will process events from the loop, otherwise it will
2859 g_main_loop_run (GMainLoop *loop)
2861 GThread *self = G_THREAD_SELF;
2863 g_return_if_fail (loop != NULL);
2864 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2866 #ifdef G_THREADS_ENABLED
2867 if (!g_main_context_acquire (loop->context))
2869 gboolean got_ownership = FALSE;
2871 /* Another thread owns this context */
2872 if (!g_thread_supported ())
2874 g_warning ("g_main_loop_run() was called from second thread but "
2875 "g_thread_init() was never called.");
2879 LOCK_CONTEXT (loop->context);
2881 g_atomic_int_inc (&loop->ref_count);
2883 if (!loop->is_running)
2884 loop->is_running = TRUE;
2886 if (!loop->context->cond)
2887 loop->context->cond = g_cond_new ();
2889 while (loop->is_running && !got_ownership)
2890 got_ownership = g_main_context_wait (loop->context,
2891 loop->context->cond,
2892 g_static_mutex_get_mutex (&loop->context->mutex));
2894 if (!loop->is_running)
2896 UNLOCK_CONTEXT (loop->context);
2898 g_main_context_release (loop->context);
2899 g_main_loop_unref (loop);
2903 g_assert (got_ownership);
2906 LOCK_CONTEXT (loop->context);
2907 #endif /* G_THREADS_ENABLED */
2909 if (loop->context->in_check_or_prepare)
2911 g_warning ("g_main_loop_run(): called recursively from within a source's "
2912 "check() or prepare() member, iteration not possible.");
2916 g_atomic_int_inc (&loop->ref_count);
2917 loop->is_running = TRUE;
2918 while (loop->is_running)
2919 g_main_context_iterate (loop->context, TRUE, TRUE, self);
2921 UNLOCK_CONTEXT (loop->context);
2923 #ifdef G_THREADS_ENABLED
2924 g_main_context_release (loop->context);
2925 #endif /* G_THREADS_ENABLED */
2927 g_main_loop_unref (loop);
2932 * @loop: a #GMainLoop
2934 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2935 * for the loop will return.
2937 * Note that sources that have already been dispatched when
2938 * g_main_loop_quit() is called will still be executed.
2941 g_main_loop_quit (GMainLoop *loop)
2943 g_return_if_fail (loop != NULL);
2944 g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2946 LOCK_CONTEXT (loop->context);
2947 loop->is_running = FALSE;
2948 g_main_context_wakeup_unlocked (loop->context);
2950 #ifdef G_THREADS_ENABLED
2951 if (loop->context->cond)
2952 g_cond_broadcast (loop->context->cond);
2953 #endif /* G_THREADS_ENABLED */
2955 UNLOCK_CONTEXT (loop->context);
2959 * g_main_loop_is_running:
2960 * @loop: a #GMainLoop.
2962 * Checks to see if the main loop is currently being run via g_main_loop_run().
2964 * Return value: %TRUE if the mainloop is currently being run.
2967 g_main_loop_is_running (GMainLoop *loop)
2969 g_return_val_if_fail (loop != NULL, FALSE);
2970 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2972 return loop->is_running;
2976 * g_main_loop_get_context:
2977 * @loop: a #GMainLoop.
2979 * Returns the #GMainContext of @loop.
2981 * Return value: the #GMainContext of @loop
2984 g_main_loop_get_context (GMainLoop *loop)
2986 g_return_val_if_fail (loop != NULL, NULL);
2987 g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2989 return loop->context;
2992 /* HOLDS: context's lock */
2994 g_main_context_poll (GMainContext *context,
3000 #ifdef G_MAIN_POLL_DEBUG
3006 GPollFunc poll_func;
3008 if (n_fds || timeout != 0)
3010 #ifdef G_MAIN_POLL_DEBUG
3011 if (g_main_poll_debug)
3013 g_print ("polling context=%p n=%d timeout=%d\n",
3014 context, n_fds, timeout);
3015 poll_timer = g_timer_new ();
3019 LOCK_CONTEXT (context);
3021 poll_func = context->poll_func;
3023 UNLOCK_CONTEXT (context);
3024 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
3027 g_warning ("poll(2) failed due to: %s.",
3028 g_strerror (errno));
3030 /* If g_poll () returns -1, it has already called g_warning() */
3034 #ifdef G_MAIN_POLL_DEBUG
3035 if (g_main_poll_debug)
3037 LOCK_CONTEXT (context);
3039 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
3042 g_timer_elapsed (poll_timer, NULL));
3043 g_timer_destroy (poll_timer);
3044 pollrec = context->poll_records;
3046 while (pollrec != NULL)
3051 if (fds[i].fd == pollrec->fd->fd &&
3052 pollrec->fd->events &&
3055 g_print (" [" GPOLLFD_FORMAT " :", fds[i].fd);
3056 if (fds[i].revents & G_IO_IN)
3058 if (fds[i].revents & G_IO_OUT)
3060 if (fds[i].revents & G_IO_PRI)
3062 if (fds[i].revents & G_IO_ERR)
3064 if (fds[i].revents & G_IO_HUP)
3066 if (fds[i].revents & G_IO_NVAL)
3072 pollrec = pollrec->next;
3076 UNLOCK_CONTEXT (context);
3079 } /* if (n_fds || timeout != 0) */
3083 * g_main_context_add_poll:
3084 * @context: a #GMainContext (or %NULL for the default context)
3085 * @fd: a #GPollFD structure holding information about a file
3086 * descriptor to watch.
3087 * @priority: the priority for this file descriptor which should be
3088 * the same as the priority used for g_source_attach() to ensure that the
3089 * file descriptor is polled whenever the results may be needed.
3091 * Adds a file descriptor to the set of file descriptors polled for
3092 * this context. This will very seldomly be used directly. Instead
3093 * a typical event source will use g_source_add_poll() instead.
3096 g_main_context_add_poll (GMainContext *context,
3101 context = g_main_context_default ();
3103 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3104 g_return_if_fail (fd);
3106 LOCK_CONTEXT (context);
3107 g_main_context_add_poll_unlocked (context, priority, fd);
3108 UNLOCK_CONTEXT (context);
3111 /* HOLDS: main_loop_lock */
3113 g_main_context_add_poll_unlocked (GMainContext *context,
3117 GPollRec *lastrec, *pollrec;
3118 GPollRec *newrec = g_slice_new (GPollRec);
3120 /* This file descriptor may be checked before we ever poll */
3123 newrec->priority = priority;
3126 pollrec = context->poll_records;
3127 while (pollrec && priority >= pollrec->priority)
3130 pollrec = pollrec->next;
3134 lastrec->next = newrec;
3136 context->poll_records = newrec;
3138 newrec->next = pollrec;
3140 context->n_poll_records++;
3142 #ifdef G_THREADS_ENABLED
3143 context->poll_changed = TRUE;
3145 /* Now wake up the main loop if it is waiting in the poll() */
3146 g_main_context_wakeup_unlocked (context);
3151 * g_main_context_remove_poll:
3152 * @context:a #GMainContext
3153 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
3155 * Removes file descriptor from the set of file descriptors to be
3156 * polled for a particular context.
3159 g_main_context_remove_poll (GMainContext *context,
3163 context = g_main_context_default ();
3165 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3166 g_return_if_fail (fd);
3168 LOCK_CONTEXT (context);
3169 g_main_context_remove_poll_unlocked (context, fd);
3170 UNLOCK_CONTEXT (context);
3174 g_main_context_remove_poll_unlocked (GMainContext *context,
3177 GPollRec *pollrec, *lastrec;
3180 pollrec = context->poll_records;
3184 if (pollrec->fd == fd)
3186 if (lastrec != NULL)
3187 lastrec->next = pollrec->next;
3189 context->poll_records = pollrec->next;
3191 g_slice_free (GPollRec, pollrec);
3193 context->n_poll_records--;
3197 pollrec = pollrec->next;
3200 #ifdef G_THREADS_ENABLED
3201 context->poll_changed = TRUE;
3203 /* Now wake up the main loop if it is waiting in the poll() */
3204 g_main_context_wakeup_unlocked (context);
3209 * g_source_get_current_time:
3210 * @source: a #GSource
3211 * @timeval: #GTimeVal structure in which to store current time.
3213 * Gets the "current time" to be used when checking
3214 * this source. The advantage of calling this function over
3215 * calling g_get_current_time() directly is that when
3216 * checking multiple sources, GLib can cache a single value
3217 * instead of having to repeatedly get the system time.
3220 g_source_get_current_time (GSource *source,
3223 GMainContext *context;
3225 g_return_if_fail (source->context != NULL);
3227 context = source->context;
3229 LOCK_CONTEXT (context);
3231 if (!context->time_is_current)
3233 g_get_current_time (&context->current_time);
3234 context->time_is_current = TRUE;
3237 *timeval = context->current_time;
3239 UNLOCK_CONTEXT (context);
3243 * g_main_context_set_poll_func:
3244 * @context: a #GMainContext
3245 * @func: the function to call to poll all file descriptors
3247 * Sets the function to use to handle polling of file descriptors. It
3248 * will be used instead of the poll() system call
3249 * (or GLib's replacement function, which is used where
3250 * poll() isn't available).
3252 * This function could possibly be used to integrate the GLib event
3253 * loop with an external event loop.
3256 g_main_context_set_poll_func (GMainContext *context,
3260 context = g_main_context_default ();
3262 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3264 LOCK_CONTEXT (context);
3267 context->poll_func = func;
3271 context->poll_func = (GPollFunc) poll;
3273 context->poll_func = (GPollFunc) g_poll;
3277 UNLOCK_CONTEXT (context);
3281 * g_main_context_get_poll_func:
3282 * @context: a #GMainContext
3284 * Gets the poll function set by g_main_context_set_poll_func().
3286 * Return value: the poll function
3289 g_main_context_get_poll_func (GMainContext *context)
3294 context = g_main_context_default ();
3296 g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3298 LOCK_CONTEXT (context);
3299 result = context->poll_func;
3300 UNLOCK_CONTEXT (context);
3305 /* HOLDS: context's lock */
3306 /* Wake the main loop up from a poll() */
3308 g_main_context_wakeup_unlocked (GMainContext *context)
3310 #ifdef G_THREADS_ENABLED
3311 if (g_thread_supported() && context->poll_waiting)
3313 context->poll_waiting = FALSE;
3315 write (context->wake_up_pipe[1], "A", 1);
3317 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3324 * g_main_context_wakeup:
3325 * @context: a #GMainContext
3327 * If @context is currently waiting in a poll(), interrupt
3328 * the poll(), and continue the iteration process.
3331 g_main_context_wakeup (GMainContext *context)
3334 context = g_main_context_default ();
3336 g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3338 LOCK_CONTEXT (context);
3339 g_main_context_wakeup_unlocked (context);
3340 UNLOCK_CONTEXT (context);
3344 * g_main_context_is_owner:
3345 * @context: a #GMainContext
3347 * Determines whether this thread holds the (recursive)
3348 * ownership of this #GMaincontext. This is useful to
3349 * know before waiting on another thread that may be
3350 * blocking to get ownership of @context.
3352 * Returns: %TRUE if current thread is owner of @context.
3357 g_main_context_is_owner (GMainContext *context)
3362 context = g_main_context_default ();
3364 #ifdef G_THREADS_ENABLED
3365 LOCK_CONTEXT (context);
3366 is_owner = context->owner == G_THREAD_SELF;
3367 UNLOCK_CONTEXT (context);
3378 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3379 GTimeVal *current_time)
3381 guint seconds = timeout_source->interval / 1000;
3382 guint msecs = timeout_source->interval - seconds * 1000;
3384 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3385 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3386 if (timeout_source->expiration.tv_usec >= 1000000)
3388 timeout_source->expiration.tv_usec -= 1000000;
3389 timeout_source->expiration.tv_sec++;
3391 if (timer_perturb==-1)
3394 * we want a per machine/session unique 'random' value; try the dbus
3395 * address first, that has a UUID in it. If there is no dbus, use the
3396 * hostname for hashing.
3398 const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3399 if (!session_bus_address)
3400 session_bus_address = g_getenv ("HOSTNAME");
3401 if (session_bus_address)
3402 timer_perturb = ABS ((gint) g_str_hash (session_bus_address));
3406 if (timeout_source->granularity)
3409 gint gran; /* in usecs */
3412 gran = timeout_source->granularity * 1000;
3413 perturb = timer_perturb % gran;
3415 * We want to give each machine a per machine pertubation;
3416 * shift time back first, and forward later after the rounding
3419 timeout_source->expiration.tv_usec -= perturb;
3420 if (timeout_source->expiration.tv_usec < 0)
3422 timeout_source->expiration.tv_usec += 1000000;
3423 timeout_source->expiration.tv_sec--;
3426 remainder = timeout_source->expiration.tv_usec % gran;
3427 if (remainder >= gran/4) /* round up */
3428 timeout_source->expiration.tv_usec += gran;
3429 timeout_source->expiration.tv_usec -= remainder;
3431 timeout_source->expiration.tv_usec += perturb;
3433 /* the rounding may have overflown tv_usec */
3434 while (timeout_source->expiration.tv_usec > 1000000)
3436 timeout_source->expiration.tv_usec -= 1000000;
3437 timeout_source->expiration.tv_sec++;
3443 g_timeout_prepare (GSource *source,
3448 GTimeVal current_time;
3450 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3452 g_source_get_current_time (source, ¤t_time);
3454 sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3455 msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3457 /* We do the following in a rather convoluted fashion to deal with
3458 * the fact that we don't have an integral type big enough to hold
3459 * the difference of two timevals in millseconds.
3461 if (sec < 0 || (sec == 0 && msec < 0))
3465 glong interval_sec = timeout_source->interval / 1000;
3466 glong interval_msec = timeout_source->interval % 1000;
3474 if (sec > interval_sec ||
3475 (sec == interval_sec && msec > interval_msec))
3477 /* The system time has been set backwards, so we
3478 * reset the expiration time to now + timeout_source->interval;
3479 * this at least avoids hanging for long periods of time.
3481 g_timeout_set_expiration (timeout_source, ¤t_time);
3482 msec = MIN (G_MAXINT, timeout_source->interval);
3486 msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3490 *timeout = (gint)msec;
3496 g_timeout_check (GSource *source)
3498 GTimeVal current_time;
3499 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3501 g_source_get_current_time (source, ¤t_time);
3503 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3504 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3505 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3509 g_timeout_dispatch (GSource *source,
3510 GSourceFunc callback,
3513 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3517 g_warning ("Timeout source dispatched without callback\n"
3518 "You must call g_source_set_callback().");
3522 if (callback (user_data))
3524 GTimeVal current_time;
3526 g_source_get_current_time (source, ¤t_time);
3527 g_timeout_set_expiration (timeout_source, ¤t_time);
3536 * g_timeout_source_new:
3537 * @interval: the timeout interval in milliseconds.
3539 * Creates a new timeout source.
3541 * The source will not initially be associated with any #GMainContext
3542 * and must be added to one with g_source_attach() before it will be
3545 * Return value: the newly-created timeout source
3548 g_timeout_source_new (guint interval)
3550 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3551 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3552 GTimeVal current_time;
3554 timeout_source->interval = interval;
3556 g_get_current_time (¤t_time);
3557 g_timeout_set_expiration (timeout_source, ¤t_time);
3563 * g_timeout_source_new_seconds:
3564 * @interval: the timeout interval in seconds
3566 * Creates a new timeout source.
3568 * The source will not initially be associated with any #GMainContext
3569 * and must be added to one with g_source_attach() before it will be
3572 * The scheduling granularity/accuracy of this timeout source will be
3575 * Return value: the newly-created timeout source
3580 g_timeout_source_new_seconds (guint interval)
3582 GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3583 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3584 GTimeVal current_time;
3586 timeout_source->interval = 1000*interval;
3587 timeout_source->granularity = 1000;
3589 g_get_current_time (¤t_time);
3590 g_timeout_set_expiration (timeout_source, ¤t_time);
3597 * g_timeout_add_full:
3598 * @priority: the priority of the timeout source. Typically this will be in
3599 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3600 * @interval: the time between calls to the function, in milliseconds
3601 * (1/1000ths of a second)
3602 * @function: function to call
3603 * @data: data to pass to @function
3604 * @notify: function to call when the timeout is removed, or %NULL
3606 * Sets a function to be called at regular intervals, with the given
3607 * priority. The function is called repeatedly until it returns
3608 * %FALSE, at which point the timeout is automatically destroyed and
3609 * the function will not be called again. The @notify function is
3610 * called when the timeout is destroyed. The first call to the
3611 * function will be at the end of the first @interval.
3613 * Note that timeout functions may be delayed, due to the processing of other
3614 * event sources. Thus they should not be relied on for precise timing.
3615 * After each call to the timeout function, the time of the next
3616 * timeout is recalculated based on the current time and the given interval
3617 * (it does not try to 'catch up' time lost in delays).
3619 * Return value: the ID (greater than 0) of the event source.
3622 g_timeout_add_full (gint priority,
3624 GSourceFunc function,
3626 GDestroyNotify notify)
3631 g_return_val_if_fail (function != NULL, 0);
3633 source = g_timeout_source_new (interval);
3635 if (priority != G_PRIORITY_DEFAULT)
3636 g_source_set_priority (source, priority);
3638 g_source_set_callback (source, function, data, notify);
3639 id = g_source_attach (source, NULL);
3640 g_source_unref (source);
3647 * @interval: the time between calls to the function, in milliseconds
3648 * (1/1000ths of a second)
3649 * @function: function to call
3650 * @data: data to pass to @function
3652 * Sets a function to be called at regular intervals, with the default
3653 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
3654 * until it returns %FALSE, at which point the timeout is automatically
3655 * destroyed and the function will not be called again. The first call
3656 * to the function will be at the end of the first @interval.
3658 * Note that timeout functions may be delayed, due to the processing of other
3659 * event sources. Thus they should not be relied on for precise timing.
3660 * After each call to the timeout function, the time of the next
3661 * timeout is recalculated based on the current time and the given interval
3662 * (it does not try to 'catch up' time lost in delays).
3664 * If you want to have a timer in the "seconds" range and do not care
3665 * about the exact time of the first call of the timer, use the
3666 * g_timeout_add_seconds() function; this function allows for more
3667 * optimizations and more efficient system power usage.
3669 * Return value: the ID (greater than 0) of the event source.
3672 g_timeout_add (guint32 interval,
3673 GSourceFunc function,
3676 return g_timeout_add_full (G_PRIORITY_DEFAULT,
3677 interval, function, data, NULL);
3681 * g_timeout_add_seconds_full:
3682 * @priority: the priority of the timeout source. Typically this will be in
3683 * the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3684 * @interval: the time between calls to the function, in seconds
3685 * @function: function to call
3686 * @data: data to pass to @function
3687 * @notify: function to call when the timeout is removed, or %NULL
3689 * Sets a function to be called at regular intervals, with @priority.
3690 * The function is called repeatedly until it returns %FALSE, at which
3691 * point the timeout is automatically destroyed and the function will
3692 * not be called again.
3694 * Unlike g_timeout_add(), this function operates at whole second granularity.
3695 * The initial starting point of the timer is determined by the implementation
3696 * and the implementation is expected to group multiple timers together so that
3697 * they fire all at the same time.
3698 * To allow this grouping, the @interval to the first timer is rounded
3699 * and can deviate up to one second from the specified interval.
3700 * Subsequent timer iterations will generally run at the specified interval.
3702 * Note that timeout functions may be delayed, due to the processing of other
3703 * event sources. Thus they should not be relied on for precise timing.
3704 * After each call to the timeout function, the time of the next
3705 * timeout is recalculated based on the current time and the given @interval
3707 * If you want timing more precise than whole seconds, use g_timeout_add()
3710 * The grouping of timers to fire at the same time results in a more power
3711 * and CPU efficient behavior so if your timer is in multiples of seconds
3712 * and you don't require the first timer exactly one second from now, the
3713 * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3715 * Return value: the ID (greater than 0) of the event source.
3720 g_timeout_add_seconds_full (gint priority,
3722 GSourceFunc function,
3724 GDestroyNotify notify)
3729 g_return_val_if_fail (function != NULL, 0);
3731 source = g_timeout_source_new_seconds (interval);
3733 if (priority != G_PRIORITY_DEFAULT)
3734 g_source_set_priority (source, priority);
3736 g_source_set_callback (source, function, data, notify);
3737 id = g_source_attach (source, NULL);
3738 g_source_unref (source);
3744 * g_timeout_add_seconds:
3745 * @interval: the time between calls to the function, in seconds
3746 * @function: function to call
3747 * @data: data to pass to @function
3749 * Sets a function to be called at regular intervals with the default
3750 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3751 * it returns %FALSE, at which point the timeout is automatically destroyed
3752 * and the function will not be called again.
3754 * See g_timeout_add_seconds_full() for the differences between
3755 * g_timeout_add() and g_timeout_add_seconds().
3757 * Return value: the ID (greater than 0) of the event source.
3762 g_timeout_add_seconds (guint interval,
3763 GSourceFunc function,
3766 g_return_val_if_fail (function != NULL, 0);
3768 return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3771 /* Child watch functions */
3776 g_child_watch_prepare (GSource *source,
3785 g_child_watch_check (GSource *source)
3787 GChildWatchSource *child_watch_source;
3788 gboolean child_exited;
3790 child_watch_source = (GChildWatchSource *) source;
3792 child_exited = child_watch_source->poll.revents & G_IO_IN;
3799 * Note: We do _not_ check for the special value of STILL_ACTIVE
3800 * since we know that the process has exited and doing so runs into
3801 * problems if the child process "happens to return STILL_ACTIVE(259)"
3802 * as Microsoft's Platform SDK puts it.
3804 if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3806 gchar *emsg = g_win32_error_message (GetLastError ());
3807 g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3810 child_watch_source->child_status = -1;
3813 child_watch_source->child_status = child_status;
3816 return child_exited;
3819 #else /* G_OS_WIN32 */
3822 check_for_child_exited (GSource *source)
3824 GChildWatchSource *child_watch_source;
3827 /* protect against another SIGCHLD in the middle of this call */
3828 count = child_watch_count;
3830 child_watch_source = (GChildWatchSource *) source;
3832 if (child_watch_source->child_exited)
3835 if (child_watch_source->count < count)
3839 if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3841 child_watch_source->child_status = child_status;
3842 child_watch_source->child_exited = TRUE;
3844 child_watch_source->count = count;
3847 return child_watch_source->child_exited;
3851 g_child_watch_prepare (GSource *source,
3856 return check_for_child_exited (source);
3861 g_child_watch_check (GSource *source)
3863 return check_for_child_exited (source);
3866 #endif /* G_OS_WIN32 */
3869 g_child_watch_dispatch (GSource *source,
3870 GSourceFunc callback,
3873 GChildWatchSource *child_watch_source;
3874 GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3876 child_watch_source = (GChildWatchSource *) source;
3880 g_warning ("Child watch source dispatched without callback\n"
3881 "You must call g_source_set_callback().");
3885 (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3887 /* We never keep a child watch source around as the child is gone */
3894 g_child_watch_signal_handler (int signum)
3896 child_watch_count ++;
3898 if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3900 write (child_watch_wake_up_pipe[1], "B", 1);
3904 /* We count on the signal interrupting the poll in the same thread.
3910 g_child_watch_source_init_single (void)
3912 struct sigaction action;
3914 g_assert (! g_thread_supported());
3915 g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3917 child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3919 action.sa_handler = g_child_watch_signal_handler;
3920 sigemptyset (&action.sa_mask);
3921 action.sa_flags = SA_NOCLDSTOP;
3922 sigaction (SIGCHLD, &action, NULL);
3926 child_watch_helper_thread (gpointer data)
3933 read (child_watch_wake_up_pipe[0], b, 20);
3935 /* We were woken up. Wake up all other contexts in all other threads */
3936 G_LOCK (main_context_list);
3937 for (list = main_context_list; list; list = list->next)
3939 GMainContext *context;
3941 context = list->data;
3942 if (g_atomic_int_get (&context->ref_count) > 0)
3943 /* Due to racing conditions we can find ref_count == 0, in
3944 * that case, however, the context is still not destroyed
3945 * and no poll can be active, otherwise the ref_count
3947 g_main_context_wakeup (context);
3949 G_UNLOCK (main_context_list);
3956 g_child_watch_source_init_multi_threaded (void)
3958 GError *error = NULL;
3959 struct sigaction action;
3961 g_assert (g_thread_supported());
3963 if (pipe (child_watch_wake_up_pipe) < 0)
3964 g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3965 fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3967 /* We create a helper thread that polls on the wakeup pipe indefinitely */
3968 /* FIXME: Think this through for races */
3969 if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3970 g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3971 child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3973 action.sa_handler = g_child_watch_signal_handler;
3974 sigemptyset (&action.sa_mask);
3975 action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3976 sigaction (SIGCHLD, &action, NULL);
3980 g_child_watch_source_init_promote_single_to_threaded (void)
3982 g_child_watch_source_init_multi_threaded ();
3986 g_child_watch_source_init (void)
3988 if (g_thread_supported())
3990 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3991 g_child_watch_source_init_multi_threaded ();
3992 else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3993 g_child_watch_source_init_promote_single_to_threaded ();
3997 if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3998 g_child_watch_source_init_single ();
4002 #endif /* !G_OS_WIN32 */
4005 * g_child_watch_source_new:
4006 * @pid: process to watch. On POSIX the pid of a child process. On
4007 * Windows a handle for a process (which doesn't have to be a child).
4009 * Creates a new child_watch source.
4011 * The source will not initially be associated with any #GMainContext
4012 * and must be added to one with g_source_attach() before it will be
4015 * Note that child watch sources can only be used in conjunction with
4016 * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
4019 * Note that on platforms where #GPid must be explicitly closed
4020 * (see g_spawn_close_pid()) @pid must not be closed while the
4021 * source is still active. Typically, you will want to call
4022 * g_spawn_close_pid() in the callback function for the source.
4024 * Note further that using g_child_watch_source_new() is not
4025 * compatible with calling <literal>waitpid(-1)</literal> in
4026 * the application. Calling waitpid() for individual pids will
4029 * Return value: the newly-created child watch source
4034 g_child_watch_source_new (GPid pid)
4036 GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
4037 GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
4040 child_watch_source->poll.fd = (int)pid;
4041 child_watch_source->poll.events = G_IO_IN;
4043 g_source_add_poll (source, &child_watch_source->poll);
4044 #else /* G_OS_WIN32 */
4045 g_child_watch_source_init ();
4046 #endif /* G_OS_WIN32 */
4048 child_watch_source->pid = pid;
4054 * g_child_watch_add_full:
4055 * @priority: the priority of the idle source. Typically this will be in the
4056 * range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4057 * @pid: process to watch. On POSIX the pid of a child process. On
4058 * Windows a handle for a process (which doesn't have to be a child).
4059 * @function: function to call
4060 * @data: data to pass to @function
4061 * @notify: function to call when the idle is removed, or %NULL
4063 * Sets a function to be called when the child indicated by @pid
4064 * exits, at the priority @priority.
4066 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4067 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4068 * the spawn function for the child watching to work.
4070 * Note that on platforms where #GPid must be explicitly closed
4071 * (see g_spawn_close_pid()) @pid must not be closed while the
4072 * source is still active. Typically, you will want to call
4073 * g_spawn_close_pid() in the callback function for the source.
4075 * GLib supports only a single callback per process id.
4077 * Return value: the ID (greater than 0) of the event source.
4082 g_child_watch_add_full (gint priority,
4084 GChildWatchFunc function,
4086 GDestroyNotify notify)
4091 g_return_val_if_fail (function != NULL, 0);
4093 source = g_child_watch_source_new (pid);
4095 if (priority != G_PRIORITY_DEFAULT)
4096 g_source_set_priority (source, priority);
4098 g_source_set_callback (source, (GSourceFunc) function, data, notify);
4099 id = g_source_attach (source, NULL);
4100 g_source_unref (source);
4106 * g_child_watch_add:
4107 * @pid: process id to watch. On POSIX the pid of a child process. On
4108 * Windows a handle for a process (which doesn't have to be a child).
4109 * @function: function to call
4110 * @data: data to pass to @function
4112 * Sets a function to be called when the child indicated by @pid
4113 * exits, at a default priority, #G_PRIORITY_DEFAULT.
4115 * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes()
4116 * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to
4117 * the spawn function for the child watching to work.
4119 * Note that on platforms where #GPid must be explicitly closed
4120 * (see g_spawn_close_pid()) @pid must not be closed while the
4121 * source is still active. Typically, you will want to call
4122 * g_spawn_close_pid() in the callback function for the source.
4124 * GLib supports only a single callback per process id.
4126 * Return value: the ID (greater than 0) of the event source.
4131 g_child_watch_add (GPid pid,
4132 GChildWatchFunc function,
4135 return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
4139 /* Idle functions */
4142 g_idle_prepare (GSource *source,
4151 g_idle_check (GSource *source)
4157 g_idle_dispatch (GSource *source,
4158 GSourceFunc callback,
4163 g_warning ("Idle source dispatched without callback\n"
4164 "You must call g_source_set_callback().");
4168 return callback (user_data);
4172 * g_idle_source_new:
4174 * Creates a new idle source.
4176 * The source will not initially be associated with any #GMainContext
4177 * and must be added to one with g_source_attach() before it will be
4178 * executed. Note that the default priority for idle sources is
4179 * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
4180 * have a default priority of %G_PRIORITY_DEFAULT.
4182 * Return value: the newly-created idle source
4185 g_idle_source_new (void)
4189 source = g_source_new (&g_idle_funcs, sizeof (GSource));
4190 g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
4197 * @priority: the priority of the idle source. Typically this will be in the
4198 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
4199 * @function: function to call
4200 * @data: data to pass to @function
4201 * @notify: function to call when the idle is removed, or %NULL
4203 * Adds a function to be called whenever there are no higher priority
4204 * events pending. If the function returns %FALSE it is automatically
4205 * removed from the list of event sources and will not be called again.
4207 * Return value: the ID (greater than 0) of the event source.
4210 g_idle_add_full (gint priority,
4211 GSourceFunc function,
4213 GDestroyNotify notify)
4218 g_return_val_if_fail (function != NULL, 0);
4220 source = g_idle_source_new ();
4222 if (priority != G_PRIORITY_DEFAULT_IDLE)
4223 g_source_set_priority (source, priority);
4225 g_source_set_callback (source, function, data, notify);
4226 id = g_source_attach (source, NULL);
4227 g_source_unref (source);
4234 * @function: function to call
4235 * @data: data to pass to @function.
4237 * Adds a function to be called whenever there are no higher priority
4238 * events pending to the default main loop. The function is given the
4239 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
4240 * returns %FALSE it is automatically removed from the list of event
4241 * sources and will not be called again.
4243 * Return value: the ID (greater than 0) of the event source.
4246 g_idle_add (GSourceFunc function,
4249 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4253 * g_idle_remove_by_data:
4254 * @data: the data for the idle source's callback.
4256 * Removes the idle function with the given data.
4258 * Return value: %TRUE if an idle source was found and removed.
4261 g_idle_remove_by_data (gpointer data)
4263 return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4266 #define __G_MAIN_C__
4267 #include "galiasdef.c"