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)
350 g_return_if_fail (result != NULL);
352 /*this is required on alpha, there the timeval structs are int's
353 not longs and a cast only would fail horribly*/
354 gettimeofday (&r, NULL);
355 result->tv_sec = r.tv_sec;
356 result->tv_usec = r.tv_usec;
359 /* Running the main loop */
361 /* HOLDS: main_loop_lock */
363 g_main_dispatch (GTimeVal *current_time)
365 while (pending_dispatches != NULL)
367 gboolean need_destroy;
368 GSource *source = pending_dispatches->data;
371 tmp_list = pending_dispatches;
372 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
373 g_slist_free_1 (tmp_list);
375 if (G_HOOK_IS_VALID (source))
377 gboolean was_in_call;
378 gpointer hook_data = source->hook.data;
379 gpointer source_data = source->source_data;
380 gboolean (*dispatch) (gpointer,
384 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
386 was_in_call = G_HOOK_IN_CALL (source);
387 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
389 G_UNLOCK (main_loop);
390 need_destroy = ! dispatch (source_data,
396 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
398 if (need_destroy && G_HOOK_IS_VALID (source))
400 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
401 g_hook_destroy_link (&source_list, (GHook *) source);
405 g_hook_unref (&source_list, (GHook *)source);
409 /* g_main_iterate () runs a single iteration of the mainloop, or,
410 * if !dispatch checks to see if any sources need dispatching.
411 * basic algorithm for dispatch=TRUE:
413 * 1) while the list of currently pending sources is non-empty,
414 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
415 * removing sources from the list after each returns.
416 * the return value of (*dispatch) determines whether the source
417 * itself is kept alive.
419 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
420 * are !IN_CALL or can_recurse. a return value of TRUE determines
421 * that the source would like to be dispatched immediatedly, it
422 * is then flagged as SOURCE_READY.
424 * 3) poll with the pollfds from all sources at the priority of the
425 * first source flagged as SOURCE_READY. if there are any sources
426 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
427 * of all timouts otherwise.
429 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
430 * (*check) returns true, add the source to the pending list.
431 * once one source returns true, stop after checking all sources
434 * 5) while the list of currently pending sources is non-empty,
435 * call (*dispatch) on each source, removing the source
440 g_main_iterate (gboolean block,
444 GTimeVal current_time ={ 0, 0 };
446 gint current_priority = 0;
448 gboolean retval = FALSE;
450 g_return_val_if_fail (!block || dispatch, FALSE);
452 g_get_current_time (¤t_time);
456 /* If recursing, finish up current dispatch, before starting over */
457 if (pending_dispatches)
460 g_main_dispatch (¤t_time);
462 G_UNLOCK (main_loop);
467 /* Prepare all sources */
469 timeout = block ? -1 : 0;
471 hook = g_hook_first_valid (&source_list, TRUE);
474 GSource *source = (GSource *)hook;
476 gint source_timeout = -1;
478 if ((n_ready > 0) && (source->priority > current_priority))
480 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
482 hook = g_hook_next_valid (&source_list, hook, TRUE);
486 g_hook_ref (&source_list, hook);
488 if (hook->flags & G_SOURCE_READY ||
489 ((GSourceFuncs *) hook->func)->prepare (source->source_data,
495 hook->flags |= G_SOURCE_READY;
496 g_hook_unref (&source_list, hook);
497 G_UNLOCK (main_loop);
503 hook->flags |= G_SOURCE_READY;
505 current_priority = source->priority;
510 if (source_timeout >= 0)
513 timeout = source_timeout;
515 timeout = MIN (timeout, source_timeout);
518 tmp = g_hook_next_valid (&source_list, hook, TRUE);
520 g_hook_unref (&source_list, hook);
524 /* poll(), if necessary */
526 g_main_poll (timeout, n_ready > 0, current_priority);
528 /* Check to see what sources need to be dispatched */
532 hook = g_hook_first_valid (&source_list, TRUE);
535 GSource *source = (GSource *)hook;
538 if ((n_ready > 0) && (source->priority > current_priority))
540 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
542 hook = g_hook_next_valid (&source_list, hook, TRUE);
546 g_hook_ref (&source_list, hook);
548 if (hook->flags & G_SOURCE_READY ||
549 ((GSourceFuncs *) hook->func)->check (source->source_data,
554 hook->flags &= ~G_SOURCE_READY;
555 g_hook_ref (&source_list, hook);
556 pending_dispatches = g_slist_prepend (pending_dispatches, source);
557 current_priority = source->priority;
562 g_hook_unref (&source_list, hook);
563 G_UNLOCK (main_loop);
569 tmp = g_hook_next_valid (&source_list, hook, TRUE);
571 g_hook_unref (&source_list, hook);
575 /* Now invoke the callbacks */
577 if (pending_dispatches)
579 pending_dispatches = g_slist_reverse (pending_dispatches);
580 g_main_dispatch (¤t_time);
584 G_UNLOCK (main_loop);
589 /* See if any events are pending
594 return g_main_iterate (FALSE, FALSE);
597 /* Run a single iteration of the mainloop. If block is FALSE,
601 g_main_iteration (gboolean block)
603 return g_main_iterate (block, TRUE);
607 g_main_new (gboolean is_running)
611 loop = g_new0 (GMainLoop, 1);
612 loop->is_running = is_running != FALSE;
618 g_main_run (GMainLoop *loop)
620 g_return_if_fail (loop != NULL);
622 loop->is_running = TRUE;
623 while (loop->is_running)
624 g_main_iterate (TRUE, TRUE);
628 g_main_quit (GMainLoop *loop)
630 g_return_if_fail (loop != NULL);
632 loop->is_running = FALSE;
636 g_main_destroy (GMainLoop *loop)
638 g_return_if_fail (loop != NULL);
644 g_main_is_running (GMainLoop *loop)
646 g_return_val_if_fail (loop != NULL, FALSE);
648 return loop->is_running;
651 /* HOLDS: main_loop_lock */
653 g_main_poll (gint timeout,
654 gboolean use_priority,
663 if (wake_up_pipe[0] < 0)
665 if (pipe (wake_up_pipe) < 0)
666 g_error ("Cannot create pipe main loop wake-up: %s\n",
669 wake_up_rec.fd = wake_up_pipe[0];
670 wake_up_rec.events = G_IO_IN;
671 g_main_add_poll_unlocked (0, &wake_up_rec);
674 fd_array = g_new (GPollFD, n_poll_records);
676 pollrec = poll_records;
678 while (pollrec && (!use_priority || priority >= pollrec->priority))
680 fd_array[i].fd = pollrec->fd->fd;
681 fd_array[i].events = pollrec->fd->events;
682 fd_array[i].revents = 0;
684 pollrec = pollrec->next;
690 G_UNLOCK (main_loop);
692 (*poll_func) (fd_array, npoll, timeout);
698 read (wake_up_pipe[0], &c, 1);
701 poll_waiting = FALSE;
703 pollrec = poll_records;
707 pollrec->fd->revents = fd_array[i].revents;
708 pollrec = pollrec->next;
716 g_main_add_poll (GPollFD *fd,
720 g_main_add_poll_unlocked (priority, fd);
721 G_UNLOCK (main_loop);
724 /* HOLDS: main_loop_lock */
726 g_main_add_poll_unlocked (gint priority,
729 GPollRec *lastrec, *pollrec, *newrec;
732 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
736 newrec = poll_free_list;
737 poll_free_list = newrec->next;
740 newrec = g_chunk_new (GPollRec, poll_chunk);
743 newrec->priority = priority;
746 pollrec = poll_records;
747 while (pollrec && priority >= pollrec->priority)
750 pollrec = pollrec->next;
754 lastrec->next = newrec;
756 poll_records = newrec;
758 newrec->next = pollrec;
764 g_main_remove_poll (GPollFD *fd)
766 GPollRec *pollrec, *lastrec;
771 pollrec = poll_records;
775 if (pollrec->fd == fd)
778 lastrec->next = pollrec->next;
780 poll_records = pollrec->next;
782 pollrec->next = poll_free_list;
783 poll_free_list = pollrec;
789 pollrec = pollrec->next;
792 G_UNLOCK (main_loop);
796 g_main_set_poll_func (GPollFunc func)
802 poll_func = (GPollFunc)poll;
804 poll_func = (GPollFunc)g_poll;
811 g_timeout_prepare (gpointer source_data,
812 GTimeVal *current_time,
816 GTimeoutData *data = source_data;
818 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
819 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
821 *timeout = (msec <= 0) ? 0 : msec;
827 g_timeout_check (gpointer source_data,
828 GTimeVal *current_time)
830 GTimeoutData *data = source_data;
832 return (data->expiration.tv_sec < current_time->tv_sec) ||
833 ((data->expiration.tv_sec == current_time->tv_sec) &&
834 (data->expiration.tv_usec <= current_time->tv_usec));
838 g_timeout_dispatch (gpointer source_data,
839 GTimeVal *current_time,
842 GTimeoutData *data = source_data;
844 if (data->callback(user_data))
846 guint seconds = data->interval / 1000;
847 guint msecs = data->interval - seconds * 1000;
849 data->expiration.tv_sec = current_time->tv_sec + seconds;
850 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
851 if (data->expiration.tv_usec >= 1000000)
853 data->expiration.tv_usec -= 1000000;
854 data->expiration.tv_sec++;
863 g_timeout_add_full (gint priority,
865 GSourceFunc function,
867 GDestroyNotify notify)
871 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
873 timeout_data->interval = interval;
874 timeout_data->callback = function;
875 g_get_current_time (&timeout_data->expiration);
877 seconds = timeout_data->interval / 1000;
878 msecs = timeout_data->interval - seconds * 1000;
880 timeout_data->expiration.tv_sec += seconds;
881 timeout_data->expiration.tv_usec += msecs * 1000;
882 if (timeout_data->expiration.tv_usec >= 1000000)
884 timeout_data->expiration.tv_usec -= 1000000;
885 timeout_data->expiration.tv_sec++;
888 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
892 g_timeout_add (guint32 interval,
893 GSourceFunc function,
896 return g_timeout_add_full (G_PRIORITY_DEFAULT,
897 interval, function, data, NULL);
903 g_idle_prepare (gpointer source_data,
904 GTimeVal *current_time,
912 g_idle_check (gpointer source_data,
913 GTimeVal *current_time)
919 g_idle_dispatch (gpointer source_data,
920 GTimeVal *current_time,
923 GIdleData *data = source_data;
925 return (*data->callback)(user_data);
929 g_idle_add_full (gint priority,
930 GSourceFunc function,
932 GDestroyNotify notify)
934 GIdleData *idle_data = g_new (GIdleData, 1);
936 idle_data->callback = function;
938 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
942 g_idle_add (GSourceFunc function,
945 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);