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 gpointer hook_data = source->hook.data;
369 gpointer source_data = source->source_data;
370 gboolean (*dispatch) (gpointer,
374 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
376 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
378 G_UNLOCK (main_loop);
379 need_destroy = ! dispatch (source_data,
384 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
387 g_hook_destroy_link (&source_list, (GHook *) source);
390 g_hook_unref (&source_list, (GHook *)source);
394 /* Run a single iteration of the mainloop, or, if !dispatch
395 * check to see if any events need dispatching, but don't
399 g_main_iterate (gboolean block,
403 GTimeVal current_time;
405 gint current_priority = 0;
407 gboolean retval = FALSE;
409 g_return_val_if_fail (!block || dispatch, FALSE);
411 g_get_current_time (¤t_time);
415 /* If recursing, finish up current dispatch, before starting over */
416 if (pending_dispatches)
419 g_main_dispatch (¤t_time);
421 G_UNLOCK (main_loop);
425 /* Prepare all sources */
427 timeout = block ? -1 : 0;
429 hook = g_hook_first_valid (&source_list, TRUE);
432 GSource *source = (GSource *)hook;
436 if ((nready > 0) && (source->priority > current_priority))
438 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
440 hook = g_hook_next_valid (hook, TRUE);
444 g_hook_ref (&source_list, hook);
446 if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
452 g_hook_unref (&source_list, hook);
453 G_UNLOCK (main_loop);
458 hook->flags |= G_SOURCE_READY;
460 current_priority = source->priority;
465 if (source_timeout >= 0)
468 timeout = source_timeout;
470 timeout = MIN (timeout, source_timeout);
473 tmp = g_hook_next_valid (hook, TRUE);
475 g_hook_unref (&source_list, hook);
479 /* poll(), if necessary */
481 g_main_poll (timeout, nready > 0, current_priority);
483 /* Check to see what sources need to be dispatched */
487 hook = g_hook_first_valid (&source_list, TRUE);
490 GSource *source = (GSource *)hook;
493 if ((nready > 0) && (source->priority > current_priority))
495 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
497 hook = g_hook_next_valid (hook, TRUE);
501 g_hook_ref (&source_list, hook);
503 if ((hook->flags & G_SOURCE_READY) ||
504 ((GSourceFuncs *)hook->func)->check (source->source_data,
509 hook->flags &= ~G_SOURCE_READY;
510 g_hook_ref (&source_list, hook);
511 pending_dispatches = g_slist_prepend (pending_dispatches, source);
512 current_priority = source->priority;
517 g_hook_unref (&source_list, hook);
518 G_UNLOCK (main_loop);
523 tmp = g_hook_next_valid (hook, TRUE);
525 g_hook_unref (&source_list, hook);
529 /* Now invoke the callbacks */
531 if (pending_dispatches)
533 pending_dispatches = g_slist_reverse (pending_dispatches);
534 g_main_dispatch (¤t_time);
538 G_UNLOCK (main_loop);
543 /* See if any events are pending
548 return g_main_iterate (FALSE, FALSE);
551 /* Run a single iteration of the mainloop. If block is FALSE,
555 g_main_iteration (gboolean block)
557 return g_main_iterate (block, TRUE);
563 GMainLoop *result = g_new (GMainLoop, 1);
564 result->flag = FALSE;
570 g_main_run (GMainLoop *loop)
574 g_main_iterate (TRUE, TRUE);
578 g_main_quit (GMainLoop *loop)
584 g_main_destroy (GMainLoop *loop)
589 /* HOLDS: main_loop_lock */
591 g_main_poll (gint timeout, gboolean use_priority, gint priority)
593 GPollFD *fd_array = g_new (GPollFD, n_poll_records);
599 if (wake_up_pipe[0] < 0)
601 if (pipe (wake_up_pipe) < 0)
602 g_error ("Cannot create pipe main loop wake-up: %s\n",
605 wake_up_rec.fd = wake_up_pipe[0];
606 wake_up_rec.events = G_IO_IN;
607 g_main_add_unlocking_poll (0, &wake_up_rec);
610 pollrec = poll_records;
612 while (pollrec && (!use_priority || priority >= pollrec->priority))
614 fd_array[i].fd = pollrec->fd->fd;
615 fd_array[i].events = pollrec->fd->events;
616 fd_array[i].revents = 0;
618 pollrec = pollrec->next;
624 G_UNLOCK (main_loop);
626 (*poll_func) (fd_array, npoll, timeout);
632 read (wake_up_pipe[0], &c, 1);
635 poll_waiting = FALSE;
637 pollrec = poll_records;
641 pollrec->fd->revents = fd_array[i].revents;
642 pollrec = pollrec->next;
650 g_main_add_poll (gint priority,
654 g_main_add_unlocking_poll (priority, fd);
655 G_UNLOCK (main_loop);
658 /* HOLDS: main_loop_lock */
660 g_main_add_unlocking_poll (gint priority,
663 GPollRec *lastrec, *pollrec, *newrec;
666 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
670 newrec = poll_free_list;
671 poll_free_list = newrec->next;
674 newrec = g_chunk_new (GPollRec, poll_chunk);
677 newrec->priority = priority;
680 pollrec = poll_records;
681 while (pollrec && priority >= pollrec->priority)
684 pollrec = pollrec->next;
688 lastrec->next = newrec;
690 poll_records = newrec;
692 newrec->next = pollrec;
698 g_main_remove_poll (GPollFD *fd)
700 GPollRec *pollrec, *lastrec;
705 pollrec = poll_records;
709 if (pollrec->fd == fd)
712 lastrec->next = pollrec->next;
714 poll_records = pollrec->next;
716 pollrec->next = poll_free_list;
717 poll_free_list = pollrec;
723 pollrec = pollrec->next;
726 G_UNLOCK (main_loop);
730 g_main_set_poll_func (GPollFunc func)
736 poll_func = (GPollFunc)poll;
738 poll_func = (GPollFunc)g_poll;
745 g_timeout_prepare (gpointer source_data,
746 GTimeVal *current_time,
750 GTimeoutData *data = source_data;
752 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
753 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
755 *timeout = (msec <= 0) ? 0 : msec;
761 g_timeout_check (gpointer source_data,
762 GTimeVal *current_time)
764 GTimeoutData *data = source_data;
766 return (data->expiration.tv_sec < current_time->tv_sec) ||
767 ((data->expiration.tv_sec == current_time->tv_sec) &&
768 (data->expiration.tv_usec <= current_time->tv_usec));
772 g_timeout_dispatch (gpointer source_data,
773 GTimeVal *current_time,
776 GTimeoutData *data = source_data;
778 if (data->callback(user_data))
780 data->expiration.tv_sec = current_time->tv_sec;
781 data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
782 if (data->expiration.tv_usec >= 1000000)
784 data->expiration.tv_usec -= 1000000;
785 data->expiration.tv_sec++;
794 g_timeout_add_full (gint priority,
796 GSourceFunc function,
798 GDestroyNotify notify)
800 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
802 timeout_data->interval = interval;
803 timeout_data->callback = function;
804 g_get_current_time (&timeout_data->expiration);
806 timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
807 if (timeout_data->expiration.tv_usec >= 1000000)
809 timeout_data->expiration.tv_usec -= 1000000;
810 timeout_data->expiration.tv_sec++;
813 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
817 g_timeout_add (guint32 interval,
818 GSourceFunc function,
821 return g_timeout_add_full (0, interval, function, data, NULL);
827 g_idle_prepare (gpointer source_data,
828 GTimeVal *current_time,
836 g_idle_check (gpointer source_data,
837 GTimeVal *current_time)
843 g_idle_dispatch (gpointer source_data,
844 GTimeVal *current_time,
847 GIdleData *data = source_data;
849 return (*data->callback)(user_data);
853 g_idle_add_full (gint priority,
854 GSourceFunc function,
856 GDestroyNotify notify)
858 GIdleData *idle_data = g_new (GIdleData, 1);
860 idle_data->callback = function;
862 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
866 g_idle_add (GSourceFunc function,
869 return g_idle_add_full (0, function, data, NULL);