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 Library 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 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library 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.
28 #include <sys/types.h>
30 #ifdef GLIB_HAVE_SYS_POLL_H
32 #endif /* GLIB_HAVE_SYS_POLL_H */
39 typedef struct _GIdleData GIdleData;
40 typedef struct _GTimeoutData GTimeoutData;
41 typedef struct _GSource GSource;
42 typedef struct _GPollRec GPollRec;
46 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
47 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
81 /* Forward declarations */
83 static void g_main_poll (gint timeout,
84 gboolean use_priority,
86 static void g_main_add_poll_unlocked (gint priority,
89 static gboolean g_timeout_prepare (gpointer source_data,
90 GTimeVal *current_time,
92 static gboolean g_timeout_check (gpointer source_data,
93 GTimeVal *current_time);
94 static gboolean g_timeout_dispatch (gpointer source_data,
95 GTimeVal *current_time,
97 static gboolean g_idle_prepare (gpointer source_data,
98 GTimeVal *current_time,
100 static gboolean g_idle_check (gpointer source_data,
101 GTimeVal *current_time);
102 static gboolean g_idle_dispatch (gpointer source_data,
103 GTimeVal *current_time,
108 static GSList *pending_dispatches = NULL;
109 static GHookList source_list = { 0 };
111 /* The following lock is used for both the list of sources
112 * and the list of poll records
114 G_LOCK_DECLARE_STATIC (main_loop);
116 static GSourceFuncs timeout_funcs = {
120 (GDestroyNotify)g_free
123 static GSourceFuncs idle_funcs = {
127 (GDestroyNotify)g_free
130 static GPollRec *poll_records = NULL;
131 static GPollRec *poll_free_list = NULL;
132 static GMemChunk *poll_chunk;
133 static guint n_poll_records = 0;
135 /* this pipe is used to wake up the main loop when a source is added.
137 static gint wake_up_pipe[2] = { -1, -1 };
138 static GPollFD wake_up_rec;
139 static gboolean poll_waiting = FALSE;
142 static GPollFunc poll_func = (GPollFunc) poll;
143 #else /* !HAVE_POLL */
145 /* The following implementation of poll() comes from the GNU C Library.
146 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
149 #include <string.h> /* for bzero on BSD systems */
151 #ifdef HAVE_SYS_SELECT_H
152 #include <sys/select.h>
153 #endif /* HAVE_SYS_SELECT_H_ */
156 # define SELECT_MASK fd_set
157 #else /* !NO_FD_SET */
159 typedef long fd_mask;
162 # define SELECT_MASK void
164 # define SELECT_MASK int
166 #endif /* !NO_FD_SET */
169 g_poll (GPollFD *fds, guint nfds, gint timeout)
172 SELECT_MASK rset, wset, xset;
181 for (f = fds; f < &fds[nfds]; ++f)
184 if (f->events & G_IO_IN)
185 FD_SET (f->fd, &rset);
186 if (f->events & G_IO_OUT)
187 FD_SET (f->fd, &wset);
188 if (f->events & G_IO_PRI)
189 FD_SET (f->fd, &xset);
190 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
194 tv.tv_sec = timeout / 1000;
195 tv.tv_usec = (timeout % 1000) * 1000;
197 ready = select (maxfd + 1, &rset, &wset, &xset,
198 timeout == -1 ? NULL : &tv);
200 for (f = fds; f < &fds[nfds]; ++f)
205 if (FD_ISSET (f->fd, &rset))
206 f->revents |= G_IO_IN;
207 if (FD_ISSET (f->fd, &wset))
208 f->revents |= G_IO_OUT;
209 if (FD_ISSET (f->fd, &xset))
210 f->revents |= G_IO_PRI;
216 static GPollFunc poll_func = g_poll;
217 #endif /* !HAVE_POLL */
219 /* Hooks for adding to the main loop */
221 /* Use knowledge of insert_sorted algorithm here to make
222 * sure we insert at the end of equal priority items
225 g_source_compare (GHook *a,
228 GSource *source_a = (GSource *)a;
229 GSource *source_b = (GSource *)b;
231 return (source_a->priority < source_b->priority) ? -1 : 1;
235 g_source_add (gint priority,
236 gboolean can_recurse,
238 gpointer source_data,
240 GDestroyNotify notify)
247 if (!source_list.is_setup)
248 g_hook_list_init (&source_list, sizeof(GSource));
250 source = (GSource *)g_hook_alloc (&source_list);
251 source->priority = priority;
252 source->source_data = source_data;
253 source->hook.func = funcs;
254 source->hook.data = user_data;
255 source->hook.destroy = notify;
257 g_hook_insert_sorted (&source_list,
262 source->hook.flags |= G_SOURCE_CAN_RECURSE;
264 return_val = source->hook.hook_id;
266 /* Now wake up the main loop if it is waiting in the poll() */
270 poll_waiting = FALSE;
271 write (wake_up_pipe[1], "A", 1);
274 G_UNLOCK (main_loop);
280 g_source_remove (guint tag)
286 hook = g_hook_get (&source_list, tag);
289 GSource *source = (GSource *)hook;
291 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
292 g_hook_destroy_link (&source_list, hook);
295 G_UNLOCK (main_loop);
299 g_source_remove_by_user_data (gpointer user_data)
305 hook = g_hook_find_data (&source_list, TRUE, user_data);
308 GSource *source = (GSource *)hook;
310 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
311 g_hook_destroy_link (&source_list, hook);
314 G_UNLOCK (main_loop);
318 g_source_find_source_data (GHook *hook,
321 GSource *source = (GSource *)hook;
323 return (source->source_data == data);
327 g_source_remove_by_source_data (gpointer source_data)
333 hook = g_hook_find (&source_list, TRUE,
334 g_source_find_source_data, source_data);
337 GSource *source = (GSource *)hook;
339 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
340 g_hook_destroy_link (&source_list, hook);
343 G_UNLOCK (main_loop);
347 g_get_current_time (GTimeVal *result)
349 g_return_if_fail (result != NULL);
351 gettimeofday ((struct timeval *) result, NULL);
354 /* Running the main loop */
356 /* HOLDS: main_loop_lock */
358 g_main_dispatch (GTimeVal *current_time)
360 while (pending_dispatches != NULL)
362 gboolean need_destroy;
363 GSource *source = pending_dispatches->data;
366 tmp_list = pending_dispatches;
367 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
368 g_slist_free_1 (tmp_list);
370 if (G_HOOK_IS_VALID (source))
372 gboolean was_in_call;
373 gpointer hook_data = source->hook.data;
374 gpointer source_data = source->source_data;
375 gboolean (*dispatch) (gpointer,
379 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
381 was_in_call = G_HOOK_IN_CALL (source);
382 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
384 G_UNLOCK (main_loop);
385 need_destroy = ! dispatch (source_data,
391 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
393 if (need_destroy && G_HOOK_IS_VALID (source))
395 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
396 g_hook_destroy_link (&source_list, (GHook *) source);
400 g_hook_unref (&source_list, (GHook *)source);
404 /* g_main_iterate () runs a single iteration of the mainloop, or,
405 * if !dispatch checks to see if any sources need dispatching.
406 * basic algorithm for dispatch=TRUE:
408 * 1) while the list of currently pending sources is non-empty,
409 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
410 * removing sources from the list after each returns.
411 * the return value of (*dispatch) determines whether the source
412 * itself is kept alive.
414 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
415 * are !IN_CALL or can_recurse. a return value of TRUE determines
416 * that the source would like to be dispatched immediatedly, it
417 * is then flagged as SOURCE_READY.
419 * 3) poll with the pollfds from all sources at the priority of the
420 * first source flagged as SOURCE_READY. if there are any sources
421 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
422 * of all timouts otherwise.
424 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
425 * (*check) returns true, add the source to the pending list.
426 * once one source returns true, stop after checking all sources
429 * 5) while the list of currently pending sources is non-empty,
430 * call (*dispatch) on each source, removing the source
435 g_main_iterate (gboolean block,
439 GTimeVal current_time;
441 gint current_priority = 0;
443 gboolean retval = FALSE;
445 g_return_val_if_fail (!block || dispatch, FALSE);
447 g_get_current_time (¤t_time);
451 /* If recursing, finish up current dispatch, before starting over */
452 if (pending_dispatches)
455 g_main_dispatch (¤t_time);
457 G_UNLOCK (main_loop);
462 /* Prepare all sources */
464 timeout = block ? -1 : 0;
466 hook = g_hook_first_valid (&source_list, TRUE);
469 GSource *source = (GSource *)hook;
473 if ((n_ready > 0) && (source->priority > current_priority))
475 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
477 hook = g_hook_next_valid (hook, TRUE);
481 g_hook_ref (&source_list, hook);
483 if (hook->flags & G_SOURCE_READY ||
484 ((GSourceFuncs *) hook->func)->prepare (source->source_data,
490 hook->flags |= G_SOURCE_READY;
491 g_hook_unref (&source_list, hook);
492 G_UNLOCK (main_loop);
498 hook->flags |= G_SOURCE_READY;
500 current_priority = source->priority;
505 if (source_timeout >= 0)
508 timeout = source_timeout;
510 timeout = MIN (timeout, source_timeout);
513 tmp = g_hook_next_valid (hook, TRUE);
515 g_hook_unref (&source_list, hook);
519 /* poll(), if necessary */
521 g_main_poll (timeout, n_ready > 0, current_priority);
523 /* Check to see what sources need to be dispatched */
527 hook = g_hook_first_valid (&source_list, TRUE);
530 GSource *source = (GSource *)hook;
533 if ((n_ready > 0) && (source->priority > current_priority))
535 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
537 hook = g_hook_next_valid (hook, TRUE);
541 g_hook_ref (&source_list, hook);
543 if (hook->flags & G_SOURCE_READY ||
544 ((GSourceFuncs *) hook->func)->check (source->source_data,
549 hook->flags &= ~G_SOURCE_READY;
550 g_hook_ref (&source_list, hook);
551 pending_dispatches = g_slist_prepend (pending_dispatches, source);
552 current_priority = source->priority;
557 g_hook_unref (&source_list, hook);
558 G_UNLOCK (main_loop);
564 tmp = g_hook_next_valid (hook, TRUE);
566 g_hook_unref (&source_list, hook);
570 /* Now invoke the callbacks */
572 if (pending_dispatches)
574 pending_dispatches = g_slist_reverse (pending_dispatches);
575 g_main_dispatch (¤t_time);
579 G_UNLOCK (main_loop);
584 /* See if any events are pending
589 return g_main_iterate (FALSE, FALSE);
592 /* Run a single iteration of the mainloop. If block is FALSE,
596 g_main_iteration (gboolean block)
598 return g_main_iterate (block, TRUE);
602 g_main_new (gboolean is_running)
606 loop = g_new0 (GMainLoop, 1);
607 loop->is_running = is_running != FALSE;
613 g_main_run (GMainLoop *loop)
615 g_return_if_fail (loop != NULL);
617 loop->is_running = TRUE;
618 while (loop->is_running)
619 g_main_iterate (TRUE, TRUE);
623 g_main_quit (GMainLoop *loop)
625 g_return_if_fail (loop != NULL);
627 loop->is_running = FALSE;
631 g_main_destroy (GMainLoop *loop)
633 g_return_if_fail (loop != NULL);
639 g_main_is_running (GMainLoop *loop)
641 g_return_val_if_fail (loop != NULL, FALSE);
643 return loop->is_running;
646 /* HOLDS: main_loop_lock */
648 g_main_poll (gint timeout,
649 gboolean use_priority,
658 if (wake_up_pipe[0] < 0)
660 if (pipe (wake_up_pipe) < 0)
661 g_error ("Cannot create pipe main loop wake-up: %s\n",
664 wake_up_rec.fd = wake_up_pipe[0];
665 wake_up_rec.events = G_IO_IN;
666 g_main_add_poll_unlocked (0, &wake_up_rec);
669 fd_array = g_new (GPollFD, n_poll_records);
671 pollrec = poll_records;
673 while (pollrec && (!use_priority || priority >= pollrec->priority))
675 fd_array[i].fd = pollrec->fd->fd;
676 fd_array[i].events = pollrec->fd->events;
677 fd_array[i].revents = 0;
679 pollrec = pollrec->next;
685 G_UNLOCK (main_loop);
687 (*poll_func) (fd_array, npoll, timeout);
693 read (wake_up_pipe[0], &c, 1);
696 poll_waiting = FALSE;
698 pollrec = poll_records;
702 pollrec->fd->revents = fd_array[i].revents;
703 pollrec = pollrec->next;
711 g_main_add_poll (GPollFD *fd,
715 g_main_add_poll_unlocked (priority, fd);
716 G_UNLOCK (main_loop);
719 /* HOLDS: main_loop_lock */
721 g_main_add_poll_unlocked (gint priority,
724 GPollRec *lastrec, *pollrec, *newrec;
727 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
731 newrec = poll_free_list;
732 poll_free_list = newrec->next;
735 newrec = g_chunk_new (GPollRec, poll_chunk);
738 newrec->priority = priority;
741 pollrec = poll_records;
742 while (pollrec && priority >= pollrec->priority)
745 pollrec = pollrec->next;
749 lastrec->next = newrec;
751 poll_records = newrec;
753 newrec->next = pollrec;
759 g_main_remove_poll (GPollFD *fd)
761 GPollRec *pollrec, *lastrec;
766 pollrec = poll_records;
770 if (pollrec->fd == fd)
773 lastrec->next = pollrec->next;
775 poll_records = pollrec->next;
777 pollrec->next = poll_free_list;
778 poll_free_list = pollrec;
784 pollrec = pollrec->next;
787 G_UNLOCK (main_loop);
791 g_main_set_poll_func (GPollFunc func)
797 poll_func = (GPollFunc)poll;
799 poll_func = (GPollFunc)g_poll;
806 g_timeout_prepare (gpointer source_data,
807 GTimeVal *current_time,
811 GTimeoutData *data = source_data;
813 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
814 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
816 *timeout = (msec <= 0) ? 0 : msec;
822 g_timeout_check (gpointer source_data,
823 GTimeVal *current_time)
825 GTimeoutData *data = source_data;
827 return (data->expiration.tv_sec < current_time->tv_sec) ||
828 ((data->expiration.tv_sec == current_time->tv_sec) &&
829 (data->expiration.tv_usec <= current_time->tv_usec));
833 g_timeout_dispatch (gpointer source_data,
834 GTimeVal *current_time,
837 GTimeoutData *data = source_data;
839 if (data->callback(user_data))
841 guint seconds = data->interval / 1000;
842 guint msecs = data->interval - seconds * 1000;
844 data->expiration.tv_sec = current_time->tv_sec + seconds;
845 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
846 if (data->expiration.tv_usec >= 1000000)
848 data->expiration.tv_usec -= 1000000;
849 data->expiration.tv_sec++;
858 g_timeout_add_full (gint priority,
860 GSourceFunc function,
862 GDestroyNotify notify)
866 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
868 timeout_data->interval = interval;
869 timeout_data->callback = function;
870 g_get_current_time (&timeout_data->expiration);
872 seconds = timeout_data->interval / 1000;
873 msecs = timeout_data->interval - seconds * 1000;
875 timeout_data->expiration.tv_sec += seconds;
876 timeout_data->expiration.tv_usec += msecs * 1000;
877 if (timeout_data->expiration.tv_usec >= 1000000)
879 timeout_data->expiration.tv_usec -= 1000000;
880 timeout_data->expiration.tv_sec++;
883 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
887 g_timeout_add (guint32 interval,
888 GSourceFunc function,
891 return g_timeout_add_full (0, interval, function, data, NULL);
897 g_idle_prepare (gpointer source_data,
898 GTimeVal *current_time,
906 g_idle_check (gpointer source_data,
907 GTimeVal *current_time)
913 g_idle_dispatch (gpointer source_data,
914 GTimeVal *current_time,
917 GIdleData *data = source_data;
919 return (*data->callback)(user_data);
923 g_idle_add_full (gint priority,
924 GSourceFunc function,
926 GDestroyNotify notify)
928 GIdleData *idle_data = g_new (GIdleData, 1);
930 idle_data->callback = function;
932 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
936 g_idle_add (GSourceFunc function,
939 return g_idle_add_full (0, function, data, NULL);