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
276 * -> First PeekMessage
278 #ifdef G_MAIN_POLL_DEBUG
279 g_print ("PeekMessage\n");
281 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
282 ready = WAIT_OBJECT_0 + nhandles;
287 /* Waiting just for messages */
288 if (timeout == INFINITE)
293 #ifdef G_MAIN_POLL_DEBUG
294 g_print ("WaitMessage\n");
297 g_warning (G_STRLOC ": WaitMessage() failed");
298 ready = WAIT_OBJECT_0 + nhandles;
300 else if (timeout == 0)
302 /* Waiting just for messages, zero timeout.
303 * If we got here, there was no message
305 ready = WAIT_TIMEOUT;
309 /* Waiting just for messages, some timeout
310 * -> Set a timer, wait for message,
311 * kill timer, use PeekMessage
313 timer = SetTimer (NULL, 0, timeout, NULL);
315 g_warning (G_STRLOC ": SetTimer() failed");
318 #ifdef G_MAIN_POLL_DEBUG
319 g_print ("WaitMessage\n");
322 KillTimer (NULL, timer);
323 #ifdef G_MAIN_POLL_DEBUG
324 g_print ("PeekMessage\n");
326 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)
327 && msg.message != WM_TIMER)
328 ready = WAIT_OBJECT_0;
330 ready = WAIT_TIMEOUT;
336 /* Wait for either message or event
337 * -> Use MsgWaitForMultipleObjects
339 #ifdef G_MAIN_POLL_DEBUG
340 g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
342 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
343 timeout, QS_ALLINPUT);
345 if (ready == WAIT_FAILED)
346 g_warning (G_STRLOC ": MsgWaitForMultipleObjects() failed");
350 else if (nhandles == 0)
352 /* Wait for nothing (huh?) */
357 /* Wait for just events
358 * -> Use WaitForMultipleObjects
360 #ifdef G_MAIN_POLL_DEBUG
361 g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout);
363 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
364 if (ready == WAIT_FAILED)
365 g_warning (G_STRLOC ": WaitForMultipleObjects() failed");
368 #ifdef G_MAIN_POLL_DEBUG
369 g_print ("wait returns %d%s\n",
371 (ready == WAIT_FAILED ? " (WAIT_FAILED)" :
372 (ready == WAIT_TIMEOUT ? " (WAIT_TIMEOUT)" :
373 (poll_msgs && ready == WAIT_OBJECT_0 + nhandles ? " (msg)" : ""))));
375 for (f = fds; f < &fds[nfds]; ++f)
378 if (ready == WAIT_FAILED)
380 else if (ready == WAIT_TIMEOUT)
382 else if (poll_msgs && ready == WAIT_OBJECT_0 + nhandles)
384 for (f = fds; f < &fds[nfds]; ++f)
387 if (f->events & G_IO_IN)
388 if (f->fd == G_WIN32_MSG_HANDLE)
389 f->revents |= G_IO_IN;
392 #if TEST_WITHOUT_THIS
393 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
394 for (f = fds; f < &fds[nfds]; ++f)
396 if ((f->events & G_IO_IN)
397 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
399 f->revents |= G_IO_IN;
400 #ifdef G_MAIN_POLL_DEBUG
401 g_print ("g_poll: got event %#x\n", f->fd);
404 ResetEvent ((HANDLE) f->fd);
413 #else /* !G_OS_WIN32 */
415 /* The following implementation of poll() comes from the GNU C Library.
416 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
419 #include <string.h> /* for bzero on BSD systems */
421 #ifdef HAVE_SYS_SELECT_H
422 #include <sys/select.h>
423 #endif /* HAVE_SYS_SELECT_H */
427 #endif /* G_OS_BEOS */
430 # define SELECT_MASK fd_set
431 #else /* !NO_FD_SET */
433 typedef long fd_mask;
436 # define SELECT_MASK void
438 # define SELECT_MASK int
439 # endif /* !_IBMR2 */
440 #endif /* !NO_FD_SET */
443 g_poll (GPollFD *fds,
448 SELECT_MASK rset, wset, xset;
457 for (f = fds; f < &fds[nfds]; ++f)
460 if (f->events & G_IO_IN)
461 FD_SET (f->fd, &rset);
462 if (f->events & G_IO_OUT)
463 FD_SET (f->fd, &wset);
464 if (f->events & G_IO_PRI)
465 FD_SET (f->fd, &xset);
466 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
470 tv.tv_sec = timeout / 1000;
471 tv.tv_usec = (timeout % 1000) * 1000;
473 ready = select (maxfd + 1, &rset, &wset, &xset,
474 timeout == -1 ? NULL : &tv);
476 for (f = fds; f < &fds[nfds]; ++f)
481 if (FD_ISSET (f->fd, &rset))
482 f->revents |= G_IO_IN;
483 if (FD_ISSET (f->fd, &wset))
484 f->revents |= G_IO_OUT;
485 if (FD_ISSET (f->fd, &xset))
486 f->revents |= G_IO_PRI;
493 #endif /* !G_OS_WIN32 */
495 #endif /* !HAVE_POLL */
497 /* Called to clean up when a thread terminates
500 g_main_context_destroy (GMainContext *context)
504 /* We need the lock here only because g_source_destroy expects
505 * to be able to unlock when destroying the source's data
507 LOCK_CONTEXT (context);
508 source = context->source_list;
511 GSource *next = source->next;
512 g_source_destroy_internal (source, context, TRUE);
515 UNLOCK_CONTEXT (context);
517 #ifdef G_THREADS_ENABLED
518 g_mutex_free (context->mutex);
521 g_ptr_array_free (context->pending_dispatches, TRUE);
522 g_free (context->cached_poll_array);
524 g_mem_chunk_destroy (context->poll_chunk);
526 #ifdef G_THREADS_ENABLED
527 if (g_thread_supported())
530 close (context->wake_up_pipe[0]);
531 close (context->wake_up_pipe[1]);
533 CloseHandle (context->wake_up_semaphore);
542 * g_main_context_get:
543 * @thread: a #GThread
545 * Retrieves the main loop context for a particular thread. This
546 * will create the main context for the thread if none previously
547 * existed. The context will exist until the thread terminates.
549 * Return value: the main loop context for @thread.
552 g_main_context_get (GThread *thread)
554 static GStaticPrivate private_key = G_STATIC_PRIVATE_INIT;
555 GMainContext *context;
557 g_return_val_if_fail (thread != NULL, NULL);
559 if (g_thread_supported ())
560 context = g_static_private_get_for_thread (&private_key, thread);
562 context = default_main_context;
566 context = g_new0 (GMainContext, 1);
568 #ifdef G_THREADS_ENABLED
569 if (g_thread_supported ())
570 context->mutex = g_mutex_new();
572 context->thread = thread;
575 context->next_id = 1;
577 context->source_list = NULL;
580 context->poll_func = (GPollFunc)poll;
582 context->poll_func = g_poll;
585 context->cached_poll_array = NULL;
586 context->cached_poll_array_size = 0;
588 context->pending_dispatches = g_ptr_array_new ();
590 context->time_is_current = FALSE;
592 #ifdef G_THREADS_ENABLED
593 if (g_thread_supported ())
596 if (pipe (context->wake_up_pipe) < 0)
597 g_error ("Cannot create pipe main loop wake-up: %s\n",
600 context->wake_up_rec.fd = context->wake_up_pipe[0];
601 context->wake_up_rec.events = G_IO_IN;
602 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
604 context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
605 if (context->wake_up_semaphore == NULL)
606 g_error ("Cannot create wake-up semaphore: %s",
607 g_win32_error_message (GetLastError ()));
608 context->wake_up_rec.fd = (gint) context->wake_up_semaphore;
609 context->wake_up_rec.events = G_IO_IN;
610 #ifdef G_MAIN_POLL_DEBUG
611 g_print ("wake-up semaphore: %#x\n", (guint) context->wake_up_semaphore);
613 g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
618 if (g_thread_supported ())
619 g_static_private_set_for_thread (&private_key, thread,
621 (GDestroyNotify)g_main_context_destroy);
623 default_main_context = context;
630 * g_main_context_default:
632 * Return the default main context. This is the main context used
633 * for main loop functions when a main loop is not explicitly
636 * Return value: the default main context.
639 g_main_context_default (void)
645 if (!default_main_context)
646 default_main_context = g_main_context_get (g_thread_self ());
648 G_UNLOCK (main_loop);
650 return default_main_context;
653 /* Hooks for adding to the main loop */
657 * @source_funcs: structure containing functions that implement
658 * the sources behavior.
659 * @struct_size: size of the #GSource structure to create
661 * Create a new GSource structure. The size is specified to
662 * allow creating structures derived from GSource that contain
663 * additional data. The size passed in must be at least
666 * The source will not initially be associated with any #GMainContext
667 * and must be added to one with g_source_add() before it will be
670 * Return value: the newly create #GSource
673 g_source_new (GSourceFuncs *source_funcs,
678 g_return_val_if_fail (source_funcs != NULL, NULL);
679 g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
681 source = (GSource*) g_malloc0 (struct_size);
683 source->source_funcs = source_funcs;
684 source->ref_count = 1;
686 source->priority = G_PRIORITY_DEFAULT;
688 source->flags = G_HOOK_FLAG_ACTIVE;
690 /* NULL/0 initialization for all other fields */
695 /* Holds context's lock
698 g_source_list_add (GSource *source,
699 GMainContext *context)
701 GSource *tmp_source, *last_source;
704 tmp_source = context->source_list;
705 while (tmp_source && tmp_source->priority <= source->priority)
707 last_source = tmp_source;
708 tmp_source = tmp_source->next;
711 source->next = tmp_source;
713 tmp_source->prev = source;
715 source->prev = last_source;
717 last_source->next = source;
719 context->source_list = source;
722 /* Holds context's lock
725 g_source_list_remove (GSource *source,
726 GMainContext *context)
729 source->prev->next = source->next;
731 context->source_list = source->next;
734 source->next->prev = source->prev;
742 * @source: a #GSource
743 * @context: a #GMainContext (if %NULL, the default context will be used)
745 * Adds a #GSource to a @context so that it will be executed within
748 * Return value: the ID for the source within the #GMainContext
751 g_source_attach (GSource *source,
752 GMainContext *context)
757 g_return_val_if_fail (source->context == NULL, 0);
758 g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
761 context = g_main_context_default ();
763 LOCK_CONTEXT (context);
765 source->context = context;
766 result = source->id = context->next_id++;
769 g_source_list_add (source, context);
771 tmp_list = source->poll_fds;
774 g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
775 tmp_list = tmp_list->next;
778 #ifdef G_THREADS_ENABLED
779 /* Now wake up the main loop if it is waiting in the poll() */
780 g_main_context_wakeup (context);
783 UNLOCK_CONTEXT (context);
789 g_source_destroy_internal (GSource *source,
790 GMainContext *context,
794 LOCK_CONTEXT (context);
796 if (!SOURCE_DESTROYED (source))
799 gpointer old_cb_data;
800 GSourceCallbackFuncs *old_cb_funcs;
802 source->flags &= ~G_HOOK_FLAG_ACTIVE;
804 old_cb_data = source->callback_data;
805 old_cb_funcs = source->callback_funcs;
807 source->callback_data = NULL;
808 source->callback_funcs = NULL;
812 UNLOCK_CONTEXT (context);
813 old_cb_funcs->unref (old_cb_data);
814 LOCK_CONTEXT (context);
817 tmp_list = source->poll_fds;
820 g_main_context_remove_poll_unlocked (context, tmp_list->data);
821 tmp_list = tmp_list->next;
824 g_source_unref_internal (source, context, TRUE);
828 UNLOCK_CONTEXT (context);
833 * @source: a #GSource
835 * Remove a source from its #GMainContext, if any, and mark it as
836 * destroyed. The source cannot be subsequently added to another
840 g_source_destroy (GSource *source)
842 GMainContext *context;
844 g_return_if_fail (source != NULL);
846 context = source->context;
849 g_source_destroy_internal (source, context, FALSE);
851 source->flags &= ~G_HOOK_FLAG_ACTIVE;
856 * @source: a #GSource
858 * Return the numeric ID for a particular source. The ID of a source
859 * is unique within a particular main loop context. The reverse
860 * mapping from ID to source is done by g_main_context_find_source_by_id().
862 * Return value: the ID for the source
865 g_source_get_id (GSource *source)
869 g_return_val_if_fail (source != NULL, 0);
870 g_return_val_if_fail (source->context != NULL, 0);
872 LOCK_CONTEXT (source->context);
874 UNLOCK_CONTEXT (source->context);
880 * g_source_get_context:
881 * @source: a #GSource
883 * Get the #GMainContext with which the source is associated.
884 * Calling this function on a destroyed source is an error.
886 * Return value: the #GMainContext with which the source is associated,
887 * or %NULL if the context has not yet been added
891 g_source_get_context (GSource *source)
893 g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
895 return source->context;
899 * g_main_source_add_poll:
901 * @fd: a #GPollFD structure holding information about a file
902 * descriptor to watch.
904 * Add a file descriptor to the set of file descriptors polled * for
905 * this source. This is usually combined with g_source_new() to add an
906 * event source. The event source's check function will typically test
907 * the revents field in the #GPollFD struct and return %TRUE if events need
911 g_source_add_poll (GSource *source,
914 GMainContext *context;
916 g_return_if_fail (source != NULL);
917 g_return_if_fail (fd != NULL);
918 g_return_if_fail (!SOURCE_DESTROYED (source));
920 context = source->context;
923 LOCK_CONTEXT (context);
925 source->poll_fds = g_slist_prepend (source->poll_fds, fd);
929 g_main_context_add_poll_unlocked (context, source->priority, fd);
930 UNLOCK_CONTEXT (context);
935 * g_source_set_callback_indirect:
936 * @source: the source
937 * @callback_data: pointer to callback data "object"
938 * @callback_funcs: functions for reference counting callback_data
939 * and getting the callback and data
941 * Set the callback function storing the data as a refcounted callback
942 * "object". This is used to implement g_source_set_callback_closure()
943 * and internally. Note that calling g_source_set_callback_indirect() assumes
944 * an initial reference count on @callback_data, and thus
945 * @callback_funcs->unref will eventually be called once more
946 * than @callback_funcs->ref.
949 g_source_set_callback_indirect (GSource *source,
950 gpointer callback_data,
951 GSourceCallbackFuncs *callback_funcs)
953 GMainContext *context;
954 gpointer old_cb_data;
955 GSourceCallbackFuncs *old_cb_funcs;
957 g_return_if_fail (source != NULL);
958 g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
960 context = source->context;
963 LOCK_CONTEXT (context);
965 old_cb_data = source->callback_data;
966 old_cb_funcs = source->callback_funcs;
968 source->callback_data = callback_data;
969 source->callback_funcs = callback_funcs;
972 UNLOCK_CONTEXT (context);
975 old_cb_funcs->unref (old_cb_data);
979 g_source_callback_ref (gpointer cb_data)
981 GSourceCallback *callback = cb_data;
983 callback->ref_count++;
988 g_source_callback_unref (gpointer cb_data)
990 GSourceCallback *callback = cb_data;
992 callback->ref_count--;
993 if (callback->ref_count == 0)
995 if (callback->notify)
996 callback->notify (callback->data);
1002 g_source_callback_get (gpointer cb_data,
1006 GSourceCallback *callback = cb_data;
1008 *func = callback->func;
1009 *data = callback->data;
1012 static GSourceCallbackFuncs g_source_callback_funcs = {
1013 g_source_callback_ref,
1014 g_source_callback_unref,
1015 g_source_callback_get,
1019 * g_source_set_callback:
1020 * @source: the source
1021 * @func: a callback function
1022 * @data: the data to pass to callback function
1023 * @notify: a function to call when @data is no longer in use, or %NULL.
1025 * Set the callback function for a source.
1028 g_source_set_callback (GSource *source,
1031 GDestroyNotify notify)
1033 GSourceCallback *new_callback;
1035 g_return_if_fail (source != NULL);
1037 new_callback = g_new (GSourceCallback, 1);
1039 new_callback->ref_count = 1;
1040 new_callback->func = func;
1041 new_callback->data = data;
1042 new_callback->notify = notify;
1044 g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
1048 * g_source_set_priority:
1049 * @source: a #GSource
1050 * @priority: the new priority.
1052 * Set the priority of a source. While the main loop is being
1053 * run, a source will
1056 g_source_set_priority (GSource *source,
1060 GMainContext *context;
1062 g_return_if_fail (source != NULL);
1064 context = source->context;
1067 LOCK_CONTEXT (context);
1069 source->priority = priority;
1073 source->next = NULL;
1074 source->prev = NULL;
1076 tmp_list = source->poll_fds;
1079 g_main_context_remove_poll_unlocked (context, tmp_list->data);
1080 g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1082 tmp_list = tmp_list->next;
1085 UNLOCK_CONTEXT (source->context);
1090 * g_source_get_priority:
1091 * @source: a #GSource
1093 * Gets the priority of a surce
1095 * Return value: the priority of the source
1098 g_source_get_priority (GSource *source)
1100 g_return_val_if_fail (source != NULL, 0);
1102 return source->priority;
1106 * g_source_set_can_recurse:
1107 * @source: a #GSource
1108 * @can_recurse: whether recursion is allowed for this source
1110 * Sets whether a source can be called recursively. If @can_recurse is
1111 * %TRUE, then while the source is being dispatched then this source
1112 * will be processed normally. Otherwise, all processing of this
1113 * source is blocked until the dispatch function returns.
1116 g_source_set_can_recurse (GSource *source,
1117 gboolean can_recurse)
1119 GMainContext *context;
1121 g_return_if_fail (source != NULL);
1123 context = source->context;
1126 LOCK_CONTEXT (context);
1129 source->flags |= G_SOURCE_CAN_RECURSE;
1131 source->flags &= ~G_SOURCE_CAN_RECURSE;
1134 UNLOCK_CONTEXT (context);
1138 * g_source_get_can_recurse:
1139 * @source: a #GSource
1141 * Checks whether a source is allowed to be called recursively.
1142 * see g_source_set_can_recurse.
1144 * Return value: whether recursion is allowed.
1147 g_source_get_can_recurse (GSource *source)
1149 g_return_val_if_fail (source != NULL, FALSE);
1151 return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1156 * @source: a #GSource
1158 * Increases the reference count on a source by one.
1160 * Return value: @source
1163 g_source_ref (GSource *source)
1165 GMainContext *context;
1167 g_return_val_if_fail (source != NULL, NULL);
1169 context = source->context;
1172 LOCK_CONTEXT (context);
1174 source->ref_count++;
1177 UNLOCK_CONTEXT (context);
1182 /* g_source_unref() but possible to call within context lock
1185 g_source_unref_internal (GSource *source,
1186 GMainContext *context,
1189 gpointer old_cb_data = NULL;
1190 GSourceCallbackFuncs *old_cb_funcs = NULL;
1192 g_return_if_fail (source != NULL);
1194 if (!have_lock && context)
1195 LOCK_CONTEXT (context);
1197 source->ref_count--;
1198 if (source->ref_count == 0)
1200 old_cb_data = source->callback_data;
1201 old_cb_funcs = source->callback_funcs;
1203 source->callback_data = NULL;
1204 source->callback_funcs = NULL;
1206 if (context && !SOURCE_DESTROYED (source))
1208 g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1209 source->ref_count++;
1212 g_source_list_remove (source, context);
1214 if (source->source_funcs->destroy)
1215 source->source_funcs->destroy (source);
1217 g_slist_free (source->poll_fds);
1218 source->poll_fds = NULL;
1222 if (!have_lock && context)
1223 UNLOCK_CONTEXT (context);
1228 UNLOCK_CONTEXT (context);
1230 old_cb_funcs->unref (old_cb_data);
1233 LOCK_CONTEXT (context);
1239 * @source: a #GSource
1241 * Decreases the reference count of a source by one. If the
1242 * resulting reference count is zero the source and associated
1243 * memory will be destroyed.
1246 g_source_unref (GSource *source)
1248 g_return_if_fail (source != NULL);
1250 g_source_unref_internal (source, source->context, FALSE);
1254 * g_main_context_find_source_by_id:
1255 * @context: a #GMainContext (if %NULL, the default context will be used)
1256 * @id: the source ID, as returned by g_source_get_id()
1258 * Finds a #GSource given a pair of context and ID
1260 * Return value: the #GSource if found, otherwise, %NULL
1263 g_main_context_find_source_by_id (GMainContext *context,
1268 g_return_val_if_fail (id > 0, FALSE);
1270 if (context == NULL)
1271 context = g_main_context_default ();
1273 LOCK_CONTEXT (context);
1275 source = context->source_list;
1278 if (!SOURCE_DESTROYED (source) &&
1281 source = source->next;
1284 UNLOCK_CONTEXT (context);
1290 * g_main_context_find_source_by_funcs_user_data:
1291 * @context: a #GMainContext (if %NULL, the default context will be used).
1292 * @funcs: the @source_funcs passed to g_source_new().
1293 * @user_data: the user data from the callback.
1295 * Finds a source with the given source functions and user data. If
1296 * multiple sources exist with the same source function and user data,
1297 * the first one found will be returned.
1299 * Return value: the source, if one was found, otherwise %NULL
1302 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1303 GSourceFuncs *funcs,
1308 g_return_val_if_fail (funcs != NULL, FALSE);
1310 if (context == NULL)
1311 context = g_main_context_default ();
1313 LOCK_CONTEXT (context);
1315 source = context->source_list;
1318 if (!SOURCE_DESTROYED (source) &&
1319 source->source_funcs == funcs &&
1320 source->callback_data == user_data)
1322 source = source->next;
1325 UNLOCK_CONTEXT (context);
1331 * g_main_context_find_source_by_user_data:
1332 * @context: a #GMainContext
1333 * @user_data: the user_data for the callback.
1335 * Finds a source with the given user data for the callback. If
1336 * multiple sources exist with the same user data, the first
1337 * one found will be returned.
1339 * Return value: the source, if one was found, otherwise %NULL
1342 g_main_context_find_source_by_user_data (GMainContext *context,
1347 if (context == NULL)
1348 context = g_main_context_default ();
1350 LOCK_CONTEXT (context);
1352 source = context->source_list;
1355 if (!SOURCE_DESTROYED (source) &&
1356 source->callback_data == user_data)
1358 source = source->next;
1361 UNLOCK_CONTEXT (context);
1368 * @tag: the id of the source to remove.
1370 * Removes the source with the given id from the default main
1371 * context. The id of a #GSource is given by g_source_get_id(),
1372 * or will be returned by the functions g_source_attach(),
1373 * g_idle_add(), g_idle_add_full(), g_timeout_add(),
1374 * g_timeout_add_full(), g_io_add_watch, and g_io_add_watch_full().
1376 * See also g_source_destroy().
1378 * Return value: %TRUE if the source was found and removed.
1381 g_source_remove (guint tag)
1385 g_return_val_if_fail (tag > 0, FALSE);
1387 source = g_main_context_find_source_by_id (NULL, tag);
1389 g_source_destroy (source);
1391 return source != NULL;
1395 * g_source_remove_by_user_data:
1396 * @user_data: the user_data for the callback.
1398 * Removes a source from the default main loop context given the user
1399 * data for the callback. If multiple sources exist with the same user
1400 * data, only one will be destroyed.
1402 * Return value: %TRUE if a source was found and removed.
1405 g_source_remove_by_user_data (gpointer user_data)
1409 source = g_main_context_find_source_by_user_data (NULL, user_data);
1412 g_source_destroy (source);
1420 * g_source_remove_by_funcs_user_data:
1421 * @funcs: The @source_funcs passed to g_source_new()
1422 * @user_data: the user data for the callback
1424 * Removes a source from the default main loop context given the
1425 * source functions and user data. If multiple sources exist with the
1426 * same source functions and user data, only one will be destroyed.
1428 * Return value: %TRUE if a source was found and removed.
1431 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1436 g_return_val_if_fail (funcs != NULL, FALSE);
1438 source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1441 g_source_destroy (source);
1449 * g_get_current_time:
1450 * @result: #GTimeVal structure in which to store current time.
1452 * Equivalent to Unix's <function>gettimeofday()</function>, but portable
1455 g_get_current_time (GTimeVal *result)
1460 g_return_if_fail (result != NULL);
1462 /*this is required on alpha, there the timeval structs are int's
1463 not longs and a cast only would fail horribly*/
1464 gettimeofday (&r, NULL);
1465 result->tv_sec = r.tv_sec;
1466 result->tv_usec = r.tv_usec;
1468 /* Avoid calling time() except for the first time.
1469 * GetTickCount() should be pretty fast and low-level?
1470 * I could also use ftime() but it seems unnecessarily overheady.
1472 static DWORD start_tick = 0;
1473 static time_t start_time;
1476 g_return_if_fail (result != NULL);
1478 if (start_tick == 0)
1480 start_tick = GetTickCount ();
1484 tick = GetTickCount ();
1486 result->tv_sec = (tick - start_tick) / 1000 + start_time;
1487 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
1491 /* Running the main loop */
1493 /* HOLDS: context's lock */
1495 g_main_dispatch (GMainContext *context)
1499 for (i = 0; i < context->pending_dispatches->len; i++)
1501 GSource *source = context->pending_dispatches->pdata[i];
1503 context->pending_dispatches->pdata[i] = NULL;
1506 source->flags &= ~G_SOURCE_READY;
1508 if (!SOURCE_DESTROYED (source))
1510 gboolean was_in_call;
1511 gpointer user_data = NULL;
1512 GSourceFunc callback = NULL;
1513 GSourceCallbackFuncs *cb_funcs;
1515 gboolean need_destroy;
1517 gboolean (*dispatch) (GSource *,
1521 dispatch = source->source_funcs->dispatch;
1522 cb_funcs = source->callback_funcs;
1523 cb_data = source->callback_data;
1526 cb_funcs->ref (cb_data);
1528 was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1529 source->flags |= G_HOOK_FLAG_IN_CALL;
1531 UNLOCK_CONTEXT (context);
1534 cb_funcs->get (cb_data, &callback, &user_data);
1536 need_destroy = ! dispatch (source,
1539 LOCK_CONTEXT (context);
1542 cb_funcs->unref (cb_data);
1545 source->flags &= ~G_HOOK_FLAG_IN_CALL;
1547 /* Note: this depends on the fact that we can't switch
1548 * sources from one main context to another
1550 if (need_destroy && !SOURCE_DESTROYED (source))
1552 g_assert (source->context == context);
1553 g_source_destroy_internal (source, context, TRUE);
1557 SOURCE_UNREF (source, context);
1560 g_ptr_array_set_size (context->pending_dispatches, 0);
1563 /* Holds context's lock */
1564 static inline GSource *
1565 next_valid_source (GMainContext *context,
1568 GSource *new_source = source ? source->next : context->source_list;
1572 if (!SOURCE_DESTROYED (new_source))
1574 new_source->ref_count++;
1578 new_source = new_source->next;
1582 SOURCE_UNREF (source, context);
1589 * g_main_context_prepare:
1590 * @context: a #GMainContext
1591 * @priority: location to store priority of highest priority
1592 * source already ready.
1594 * Prepares to poll sources within a main loop. The resulting information
1595 * for polling is determined by calling g_main_context_query ().
1597 * Return value: %TRUE if some source is ready to be dispatched
1601 g_main_context_prepare (GMainContext *context,
1605 gint current_priority = G_MAXINT;
1608 if (context == NULL)
1609 context = g_main_context_default ();
1611 LOCK_CONTEXT (context);
1613 context->time_is_current = FALSE;
1615 if (context->in_check_or_prepare)
1617 g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
1618 "prepare() member.");
1622 #ifdef G_THREADS_ENABLED
1623 if (context->poll_waiting)
1625 g_warning("g_main_context_prepare(): main loop already active in another thread");
1626 UNLOCK_CONTEXT (context);
1630 context->poll_waiting = TRUE;
1631 #endif /* G_THREADS_ENABLED */
1634 /* If recursing, finish up current dispatch, before starting over */
1635 if (context->pending_dispatches)
1638 g_main_dispatch (context, ¤t_time);
1640 UNLOCK_CONTEXT (context);
1645 /* If recursing, clear list of pending dispatches */
1646 g_ptr_array_set_size (context->pending_dispatches, 0);
1648 /* Prepare all sources */
1650 context->timeout = -1;
1652 source = next_valid_source (context, NULL);
1655 gint source_timeout = -1;
1657 if ((n_ready > 0) && (source->priority > current_priority))
1659 SOURCE_UNREF (source, context);
1662 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
1665 if (!(source->flags & G_SOURCE_READY))
1668 gboolean (*prepare) (GSource *source,
1671 prepare = source->source_funcs->prepare;
1672 context->in_check_or_prepare++;
1673 UNLOCK_CONTEXT (context);
1675 result = (*prepare) (source, &source_timeout);
1677 LOCK_CONTEXT (context);
1678 context->in_check_or_prepare--;
1681 source->flags |= G_SOURCE_READY;
1684 if (source->flags & G_SOURCE_READY)
1687 current_priority = source->priority;
1688 context->timeout = 0;
1691 if (source_timeout >= 0)
1693 if (context->timeout < 0)
1694 context->timeout = source_timeout;
1696 context->timeout = MIN (context->timeout, source_timeout);
1700 source = next_valid_source (context, source);
1703 UNLOCK_CONTEXT (context);
1706 *priority = current_priority;
1708 return (n_ready > 0);
1712 * g_main_context_query:
1713 * @context: a #GMainContext
1714 * @max_priority: maximum priority source to check
1715 * @timeout: location to store timeout to be used in polling
1716 * @fds: location to store #GPollFD records that need to be polled.
1717 * @n_fds: length of @fds.
1719 * Determines information necessary to poll this main loop.
1724 g_main_context_query (GMainContext *context,
1733 LOCK_CONTEXT (context);
1735 pollrec = context->poll_records;
1737 while (pollrec && max_priority >= pollrec->priority)
1739 if (pollrec->fd->events)
1743 fds[n_poll].fd = pollrec->fd->fd;
1744 /* In direct contradiction to the Unix98 spec, IRIX runs into
1745 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
1746 * flags in the events field of the pollfd while it should
1747 * just ignoring them. So we mask them out here.
1749 fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
1750 fds[n_poll].revents = 0;
1755 pollrec = pollrec->next;
1758 #ifdef G_THREADS_ENABLED
1759 context->poll_changed = FALSE;
1764 *timeout = context->timeout;
1766 context->time_is_current = FALSE;
1769 UNLOCK_CONTEXT (context);
1775 * g_main_context_check:
1776 * @context: a #GMainContext
1777 * @max_priority: the maximum numerical priority of sources to check
1778 * @fds: array of #GPollFD's that was passed to the last call to
1779 * g_main_context_query()
1780 * @n_fds: return value of g_main_context_query()
1782 * Pass the results of polling back to the main loop.
1784 * Return value: %TRUE if some sources are ready to be dispatched.
1787 g_main_context_check (GMainContext *context,
1797 LOCK_CONTEXT (context);
1799 if (context->in_check_or_prepare)
1801 g_warning ("g_main_context_check() called recursively from within a source's check() or "
1802 "prepare() member.");
1806 #ifdef G_THREADS_ENABLED
1807 if (!context->poll_waiting)
1811 read (context->wake_up_pipe[0], &c, 1);
1815 context->poll_waiting = FALSE;
1817 /* If the set of poll file descriptors changed, bail out
1818 * and let the main loop rerun
1820 if (context->poll_changed)
1822 #endif /* G_THREADS_ENABLED */
1824 pollrec = context->poll_records;
1828 if (pollrec->fd->events)
1830 pollrec->fd->revents = fds[i].revents;
1833 pollrec = pollrec->next;
1836 source = next_valid_source (context, NULL);
1839 if ((n_ready > 0) && (source->priority > max_priority))
1841 SOURCE_UNREF (source, context);
1844 if ((source->flags & G_HOOK_FLAG_IN_CALL) && !(source->flags & G_SOURCE_CAN_RECURSE))
1847 if (!(source->flags & G_SOURCE_READY))
1850 gboolean (*check) (GSource *source);
1852 check = source->source_funcs->check;
1854 context->in_check_or_prepare++;
1855 UNLOCK_CONTEXT (context);
1857 result = (*check) (source);
1859 LOCK_CONTEXT (context);
1860 context->in_check_or_prepare--;
1863 source->flags |= G_SOURCE_READY;
1866 if (source->flags & G_SOURCE_READY)
1868 source->ref_count++;
1869 g_ptr_array_add (context->pending_dispatches, source);
1875 source = next_valid_source (context, source);
1878 UNLOCK_CONTEXT (context);
1884 * g_main_context_dispatch:
1885 * @context: a #GMainContext
1887 * Dispatch all pending sources()
1890 g_main_context_dispatch (GMainContext *context)
1892 LOCK_CONTEXT (context);
1894 if (context->pending_dispatches->len > 0)
1896 g_main_dispatch (context);
1899 UNLOCK_CONTEXT (context);
1903 g_main_context_iterate (GMainContext *context,
1909 gboolean some_ready;
1910 gint nfds, new_nfds;
1913 some_ready = g_main_context_prepare (context, &max_priority);
1917 LOCK_CONTEXT (context);
1919 if (context->cached_poll_array)
1921 nfds = context->cached_poll_array_size;
1922 fds = context->cached_poll_array;
1923 context->cached_poll_array = NULL;
1927 nfds = context->cached_poll_array_size = context->n_poll_records;
1928 fds = g_new (GPollFD, nfds);
1931 UNLOCK_CONTEXT (context);
1933 new_nfds = g_main_context_query (context, max_priority,
1934 &timeout, fds, nfds);
1936 while (new_nfds > nfds);
1941 g_main_context_poll (context, timeout, max_priority,
1944 g_main_context_check (context,
1948 LOCK_CONTEXT (context);
1950 g_assert (!context->cached_poll_array);
1952 context->cached_poll_array = fds;
1953 context->cached_poll_array_size = nfds;
1955 UNLOCK_CONTEXT (context);
1958 g_main_context_dispatch (context);
1964 * g_main_context_pending:
1965 * @context: a #GMainContext (if %NULL, the default context will be used)
1967 * Check if any sources have pending events for the given context.
1969 * Return value: %TRUE if events are pending.
1972 g_main_context_pending (GMainContext *context)
1975 context = g_main_context_default();
1977 return g_main_context_iterate (context, FALSE, FALSE);
1981 * g_main_context_iteration:
1982 * @context: a #GMainContext (if %NULL, the default context will be used)
1983 * @may_block: whether the call may block.
1985 * Run a single iteration for the given main loop. This involves
1986 * checking to see if any event sources are ready to be processed,
1987 * then if no events sources are ready and @may_block is %TRUE, waiting
1988 * for a source to become ready, then dispatching the highest priority
1989 * events sources that are ready. Note that even when @may_block is %TRUE,
1990 * it is still possible for g_main_context_iteration() to return
1991 * %FALSE, since the the wait may be interrupted for other
1992 * reasons than an event source becoming ready.
1994 * Return value: %TRUE if events were dispatched.
1997 g_main_context_iteration (GMainContext *context, gboolean may_block)
2000 context = g_main_context_default();
2002 return g_main_context_iterate (context, may_block, TRUE);
2007 * @context: a #GMainContext (if %NULL, the default context will be used).
2008 * @is_running: set to TRUE to indicate that the loop is running. This
2009 * is not very important since calling g_main_run() will set this to
2012 * Create a new #GMainLoop structure
2017 g_main_loop_new (GMainContext *context,
2018 gboolean is_running)
2023 context = g_main_context_default();
2025 loop = g_new0 (GMainLoop, 1);
2026 loop->context = context;
2027 loop->is_running = is_running != FALSE;
2028 loop->ref_count = 1;
2030 #ifdef G_THREADS_ENABLED
2031 if (g_thread_supported ())
2032 loop->mutex = g_mutex_new ();
2035 loop->sem_cond = NULL;
2036 #endif /* G_THREADS_ENABLED */
2043 * @loop: a #GMainLoop
2045 * Increase the reference count on a #GMainLoop object by one.
2047 * Return value: @loop
2050 g_main_loop_ref (GMainLoop *loop)
2052 g_return_val_if_fail (loop != NULL, NULL);
2062 main_loop_destroy (GMainLoop *loop)
2064 #ifdef G_THREADS_ENABLED
2065 g_mutex_free (loop->mutex);
2067 g_cond_free (loop->sem_cond);
2068 #endif /* G_THREADS_ENABLED */
2074 * g_main_loop_unref:
2075 * @loop: a #GMainLoop
2077 * Decreases the reference count on a #GMainLoop object by one. If
2078 * the result is zero, free the loop and free all associated memory.
2081 g_main_loop_unref (GMainLoop *loop)
2083 g_return_if_fail (loop != NULL);
2084 g_return_if_fail (loop->ref_count > 0);
2089 if (loop->ref_count == 0)
2091 /* When the ref_count is 0, there can be nobody else using the
2092 * loop, so it is safe to unlock before destroying.
2095 main_loop_destroy (loop);
2103 * @loop: a #GMainLoop
2105 * Run a main loop until g_main_quit() is called on the loop.
2106 * If this is called for the thread of the loop's #GMainContext,
2107 * it will process events from the loop, otherwise it will
2111 g_main_loop_run (GMainLoop *loop)
2113 g_return_if_fail (loop != NULL);
2115 /* The assumption here is that a reference is held to the loop
2116 * until we recursively iterate
2118 #ifdef G_THREADS_ENABLED
2119 if (loop->context->thread != g_thread_self ())
2125 if (!g_thread_supported ())
2127 g_warning ("g_main_loop_run() was called from second thread but"
2128 "g_thread_init() was never called.");
2132 if (!loop->sem_cond)
2133 loop->sem_cond = g_cond_new ();
2135 if (!loop->is_running)
2136 loop->is_running = TRUE;
2138 while (loop->is_running)
2139 g_cond_wait (loop->sem_cond, loop->mutex);
2143 #endif /* G_THREADS_ENABLED */
2145 LOCK_CONTEXT (loop->context);
2146 if (loop->context->in_check_or_prepare)
2148 g_warning ("g_main_run(): called recursively from within a source's check() or "
2149 "prepare() member, iteration not possible.");
2152 UNLOCK_CONTEXT (loop->context);
2157 loop->is_running = TRUE;
2158 while (loop->is_running)
2161 g_main_context_iterate (loop->context, TRUE, TRUE);
2166 /* We inline this here rather than calling g_main_loop_unref() to
2167 * avoid an extra unlock/lock.
2170 if (loop->ref_count == 0)
2173 main_loop_destroy (loop);
2181 * @loop: a #GMainLoop
2183 * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2184 * for the loop will return.
2187 g_main_loop_quit (GMainLoop *loop)
2189 g_return_if_fail (loop != NULL);
2192 loop->is_running = FALSE;
2194 #ifdef G_THREADS_ENABLED
2196 g_cond_broadcast (loop->sem_cond);
2201 LOCK_CONTEXT (loop->context);
2203 g_main_context_wakeup (loop->context);
2204 UNLOCK_CONTEXT (loop->context);
2208 * g_main_loop_is_running:
2209 * @loop: a #GMainLoop.
2211 * Check to see if the main loop is currently being run via g_main_run()
2213 * Return value: %TRUE if the mainloop is currently being run.
2216 g_main_loop_is_running (GMainLoop *loop)
2220 g_return_val_if_fail (loop != NULL, FALSE);
2223 result = loop->is_running;
2229 /* HOLDS: context's lock */
2231 g_main_context_poll (GMainContext *context,
2237 #ifdef G_MAIN_POLL_DEBUG
2243 GPollFunc poll_func;
2245 if (n_fds || timeout != 0)
2247 #ifdef G_MAIN_POLL_DEBUG
2248 g_print ("g_main_poll(%d) timeout: %d\n", n_fds, timeout);
2249 poll_timer = g_timer_new ();
2252 LOCK_CONTEXT (context);
2254 poll_func = context->poll_func;
2256 UNLOCK_CONTEXT (context);
2257 if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2258 g_warning ("poll(2) failed due to: %s.",
2259 g_strerror (errno));
2261 #ifdef G_MAIN_POLL_DEBUG
2262 LOCK_CONTEXT (context);
2264 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2267 g_timer_elapsed (poll_timer, NULL));
2268 g_timer_destroy (poll_timer);
2269 pollrec = context->poll_records;
2273 if (pollrec->fd->events)
2277 g_print (" [%d:", fds[i].fd);
2278 if (fds[i].revents & G_IO_IN)
2280 if (fds[i].revents & G_IO_OUT)
2282 if (fds[i].revents & G_IO_PRI)
2284 if (fds[i].revents & G_IO_ERR)
2286 if (fds[i].revents & G_IO_HUP)
2288 if (fds[i].revents & G_IO_NVAL)
2294 pollrec = pollrec->next;
2298 UNLOCK_CONTEXT (context);
2300 } /* if (n_fds || timeout != 0) */
2304 * g_main_context_add_poll:
2305 * @context: a #GMainContext (or %NULL for the default context)
2306 * @fd: a #GPollFD structure holding information about a file
2307 * descriptor to watch.
2308 * @priority: the priority for this file descriptor which should be
2309 * the same as the priority used for g_source_attach() to ensure that the
2310 * file descriptor is polled whenever the results may be needed.
2312 * Add a file descriptor to the set of file descriptors polled * for
2313 * this context. This will very seldom be used directly. Instead
2314 * a typical event source will use g_source_add_poll() instead.
2317 g_main_context_add_poll (GMainContext *context,
2322 context = g_main_context_default ();
2324 LOCK_CONTEXT (context);
2325 g_main_context_add_poll_unlocked (context, priority, fd);
2326 UNLOCK_CONTEXT (context);
2329 /* HOLDS: main_loop_lock */
2331 g_main_context_add_poll_unlocked (GMainContext *context,
2335 GPollRec *lastrec, *pollrec, *newrec;
2337 if (!context->poll_chunk)
2338 context->poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
2340 if (context->poll_free_list)
2342 newrec = context->poll_free_list;
2343 context->poll_free_list = newrec->next;
2346 newrec = g_chunk_new (GPollRec, context->poll_chunk);
2348 /* This file descriptor may be checked before we ever poll */
2351 newrec->priority = priority;
2354 pollrec = context->poll_records;
2355 while (pollrec && priority >= pollrec->priority)
2358 pollrec = pollrec->next;
2362 lastrec->next = newrec;
2364 context->poll_records = newrec;
2366 newrec->next = pollrec;
2368 context->n_poll_records++;
2369 if (context->cached_poll_array &&
2370 context->cached_poll_array_size < context->n_poll_records)
2372 g_free (context->cached_poll_array);
2373 context->cached_poll_array = NULL;
2376 #ifdef G_THREADS_ENABLED
2377 context->poll_changed = TRUE;
2379 /* Now wake up the main loop if it is waiting in the poll() */
2380 g_main_context_wakeup (context);
2385 * g_main_context_remove_poll:
2386 * @context:a #GMainContext
2387 * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
2389 * Remove file descriptor from the set of file descriptors to be
2390 * polled for a particular context.
2393 g_main_context_remove_poll (GMainContext *context,
2397 context = g_main_context_default ();
2399 LOCK_CONTEXT (context);
2401 g_main_context_remove_poll_unlocked (context, fd);
2403 UNLOCK_CONTEXT (context);
2407 g_main_context_remove_poll_unlocked (GMainContext *context,
2410 GPollRec *pollrec, *lastrec;
2413 pollrec = context->poll_records;
2417 if (pollrec->fd == fd)
2419 if (lastrec != NULL)
2420 lastrec->next = pollrec->next;
2422 context->poll_records = pollrec->next;
2424 #ifdef ENABLE_GC_FRIENDLY
2426 #endif /* ENABLE_GC_FRIENDLY */
2428 pollrec->next = context->poll_free_list;
2429 context->poll_free_list = pollrec;
2431 context->n_poll_records--;
2435 pollrec = pollrec->next;
2438 #ifdef G_THREADS_ENABLED
2439 context->poll_changed = TRUE;
2441 /* Now wake up the main loop if it is waiting in the poll() */
2442 g_main_context_wakeup (context);
2447 * g_source_get_current_time:
2448 * @source: a #GSource
2449 * @timeval: #GTimeVal structure in which to store current time.
2451 * Gets the "current time" to be used when checking
2452 * this source. The advantage of calling this function over
2453 * calling g_get_current_time() directly is that when
2454 * checking multiple sources, GLib can cache a single value
2455 * instead of having to repeatedly get the system time.
2458 g_source_get_current_time (GSource *source,
2461 GMainContext *context;
2463 g_return_if_fail (source->context != NULL);
2465 context = source->context;
2467 LOCK_CONTEXT (context);
2469 if (!context->time_is_current)
2471 g_get_current_time (&context->current_time);
2472 context->time_is_current = TRUE;
2475 *timeval = context->current_time;
2477 UNLOCK_CONTEXT (context);
2481 * g_main_context_set_poll_func:
2482 * @context: a #GMainContext
2483 * @func: the function to call to poll all file descriptors
2485 * Sets the function to use to handle polling of file descriptors. It
2486 * will be used instead of the poll() system call (or GLib's
2487 * replacement function, which is used where poll() isn't available).
2489 * This function could possibly be used to integrate the GLib event
2490 * loop with an external event loop.
2493 g_main_context_set_poll_func (GMainContext *context,
2497 context = g_main_context_default ();
2499 LOCK_CONTEXT (context);
2502 context->poll_func = func;
2506 context->poll_func = (GPollFunc) poll;
2508 context->poll_func = (GPollFunc) g_poll;
2512 UNLOCK_CONTEXT (context);
2516 * g_main_context_get_poll_func:
2517 * @context: a #GMainContext
2519 * Gets the poll function set by g_main_context_set_poll_func()
2521 * Return value: the poll function
2524 g_main_context_get_poll_func (GMainContext *context)
2529 context = g_main_context_default ();
2531 LOCK_CONTEXT (context);
2532 result = context->poll_func;
2533 UNLOCK_CONTEXT (context);
2538 /* HOLDS: context's lock */
2539 /* Wake the main loop up from a poll() */
2541 g_main_context_wakeup (GMainContext *context)
2543 #ifdef G_THREADS_ENABLED
2544 if (g_thread_supported() && context->poll_waiting)
2546 context->poll_waiting = FALSE;
2548 write (context->wake_up_pipe[1], "A", 1);
2550 ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
2559 g_timeout_set_expiration (GTimeoutSource *timeout_source,
2560 GTimeVal *current_time)
2562 guint seconds = timeout_source->interval / 1000;
2563 guint msecs = timeout_source->interval - seconds * 1000;
2565 timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
2566 timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
2567 if (timeout_source->expiration.tv_usec >= 1000000)
2569 timeout_source->expiration.tv_usec -= 1000000;
2570 timeout_source->expiration.tv_sec++;
2575 g_timeout_prepare (GSource *source,
2579 GTimeVal current_time;
2581 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2583 g_source_get_current_time (source, ¤t_time);
2585 msec = ((timeout_source->expiration.tv_sec - current_time.tv_sec) * 1000 +
2586 (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000);
2590 else if (msec > timeout_source->interval)
2592 /* The system time has been set backwards, so we
2593 * reset the expiration time to now + timeout_source->interval;
2594 * this at least avoids hanging for long periods of time.
2596 g_timeout_set_expiration (timeout_source, ¤t_time);
2597 msec = timeout_source->interval;
2606 g_timeout_check (GSource *source)
2608 GTimeVal current_time;
2609 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2611 g_source_get_current_time (source, ¤t_time);
2613 return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
2614 ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
2615 (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
2619 g_timeout_dispatch (GSource *source,
2620 GSourceFunc callback,
2623 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2627 g_warning ("Timeout source dispatched without callback\n"
2628 "You must call g_source_set_callback().");
2632 if (callback (user_data))
2634 GTimeVal current_time;
2636 g_source_get_current_time (source, ¤t_time);
2637 g_timeout_set_expiration (timeout_source, ¤t_time);
2646 * g_timeout_source_new:
2647 * @interval: the timeout interval in milliseconds.
2649 * Create a new timeout source.
2651 * The source will not initially be associated with any #GMainContext
2652 * and must be added to one with g_source_attach() before it will be
2655 * Return value: the newly create timeout source
2658 g_timeout_source_new (guint interval)
2660 GSource *source = g_source_new (&timeout_funcs, sizeof (GTimeoutSource));
2661 GTimeoutSource *timeout_source = (GTimeoutSource *)source;
2662 GTimeVal current_time;
2664 timeout_source->interval = interval;
2666 g_get_current_time (¤t_time);
2667 g_timeout_set_expiration (timeout_source, ¤t_time);
2673 * g_timeout_add_full:
2674 * @priority: the priority of the idle source. Typically this will be in the
2675 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
2676 * @interval: the time between calls to the function, in milliseconds
2677 * (1/1000ths of a second.)
2678 * @function: function to call
2679 * @data: data to pass to @function
2680 * @notify: function to call when the idle is removed, or %NULL
2682 * Sets a function to be called at regular intervals, with the given
2683 * priority. The function is called repeatedly until it returns
2684 * FALSE, at which point the timeout is automatically destroyed and
2685 * the function will not be called again. The @notify function is
2686 * called when the timeout is destroyed. The first call to the
2687 * function will be at the end of the first @interval.
2689 * Note that timeout functions may be delayed, due to the processing of other
2690 * event sources. Thus they should not be relied on for precise timing.
2691 * After each call to the timeout function, the time of the next
2692 * timeout is recalculated based on the current time and the given interval
2693 * (it does not try to 'catch up' time lost in delays).
2695 * Return value: the id of event source.
2698 g_timeout_add_full (gint priority,
2700 GSourceFunc function,
2702 GDestroyNotify notify)
2707 g_return_val_if_fail (function != NULL, 0);
2709 source = g_timeout_source_new (interval);
2711 if (priority != G_PRIORITY_DEFAULT)
2712 g_source_set_priority (source, priority);
2714 g_source_set_callback (source, function, data, notify);
2715 id = g_source_attach (source, NULL);
2716 g_source_unref (source);
2723 * @interval: the time between calls to the function, in milliseconds
2724 * (1/1000ths of a second.)
2725 * @function: function to call
2726 * @data: data to pass to @function
2728 * Sets a function to be called at regular intervals, with the default
2729 * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly
2730 * until it returns FALSE, at which point the timeout is automatically
2731 * destroyed and the function will not be called again. The @notify
2732 * function is called when the timeout is destroyed. The first call
2733 * to the function will be at the end of the first @interval.
2735 * Note that timeout functions may be delayed, due to the processing of other
2736 * event sources. Thus they should not be relied on for precise timing.
2737 * After each call to the timeout function, the time of the next
2738 * timeout is recalculated based on the current time and the given interval
2739 * (it does not try to 'catch up' time lost in delays).
2741 * Return value: the id of event source.
2744 g_timeout_add (guint32 interval,
2745 GSourceFunc function,
2748 return g_timeout_add_full (G_PRIORITY_DEFAULT,
2749 interval, function, data, NULL);
2752 /* Idle functions */
2755 g_idle_prepare (GSource *source,
2764 g_idle_check (GSource *source)
2770 g_idle_dispatch (GSource *source,
2771 GSourceFunc callback,
2776 g_warning ("Idle source dispatched without callback\n"
2777 "You must call g_source_set_callback().");
2781 return callback (user_data);
2785 * g_idle_source_new:
2787 * Create a new idle source.
2789 * The source will not initially be associated with any #GMainContext
2790 * and must be added to one with g_source_attach() before it will be
2793 * Return value: the newly created idle source
2796 g_idle_source_new (void)
2798 return g_source_new (&idle_funcs, sizeof (GSource));
2803 * @priority: the priority of the idle source. Typically this will be in the
2804 * range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
2805 * @function: function to call
2806 * @data: data to pass to @function
2807 * @notify: function to call when the idle is removed, or %NULL
2809 * Adds a function to be called whenever there are no higher priority
2810 * events pending. If the function returns FALSE it is automatically
2811 * removed from the list of event sources and will not be called again.
2813 * Return value: the id of the event source.
2816 g_idle_add_full (gint priority,
2817 GSourceFunc function,
2819 GDestroyNotify notify)
2824 g_return_val_if_fail (function != NULL, 0);
2826 source = g_idle_source_new ();
2828 if (priority != G_PRIORITY_DEFAULT)
2829 g_source_set_priority (source, priority);
2831 g_source_set_callback (source, function, data, notify);
2832 id = g_source_attach (source, NULL);
2833 g_source_unref (source);
2840 * @function: function to call
2841 * @data: data to pass to @function.
2843 * Adds a function to be called whenever there are no higher priority
2844 * events pending to the default main loop. The function is given the
2845 * default idle priority, #G_PRIORITY_DEFAULT_IDLE. If the function
2846 * returns FALSE it is automatically removed from the list of event
2847 * sources and will not be called again.
2849 * Return value: the id of the event source.
2852 g_idle_add (GSourceFunc function,
2855 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
2859 * g_idle_remove_by_data:
2860 * @data: the data for the idle source's callback.
2862 * Removes the idle function with the given data.
2864 * Return value: %TRUE if an idle source was found and removed.
2867 g_idle_remove_by_data (gpointer data)
2869 return g_source_remove_by_funcs_user_data (&idle_funcs, data);