1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * gmain.c: Main loop abstraction, timeouts, and idle functions
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 /* uncomment the next line to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
40 #include <sys/types.h>
42 #ifdef HAVE_SYS_TIME_H
44 #endif /* HAVE_SYS_TIME_H */
45 #ifdef GLIB_HAVE_SYS_POLL_H
46 # include <sys/poll.h>
47 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
48 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
49 #endif /* GLIB_HAVE_SYS_POLL_H */
52 #endif /* HAVE_UNISTD_H */
58 #endif /* G_OS_WIN32 */
61 #include <net/socket.h>
62 #endif /* G_OS_BEOS */
66 typedef struct _GTimeoutSource GTimeoutSource;
67 typedef struct _GPollRec GPollRec;
68 typedef struct _GSourceCallback GSourceCallback;
72 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
73 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
78 #ifdef G_THREADS_ENABLED
79 /* The following lock is used for both the list of sources
80 * and the list of poll records
86 GPtrArray *pending_dispatches;
87 gint timeout; /* Timeout for current iteration */
91 gint in_check_or_prepare;
93 GPollRec *poll_records;
94 GPollRec *poll_free_list;
95 GMemChunk *poll_chunk;
97 GPollFD *cached_poll_array;
98 gint cached_poll_array_size;
100 #ifdef G_THREADS_ENABLED
102 /* this pipe is used to wake up the main loop when a source is added.
104 gint wake_up_pipe[2];
105 #else /* G_OS_WIN32 */
106 HANDLE wake_up_semaphore;
107 #endif /* G_OS_WIN32 */
110 gboolean poll_waiting;
112 /* Flag indicating whether the set of fd's changed during a poll */
113 gboolean poll_changed;
114 #endif /* G_THREADS_ENABLED */
118 GTimeVal current_time;
119 gboolean time_is_current;
122 struct _GSourceCallback
127 GDestroyNotify notify;
132 GMainContext *context;
136 #ifdef G_THREADS_ENABLED
139 #endif /* G_THREADS_ENABLED */
142 struct _GTimeoutSource
156 #ifdef G_THREADS_ENABLED
157 #define LOCK_CONTEXT(context) g_mutex_lock(context->mutex)
158 #define UNLOCK_CONTEXT(context) g_mutex_unlock(context->mutex)
159 #define LOCK_LOOP(loop) g_mutex_lock(loop->mutex)
160 #define UNLOCK_LOOP(loop) g_mutex_unlock(loop->mutex)
162 #define LOCK_CONTEXT(context) (void)0
163 #define UNLOCK_CONTEXT(context) (void)0
164 #define LOCK_LOOP(context) (void)0
165 #define UNLOCK_LOOP(context) (void)0
168 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
170 #define SOURCE_UNREF(source, context) \
172 if ((source)->ref_count > 1) \
173 (source)->ref_count--; \
175 g_source_unref_internal ((source), (context), TRUE); \
179 /* Forward declarations */
181 static void g_source_unref_internal (GSource *source,
182 GMainContext *context,
184 static void g_source_destroy_internal (GSource *source,
185 GMainContext *context,
187 static void g_main_context_poll (GMainContext *context,
192 static void g_main_context_add_poll_unlocked (GMainContext *context,
195 static void g_main_context_remove_poll_unlocked (GMainContext *context,
197 static void g_main_context_wakeup (GMainContext *context);
199 static gboolean g_timeout_prepare (GSource *source,
201 static gboolean g_timeout_check (GSource *source);
202 static gboolean g_timeout_dispatch (GSource *source,
203 GSourceFunc callback,
205 static gboolean g_idle_prepare (GSource *source,
207 static gboolean g_idle_check (GSource *source);
208 static gboolean g_idle_dispatch (GSource *source,
209 GSourceFunc callback,
212 G_LOCK_DEFINE_STATIC (main_loop);
213 static GMainContext *default_main_context;
215 static GSourceFuncs timeout_funcs =
223 static GSourceFuncs idle_funcs =
232 /* SunOS has poll, but doesn't provide a prototype. */
233 # if defined (sun) && !defined (__SVR4)
234 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
236 #else /* !HAVE_POLL */
241 g_poll (GPollFD *fds,
245 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
246 gboolean poll_msgs = FALSE;
253 for (f = fds; f < &fds[nfds]; ++f)
256 if (f->events & G_IO_IN)
258 if (f->fd == G_WIN32_MSG_HANDLE)
262 #ifdef G_MAIN_POLL_DEBUG
263 g_print ("g_poll: waiting for %#x\n", f->fd);
265 handles[nhandles++] = (HANDLE) f->fd;
275 /* Waiting for messages, and maybe events */
278 if (timeout == INFINITE)
280 /* Waiting just for messages, infinite timeout
281 * -> Use PeekMessage, then WaitMessage
283 #ifdef G_MAIN_POLL_DEBUG
284 g_print ("PeekMessage, then WaitMessage\n");
286 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
287 ready = WAIT_OBJECT_0;
288 else if (!WaitMessage ())
289 g_warning ("g_poll: WaitMessage failed");
290 ready = WAIT_OBJECT_0;
292 else if (timeout == 0)
294 /* Waiting just for messages, zero timeout
297 #ifdef G_MAIN_POLL_DEBUG
298 g_print ("PeekMessage\n");
300 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
301 ready = WAIT_OBJECT_0;
303 ready = WAIT_TIMEOUT;
307 /* Waiting just for messages, some timeout
308 * -> First try PeekMessage, then set a timer, wait for message,
309 * kill timer, use PeekMessage
311 #ifdef G_MAIN_POLL_DEBUG
312 g_print ("PeekMessage\n");
314 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
315 ready = WAIT_OBJECT_0;
316 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
317 g_warning ("g_poll: SetTimer failed");
320 #ifdef G_MAIN_POLL_DEBUG
321 g_print ("WaitMessage\n");
324 KillTimer (NULL, timer);
325 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
326 && msg.message != WM_TIMER)
327 ready = WAIT_OBJECT_0;
329 ready = WAIT_TIMEOUT;
335 /* Wait for either message or event
336 * -> Use MsgWaitForMultipleObjects
338 #ifdef G_MAIN_POLL_DEBUG
339 g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
341 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
342 timeout, QS_ALLINPUT);
344 if (ready == WAIT_FAILED)
345 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
348 else if (nhandles == 0)
350 /* Wait for nothing (huh?) */
355 /* Wait for just events
356 * -> Use WaitForMultipleObjects
358 #ifdef G_MAIN_POLL_DEBUG
359 g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
361 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
362 if (ready == WAIT_FAILED)
363 g_warning ("g_poll: WaitForMultipleObjects failed");
366 for (f = fds; f < &fds[nfds]; ++f)
369 if (ready == WAIT_FAILED)
371 else if (ready == WAIT_TIMEOUT)
373 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
375 for (f = fds; f < &fds[nfds]; ++f)
378 if (f->events & G_IO_IN)
379 if (f->fd == G_WIN32_MSG_HANDLE)
380 f->revents |= G_IO_IN;
383 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
384 for (f = fds; f < &fds[nfds]; ++f)
386 if ((f->events & G_IO_IN)
387 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
389 f->revents |= G_IO_IN;
390 #ifdef G_MAIN_POLL_DEBUG
391 g_print ("g_poll: got event %#x\n", f->fd);
394 ResetEvent ((HANDLE) f->fd);
399 if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
400 return ready - WAIT_OBJECT_0 + 1;
405 #else /* !G_OS_WIN32 */
407 /* The following implementation of poll() comes from the GNU C Library.
408 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
411 #include <string.h> /* for bzero on BSD systems */
413 #ifdef HAVE_SYS_SELECT_H
414 #include <sys/select.h>
415 #endif /* HAVE_SYS_SELECT_H */
419 #endif /* G_OS_BEOS */
422 # define SELECT_MASK fd_set
423 #else /* !NO_FD_SET */
425 typedef long fd_mask;
428 # define SELECT_MASK void
430 # define SELECT_MASK int
431 # endif /* !_IBMR2 */
432 #endif /* !NO_FD_SET */
435 g_poll (GPollFD *fds,
440 SELECT_MASK rset, wset, xset;
449 for (f = fds; f < &fds[nfds]; ++f)
452 if (f->events & G_IO_IN)
453 FD_SET (f->fd, &rset);
454 if (f->events & G_IO_OUT)
455 FD_SET (f->fd, &wset);
456 if (f->events & G_IO_PRI)
457 FD_SET (f->fd, &xset);
458 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
462 tv.tv_sec = timeout / 1000;
463 tv.tv_usec = (timeout % 1000) * 1000;
465 ready = select (maxfd + 1, &rset, &wset, &xset,
466 timeout == -1 ? NULL : &tv);
468 for (f = fds; f < &fds[nfds]; ++f)
473 if (FD_ISSET (f->fd, &rset))
474 f->revents |= G_IO_IN;
475 if (FD_ISSET (f->fd, &wset))
476 f->revents |= G_IO_OUT;
477 if (FD_ISSET (f->fd, &xset))
478 f->revents |= G_IO_PRI;
485 #endif /* !G_OS_WIN32 */
487 #endif /* !HAVE_POLL */
489 /* Called to clean up when a thread terminates
492 g_main_context_destroy (GMainContext *context)
496 /* We need the lock here only because g_source_destroy expects
497 * to be able to unlock when destroying the source's data
499 LOCK_CONTEXT (context);
500 source = context->source_list;
503 GSource *next = source->next;
504 g_source_destroy_internal (source, context, TRUE);
507 UNLOCK_CONTEXT (context);
509 #ifdef G_THREADS_ENABLED
510 g_mutex_free (context->mutex);
513 g_ptr_array_free (context->pending_dispatches, TRUE);
514 g_free (context->cached_poll_array);
516 g_mem_chunk_destroy (context->poll_chunk);
518 #ifdef G_THREADS_ENABLED
519 if (g_thread_supported())
522 close (context->wake_up_pipe[0]);
523 close (context->wake_up_pipe[1]);
525 CloseHandle (context->wake_up_semaphore);
534 * g_main_context_get:
535 * @thread: a #GThread
537 * Retrieves the main loop context for a particular thread. This
538 * will create the main context for the thread if none previously
539 * existed. The context will exist until the thread terminates.
541 * Return value: the main loop context for @thread.
544 g_main_context_get (GThread *thread)
546 static GStaticPrivate private_key = G_STATIC_PRIVATE_INIT;
547 GMainContext *context;
549 g_return_val_if_fail (thread != NULL, NULL);
551 if (g_thread_supported ())
552 context = g_static_private_get_for_thread (&private_key, thread);
554 context = default_main_context;
558 context = g_new0 (GMainContext, 1);
560 #ifdef G_THREADS_ENABLED
561 if (g_thread_supported ())
562 context->mutex = g_mutex_new();
564 context->thread = thread;
567 context->next_id = 1;
569 context->source_list = NULL;
572 context->poll_func = (GPollFunc)poll;
574 context->poll_func = g_poll;
577 context->cached_poll_array = NULL;
578 context->cached_poll_array_size = 0;
580 context->pending_dispatches = g_ptr_array_new ();
582 context->time_is_current = FALSE;
584 #ifdef G_THREADS_ENABLED
585 if (g_thread_supported ())
588 if (pipe (context->wake_up_pipe) < 0)
589 g_error ("Cannot create pipe main loop wake-up: %s\n",
592 context->wake_up_rec.fd = context->wake_up_pipe[0];
593 context->wake_up_rec.events = G_IO_IN;
594 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
596 if ((context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
597 g_error ("Cannot create wake-up semaphore: %s", g_win32_error_message (GetLastError ()));
598 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
599 context->wake_up_rec.events = G_IO_IN;
600 #ifdef G_MAIN_POLL_DEBUG
601 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
603 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
608 if (g_thread_supported ())
609 g_static_private_set_for_thread (&private_key, thread,
611 (GDestroyNotify)g_main_context_destroy);
613 default_main_context = context;
620 * g_main_context_default:
622 * Return the default main context. This is the main context used
623 * for main loop functions when a main loop is not explicitly
626 * Return value: the default main context.
629 g_main_context_default (void)
635 if (!default_main_context)
636 default_main_context = g_main_context_get (g_thread_self ());
638 G_UNLOCK (main_loop);
640 return default_main_context;
643 /* Hooks for adding to the main loop */
647 * @source_funcs: structure containing functions that implement
648 * the sources behavior.
649 * @struct_size: size of the #GSource structure to create
651 * Create a new GSource structure. The size is specified to
652 * allow creating structures derived from GSource that contain
653 * additional data. The size passed in must be at least
656 * The source will not initially be associated with any #GMainContext
657 * and must be added to one with g_source_add() before it will be
660 * Return value: the newly create #GSource
663 g_source_new (GSourceFuncs *source_funcs,
668 g_return_val_if_fail (source_funcs != NULL, NULL);
669 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
671 source = (GSource*) g_malloc0 (struct_size);
673 source->source_funcs = source_funcs;
674 source->ref_count = 1;
676 source->priority = G_PRIORITY_DEFAULT;
678 source->flags = G_HOOK_FLAG_ACTIVE;
680 /* NULL/0 initialization for all other fields */
685 /* Holds context's lock
688 g_source_list_add (GSource *source,
689 GMainContext *context)
691 GSource *tmp_source, *last_source;
694 tmp_source = context->source_list;
695 while (tmp_source && tmp_source->priority <= source->priority)
697 last_source = tmp_source;
698 tmp_source = tmp_source->next;
701 source->next = tmp_source;
703 tmp_source->prev = source;
705 source->prev = last_source;
707 last_source->next = source;
709 context->source_list = source;
712 /* Holds context's lock
715 g_source_list_remove (GSource *source,
716 GMainContext *context)
719 source->prev->next = source->next;
721 context->source_list = source->next;
724 source->next->prev = source->prev;
732 * @source: a #GSource
733 * @context: a #GMainContext (if %NULL, the default context will be used)
735 * Adds a #GSource to a @context so that it will be executed within
738 * Return value: the ID for the source within the #GMainContext
741 g_source_attach (GSource *source,
742 GMainContext *context)
747 g_return_val_if_fail (source->context == NULL, 0);
748 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
751 context = g_main_context_default ();
753 LOCK_CONTEXT (context);
755 source->context = context;
756 result = source->id = context->next_id++;
759 g_source_list_add (source, context);
761 tmp_list = source->poll_fds;
764 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
765 tmp_list = tmp_list->next;
768 #ifdef G_THREADS_ENABLED
769 /* Now wake up the main loop if it is waiting in the poll() */
770 g_main_context_wakeup (context);
773 UNLOCK_CONTEXT (context);
779 g_source_destroy_internal (GSource *source,
780 GMainContext *context,
784 LOCK_CONTEXT (context);
786 if (!SOURCE_DESTROYED (source))
789 gpointer old_cb_data;
790 GSourceCallbackFuncs *old_cb_funcs;
792 source->flags &= ~G_HOOK_FLAG_ACTIVE;
794 old_cb_data = source->callback_data;
795 old_cb_funcs = source->callback_funcs;
797 source->callback_data = NULL;
798 source->callback_funcs = NULL;
802 UNLOCK_CONTEXT (context);
803 old_cb_funcs->unref (old_cb_data);
804 LOCK_CONTEXT (context);
807 tmp_list = source->poll_fds;
810 g_main_context_remove_poll_unlocked (context, tmp_list->data);
811 tmp_list = tmp_list->next;
814 g_source_unref_internal (source, context, TRUE);
818 UNLOCK_CONTEXT (context);
823 * @source: a #GSource
825 * Remove a source from its #GMainContext, if any, and mark it as
826 * destroyed. The source cannot be subsequently added to another
830 g_source_destroy (GSource *source)
832 GMainContext *context;
834 g_return_if_fail (source != NULL);
836 context = source->context;
839 g_source_destroy_internal (source, context, FALSE);
841 source->flags &= ~G_HOOK_FLAG_ACTIVE;
846 * @source: a #GSource
848 * Return the numeric ID for a particular source. The ID of a source
849 * is unique within a particular main loop context. The reverse
850 * mapping from ID to source is done by g_main_context_find_source_by_id().
852 * Return value: the ID for the source
855 g_source_get_id (GSource *source)
859 g_return_val_if_fail (source != NULL, 0);
860 g_return_val_if_fail (source->context != NULL, 0);
862 LOCK_CONTEXT (source->context);
864 UNLOCK_CONTEXT (source->context);
870 * g_source_get_context:
871 * @source: a #GSource
873 * Get the #GMainContext with which the source is associated.
874 * Calling this function on a destroyed source is an error.
876 * Return value: the #GMainContext with which the source is associated,
877 * or %NULL if the context has not yet been added
881 g_source_get_context (GSource *source)
883 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
885 return source->context;
889 * g_main_source_add_poll:
891 * @fd: a #GPollFD structure holding information about a file
892 * descriptor to watch.
894 * Add a file descriptor to the set of file descriptors polled * for
895 * this source. This is usually combined with g_source_new() to add an
896 * event source. The event source's check function will typically test
897 * the revents field in the #GPollFD struct and return %TRUE if events need
901 g_source_add_poll (GSource *source,
904 GMainContext *context;
906 g_return_if_fail (source != NULL);
907 g_return_if_fail (fd != NULL);
908 g_return_if_fail (!SOURCE_DESTROYED (source));
910 context = source->context;
913 LOCK_CONTEXT (context);
915 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
919 g_main_context_add_poll_unlocked (context, source->priority, fd);
920 UNLOCK_CONTEXT (context);
925 * g_source_set_callback_indirect:
926 * @source: the source
927 * @callback_data: pointer to callback data "object"
928 * @callback_funcs: functions for reference counting callback_data
929 * and getting the callback and data
931 * Set the callback function storing the data as a refcounted callback
932 * "object". This is used to implement g_source_set_callback_closure()
933 * and internally. Note that calling g_source_set_callback_indirect() assumes
934 * an initial reference count on @callback_data, and thus
935 * @callback_funcs->unref will eventually be called once more
936 * than @callback_funcs->ref.
939 g_source_set_callback_indirect (GSource *source,
940 gpointer callback_data,
941 GSourceCallbackFuncs *callback_funcs)
943 GMainContext *context;
944 gpointer old_cb_data;
945 GSourceCallbackFuncs *old_cb_funcs;
947 g_return_if_fail (source != NULL);
948 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
950 context = source->context;
953 LOCK_CONTEXT (context);
955 old_cb_data = source->callback_data;
956 old_cb_funcs = source->callback_funcs;
958 source->callback_data = callback_data;
959 source->callback_funcs = callback_funcs;
962 UNLOCK_CONTEXT (context);
965 old_cb_funcs->unref (old_cb_data);
969 g_source_callback_ref (gpointer cb_data)
971 GSourceCallback *callback = cb_data;
973 callback->ref_count++;
978 g_source_callback_unref (gpointer cb_data)
980 GSourceCallback *callback = cb_data;
982 callback->ref_count--;
983 if (callback->ref_count == 0)
985 if (callback->notify)
986 callback->notify (callback->data);
992 g_source_callback_get (gpointer cb_data,
996 GSourceCallback *callback = cb_data;
998 *func = callback->func;
999 *data = callback->data;
1002 static GSourceCallbackFuncs g_source_callback_funcs = {
1003 g_source_callback_ref,
1004 g_source_callback_unref,
1005 g_source_callback_get,
1009 * g_source_set_callback:
1010 * @source: the source
1011 * @func: a callback function
1012 * @data: the data to pass to callback function
1013 * @notify: a function to call when @data is no longer in use, or %NULL.
1015 * Set the callback function for a source.
1018 g_source_set_callback (GSource *source,
1021 GDestroyNotify notify)
1023 GSourceCallback *new_callback;
1025 g_return_if_fail (source != NULL);
1027 new_callback = g_new (GSourceCallback, 1);
1029 new_callback->ref_count = 1;
1030 new_callback->func = func;
1031 new_callback->data = data;
1032 new_callback->notify = notify;
1034 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1038 * g_source_set_priority:
1039 * @source: a #GSource
1040 * @priority: the new priority.
1042 * Set the priority of a source. While the main loop is being
1043 * run, a source will
1046 g_source_set_priority (GSource *source,
1050 GMainContext *context;
1052 g_return_if_fail (source != NULL);
1054 context = source->context;
1057 LOCK_CONTEXT (context);
1059 source->priority = priority;
1063 source->next = NULL;
1064 source->prev = NULL;
1066 tmp_list = source->poll_fds;
1069 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1070 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1072 tmp_list = tmp_list->next;
1075 UNLOCK_CONTEXT (source->context);
1080 * g_source_get_priority:
1081 * @source: a #GSource
1083 * Gets the priority of a surce
1085 * Return value: the priority of the source
1088 g_source_get_priority (GSource *source)
1090 g_return_val_if_fail (source != NULL, 0);
1092 return source->priority;
1096 * g_source_set_can_recurse:
1097 * @source: a #GSource
1098 * @can_recurse: whether recursion is allowed for this source
1100 * Sets whether a source can be called recursively. If @can_recurse is
1101 * %TRUE, then while the source is being dispatched then this source
1102 * will be processed normally. Otherwise, all processing of this
1103 * source is blocked until the dispatch function returns.
1106 g_source_set_can_recurse (GSource *source,
1107 gboolean can_recurse)
1109 GMainContext *context;
1111 g_return_if_fail (source != NULL);
1113 context = source->context;
1116 LOCK_CONTEXT (context);
1119 source->flags |= G_SOURCE_CAN_RECURSE;
1121 source->flags &= ~G_SOURCE_CAN_RECURSE;
1124 UNLOCK_CONTEXT (context);
1128 * g_source_get_can_recurse:
1129 * @source: a #GSource
1131 * Checks whether a source is allowed to be called recursively.
1132 * see g_source_set_can_recurse.
1134 * Return value: whether recursion is allowed.
1137 g_source_get_can_recurse (GSource *source)
1139 g_return_val_if_fail (source != NULL, FALSE);
1141 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1146 * @source: a #GSource
1148 * Increases the reference count on a source by one.
1150 * Return value: @source
1153 g_source_ref (GSource *source)
1155 GMainContext *context;
1157 g_return_val_if_fail (source != NULL, NULL);
1159 context = source->context;
1162 LOCK_CONTEXT (context);
1164 source->ref_count++;
1167 UNLOCK_CONTEXT (context);
1172 /* g_source_unref() but possible to call within context lock
1175 g_source_unref_internal (GSource *source,
1176 GMainContext *context,
1179 gpointer old_cb_data = NULL;
1180 GSourceCallbackFuncs *old_cb_funcs = NULL;
1182 g_return_if_fail (source != NULL);
1184 if (!have_lock && context)
1185 LOCK_CONTEXT (context);
1187 source->ref_count--;
1188 if (source->ref_count == 0)
1190 old_cb_data = source->callback_data;
1191 old_cb_funcs = source->callback_funcs;
1193 source->callback_data = NULL;
1194 source->callback_funcs = NULL;
1196 if (context && !SOURCE_DESTROYED (source))
1198 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1199 source->ref_count++;
1202 g_source_list_remove (source, context);
1204 if (source->source_funcs->destroy)
1205 source->source_funcs->destroy (source);
1207 g_slist_free (source->poll_fds);
1208 source->poll_fds = NULL;
1212 if (!have_lock && context)
1213 UNLOCK_CONTEXT (context);
1218 UNLOCK_CONTEXT (context);
1220 old_cb_funcs->unref (old_cb_data);
1223 LOCK_CONTEXT (context);
1229 * @source: a #GSource
1231 * Decreases the reference count of a source by one. If the
1232 * resulting reference count is zero the source and associated
1233 * memory will be destroyed.
1236 g_source_unref (GSource *source)
1238 g_return_if_fail (source != NULL);
1240 g_source_unref_internal (source, source->context, FALSE);
1244 * g_main_context_find_source_by_id:
1245 * @context: a #GMainContext (if %NULL, the default context will be used)
1246 * @id: the source ID, as returned by g_source_get_id()
1248 * Finds a #GSource given a pair of context and ID
1250 * Return value: the #GSource if found, otherwise, %NULL
1253 g_main_context_find_source_by_id (GMainContext *context,
1258 g_return_val_if_fail (id > 0, FALSE);
1260 if (context == NULL)
1261 context = g_main_context_default ();
1263 LOCK_CONTEXT (context);
1265 source = context->source_list;
1268 if (!SOURCE_DESTROYED (source) &&
1271 source = source->next;
1274 UNLOCK_CONTEXT (context);
1280 * g_main_context_find_source_by_funcs_user_data:
1281 * @context: a #GMainContext (if %NULL, the default context will be used).
1282 * @funcs: the @source_funcs passed to g_source_new().
1283 * @user_data: the user data from the callback.
1285 * Finds a source with the given source functions and user data. If
1286 * multiple sources exist with the same source function and user data,
1287 * the first one found will be returned.
1289 * Return value: the source, if one was found, otherwise %NULL
1292 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1293 GSourceFuncs *funcs,
1298 g_return_val_if_fail (funcs != NULL, FALSE);
1300 if (context == NULL)
1301 context = g_main_context_default ();
1303 LOCK_CONTEXT (context);
1305 source = context->source_list;
1308 if (!SOURCE_DESTROYED (source) &&
1309 source->source_funcs == funcs &&
1310 source->callback_data == user_data)
1312 source = source->next;
1315 UNLOCK_CONTEXT (context);
1321 * g_main_context_find_source_by_user_data:
1322 * @context: a #GMainContext
1323 * @user_data: the user_data for the callback.
1325 * Finds a source with the given user data for the callback. If
1326 * multiple sources exist with the same user data, the first
1327 * one found will be returned.
1329 * Return value: the source, if one was found, otherwise %NULL
1332 g_main_context_find_source_by_user_data (GMainContext *context,
1337 if (context == NULL)
1338 context = g_main_context_default ();
1340 LOCK_CONTEXT (context);
1342 source = context->source_list;
1345 if (!SOURCE_DESTROYED (source) &&
1346 source->callback_data == user_data)
1348 source = source->next;
1351 UNLOCK_CONTEXT (context);
1358 * @tag: the id of the source to remove.
1360 * Removes the source with the given id from the default main
1361 * context. The id of a #GSource is given by g_source_get_id(),
1362 * or will be returned by the functions g_source_attach(),
1363 * g_idle_add(), g_idle_add_full(), g_timeout_add(),
1364 * g_timeout_add_full(), g_io_add_watch, and g_io_add_watch_full().
1366 * See also g_source_destroy().
1368 * Return value: %TRUE if the source was found and removed.
1371 g_source_remove (guint tag)
1375 g_return_val_if_fail (tag > 0, FALSE);
1377 source = g_main_context_find_source_by_id (NULL, tag);
1379 g_source_destroy (source);
1381 return source != NULL;
1385 * g_source_remove_by_user_data:
1386 * @user_data: the user_data for the callback.
1388 * Removes a source from the default main loop context given the user
1389 * data for the callback. If multiple sources exist with the same user
1390 * data, only one will be destroyed.
1392 * Return value: %TRUE if a source was found and removed.
1395 g_source_remove_by_user_data (gpointer user_data)
1399 source = g_main_context_find_source_by_user_data (NULL, user_data);
1402 g_source_destroy (source);
1410 * g_source_remove_by_funcs_user_data:
1411 * @funcs: The @source_funcs passed to g_source_new()
1412 * @user_data: the user data for the callback
1414 * Removes a source from the default main loop context given the
1415 * source functions and user data. If multiple sources exist with the
1416 * same source functions and user data, only one will be destroyed.
1418 * Return value: %TRUE if a source was found and removed.
1421 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1426 g_return_val_if_fail (funcs != NULL, FALSE);
1428 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1431 g_source_destroy (source);
1439 * g_get_current_time:
1440 * @result: #GTimeVal structure in which to store current time.
1442 * Equivalent to Unix's <function>gettimeofday()</function>, but portable
1445 g_get_current_time (GTimeVal *result)
1450 g_return_if_fail (result != NULL);
1452 /*this is required on alpha, there the timeval structs are int's
1453 not longs and a cast only would fail horribly*/
1454 gettimeofday (&r, NULL);
1455 result->tv_sec = r.tv_sec;
1456 result->tv_usec = r.tv_usec;
1458 /* Avoid calling time() except for the first time.
1459 * GetTickCount() should be pretty fast and low-level?
1460 * I could also use ftime() but it seems unnecessarily overheady.
1462 static DWORD start_tick = 0;
1463 static time_t start_time;
1466 g_return_if_fail (result != NULL);
1468 if (start_tick == 0)
1470 start_tick = GetTickCount ();
1474 tick = GetTickCount ();
1476 result->tv_sec = (tick - start_tick) / 1000 + start_time;
1477 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
1481 /* Running the main loop */
1483 /* HOLDS: context's lock */
1485 g_main_dispatch (GMainContext *context)
1489 for (i = 0; i < context->pending_dispatches->len; i++)
1491 GSource *source = context->pending_dispatches->pdata[i];
1493 context->pending_dispatches->pdata[i] = NULL;
1496 source->flags &= ~G_SOURCE_READY;
1498 if (!SOURCE_DESTROYED (source))
1500 gboolean was_in_call;
1501 gpointer user_data = NULL;
1502 GSourceFunc callback = NULL;
1503 GSourceCallbackFuncs *cb_funcs;
1505 gboolean need_destroy;
1507 gboolean (*dispatch) (GSource *,
1511 dispatch = source->source_funcs->dispatch;
1512 cb_funcs = source->callback_funcs;
1513 cb_data = source->callback_data;
1516 cb_funcs->ref (cb_data);
1518 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1519 source->flags |= G_HOOK_FLAG_IN_CALL;
1521 UNLOCK_CONTEXT (context);
1524 cb_funcs->get (cb_data, &callback, &user_data);
1526 need_destroy = ! dispatch (source,
1529 LOCK_CONTEXT (context);
1532 cb_funcs->unref (cb_data);
1535 source->flags &= ~G_HOOK_FLAG_IN_CALL;
1537 /* Note: this depends on the fact that we can't switch
1538 * sources from one main context to another
1540 if (need_destroy && !SOURCE_DESTROYED (source))
1542 g_assert (source->context == context);
1543 g_source_destroy_internal (source, context, TRUE);
1547 SOURCE_UNREF (source, context);
1550 g_ptr_array_set_size (context->pending_dispatches, 0);
1553 /* Holds context's lock */
1554 static inline GSource *
1555 next_valid_source (GMainContext *context,
1558 GSource *new_source = source ? source->next : context->source_list;
1562 if (!SOURCE_DESTROYED (new_source))
1564 new_source->ref_count++;
1568 new_source = new_source->next;
1572 SOURCE_UNREF (source, context);
1579 * g_main_context_prepare:
1580 * @context: a #GMainContext
1581 * @priority: location to store priority of highest priority
1582 * source already ready.
1584 * Prepares to poll sources within a main loop. The resulting information
1585 * for polling is determined by calling g_main_context_query ().
1587 * Return value: %TRUE if some source is ready to be dispatched
1591 g_main_context_prepare (GMainContext *context,
1595 gint current_priority = G_MAXINT;
1598 if (context == NULL)
1599 context = g_main_context_default ();
1601 LOCK_CONTEXT (context);
1603 context->time_is_current = FALSE;
1605 if (context->in_check_or_prepare)
1607 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
1608 "prepare() member.");
1612 #ifdef G_THREADS_ENABLED
1613 if (context->poll_waiting)
1615 g_warning("g_main_context_prepare(): main loop already active in another thread");
1616 UNLOCK_CONTEXT (context);
1620 context->poll_waiting = TRUE;
1621 #endif /* G_THREADS_ENABLED */
1624 /* If recursing, finish up current dispatch, before starting over */
1625 if (context->pending_dispatches)
1628 g_main_dispatch (context, ¤t_time);
1630 UNLOCK_CONTEXT (context);
1635 /* If recursing, clear list of pending dispatches */
1636 g_ptr_array_set_size (context->pending_dispatches, 0);
1638 /* Prepare all sources */
1640 context->timeout = -1;
1642 source = next_valid_source (context, NULL);
1645 gint source_timeout = -1;
1647 if ((n_ready > 0) && (source->priority > current_priority))
1649 SOURCE_UNREF (source, context);
1652 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
1655 if (!(source->flags & G_SOURCE_READY))
1658 gboolean (*prepare) (GSource *source,
1661 prepare = source->source_funcs->prepare;
1662 context->in_check_or_prepare++;
1663 UNLOCK_CONTEXT (context);
1665 result = (*prepare) (source, &source_timeout);
1667 LOCK_CONTEXT (context);
1668 context->in_check_or_prepare--;
1671 source->flags |= G_SOURCE_READY;
1674 if (source->flags & G_SOURCE_READY)
1677 current_priority = source->priority;
1678 context->timeout = 0;
1681 if (source_timeout >= 0)
1683 if (context->timeout < 0)
1684 context->timeout = source_timeout;
1686 context->timeout = MIN (context->timeout, source_timeout);
1690 source = next_valid_source (context, source);
1693 UNLOCK_CONTEXT (context);
1696 *priority = current_priority;
1698 return (n_ready > 0);
1702 * g_main_context_query:
1703 * @context: a #GMainContext
1704 * @max_priority: maximum priority source to check
1705 * @timeout: location to store timeout to be used in polling
1706 * @fds: location to store #GPollFD records that need to be polled.
1707 * @n_fds: length of @fds.
1709 * Determines information necessary to poll this main loop.
1714 g_main_context_query (GMainContext *context,
1723 LOCK_CONTEXT (context);
1725 pollrec = context->poll_records;
1727 while (pollrec && max_priority >= pollrec->priority)
1729 if (pollrec->fd->events)
1733 fds[n_poll].fd = pollrec->fd->fd;
1734 /* In direct contradiction to the Unix98 spec, IRIX runs into
1735 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
1736 * flags in the events field of the pollfd while it should
1737 * just ignoring them. So we mask them out here.
1739 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
1740 fds[n_poll].revents = 0;
1745 pollrec = pollrec->next;
1748 #ifdef G_THREADS_ENABLED
1749 context->poll_changed = FALSE;
1754 *timeout = context->timeout;
1756 context->time_is_current = FALSE;
1759 UNLOCK_CONTEXT (context);
1765 * g_main_context_check:
1766 * @context: a #GMainContext
1767 * @max_priority: the maximum numerical priority of sources to check
1768 * @fds: array of #GPollFD's that was passed to the last call to
1769 * g_main_context_query()
1770 * @n_fds: return value of g_main_context_query()
1772 * Pass the results of polling back to the main loop.
1774 * Return value: %TRUE if some sources are ready to be dispatched.
1777 g_main_context_check (GMainContext *context,
1787 LOCK_CONTEXT (context);
1789 if (context->in_check_or_prepare)
1791 g_warning ("g_main_context_check() called recursively from within a source's check() or "
1792 "prepare() member.");
1796 #ifdef G_THREADS_ENABLED
1797 if (!context->poll_waiting)
1801 read (context->wake_up_pipe[0], &c, 1);
1805 context->poll_waiting = FALSE;
1807 /* If the set of poll file descriptors changed, bail out
1808 * and let the main loop rerun
1810 if (context->poll_changed)
1812 #endif /* G_THREADS_ENABLED */
1814 pollrec = context->poll_records;
1818 if (pollrec->fd->events)
1820 pollrec->fd->revents = fds[i].revents;
1823 pollrec = pollrec->next;
1826 source = next_valid_source (context, NULL);
1829 if ((n_ready > 0) && (source->priority > max_priority))
1831 SOURCE_UNREF (source, context);
1834 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
1837 if (!(source->flags & G_SOURCE_READY))
1840 gboolean (*check) (GSource *source);
1842 check = source->source_funcs->check;
1844 context->in_check_or_prepare++;
1845 UNLOCK_CONTEXT (context);
1847 result = (*check) (source);
1849 LOCK_CONTEXT (context);
1850 context->in_check_or_prepare--;
1853 source->flags |= G_SOURCE_READY;
1856 if (source->flags & G_SOURCE_READY)
1858 source->ref_count++;
1859 g_ptr_array_add (context->pending_dispatches, source);
1865 source = next_valid_source (context, source);
1868 UNLOCK_CONTEXT (context);
1874 * g_main_context_dispatch:
1875 * @context: a #GMainContext
1877 * Dispatch all pending sources()
1880 g_main_context_dispatch (GMainContext *context)
1882 LOCK_CONTEXT (context);
1884 if (context->pending_dispatches->len > 0)
1886 g_main_dispatch (context);
1889 UNLOCK_CONTEXT (context);
1893 g_main_context_iterate (GMainContext *context,
1899 gboolean some_ready;
1900 gint nfds, new_nfds;
1903 some_ready = g_main_context_prepare (context, &max_priority);
1907 LOCK_CONTEXT (context);
1909 if (context->cached_poll_array)
1911 nfds = context->cached_poll_array_size;
1912 fds = context->cached_poll_array;
1913 context->cached_poll_array = NULL;
1917 nfds = context->cached_poll_array_size = context->n_poll_records;
1918 fds = g_new (GPollFD, nfds);
1921 UNLOCK_CONTEXT (context);
1923 new_nfds = g_main_context_query (context, max_priority,
1924 &timeout, fds, nfds);
1926 while (new_nfds > nfds);
1931 g_main_context_poll (context, timeout, max_priority,
1934 g_main_context_check (context,
1938 LOCK_CONTEXT (context);
1940 g_assert (!context->cached_poll_array);
1942 context->cached_poll_array = fds;
1943 context->cached_poll_array_size = nfds;
1945 UNLOCK_CONTEXT (context);
1948 g_main_context_dispatch (context);
1954 * g_main_context_pending:
1955 * @context: a #GMainContext (if %NULL, the default context will be used)
1957 * Check if any sources have pending events for the given context.
1959 * Return value: %TRUE if events are pending.
1962 g_main_context_pending (GMainContext *context)
1965 context = g_main_context_default();
1967 return g_main_context_iterate (context, FALSE, FALSE);
1971 * g_main_context_iteration:
1972 * @context: a #GMainContext (if %NULL, the default context will be used)
1973 * @may_block: whether the call may block.
1975 * Run a single iteration for the given main loop. This involves
1976 * checking to see if any event sources are ready to be processed,
1977 * then if no events sources are ready and @may_block is %TRUE, waiting
1978 * for a source to become ready, then dispatching the highest priority
1979 * events sources that are ready. Note that even when @may_block is %TRUE,
1980 * it is still possible for g_main_context_iteration() to return
1981 * %FALSE, since the the wait may be interrupted for other
1982 * reasons than an event source becoming ready.
1984 * Return value: %TRUE if events were dispatched.
1987 g_main_context_iteration (GMainContext *context, gboolean may_block)
1990 context = g_main_context_default();
1992 return g_main_context_iterate (context, may_block, TRUE);
1997 * @context: a #GMainContext (if %NULL, the default context will be used).
1998 * @is_running: set to TRUE to indicate that the loop is running. This
1999 * is not very important since calling g_main_run() will set this to
2002 * Create a new #GMainLoop structure
2007 g_main_loop_new (GMainContext *context,
2008 gboolean is_running)
2013 context = g_main_context_default();
2015 loop = g_new0 (GMainLoop, 1);
2016 loop->context = context;
2017 loop->is_running = is_running != FALSE;
2018 loop->ref_count = 1;
2020 #ifdef G_THREADS_ENABLED
2021 if (g_thread_supported ())
2022 loop->mutex = g_mutex_new ();
2025 loop->sem_cond = NULL;
2026 #endif /* G_THREADS_ENABLED */
2033 * @loop: a #GMainLoop
2035 * Increase the reference count on a #GMainLoop object by one.
2037 * Return value: @loop
2040 g_main_loop_ref (GMainLoop *loop)
2042 g_return_val_if_fail (loop != NULL, NULL);
2052 main_loop_destroy (GMainLoop *loop)
2054 #ifdef G_THREADS_ENABLED
2055 g_mutex_free (loop->mutex);
2057 g_cond_free (loop->sem_cond);
2058 #endif /* G_THREADS_ENABLED */
2064 * g_main_loop_unref:
2065 * @loop: a #GMainLoop
2067 * Decreases the reference count on a #GMainLoop object by one. If
2068 * the result is zero, free the loop and free all associated memory.
2071 g_main_loop_unref (GMainLoop *loop)
2073 g_return_if_fail (loop != NULL);
2074 g_return_if_fail (loop->ref_count > 0);
2079 if (loop->ref_count == 0)
2081 /* When the ref_count is 0, there can be nobody else using the
2082 * loop, so it is safe to unlock before destroying.
2085 main_loop_destroy (loop);
2093 * @loop: a #GMainLoop
2095 * Run a main loop until g_main_quit() is called on the loop.
2096 * If this is called for the thread of the loop's #GMainContext,
2097 * it will process events from the loop, otherwise it will
2101 g_main_loop_run (GMainLoop *loop)
2103 g_return_if_fail (loop != NULL);
2105 /* The assumption here is that a reference is held to the loop
2106 * until we recursively iterate
2108 #ifdef G_THREADS_ENABLED
2109 if (loop->context->thread != g_thread_self ())
2115 if (!g_thread_supported ())
2117 g_warning ("g_main_loop_run() was called from second thread but"
2118 "g_thread_init() was never called.");
2122 if (!loop->sem_cond)
2123 loop->sem_cond = g_cond_new ();
2125 if (!loop->is_running)
2126 loop->is_running = TRUE;
2128 while (loop->is_running)
2129 g_cond_wait (loop->sem_cond, loop->mutex);
2133 #endif /* G_THREADS_ENABLED */
2135 LOCK_CONTEXT (loop->context);
2136 if (loop->context->in_check_or_prepare)
2138 g_warning ("g_main_run(): called recursively from within a source's check() or "
2139 "prepare() member, iteration not possible.");
2142 UNLOCK_CONTEXT (loop->context);
2147 loop->is_running = TRUE;
2148 while (loop->is_running)
2151 g_main_context_iterate (loop->context, TRUE, TRUE);
2156 /* We inline this here rather than calling g_main_loop_unref() to
2157 * avoid an extra unlock/lock.
2160 if (loop->ref_count == 0)
2163 main_loop_destroy (loop);
2171 * @loop: a #GMainLoop
2173 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2174 * for the loop will return.
2177 g_main_loop_quit (GMainLoop *loop)
2179 g_return_if_fail (loop != NULL);
2182 loop->is_running = FALSE;
2184 #ifdef G_THREADS_ENABLED
2186 g_cond_broadcast (loop->sem_cond);
2191 LOCK_CONTEXT (loop->context);
2193 g_main_context_wakeup (loop->context);
2194 UNLOCK_CONTEXT (loop->context);
2198 * g_main_loop_is_running:
2199 * @loop: a #GMainLoop.
2201 * Check to see if the main loop is currently being run via g_main_run()
2203 * Return value: %TRUE if the mainloop is currently being run.
2206 g_main_loop_is_running (GMainLoop *loop)
2210 g_return_val_if_fail (loop != NULL, FALSE);
2213 result = loop->is_running;
2219 /* HOLDS: context's lock */
2221 g_main_context_poll (GMainContext *context,
2227 #ifdef G_MAIN_POLL_DEBUG
2233 GPollFunc poll_func;
2235 if (n_fds || timeout != 0)
2237 #ifdef G_MAIN_POLL_DEBUG
2238 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2239 poll_timer = g_timer_new ();
2242 LOCK_CONTEXT (context);
2244 poll_func = context->poll_func;
2246 UNLOCK_CONTEXT (context);
2247 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2248 g_warning ("poll(2) failed due to: %s.",
2249 g_strerror (errno));
2251 #ifdef G_MAIN_POLL_DEBUG
2252 LOCK_CONTEXT (context);
2254 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2257 g_timer_elapsed (poll_timer, NULL));
2258 g_timer_destroy (poll_timer);
2259 pollrec = context->poll_records;
2263 if (pollrec->fd->events)
2267 g_print (" [%d:", fds[i].fd);
2268 if (fds[i].revents & G_IO_IN)
2270 if (fds[i].revents & G_IO_OUT)
2272 if (fds[i].revents & G_IO_PRI)
2274 if (fds[i].revents & G_IO_ERR)
2276 if (fds[i].revents & G_IO_HUP)
2278 if (fds[i].revents & G_IO_NVAL)
2284 pollrec = pollrec->next;
2288 UNLOCK_CONTEXT (context);
2290 } /* if (n_fds || timeout != 0) */
2294 * g_main_context_add_poll:
2295 * @context: a #GMainContext (or %NULL for the default context)
2296 * @fd: a #GPollFD structure holding information about a file
2297 * descriptor to watch.
2298 * @priority: the priority for this file descriptor which should be
2299 * the same as the priority used for g_source_attach() to ensure that the
2300 * file descriptor is polled whenever the results may be needed.
2302 * Add a file descriptor to the set of file descriptors polled * for
2303 * this context. This will very seldom be used directly. Instead
2304 * a typical event source will use g_source_add_poll() instead.
2307 g_main_context_add_poll (GMainContext *context,
2312 context = g_main_context_default ();
2314 LOCK_CONTEXT (context);
2315 g_main_context_add_poll_unlocked (context, priority, fd);
2316 UNLOCK_CONTEXT (context);
2319 /* HOLDS: main_loop_lock */
2321 g_main_context_add_poll_unlocked (GMainContext *context,
2325 GPollRec *lastrec, *pollrec, *newrec;
2327 if (!context->poll_chunk)
2328 context->poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
2330 if (context->poll_free_list)
2332 newrec = context->poll_free_list;
2333 context->poll_free_list = newrec->next;
2336 newrec = g_chunk_new (GPollRec, context->poll_chunk);
2338 /* This file descriptor may be checked before we ever poll */
2341 newrec->priority = priority;
2344 pollrec = context->poll_records;
2345 while (pollrec && priority >= pollrec->priority)
2348 pollrec = pollrec->next;
2352 lastrec->next = newrec;
2354 context->poll_records = newrec;
2356 newrec->next = pollrec;
2358 context->n_poll_records++;
2359 if (context->cached_poll_array &&
2360 context->cached_poll_array_size < context->n_poll_records)
2362 g_free (context->cached_poll_array);
2363 context->cached_poll_array = NULL;
2366 #ifdef G_THREADS_ENABLED
2367 context->poll_changed = TRUE;
2369 /* Now wake up the main loop if it is waiting in the poll() */
2370 g_main_context_wakeup (context);
2375 * g_main_context_remove_poll:
2376 * @context:a #GMainContext
2377 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
2379 * Remove file descriptor from the set of file descriptors to be
2380 * polled for a particular context.
2383 g_main_context_remove_poll (GMainContext *context,
2387 context = g_main_context_default ();
2389 LOCK_CONTEXT (context);
2391 g_main_context_remove_poll_unlocked (context, fd);
2393 UNLOCK_CONTEXT (context);
2397 g_main_context_remove_poll_unlocked (GMainContext *context,
2400 GPollRec *pollrec, *lastrec;
2403 pollrec = context->poll_records;
2407 if (pollrec->fd == fd)
2409 if (lastrec != NULL)
2410 lastrec->next = pollrec->next;
2412 context->poll_records = pollrec->next;
2414 #ifdef ENABLE_GC_FRIENDLY
2416 #endif /* ENABLE_GC_FRIENDLY */
2418 pollrec->next = context->poll_free_list;
2419 context->poll_free_list = pollrec;
2421 context->n_poll_records--;
2425 pollrec = pollrec->next;
2428 #ifdef G_THREADS_ENABLED
2429 context->poll_changed = TRUE;
2431 /* Now wake up the main loop if it is waiting in the poll() */
2432 g_main_context_wakeup (context);
2437 * g_source_get_current_time:
2438 * @source: a #GSource
2439 * @timeval: #GTimeVal structure in which to store current time.
2441 * Gets the "current time" to be used when checking
2442 * this source. The advantage of calling this function over
2443 * calling g_get_current_time() directly is that when
2444 * checking multiple sources, GLib can cache a single value
2445 * instead of having to repeatedly get the system time.
2448 g_source_get_current_time (GSource *source,
2451 GMainContext *context;
2453 g_return_if_fail (source->context != NULL);
2455 context = source->context;
2457 LOCK_CONTEXT (context);
2459 if (!context->time_is_current)
2461 g_get_current_time (&context->current_time);
2462 context->time_is_current = TRUE;
2465 *timeval = context->current_time;
2467 UNLOCK_CONTEXT (context);
2471 * g_main_context_set_poll_func:
2472 * @context: a #GMainContext
2473 * @func: the function to call to poll all file descriptors
2475 * Sets the function to use to handle polling of file descriptors. It
2476 * will be used instead of the poll() system call (or GLib's
2477 * replacement function, which is used where poll() isn't available).
2479 * This function could possibly be used to integrate the GLib event
2480 * loop with an external event loop.
2483 g_main_context_set_poll_func (GMainContext *context,
2487 context = g_main_context_default ();
2489 LOCK_CONTEXT (context);
2492 context->poll_func = func;
2496 context->poll_func = (GPollFunc) poll;
2498 context->poll_func = (GPollFunc) g_poll;
2502 UNLOCK_CONTEXT (context);
2506 * g_main_context_get_poll_func:
2507 * @context: a #GMainContext
2509 * Gets the poll function set by g_main_context_set_poll_func()
2511 * Return value: the poll function
2514 g_main_context_get_poll_func (GMainContext *context)
2519 context = g_main_context_default ();
2521 LOCK_CONTEXT (context);
2522 result = context->poll_func;
2523 UNLOCK_CONTEXT (context);
2528 /* HOLDS: context's lock */
2529 /* Wake the main loop up from a poll() */
2531 g_main_context_wakeup (GMainContext *context)
2533 #ifdef G_THREADS_ENABLED
2534 if (g_thread_supported() && context->poll_waiting)
2536 context->poll_waiting = FALSE;
2538 write (context->wake_up_pipe[1], "A", 1);
2540 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
2549 g_timeout_set_expiration (GTimeoutSource *timeout_source,
2550 GTimeVal *current_time)
2552 guint seconds = timeout_source->interval / 1000;
2553 guint msecs = timeout_source->interval - seconds * 1000;
2555 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
2556 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
2557 if (timeout_source->expiration.tv_usec >= 1000000)
2559 timeout_source->expiration.tv_usec -= 1000000;
2560 timeout_source->expiration.tv_sec++;
2565 g_timeout_prepare (GSource *source,
2569 GTimeVal current_time;
2571 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2573 g_source_get_current_time (source, ¤t_time);
2575 msec = ((timeout_source->expiration.tv_sec - current_time.tv_sec) * 1000 +
2576 (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000);
2580 else if (msec > timeout_source->interval)
2582 /* The system time has been set backwards, so we
2583 * reset the expiration time to now + timeout_source->interval;
2584 * this at least avoids hanging for long periods of time.
2586 g_timeout_set_expiration (timeout_source, ¤t_time);
2587 msec = timeout_source->interval;
2596 g_timeout_check (GSource *source)
2598 GTimeVal current_time;
2599 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2601 g_source_get_current_time (source, ¤t_time);
2603 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
2604 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
2605 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
2609 g_timeout_dispatch (GSource *source,
2610 GSourceFunc callback,
2613 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2617 g_warning ("Timeout source dispatched without callback\n"
2618 "You must call g_source_set_callback().");
2622 if (callback (user_data))
2624 GTimeVal current_time;
2626 g_source_get_current_time (source, ¤t_time);
2627 g_timeout_set_expiration (timeout_source, ¤t_time);
2636 * g_timeout_source_new:
2637 * @interval: the timeout interval in milliseconds.
2639 * Create a new timeout source.
2641 * The source will not initially be associated with any #GMainContext
2642 * and must be added to one with g_source_attach() before it will be
2645 * Return value: the newly create timeout source
2648 g_timeout_source_new (guint interval)
2650 GSource *source = g_source_new (&timeout_funcs, sizeof (GTimeoutSource));
2651 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2652 GTimeVal current_time;
2654 timeout_source->interval = interval;
2656 g_get_current_time (¤t_time);
2657 g_timeout_set_expiration (timeout_source, ¤t_time);
2663 * g_timeout_add_full:
2664 * @priority: the priority of the idle source. Typically this will be in the
2665 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
2666 * @interval: the time between calls to the function, in milliseconds
2667 * (1/1000ths of a second.)
2668 * @function: function to call
2669 * @data: data to pass to @function
2670 * @notify: function to call when the idle is removed, or %NULL
2672 * Sets a function to be called at regular intervals, with the given
2673 * priority. The function is called repeatedly until it returns
2674 * FALSE, at which point the timeout is automatically destroyed and
2675 * the function will not be called again. The @notify function is
2676 * called when the timeout is destroyed. The first call to the
2677 * function will be at the end of the first @interval.
2679 * Note that timeout functions may be delayed, due to the processing of other
2680 * event sources. Thus they should not be relied on for precise timing.
2681 * After each call to the timeout function, the time of the next
2682 * timeout is recalculated based on the current time and the given interval
2683 * (it does not try to 'catch up' time lost in delays).
2685 * Return value: the id of event source.
2688 g_timeout_add_full (gint priority,
2690 GSourceFunc function,
2692 GDestroyNotify notify)
2697 g_return_val_if_fail (function != NULL, 0);
2699 source = g_timeout_source_new (interval);
2701 if (priority != G_PRIORITY_DEFAULT)
2702 g_source_set_priority (source, priority);
2704 g_source_set_callback (source, function, data, notify);
2705 id = g_source_attach (source, NULL);
2706 g_source_unref (source);
2713 * @interval: the time between calls to the function, in milliseconds
2714 * (1/1000ths of a second.)
2715 * @function: function to call
2716 * @data: data to pass to @function
2718 * Sets a function to be called at regular intervals, with the default
2719 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
2720 * until it returns FALSE, at which point the timeout is automatically
2721 * destroyed and the function will not be called again. The @notify
2722 * function is called when the timeout is destroyed. The first call
2723 * to the function will be at the end of the first @interval.
2725 * Note that timeout functions may be delayed, due to the processing of other
2726 * event sources. Thus they should not be relied on for precise timing.
2727 * After each call to the timeout function, the time of the next
2728 * timeout is recalculated based on the current time and the given interval
2729 * (it does not try to 'catch up' time lost in delays).
2731 * Return value: the id of event source.
2734 g_timeout_add (guint32 interval,
2735 GSourceFunc function,
2738 return g_timeout_add_full (G_PRIORITY_DEFAULT,
2739 interval, function, data, NULL);
2742 /* Idle functions */
2745 g_idle_prepare (GSource *source,
2754 g_idle_check (GSource *source)
2760 g_idle_dispatch (GSource *source,
2761 GSourceFunc callback,
2766 g_warning ("Idle source dispatched without callback\n"
2767 "You must call g_source_set_callback().");
2771 return callback (user_data);
2775 * g_idle_source_new:
2777 * Create a new idle source.
2779 * The source will not initially be associated with any #GMainContext
2780 * and must be added to one with g_source_attach() before it will be
2783 * Return value: the newly created idle source
2786 g_idle_source_new (void)
2788 return g_source_new (&idle_funcs, sizeof (GSource));
2793 * @priority: the priority of the idle source. Typically this will be in the
2794 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
2795 * @function: function to call
2796 * @data: data to pass to @function
2797 * @notify: function to call when the idle is removed, or %NULL
2799 * Adds a function to be called whenever there are no higher priority
2800 * events pending. If the function returns FALSE it is automatically
2801 * removed from the list of event sources and will not be called again.
2803 * Return value: the id of the event source.
2806 g_idle_add_full (gint priority,
2807 GSourceFunc function,
2809 GDestroyNotify notify)
2814 g_return_val_if_fail (function != NULL, 0);
2816 source = g_idle_source_new ();
2818 if (priority != G_PRIORITY_DEFAULT)
2819 g_source_set_priority (source, priority);
2821 g_source_set_callback (source, function, data, notify);
2822 id = g_source_attach (source, NULL);
2823 g_source_unref (source);
2830 * @function: function to call
2831 * @data: data to pass to @function.
2833 * Adds a function to be called whenever there are no higher priority
2834 * events pending to the default main loop. The function is given the
2835 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
2836 * returns FALSE it is automatically removed from the list of event
2837 * sources and will not be called again.
2839 * Return value: the id of the event source.
2842 g_idle_add (GSourceFunc function,
2845 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2849 * g_idle_remove_by_data:
2850 * @data: the data for the idle source's callback.
2852 * Removes the idle function with the given data.
2854 * Return value: %TRUE if an idle source was found and removed.
2857 g_idle_remove_by_data (gpointer data)
2859 return g_source_remove_by_funcs_user_data (&idle_funcs, data);