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;
41 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
42 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
59 struct _GTimeoutData {
71 /* Forward declarations */
73 static void g_main_poll (gint timeout,
74 gboolean use_priority,
76 static void g_main_poll_add_unlocking (gint priority,
79 static gboolean g_timeout_prepare (gpointer source_data,
80 GTimeVal *current_time,
82 static gboolean g_timeout_check (gpointer source_data,
83 GTimeVal *current_time);
84 static gboolean g_timeout_dispatch (gpointer source_data,
85 GTimeVal *current_time,
87 static gboolean g_idle_prepare (gpointer source_data,
88 GTimeVal *current_time,
90 static gboolean g_idle_check (gpointer source_data,
91 GTimeVal *current_time);
92 static gboolean g_idle_dispatch (gpointer source_data,
93 GTimeVal *current_time,
98 static GSList *pending_dispatches = NULL;
99 static GHookList source_list = { 0 };
101 /* The following lock is used for both the list of sources
102 * and the list of poll records
104 G_LOCK_DECLARE_STATIC (main_loop);
106 static GSourceFuncs timeout_funcs = {
110 (GDestroyNotify)g_free
113 static GSourceFuncs idle_funcs = {
117 (GDestroyNotify)g_free
120 static GPollRec *poll_records = NULL;
121 static GPollRec *poll_free_list = NULL;
122 static GMemChunk *poll_chunk;
123 static guint n_poll_records = 0;
125 /* this pipe is used to wake up the main loop when a source is added.
127 static gint wake_up_pipe[2] = { -1, -1 };
128 static GPollFD wake_up_rec;
129 static gboolean poll_waiting = FALSE;
132 static GPollFunc poll_func = (GPollFunc)poll;
135 /* The following implementation of poll() comes from the GNU C Library.
136 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
139 #include <string.h> /* for bzero on BSD systems */
141 #ifdef HAVE_SYS_SELECT_H
142 #include <sys/select.h>
143 #endif /* HAVE_SYS_SELECT_H_ */
146 # define SELECT_MASK fd_set
149 typedef long fd_mask;
152 # define SELECT_MASK void
154 # define SELECT_MASK int
159 g_poll (GPollFD *fds, guint nfds, gint timeout)
162 SELECT_MASK rset, wset, xset;
171 for (f = fds; f < &fds[nfds]; ++f)
174 if (f->events & G_IO_IN)
175 FD_SET (f->fd, &rset);
176 if (f->events & G_IO_OUT)
177 FD_SET (f->fd, &wset);
178 if (f->events & G_IO_PRI)
179 FD_SET (f->fd, &xset);
180 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
184 tv.tv_sec = timeout / 1000;
185 tv.tv_usec = (timeout % 1000) * 1000;
187 ready = select (maxfd + 1, &rset, &wset, &xset,
188 timeout == -1 ? NULL : &tv);
190 for (f = fds; f < &fds[nfds]; ++f)
195 if (FD_ISSET (f->fd, &rset))
196 f->revents |= G_IO_IN;
197 if (FD_ISSET (f->fd, &wset))
198 f->revents |= G_IO_OUT;
199 if (FD_ISSET (f->fd, &xset))
200 f->revents |= G_IO_PRI;
207 static GPollFunc poll_func = g_poll;
210 /* Hooks for adding to the main loop */
212 /* Use knowledge of insert_sorted algorithm here to make
213 * sure we insert at the end of equal priority items
216 g_source_compare (GHook *a, GHook *b)
218 GSource *source_a = (GSource *)a;
219 GSource *source_b = (GSource *)b;
221 return (source_a->priority < source_b->priority) ? -1 : 1;
225 g_source_add (gint priority,
226 gboolean can_recurse,
228 gpointer source_data,
230 GDestroyNotify notify)
237 if (!source_list.is_setup)
238 g_hook_list_init (&source_list, sizeof(GSource));
240 source = (GSource *)g_hook_alloc (&source_list);
241 source->priority = priority;
242 source->source_data = source_data;
243 source->hook.func = funcs;
244 source->hook.data = user_data;
245 source->hook.destroy = notify;
247 g_hook_insert_sorted (&source_list,
252 source->hook.flags |= G_SOURCE_CAN_RECURSE;
254 return_val = source->hook.hook_id;
256 /* Now wake up the main loop if it is waiting in the poll() */
260 poll_waiting = FALSE;
261 write (wake_up_pipe[1], "A", 1);
264 G_UNLOCK (main_loop);
270 g_source_remove (guint tag)
276 hook = g_hook_get (&source_list, tag);
279 GSource *source = (GSource *)hook;
280 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
281 g_hook_destroy_link (&source_list, hook);
284 G_UNLOCK (main_loop);
288 g_source_remove_by_user_data (gpointer user_data)
294 hook = g_hook_find_data (&source_list, TRUE, user_data);
297 GSource *source = (GSource *)hook;
298 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
299 g_hook_destroy_link (&source_list, hook);
302 G_UNLOCK (main_loop);
306 g_source_find_source_data (GHook *hook,
309 GSource *source = (GSource *)hook;
310 return (source->source_data == data);
314 g_source_remove_by_source_data (gpointer source_data)
320 hook = g_hook_find (&source_list, TRUE,
321 g_source_find_source_data, source_data);
324 GSource *source = (GSource *)hook;
325 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
326 g_hook_destroy_link (&source_list, hook);
329 G_UNLOCK (main_loop);
332 void g_get_current_time (GTimeVal *result)
334 gettimeofday ((struct timeval *)result, NULL);
337 /* Running the main loop */
339 /* HOLDS: main_loop_lock */
341 g_main_dispatch (GTimeVal *current_time)
343 while (pending_dispatches != NULL)
345 gboolean need_destroy;
346 GSource *source = pending_dispatches->data;
349 tmp_list = pending_dispatches;
350 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
351 g_slist_free_1 (tmp_list);
353 if (G_HOOK_IS_VALID (source))
355 gboolean (*dispatch) (gpointer, GTimeVal *, gpointer);
356 gpointer hook_data = source->hook.data;
357 gpointer source_data = source->source_data;
359 dispatch = ((GSourceFuncs *)source->hook.func)->dispatch;
361 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
363 G_UNLOCK (main_loop);
364 need_destroy = ! dispatch(source_data,
369 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
372 g_hook_destroy_link (&source_list, (GHook *)source);
375 g_hook_unref (&source_list, (GHook *)source);
379 /* Run a single iteration of the mainloop, or, if !dispatch
380 * check to see if any events need dispatching, but don't
384 g_main_iterate (gboolean block, gboolean dispatch)
387 GTimeVal current_time;
389 gint current_priority = 0;
391 gboolean retval = FALSE;
393 g_return_val_if_fail (!block || dispatch, FALSE);
395 g_get_current_time (¤t_time);
399 /* If recursing, finish up current dispatch, before starting over */
400 if (pending_dispatches)
403 g_main_dispatch (¤t_time);
405 G_UNLOCK (main_loop);
409 /* Prepare all sources */
411 timeout = block ? -1 : 0;
413 hook = g_hook_first_valid (&source_list, TRUE);
416 GSource *source = (GSource *)hook;
420 if ((nready > 0) && (source->priority > current_priority))
422 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
424 hook = g_hook_next_valid (hook, TRUE);
428 g_hook_ref (&source_list, hook);
430 if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
436 g_hook_unref (&source_list, hook);
437 G_UNLOCK (main_loop);
442 hook->flags |= G_SOURCE_READY;
444 current_priority = source->priority;
449 if (source_timeout >= 0)
452 timeout = source_timeout;
454 timeout = MIN (timeout, source_timeout);
457 tmp = g_hook_next_valid (hook, TRUE);
459 g_hook_unref (&source_list, hook);
463 /* poll(), if necessary */
465 g_main_poll (timeout, nready > 0, current_priority);
467 /* Check to see what sources need to be dispatched */
471 hook = g_hook_first_valid (&source_list, TRUE);
474 GSource *source = (GSource *)hook;
477 if ((nready > 0) && (source->priority > current_priority))
479 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
481 hook = g_hook_next_valid (hook, TRUE);
485 g_hook_ref (&source_list, hook);
487 if ((hook->flags & G_SOURCE_READY) ||
488 ((GSourceFuncs *)hook->func)->check (source->source_data,
493 hook->flags &= ~G_SOURCE_READY;
494 g_hook_ref (&source_list, hook);
495 pending_dispatches = g_slist_prepend (pending_dispatches, source);
496 current_priority = source->priority;
501 g_hook_unref (&source_list, hook);
502 G_UNLOCK (main_loop);
507 tmp = g_hook_next_valid (hook, TRUE);
509 g_hook_unref (&source_list, hook);
513 /* Now invoke the callbacks */
515 if (pending_dispatches)
517 pending_dispatches = g_slist_reverse (pending_dispatches);
518 g_main_dispatch (¤t_time);
522 G_UNLOCK (main_loop);
527 /* See if any events are pending
532 return g_main_iterate (FALSE, FALSE);
535 /* Run a single iteration of the mainloop. If block is FALSE,
539 g_main_iteration (gboolean block)
541 return g_main_iterate (block, TRUE);
547 GMainLoop *result = g_new (GMainLoop, 1);
548 result->flag = FALSE;
554 g_main_run (GMainLoop *loop)
558 g_main_iterate (TRUE, TRUE);
562 g_main_quit (GMainLoop *loop)
568 g_main_destroy (GMainLoop *loop)
573 /* HOLDS: main_loop_lock */
575 g_main_poll (gint timeout, gboolean use_priority, gint priority)
577 GPollFD *fd_array = g_new (GPollFD, n_poll_records);
583 if (wake_up_pipe[0] < 0)
585 if (pipe (wake_up_pipe) < 0)
586 g_error ("Cannot create pipe main loop wake-up: %s\n",
589 wake_up_rec.fd = wake_up_pipe[0];
590 wake_up_rec.events = G_IO_IN;
591 g_main_poll_add_unlocking (0, &wake_up_rec);
594 pollrec = poll_records;
596 while (pollrec && (!use_priority || priority >= pollrec->priority))
598 fd_array[i].fd = pollrec->fd->fd;
599 fd_array[i].events = pollrec->fd->events;
600 fd_array[i].revents = 0;
602 pollrec = pollrec->next;
608 G_UNLOCK (main_loop);
610 (*poll_func) (fd_array, npoll, timeout);
616 read (wake_up_pipe[0], &c, 1);
619 poll_waiting = FALSE;
621 pollrec = poll_records;
625 pollrec->fd->revents = fd_array[i].revents;
626 pollrec = pollrec->next;
634 g_main_poll_add (gint priority,
638 g_main_poll_add_unlocking (priority, fd);
639 G_UNLOCK (main_loop);
642 /* HOLDS: main_loop_lock */
644 g_main_poll_add_unlocking (gint priority,
647 GPollRec *lastrec, *pollrec, *newrec;
650 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
654 newrec = poll_free_list;
655 poll_free_list = newrec->next;
658 newrec = g_chunk_new (GPollRec, poll_chunk);
661 newrec->priority = priority;
664 pollrec = poll_records;
665 while (pollrec && priority >= pollrec->priority)
668 pollrec = pollrec->next;
672 lastrec->next = newrec;
674 poll_records = newrec;
676 newrec->next = pollrec;
682 g_main_poll_remove (GPollFD *fd)
684 GPollRec *pollrec, *lastrec;
689 pollrec = poll_records;
693 if (pollrec->fd == fd)
696 lastrec->next = pollrec->next;
698 poll_records = pollrec->next;
700 pollrec->next = poll_free_list;
701 poll_free_list = pollrec;
707 pollrec = pollrec->next;
710 G_UNLOCK (main_loop);
714 g_main_set_poll_func (GPollFunc func)
720 poll_func = (GPollFunc)poll;
722 poll_func = (GPollFunc)g_poll;
729 g_timeout_prepare (gpointer source_data,
730 GTimeVal *current_time,
734 GTimeoutData *data = source_data;
736 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
737 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
739 *timeout = (msec <= 0) ? 0 : msec;
745 g_timeout_check (gpointer source_data,
746 GTimeVal *current_time)
748 GTimeoutData *data = source_data;
750 return (data->expiration.tv_sec < current_time->tv_sec) ||
751 ((data->expiration.tv_sec == current_time->tv_sec) &&
752 (data->expiration.tv_usec <= current_time->tv_usec));
756 g_timeout_dispatch (gpointer source_data,
757 GTimeVal *current_time,
760 GTimeoutData *data = source_data;
762 if (data->callback(user_data))
764 data->expiration.tv_sec = current_time->tv_sec;
765 data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
766 if (data->expiration.tv_usec >= 1000000)
768 data->expiration.tv_usec -= 1000000;
769 data->expiration.tv_sec++;
778 g_timeout_add_full (gint priority,
780 GSourceFunc function,
782 GDestroyNotify notify)
784 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
786 timeout_data->interval = interval;
787 timeout_data->callback = function;
788 g_get_current_time (&timeout_data->expiration);
790 timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
791 if (timeout_data->expiration.tv_usec >= 1000000)
793 timeout_data->expiration.tv_usec -= 1000000;
794 timeout_data->expiration.tv_sec++;
797 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
801 g_timeout_add (guint32 interval,
802 GSourceFunc function,
805 return g_timeout_add_full (0, interval, function, data, NULL);
811 g_idle_prepare (gpointer source_data,
812 GTimeVal *current_time,
820 g_idle_check (gpointer source_data,
821 GTimeVal *current_time)
827 g_idle_dispatch (gpointer source_data,
828 GTimeVal *current_time,
831 GIdleData *data = source_data;
833 return (*data->callback)(user_data);
837 g_idle_add_full (gint priority,
838 GSourceFunc function,
840 GDestroyNotify notify)
842 GIdleData *idle_data = g_new (GIdleData, 1);
844 idle_data->callback = function;
846 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
850 g_idle_add (GSourceFunc function,
853 return g_idle_add_full (0, function, data, NULL);