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.
30 typedef struct _GIdleData GIdleData;
31 typedef struct _GTimeoutData GTimeoutData;
32 typedef struct _GSource GSource;
33 typedef struct _GPollRec GPollRec;
36 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
37 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
54 struct _GTimeoutData {
66 /* Forward declarations */
68 static void g_main_poll (gint timeout,
69 gboolean use_priority,
71 static gboolean g_timeout_prepare (gpointer source_data,
72 GTimeVal *current_time,
74 static gboolean g_timeout_check (gpointer source_data,
75 GTimeVal *current_time);
76 static gboolean g_timeout_dispatch (gpointer source_data,
77 GTimeVal *current_time,
79 static gboolean g_idle_prepare (gpointer source_data,
80 GTimeVal *current_time,
82 static gboolean g_idle_check (gpointer source_data,
83 GTimeVal *current_time);
84 static gboolean g_idle_dispatch (gpointer source_data,
85 GTimeVal *current_time,
90 static GSList *pending_dispatches = NULL;
91 static GHookList source_list = { 0 };
93 static GSourceFuncs timeout_funcs = {
97 (GDestroyNotify)g_free
100 static GSourceFuncs idle_funcs = {
104 (GDestroyNotify)g_free
108 static GPollFunc poll_func = (GPollFunc)poll;
111 /* The following implementation of poll() comes from the GNU C Library.
112 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
115 #include <string.h> /* for bzero on BSD systems */
117 #ifdef HAVE_SYS_SELECT_H
118 #include <sys/select.h>
119 #endif /* HAVE_SYS_SELECT_H_ */
122 # define SELECT_MASK fd_set
125 typedef long fd_mask;
128 # define SELECT_MASK void
130 # define SELECT_MASK int
135 g_poll (GPollFD *fds, guint nfds, gint timeout)
138 SELECT_MASK rset, wset, xset;
147 for (f = fds; f < &fds[nfds]; ++f)
150 if (f->events & G_IO_IN)
151 FD_SET (f->fd, &rset);
152 if (f->events & G_IO_OUT)
153 FD_SET (f->fd, &wset);
154 if (f->events & G_IO_PRI)
155 FD_SET (f->fd, &xset);
156 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
160 tv.tv_sec = timeout / 1000;
161 tv.tv_usec = (timeout % 1000) * 1000;
163 ready = select (maxfd + 1, &rset, &wset, &xset,
164 timeout == -1 ? NULL : &tv);
166 for (f = fds; f < &fds[nfds]; ++f)
171 if (FD_ISSET (f->fd, &rset))
172 f->revents |= G_IO_IN;
173 if (FD_ISSET (f->fd, &wset))
174 f->revents |= G_IO_OUT;
175 if (FD_ISSET (f->fd, &xset))
176 f->revents |= G_IO_PRI;
183 static GPollFunc poll_func = g_poll;
186 /* Hooks for adding to the main loop */
188 /* Use knowledge of insert_sorted algorithm here to make
189 * sure we insert at the end of equal priority items
192 g_source_compare (GHook *a, GHook *b)
194 GSource *source_a = (GSource *)a;
195 GSource *source_b = (GSource *)b;
197 return (source_a->priority < source_b->priority) ? -1 : 1;
201 g_source_add (gint priority,
202 gboolean can_recurse,
204 gpointer source_data,
206 GDestroyNotify notify)
210 if (!source_list.is_setup)
211 g_hook_list_init (&source_list, sizeof(GSource));
213 source = (GSource *)g_hook_alloc (&source_list);
214 source->priority = priority;
215 source->source_data = source_data;
216 source->hook.func = funcs;
217 source->hook.data = user_data;
218 source->hook.destroy = notify;
220 g_hook_insert_sorted (&source_list,
225 source->hook.flags |= G_SOURCE_CAN_RECURSE;
227 return source->hook.hook_id;
231 g_source_remove (guint tag)
233 GHook *hook = g_hook_get (&source_list, tag);
236 GSource *source = (GSource *)hook;
237 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
238 g_hook_destroy_link (&source_list, hook);
243 g_source_remove_by_user_data (gpointer user_data)
245 GHook *hook = g_hook_find_data (&source_list, TRUE, user_data);
248 GSource *source = (GSource *)hook;
249 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
250 g_hook_destroy_link (&source_list, hook);
255 g_source_find_source_data (GHook *hook,
258 GSource *source = (GSource *)hook;
259 return (source->source_data == data);
263 g_source_remove_by_source_data (gpointer source_data)
265 GHook *hook = g_hook_find (&source_list, TRUE,
266 g_source_find_source_data, source_data);
269 GSource *source = (GSource *)hook;
270 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
271 g_hook_destroy_link (&source_list, hook);
275 void g_get_current_time (GTimeVal *result)
277 gettimeofday ((struct timeval *)result, NULL);
280 /* Running the main loop */
283 g_main_dispatch (GTimeVal *current_time)
285 while (pending_dispatches != NULL)
287 gboolean need_destroy;
288 GSource *source = pending_dispatches->data;
291 tmp_list = pending_dispatches;
292 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
293 g_slist_free_1 (tmp_list);
295 if (G_HOOK_IS_VALID (source))
297 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
298 need_destroy = !((GSourceFuncs *)source->hook.func)->dispatch (source->source_data,
301 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
304 g_hook_destroy_link (&source_list, (GHook *)source);
307 g_hook_unref (&source_list, (GHook *)source);
311 /* Run a single iteration of the mainloop, or, if !dispatch
312 * check to see if any events need dispatching, but don't
316 g_main_iterate (gboolean block, gboolean dispatch)
319 GTimeVal current_time;
321 gint current_priority = 0;
324 g_return_val_if_fail (!block || dispatch, FALSE);
326 g_get_current_time (¤t_time);
328 /* If recursing, finish up current dispatch, before starting over */
329 if (pending_dispatches)
332 g_main_dispatch (¤t_time);
337 /* Prepare all sources */
339 timeout = block ? -1 : 0;
341 hook = g_hook_first_valid (&source_list, TRUE);
344 GSource *source = (GSource *)hook;
348 if ((nready > 0) && (source->priority > current_priority))
350 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
352 hook = g_hook_next_valid (hook, TRUE);
356 g_hook_ref (&source_list, hook);
358 if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
364 g_hook_unref (&source_list, hook);
369 hook->flags |= G_SOURCE_READY;
371 current_priority = source->priority;
376 if (source_timeout >= 0)
379 timeout = source_timeout;
381 timeout = MIN (timeout, source_timeout);
384 tmp = g_hook_next_valid (hook, TRUE);
386 g_hook_unref (&source_list, hook);
390 /* poll(), if necessary */
392 g_main_poll (timeout, nready > 0, current_priority);
394 /* Check to see what sources need to be dispatched */
398 hook = g_hook_first_valid (&source_list, TRUE);
401 GSource *source = (GSource *)hook;
404 if ((nready > 0) && (source->priority > current_priority))
406 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
408 hook = g_hook_next_valid (hook, TRUE);
412 g_hook_ref (&source_list, hook);
414 if ((hook->flags & G_SOURCE_READY) ||
415 ((GSourceFuncs *)hook->func)->check (source->source_data,
420 hook->flags &= ~G_SOURCE_READY;
421 g_hook_ref (&source_list, hook);
422 pending_dispatches = g_slist_prepend (pending_dispatches, source);
423 current_priority = source->priority;
428 g_hook_unref (&source_list, hook);
433 tmp = g_hook_next_valid (hook, TRUE);
435 g_hook_unref (&source_list, hook);
439 /* Now invoke the callbacks */
441 if (pending_dispatches)
443 pending_dispatches = g_slist_reverse (pending_dispatches);
444 g_main_dispatch (¤t_time);
451 /* See if any events are pending
456 return g_main_iterate (FALSE, FALSE);
459 /* Run a single iteration of the mainloop. If block is FALSE,
463 g_main_iteration (gboolean block)
465 return g_main_iterate (block, TRUE);
471 GMainLoop *result = g_new (GMainLoop, 1);
472 result->flag = FALSE;
478 g_main_run (GMainLoop *loop)
482 g_main_iterate (TRUE, TRUE);
486 g_main_quit (GMainLoop *loop)
492 g_main_destroy (GMainLoop *loop)
497 static GPollRec *poll_records = NULL;
498 static GPollRec *poll_free_list = NULL;
499 static GMemChunk *poll_chunk;
500 static guint n_poll_records = 0;
503 g_main_poll (gint timeout, gboolean use_priority, gint priority)
505 GPollFD *fd_array = g_new (GPollFD, n_poll_records);
511 pollrec = poll_records;
513 while (pollrec && (!use_priority || priority >= pollrec->priority))
515 fd_array[i].fd = pollrec->fd->fd;
516 fd_array[i].events = pollrec->fd->events;
517 fd_array[i].revents = 0;
519 pollrec = pollrec->next;
524 (*poll_func) (fd_array, npoll, timeout);
526 pollrec = poll_records;
530 pollrec->fd->revents = fd_array[i].revents;
531 pollrec = pollrec->next;
539 g_main_poll_add (gint priority,
542 GPollRec *lastrec, *pollrec, *newrec;
545 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
547 newrec = g_chunk_new (GPollRec, poll_chunk);
549 newrec->priority = priority;
552 pollrec = poll_records;
553 while (pollrec && priority >= pollrec->priority)
556 pollrec = pollrec->next;
560 lastrec->next = newrec;
562 poll_records = newrec;
564 newrec->next = pollrec;
570 g_main_poll_remove (GPollFD *fd)
572 GPollRec *pollrec, *lastrec;
575 pollrec = poll_records;
579 if (pollrec->fd == fd)
582 lastrec->next = pollrec->next;
584 poll_records = pollrec->next;
586 pollrec->next = poll_free_list;
587 poll_free_list = pollrec;
590 pollrec = pollrec->next;
597 g_main_set_poll_func (GPollFunc func)
603 poll_func = (GPollFunc)poll;
605 poll_func = (GPollFunc)g_poll;
612 g_timeout_prepare (gpointer source_data,
613 GTimeVal *current_time,
617 GTimeoutData *data = source_data;
619 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
620 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
622 *timeout = (msec <= 0) ? 0 : msec;
628 g_timeout_check (gpointer source_data,
629 GTimeVal *current_time)
631 GTimeoutData *data = source_data;
633 return (data->expiration.tv_sec < current_time->tv_sec) ||
634 ((data->expiration.tv_sec == current_time->tv_sec) &&
635 (data->expiration.tv_usec <= current_time->tv_usec));
639 g_timeout_dispatch (gpointer source_data,
640 GTimeVal *current_time,
643 GTimeoutData *data = source_data;
645 if (data->callback(user_data))
647 data->expiration.tv_sec = current_time->tv_sec;
648 data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
649 if (data->expiration.tv_usec >= 1000000)
651 data->expiration.tv_usec -= 1000000;
652 data->expiration.tv_sec++;
661 g_timeout_add_full (gint priority,
663 GSourceFunc function,
665 GDestroyNotify notify)
667 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
669 timeout_data->interval = interval;
670 timeout_data->callback = function;
671 g_get_current_time (&timeout_data->expiration);
673 timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
674 if (timeout_data->expiration.tv_usec >= 1000000)
676 timeout_data->expiration.tv_usec -= 1000000;
677 timeout_data->expiration.tv_sec++;
680 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
684 g_timeout_add (guint32 interval,
685 GSourceFunc function,
688 return g_timeout_add_full (0, interval, function, data, NULL);
694 g_idle_prepare (gpointer source_data,
695 GTimeVal *current_time,
703 g_idle_check (gpointer source_data,
704 GTimeVal *current_time)
710 g_idle_dispatch (gpointer source_data,
711 GTimeVal *current_time,
714 GIdleData *data = source_data;
716 return (*data->callback)(user_data);
720 g_idle_add_full (gint priority,
721 GSourceFunc function,
723 GDestroyNotify notify)
725 GIdleData *idle_data = g_new (GIdleData, 1);
727 idle_data->callback = function;
729 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
733 g_idle_add (GSourceFunc function,
736 return g_idle_add_full (0, function, data, NULL);