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
31 # include <sys/poll.h>
32 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
33 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
34 #endif /* GLIB_HAVE_SYS_POLL_H */
41 typedef struct _GIdleData GIdleData;
42 typedef struct _GTimeoutData GTimeoutData;
43 typedef struct _GSource GSource;
44 typedef struct _GPollRec GPollRec;
48 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
49 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
83 /* Forward declarations */
85 static void g_main_poll (gint timeout,
86 gboolean use_priority,
88 static void g_main_add_poll_unlocked (gint priority,
91 static gboolean g_timeout_prepare (gpointer source_data,
92 GTimeVal *current_time,
94 static gboolean g_timeout_check (gpointer source_data,
95 GTimeVal *current_time);
96 static gboolean g_timeout_dispatch (gpointer source_data,
97 GTimeVal *current_time,
99 static gboolean g_idle_prepare (gpointer source_data,
100 GTimeVal *current_time,
102 static gboolean g_idle_check (gpointer source_data,
103 GTimeVal *current_time);
104 static gboolean g_idle_dispatch (gpointer source_data,
105 GTimeVal *current_time,
110 static GSList *pending_dispatches = NULL;
111 static GHookList source_list = { 0 };
113 /* The following lock is used for both the list of sources
114 * and the list of poll records
116 G_LOCK_DECLARE_STATIC (main_loop);
118 static GSourceFuncs timeout_funcs = {
122 (GDestroyNotify)g_free
125 static GSourceFuncs idle_funcs = {
129 (GDestroyNotify)g_free
132 static GPollRec *poll_records = NULL;
133 static GPollRec *poll_free_list = NULL;
134 static GMemChunk *poll_chunk;
135 static guint n_poll_records = 0;
137 /* this pipe is used to wake up the main loop when a source is added.
139 static gint wake_up_pipe[2] = { -1, -1 };
140 static GPollFD wake_up_rec;
141 static gboolean poll_waiting = FALSE;
144 static GPollFunc poll_func = (GPollFunc) poll;
145 #else /* !HAVE_POLL */
147 /* The following implementation of poll() comes from the GNU C Library.
148 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
151 #include <string.h> /* for bzero on BSD systems */
153 #ifdef HAVE_SYS_SELECT_H
154 #include <sys/select.h>
155 #endif /* HAVE_SYS_SELECT_H_ */
158 # define SELECT_MASK fd_set
159 #else /* !NO_FD_SET */
161 typedef long fd_mask;
164 # define SELECT_MASK void
166 # define SELECT_MASK int
168 #endif /* !NO_FD_SET */
171 g_poll (GPollFD *fds, guint nfds, gint timeout)
174 SELECT_MASK rset, wset, xset;
183 for (f = fds; f < &fds[nfds]; ++f)
186 if (f->events & G_IO_IN)
187 FD_SET (f->fd, &rset);
188 if (f->events & G_IO_OUT)
189 FD_SET (f->fd, &wset);
190 if (f->events & G_IO_PRI)
191 FD_SET (f->fd, &xset);
192 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
196 tv.tv_sec = timeout / 1000;
197 tv.tv_usec = (timeout % 1000) * 1000;
199 ready = select (maxfd + 1, &rset, &wset, &xset,
200 timeout == -1 ? NULL : &tv);
202 for (f = fds; f < &fds[nfds]; ++f)
207 if (FD_ISSET (f->fd, &rset))
208 f->revents |= G_IO_IN;
209 if (FD_ISSET (f->fd, &wset))
210 f->revents |= G_IO_OUT;
211 if (FD_ISSET (f->fd, &xset))
212 f->revents |= G_IO_PRI;
218 static GPollFunc poll_func = g_poll;
219 #endif /* !HAVE_POLL */
221 /* Hooks for adding to the main loop */
223 /* Use knowledge of insert_sorted algorithm here to make
224 * sure we insert at the end of equal priority items
227 g_source_compare (GHook *a,
230 GSource *source_a = (GSource *)a;
231 GSource *source_b = (GSource *)b;
233 return (source_a->priority < source_b->priority) ? -1 : 1;
237 g_source_add (gint priority,
238 gboolean can_recurse,
240 gpointer source_data,
242 GDestroyNotify notify)
249 if (!source_list.is_setup)
250 g_hook_list_init (&source_list, sizeof(GSource));
252 source = (GSource *)g_hook_alloc (&source_list);
253 source->priority = priority;
254 source->source_data = source_data;
255 source->hook.func = funcs;
256 source->hook.data = user_data;
257 source->hook.destroy = notify;
259 g_hook_insert_sorted (&source_list,
264 source->hook.flags |= G_SOURCE_CAN_RECURSE;
266 return_val = source->hook.hook_id;
268 /* Now wake up the main loop if it is waiting in the poll() */
272 poll_waiting = FALSE;
273 write (wake_up_pipe[1], "A", 1);
276 G_UNLOCK (main_loop);
282 g_source_remove (guint tag)
288 hook = g_hook_get (&source_list, tag);
291 GSource *source = (GSource *)hook;
293 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
294 g_hook_destroy_link (&source_list, hook);
297 G_UNLOCK (main_loop);
301 g_source_remove_by_user_data (gpointer user_data)
307 hook = g_hook_find_data (&source_list, TRUE, user_data);
310 GSource *source = (GSource *)hook;
312 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
313 g_hook_destroy_link (&source_list, hook);
316 G_UNLOCK (main_loop);
320 g_source_find_source_data (GHook *hook,
323 GSource *source = (GSource *)hook;
325 return (source->source_data == data);
329 g_source_remove_by_source_data (gpointer source_data)
335 hook = g_hook_find (&source_list, TRUE,
336 g_source_find_source_data, source_data);
339 GSource *source = (GSource *)hook;
341 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
342 g_hook_destroy_link (&source_list, hook);
345 G_UNLOCK (main_loop);
349 g_get_current_time (GTimeVal *result)
352 g_return_if_fail (result != NULL);
354 /*this is required on alpha, there the timeval structs are int's
355 not longs and a cast only would fail horribly*/
356 gettimeofday (&r, NULL);
357 result->tv_sec = r.tv_sec;
358 result->tv_usec = r.tv_usec;
361 /* Running the main loop */
363 /* HOLDS: main_loop_lock */
365 g_main_dispatch (GTimeVal *current_time)
367 while (pending_dispatches != NULL)
369 gboolean need_destroy;
370 GSource *source = pending_dispatches->data;
373 tmp_list = pending_dispatches;
374 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
375 g_slist_free_1 (tmp_list);
377 if (G_HOOK_IS_VALID (source))
379 gboolean was_in_call;
380 gpointer hook_data = source->hook.data;
381 gpointer source_data = source->source_data;
382 gboolean (*dispatch) (gpointer,
386 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
388 was_in_call = G_HOOK_IN_CALL (source);
389 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
391 G_UNLOCK (main_loop);
392 need_destroy = ! dispatch (source_data,
398 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
400 if (need_destroy && G_HOOK_IS_VALID (source))
402 ((GSourceFuncs *) source->hook.func)->destroy (source->source_data);
403 g_hook_destroy_link (&source_list, (GHook *) source);
407 g_hook_unref (&source_list, (GHook*) source);
411 /* g_main_iterate () runs a single iteration of the mainloop, or,
412 * if !dispatch checks to see if any sources need dispatching.
413 * basic algorithm for dispatch=TRUE:
415 * 1) while the list of currently pending sources is non-empty,
416 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
417 * removing sources from the list after each returns.
418 * the return value of (*dispatch) determines whether the source
419 * itself is kept alive.
421 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
422 * are !IN_CALL or can_recurse. a return value of TRUE determines
423 * that the source would like to be dispatched immediatedly, it
424 * is then flagged as SOURCE_READY.
426 * 3) poll with the pollfds from all sources at the priority of the
427 * first source flagged as SOURCE_READY. if there are any sources
428 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
429 * of all timouts otherwise.
431 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
432 * (*check) returns true, add the source to the pending list.
433 * once one source returns true, stop after checking all sources
436 * 5) while the list of currently pending sources is non-empty,
437 * call (*dispatch) on each source, removing the source
442 g_main_iterate (gboolean block,
446 GTimeVal current_time ={ 0, 0 };
448 gint current_priority = 0;
450 gboolean retval = FALSE;
452 g_return_val_if_fail (!block || dispatch, FALSE);
454 g_get_current_time (¤t_time);
458 /* If recursing, finish up current dispatch, before starting over */
459 if (pending_dispatches)
462 g_main_dispatch (¤t_time);
464 G_UNLOCK (main_loop);
469 /* Prepare all sources */
471 timeout = block ? -1 : 0;
473 hook = g_hook_first_valid (&source_list, TRUE);
476 GSource *source = (GSource *)hook;
477 gint source_timeout = -1;
479 if ((n_ready > 0) && (source->priority > current_priority))
481 g_hook_unref (&source_list, hook);
484 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
486 hook = g_hook_next_valid (&source_list, hook, TRUE);
490 if (hook->flags & G_SOURCE_READY ||
491 ((GSourceFuncs *) hook->func)->prepare (source->source_data,
497 hook->flags |= G_SOURCE_READY;
498 g_hook_unref (&source_list, hook);
499 G_UNLOCK (main_loop);
505 hook->flags |= G_SOURCE_READY;
507 current_priority = source->priority;
512 if (source_timeout >= 0)
515 timeout = source_timeout;
517 timeout = MIN (timeout, source_timeout);
520 hook = g_hook_next_valid (&source_list, hook, TRUE);
523 /* poll(), if necessary */
525 g_main_poll (timeout, n_ready > 0, current_priority);
527 /* Check to see what sources need to be dispatched */
531 hook = g_hook_first_valid (&source_list, TRUE);
534 GSource *source = (GSource *)hook;
536 if ((n_ready > 0) && (source->priority > current_priority))
538 g_hook_unref (&source_list, hook);
541 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
543 hook = g_hook_next_valid (&source_list, hook, TRUE);
547 if (hook->flags & G_SOURCE_READY ||
548 ((GSourceFuncs *) hook->func)->check (source->source_data,
553 hook->flags &= ~G_SOURCE_READY;
554 g_hook_ref (&source_list, hook);
555 pending_dispatches = g_slist_prepend (pending_dispatches, source);
556 current_priority = source->priority;
561 g_hook_unref (&source_list, hook);
562 G_UNLOCK (main_loop);
568 hook = g_hook_next_valid (&source_list, hook, TRUE);
571 /* Now invoke the callbacks */
573 if (pending_dispatches)
575 pending_dispatches = g_slist_reverse (pending_dispatches);
576 g_main_dispatch (¤t_time);
580 G_UNLOCK (main_loop);
585 /* See if any events are pending
590 return g_main_iterate (FALSE, FALSE);
593 /* Run a single iteration of the mainloop. If block is FALSE,
597 g_main_iteration (gboolean block)
599 return g_main_iterate (block, TRUE);
603 g_main_new (gboolean is_running)
607 loop = g_new0 (GMainLoop, 1);
608 loop->is_running = is_running != FALSE;
614 g_main_run (GMainLoop *loop)
616 g_return_if_fail (loop != NULL);
618 loop->is_running = TRUE;
619 while (loop->is_running)
620 g_main_iterate (TRUE, TRUE);
624 g_main_quit (GMainLoop *loop)
626 g_return_if_fail (loop != NULL);
628 loop->is_running = FALSE;
632 g_main_destroy (GMainLoop *loop)
634 g_return_if_fail (loop != NULL);
640 g_main_is_running (GMainLoop *loop)
642 g_return_val_if_fail (loop != NULL, FALSE);
644 return loop->is_running;
647 /* HOLDS: main_loop_lock */
649 g_main_poll (gint timeout,
650 gboolean use_priority,
659 if (wake_up_pipe[0] < 0)
661 if (pipe (wake_up_pipe) < 0)
662 g_error ("Cannot create pipe main loop wake-up: %s\n",
665 wake_up_rec.fd = wake_up_pipe[0];
666 wake_up_rec.events = G_IO_IN;
667 g_main_add_poll_unlocked (0, &wake_up_rec);
670 fd_array = g_new (GPollFD, n_poll_records);
672 pollrec = poll_records;
674 while (pollrec && (!use_priority || priority >= pollrec->priority))
676 fd_array[i].fd = pollrec->fd->fd;
677 fd_array[i].events = pollrec->fd->events;
678 fd_array[i].revents = 0;
680 pollrec = pollrec->next;
686 G_UNLOCK (main_loop);
688 (*poll_func) (fd_array, npoll, timeout);
694 read (wake_up_pipe[0], &c, 1);
697 poll_waiting = FALSE;
699 pollrec = poll_records;
703 pollrec->fd->revents = fd_array[i].revents;
704 pollrec = pollrec->next;
712 g_main_add_poll (GPollFD *fd,
716 g_main_add_poll_unlocked (priority, fd);
717 G_UNLOCK (main_loop);
720 /* HOLDS: main_loop_lock */
722 g_main_add_poll_unlocked (gint priority,
725 GPollRec *lastrec, *pollrec, *newrec;
728 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
732 newrec = poll_free_list;
733 poll_free_list = newrec->next;
736 newrec = g_chunk_new (GPollRec, poll_chunk);
739 newrec->priority = priority;
742 pollrec = poll_records;
743 while (pollrec && priority >= pollrec->priority)
746 pollrec = pollrec->next;
750 lastrec->next = newrec;
752 poll_records = newrec;
754 newrec->next = pollrec;
760 g_main_remove_poll (GPollFD *fd)
762 GPollRec *pollrec, *lastrec;
767 pollrec = poll_records;
771 if (pollrec->fd == fd)
774 lastrec->next = pollrec->next;
776 poll_records = pollrec->next;
778 pollrec->next = poll_free_list;
779 poll_free_list = pollrec;
785 pollrec = pollrec->next;
788 G_UNLOCK (main_loop);
792 g_main_set_poll_func (GPollFunc func)
798 poll_func = (GPollFunc)poll;
800 poll_func = (GPollFunc)g_poll;
807 g_timeout_prepare (gpointer source_data,
808 GTimeVal *current_time,
812 GTimeoutData *data = source_data;
814 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
815 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
817 *timeout = (msec <= 0) ? 0 : msec;
823 g_timeout_check (gpointer source_data,
824 GTimeVal *current_time)
826 GTimeoutData *data = source_data;
828 return (data->expiration.tv_sec < current_time->tv_sec) ||
829 ((data->expiration.tv_sec == current_time->tv_sec) &&
830 (data->expiration.tv_usec <= current_time->tv_usec));
834 g_timeout_dispatch (gpointer source_data,
835 GTimeVal *current_time,
838 GTimeoutData *data = source_data;
840 if (data->callback(user_data))
842 guint seconds = data->interval / 1000;
843 guint msecs = data->interval - seconds * 1000;
845 data->expiration.tv_sec = current_time->tv_sec + seconds;
846 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
847 if (data->expiration.tv_usec >= 1000000)
849 data->expiration.tv_usec -= 1000000;
850 data->expiration.tv_sec++;
859 g_timeout_add_full (gint priority,
861 GSourceFunc function,
863 GDestroyNotify notify)
867 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
869 timeout_data->interval = interval;
870 timeout_data->callback = function;
871 g_get_current_time (&timeout_data->expiration);
873 seconds = timeout_data->interval / 1000;
874 msecs = timeout_data->interval - seconds * 1000;
876 timeout_data->expiration.tv_sec += seconds;
877 timeout_data->expiration.tv_usec += msecs * 1000;
878 if (timeout_data->expiration.tv_usec >= 1000000)
880 timeout_data->expiration.tv_usec -= 1000000;
881 timeout_data->expiration.tv_sec++;
884 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
888 g_timeout_add (guint32 interval,
889 GSourceFunc function,
892 return g_timeout_add_full (G_PRIORITY_DEFAULT,
893 interval, function, data, NULL);
899 g_idle_prepare (gpointer source_data,
900 GTimeVal *current_time,
908 g_idle_check (gpointer source_data,
909 GTimeVal *current_time)
915 g_idle_dispatch (gpointer source_data,
916 GTimeVal *current_time,
919 GIdleData *data = source_data;
921 return (*data->callback)(user_data);
925 g_idle_add_full (gint priority,
926 GSourceFunc function,
928 GDestroyNotify notify)
930 GIdleData *idle_data = g_new (GIdleData, 1);
932 idle_data->callback = function;
934 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
938 g_idle_add (GSourceFunc function,
941 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);