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.
35 typedef struct _GIdleData GIdleData;
36 typedef struct _GTimeoutData GTimeoutData;
37 typedef struct _GSource GSource;
38 typedef struct _GPollRec GPollRec;
42 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
43 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
77 /* Forward declarations */
79 static void g_main_poll (gint timeout,
80 gboolean use_priority,
82 static void g_main_add_unlocking_poll (gint priority,
85 static gboolean g_timeout_prepare (gpointer source_data,
86 GTimeVal *current_time,
88 static gboolean g_timeout_check (gpointer source_data,
89 GTimeVal *current_time);
90 static gboolean g_timeout_dispatch (gpointer source_data,
91 GTimeVal *current_time,
93 static gboolean g_idle_prepare (gpointer source_data,
94 GTimeVal *current_time,
96 static gboolean g_idle_check (gpointer source_data,
97 GTimeVal *current_time);
98 static gboolean g_idle_dispatch (gpointer source_data,
99 GTimeVal *current_time,
104 static GSList *pending_dispatches = NULL;
105 static GHookList source_list = { 0 };
107 /* The following lock is used for both the list of sources
108 * and the list of poll records
110 G_LOCK_DECLARE_STATIC (main_loop);
112 static GSourceFuncs timeout_funcs = {
116 (GDestroyNotify)g_free
119 static GSourceFuncs idle_funcs = {
123 (GDestroyNotify)g_free
126 static GPollRec *poll_records = NULL;
127 static GPollRec *poll_free_list = NULL;
128 static GMemChunk *poll_chunk;
129 static guint n_poll_records = 0;
131 /* this pipe is used to wake up the main loop when a source is added.
133 static gint wake_up_pipe[2] = { -1, -1 };
134 static GPollFD wake_up_rec;
135 static gboolean poll_waiting = FALSE;
138 static GPollFunc poll_func = (GPollFunc) poll;
139 #else /* !HAVE_POLL */
141 /* The following implementation of poll() comes from the GNU C Library.
142 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
145 #include <string.h> /* for bzero on BSD systems */
147 #ifdef HAVE_SYS_SELECT_H
148 #include <sys/select.h>
149 #endif /* HAVE_SYS_SELECT_H_ */
152 # define SELECT_MASK fd_set
153 #else /* !NO_FD_SET */
155 typedef long fd_mask;
158 # define SELECT_MASK void
160 # define SELECT_MASK int
162 #endif /* !NO_FD_SET */
165 g_poll (GPollFD *fds, guint nfds, gint timeout)
168 SELECT_MASK rset, wset, xset;
177 for (f = fds; f < &fds[nfds]; ++f)
180 if (f->events & G_IO_IN)
181 FD_SET (f->fd, &rset);
182 if (f->events & G_IO_OUT)
183 FD_SET (f->fd, &wset);
184 if (f->events & G_IO_PRI)
185 FD_SET (f->fd, &xset);
186 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
190 tv.tv_sec = timeout / 1000;
191 tv.tv_usec = (timeout % 1000) * 1000;
193 ready = select (maxfd + 1, &rset, &wset, &xset,
194 timeout == -1 ? NULL : &tv);
196 for (f = fds; f < &fds[nfds]; ++f)
201 if (FD_ISSET (f->fd, &rset))
202 f->revents |= G_IO_IN;
203 if (FD_ISSET (f->fd, &wset))
204 f->revents |= G_IO_OUT;
205 if (FD_ISSET (f->fd, &xset))
206 f->revents |= G_IO_PRI;
212 static GPollFunc poll_func = g_poll;
213 #endif /* !HAVE_POLL */
215 /* Hooks for adding to the main loop */
217 /* Use knowledge of insert_sorted algorithm here to make
218 * sure we insert at the end of equal priority items
221 g_source_compare (GHook *a,
224 GSource *source_a = (GSource *)a;
225 GSource *source_b = (GSource *)b;
227 return (source_a->priority < source_b->priority) ? -1 : 1;
231 g_source_add (gint priority,
232 gboolean can_recurse,
234 gpointer source_data,
236 GDestroyNotify notify)
243 if (!source_list.is_setup)
244 g_hook_list_init (&source_list, sizeof(GSource));
246 source = (GSource *)g_hook_alloc (&source_list);
247 source->priority = priority;
248 source->source_data = source_data;
249 source->hook.func = funcs;
250 source->hook.data = user_data;
251 source->hook.destroy = notify;
253 g_hook_insert_sorted (&source_list,
258 source->hook.flags |= G_SOURCE_CAN_RECURSE;
260 return_val = source->hook.hook_id;
262 /* Now wake up the main loop if it is waiting in the poll() */
266 poll_waiting = FALSE;
267 write (wake_up_pipe[1], "A", 1);
270 G_UNLOCK (main_loop);
276 g_source_remove (guint tag)
282 hook = g_hook_get (&source_list, tag);
285 GSource *source = (GSource *)hook;
287 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
288 g_hook_destroy_link (&source_list, hook);
291 G_UNLOCK (main_loop);
295 g_source_remove_by_user_data (gpointer user_data)
301 hook = g_hook_find_data (&source_list, TRUE, user_data);
304 GSource *source = (GSource *)hook;
306 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
307 g_hook_destroy_link (&source_list, hook);
310 G_UNLOCK (main_loop);
314 g_source_find_source_data (GHook *hook,
317 GSource *source = (GSource *)hook;
319 return (source->source_data == data);
323 g_source_remove_by_source_data (gpointer source_data)
329 hook = g_hook_find (&source_list, TRUE,
330 g_source_find_source_data, source_data);
333 GSource *source = (GSource *)hook;
335 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
336 g_hook_destroy_link (&source_list, hook);
339 G_UNLOCK (main_loop);
343 g_get_current_time (GTimeVal *result)
345 g_return_if_fail (result != NULL);
347 gettimeofday ((struct timeval *) result, NULL);
350 /* Running the main loop */
352 /* HOLDS: main_loop_lock */
354 g_main_dispatch (GTimeVal *current_time)
356 while (pending_dispatches != NULL)
358 gboolean need_destroy;
359 GSource *source = pending_dispatches->data;
362 tmp_list = pending_dispatches;
363 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
364 g_slist_free_1 (tmp_list);
366 if (G_HOOK_IS_VALID (source))
368 gboolean was_in_call;
369 gpointer hook_data = source->hook.data;
370 gpointer source_data = source->source_data;
371 gboolean (*dispatch) (gpointer,
375 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
377 was_in_call = G_HOOK_IN_CALL (source);
378 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
380 G_UNLOCK (main_loop);
381 need_destroy = ! dispatch (source_data,
387 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
389 if (need_destroy && G_HOOK_IS_VALID (source))
391 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
392 g_hook_destroy_link (&source_list, (GHook *) source);
396 g_hook_unref (&source_list, (GHook *)source);
400 /* g_main_iterate () runs a single iteration of the mainloop, or,
401 * if !dispatch checks to see if any sources need dispatching.
402 * basic algorithm for dispatch=TRUE:
404 * 1) while the list of currently pending sources is non-empty,
405 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
406 * removing sources from the list after each returns.
407 * the return value of (*dispatch) determines whether the source
408 * itself is kept alive.
410 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
411 * are !IN_CALL or can_recurse. a return value of TRUE determines
412 * that the source would like to be dispatched immediatedly, it
413 * is then flagged as SOURCE_READY.
415 * 3) poll with the pollfds from all sources at the priority of the
416 * first source flagged as SOURCE_READY. if there are any sources
417 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
418 * of all timouts otherwise.
420 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
421 * (*check) returns true, add the source to the pending list.
422 * once one source returns true, stop after checking all sources
425 * 5) while the list of currently pending sources is non-empty,
426 * call (*dispatch) on each source, removing the source
431 g_main_iterate (gboolean block,
435 GTimeVal current_time;
437 gint current_priority = 0;
439 gboolean retval = FALSE;
441 g_return_val_if_fail (!block || dispatch, FALSE);
443 g_get_current_time (¤t_time);
447 /* If recursing, finish up current dispatch, before starting over */
448 if (pending_dispatches)
451 g_main_dispatch (¤t_time);
453 G_UNLOCK (main_loop);
458 /* Prepare all sources */
460 timeout = block ? -1 : 0;
462 hook = g_hook_first_valid (&source_list, TRUE);
465 GSource *source = (GSource *)hook;
469 if ((n_ready > 0) && (source->priority > current_priority))
471 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
473 hook = g_hook_next_valid (hook, TRUE);
477 g_hook_ref (&source_list, hook);
479 if (hook->flags & G_SOURCE_READY ||
480 ((GSourceFuncs *) hook->func)->prepare (source->source_data,
486 hook->flags |= G_SOURCE_READY;
487 g_hook_unref (&source_list, hook);
488 G_UNLOCK (main_loop);
494 hook->flags |= G_SOURCE_READY;
496 current_priority = source->priority;
501 if (source_timeout >= 0)
504 timeout = source_timeout;
506 timeout = MIN (timeout, source_timeout);
509 tmp = g_hook_next_valid (hook, TRUE);
511 g_hook_unref (&source_list, hook);
515 /* poll(), if necessary */
517 g_main_poll (timeout, n_ready > 0, current_priority);
519 /* Check to see what sources need to be dispatched */
523 hook = g_hook_first_valid (&source_list, TRUE);
526 GSource *source = (GSource *)hook;
529 if ((n_ready > 0) && (source->priority > current_priority))
531 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
533 hook = g_hook_next_valid (hook, TRUE);
537 g_hook_ref (&source_list, hook);
539 if (hook->flags & G_SOURCE_READY ||
540 ((GSourceFuncs *) hook->func)->check (source->source_data,
545 hook->flags &= ~G_SOURCE_READY;
546 g_hook_ref (&source_list, hook);
547 pending_dispatches = g_slist_prepend (pending_dispatches, source);
548 current_priority = source->priority;
553 g_hook_unref (&source_list, hook);
554 G_UNLOCK (main_loop);
560 tmp = g_hook_next_valid (hook, TRUE);
562 g_hook_unref (&source_list, hook);
566 /* Now invoke the callbacks */
568 if (pending_dispatches)
570 pending_dispatches = g_slist_reverse (pending_dispatches);
571 g_main_dispatch (¤t_time);
575 G_UNLOCK (main_loop);
580 /* See if any events are pending
585 return g_main_iterate (FALSE, FALSE);
588 /* Run a single iteration of the mainloop. If block is FALSE,
592 g_main_iteration (gboolean block)
594 return g_main_iterate (block, TRUE);
598 g_main_new (gboolean is_running)
602 loop = g_new0 (GMainLoop, 1);
603 loop->is_running = is_running != FALSE;
609 g_main_run (GMainLoop *loop)
611 g_return_if_fail (loop != NULL);
613 loop->is_running = TRUE;
614 while (loop->is_running)
615 g_main_iterate (TRUE, TRUE);
619 g_main_quit (GMainLoop *loop)
621 g_return_if_fail (loop != NULL);
623 loop->is_running = FALSE;
627 g_main_destroy (GMainLoop *loop)
629 g_return_if_fail (loop != NULL);
635 g_main_is_running (GMainLoop *loop)
637 g_return_val_if_fail (loop != NULL, FALSE);
639 return loop->is_running;
642 /* HOLDS: main_loop_lock */
644 g_main_poll (gint timeout,
645 gboolean use_priority,
648 GPollFD *fd_array = g_new (GPollFD, n_poll_records);
654 if (wake_up_pipe[0] < 0)
656 if (pipe (wake_up_pipe) < 0)
657 g_error ("Cannot create pipe main loop wake-up: %s\n",
660 wake_up_rec.fd = wake_up_pipe[0];
661 wake_up_rec.events = G_IO_IN;
662 g_main_add_unlocking_poll (0, &wake_up_rec);
665 pollrec = poll_records;
667 while (pollrec && (!use_priority || priority >= pollrec->priority))
669 fd_array[i].fd = pollrec->fd->fd;
670 fd_array[i].events = pollrec->fd->events;
671 fd_array[i].revents = 0;
673 pollrec = pollrec->next;
679 G_UNLOCK (main_loop);
681 (*poll_func) (fd_array, npoll, timeout);
687 read (wake_up_pipe[0], &c, 1);
690 poll_waiting = FALSE;
692 pollrec = poll_records;
696 pollrec->fd->revents = fd_array[i].revents;
697 pollrec = pollrec->next;
705 g_main_add_poll (GPollFD *fd,
709 g_main_add_unlocking_poll (priority, fd);
710 G_UNLOCK (main_loop);
713 /* HOLDS: main_loop_lock */
715 g_main_add_unlocking_poll (gint priority,
718 GPollRec *lastrec, *pollrec, *newrec;
721 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
725 newrec = poll_free_list;
726 poll_free_list = newrec->next;
729 newrec = g_chunk_new (GPollRec, poll_chunk);
732 newrec->priority = priority;
735 pollrec = poll_records;
736 while (pollrec && priority >= pollrec->priority)
739 pollrec = pollrec->next;
743 lastrec->next = newrec;
745 poll_records = newrec;
747 newrec->next = pollrec;
753 g_main_remove_poll (GPollFD *fd)
755 GPollRec *pollrec, *lastrec;
760 pollrec = poll_records;
764 if (pollrec->fd == fd)
767 lastrec->next = pollrec->next;
769 poll_records = pollrec->next;
771 pollrec->next = poll_free_list;
772 poll_free_list = pollrec;
778 pollrec = pollrec->next;
781 G_UNLOCK (main_loop);
785 g_main_set_poll_func (GPollFunc func)
791 poll_func = (GPollFunc)poll;
793 poll_func = (GPollFunc)g_poll;
800 g_timeout_prepare (gpointer source_data,
801 GTimeVal *current_time,
805 GTimeoutData *data = source_data;
807 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
808 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
810 *timeout = (msec <= 0) ? 0 : msec;
816 g_timeout_check (gpointer source_data,
817 GTimeVal *current_time)
819 GTimeoutData *data = source_data;
821 return (data->expiration.tv_sec < current_time->tv_sec) ||
822 ((data->expiration.tv_sec == current_time->tv_sec) &&
823 (data->expiration.tv_usec <= current_time->tv_usec));
827 g_timeout_dispatch (gpointer source_data,
828 GTimeVal *current_time,
831 GTimeoutData *data = source_data;
833 if (data->callback(user_data))
835 data->expiration.tv_sec = current_time->tv_sec;
836 data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
837 if (data->expiration.tv_usec >= 1000000)
839 data->expiration.tv_usec -= 1000000;
840 data->expiration.tv_sec++;
849 g_timeout_add_full (gint priority,
851 GSourceFunc function,
853 GDestroyNotify notify)
855 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
857 timeout_data->interval = interval;
858 timeout_data->callback = function;
859 g_get_current_time (&timeout_data->expiration);
861 timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
862 if (timeout_data->expiration.tv_usec >= 1000000)
864 timeout_data->expiration.tv_usec -= 1000000;
865 timeout_data->expiration.tv_sec++;
868 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
872 g_timeout_add (guint32 interval,
873 GSourceFunc function,
876 return g_timeout_add_full (0, interval, function, data, NULL);
882 g_idle_prepare (gpointer source_data,
883 GTimeVal *current_time,
891 g_idle_check (gpointer source_data,
892 GTimeVal *current_time)
898 g_idle_dispatch (gpointer source_data,
899 GTimeVal *current_time,
902 GIdleData *data = source_data;
904 return (*data->callback)(user_data);
908 g_idle_add_full (gint priority,
909 GSourceFunc function,
911 GDestroyNotify notify)
913 GIdleData *idle_data = g_new (GIdleData, 1);
915 idle_data->callback = function;
917 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
921 g_idle_add (GSourceFunc function,
924 return g_idle_add_full (0, function, data, NULL);