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 #ifdef HAVE_SYS_SELECT_H
116 #include <sys/select.h>
117 #endif /* HAVE_SYS_SELECT_H_ */
120 # define SELECT_MASK fd_set
123 typedef long fd_mask;
126 # define SELECT_MASK void
128 # define SELECT_MASK int
133 g_poll (GPollFD *fds, guint nfds, gint timeout)
136 SELECT_MASK rset, wset, xset;
145 for (f = fds; f < &fds[nfds]; ++f)
148 if (f->events & G_IO_IN)
149 FD_SET (f->fd, &rset);
150 if (f->events & G_IO_OUT)
151 FD_SET (f->fd, &wset);
152 if (f->events & G_IO_PRI)
153 FD_SET (f->fd, &xset);
154 if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
158 tv.tv_sec = timeout / 1000;
159 tv.tv_usec = (timeout % 1000) * 1000;
161 ready = select (maxfd + 1, &rset, &wset, &xset,
162 timeout == -1 ? NULL : &tv);
164 for (f = fds; f < &fds[nfds]; ++f)
169 if (FD_ISSET (f->fd, &rset))
170 f->revents |= G_IO_IN;
171 if (FD_ISSET (f->fd, &wset))
172 f->revents |= G_IO_OUT;
173 if (FD_ISSET (f->fd, &xset))
174 f->revents |= G_IO_PRI;
181 static GPollFunc poll_func = g_poll;
184 /* Hooks for adding to the main loop */
186 /* Use knowledge of insert_sorted algorithm here to make
187 * sure we insert at the end of equal priority items
190 g_source_compare (GHook *a, GHook *b)
192 GSource *source_a = (GSource *)a;
193 GSource *source_b = (GSource *)b;
195 return (source_a->priority < source_b->priority) ? -1 : 1;
199 g_source_add (gint priority,
200 gboolean can_recurse,
202 gpointer source_data,
204 GDestroyNotify notify)
208 if (!source_list.is_setup)
209 g_hook_list_init (&source_list, sizeof(GSource));
211 source = (GSource *)g_hook_alloc (&source_list);
212 source->priority = priority;
213 source->source_data = source_data;
214 source->hook.func = funcs;
215 source->hook.data = user_data;
216 source->hook.destroy = notify;
218 g_hook_insert_sorted (&source_list,
223 source->hook.flags |= G_SOURCE_CAN_RECURSE;
225 return source->hook.hook_id;
229 g_source_remove (guint tag)
231 GHook *hook = g_hook_get (&source_list, tag);
234 GSource *source = (GSource *)hook;
235 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
236 g_hook_destroy_link (&source_list, hook);
241 g_source_remove_by_user_data (gpointer user_data)
243 GHook *hook = g_hook_find_data (&source_list, TRUE, user_data);
246 GSource *source = (GSource *)hook;
247 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
248 g_hook_destroy_link (&source_list, hook);
253 g_source_find_source_data (GHook *hook,
256 GSource *source = (GSource *)hook;
257 return (source->source_data == data);
261 g_source_remove_by_source_data (gpointer source_data)
263 GHook *hook = g_hook_find (&source_list, TRUE,
264 g_source_find_source_data, source_data);
267 GSource *source = (GSource *)hook;
268 ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
269 g_hook_destroy_link (&source_list, hook);
273 void g_get_current_time (GTimeVal *result)
275 gettimeofday ((struct timeval *)result, NULL);
278 /* Running the main loop */
281 g_main_dispatch (GTimeVal *current_time)
283 while (pending_dispatches != NULL)
285 gboolean need_destroy;
286 GSource *source = pending_dispatches->data;
289 tmp_list = pending_dispatches;
290 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
291 g_slist_free_1 (tmp_list);
293 if (G_HOOK_IS_VALID (source))
295 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
296 need_destroy = !((GSourceFuncs *)source->hook.func)->dispatch (source->source_data,
299 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
302 g_hook_destroy_link (&source_list, (GHook *)source);
305 g_hook_unref (&source_list, (GHook *)source);
309 /* Run a single iteration of the mainloop, or, if !dispatch
310 * check to see if any events need dispatching, but don't
314 g_main_iterate (gboolean block, gboolean dispatch)
317 GTimeVal current_time;
319 gint current_priority = 0;
322 g_return_val_if_fail (!block || dispatch, FALSE);
324 g_get_current_time (¤t_time);
326 /* If recursing, finish up current dispatch, before starting over */
327 if (pending_dispatches)
330 g_main_dispatch (¤t_time);
335 /* Prepare all sources */
337 timeout = block ? -1 : 0;
339 hook = g_hook_first_valid (&source_list, TRUE);
342 GSource *source = (GSource *)hook;
346 if ((nready > 0) && (source->priority > current_priority))
348 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
350 hook = g_hook_next_valid (hook, TRUE);
354 g_hook_ref (&source_list, hook);
356 if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
362 g_hook_unref (&source_list, hook);
367 hook->flags |= G_SOURCE_READY;
369 current_priority = source->priority;
374 if (source_timeout >= 0)
377 timeout = source_timeout;
379 timeout = MIN (timeout, source_timeout);
382 tmp = g_hook_next_valid (hook, TRUE);
384 g_hook_unref (&source_list, hook);
388 /* poll(), if necessary */
390 g_main_poll (timeout, nready > 0, current_priority);
392 /* Check to see what sources need to be dispatched */
396 hook = g_hook_first_valid (&source_list, TRUE);
399 GSource *source = (GSource *)hook;
402 if ((nready > 0) && (source->priority > current_priority))
404 if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
406 hook = g_hook_next_valid (hook, TRUE);
410 g_hook_ref (&source_list, hook);
412 if ((hook->flags & G_SOURCE_READY) ||
413 ((GSourceFuncs *)hook->func)->check (source->source_data,
418 hook->flags &= ~G_SOURCE_READY;
419 g_hook_ref (&source_list, hook);
420 pending_dispatches = g_slist_prepend (pending_dispatches, source);
421 current_priority = source->priority;
426 g_hook_unref (&source_list, hook);
431 tmp = g_hook_next_valid (hook, TRUE);
433 g_hook_unref (&source_list, hook);
437 /* Now invoke the callbacks */
439 if (pending_dispatches)
441 pending_dispatches = g_slist_reverse (pending_dispatches);
442 g_main_dispatch (¤t_time);
449 /* See if any events are pending
454 return g_main_iterate (FALSE, FALSE);
457 /* Run a single iteration of the mainloop. If block is FALSE,
461 g_main_iteration (gboolean block)
463 return g_main_iterate (block, TRUE);
469 GMainLoop *result = g_new (GMainLoop, 1);
470 result->flag = FALSE;
476 g_main_run (GMainLoop *loop)
480 g_main_iterate (TRUE, TRUE);
484 g_main_quit (GMainLoop *loop)
490 g_main_destroy (GMainLoop *loop)
495 static GPollRec *poll_records = NULL;
496 static GPollRec *poll_free_list = NULL;
497 static GMemChunk *poll_chunk;
498 static guint n_poll_records = 0;
501 g_main_poll (gint timeout, gboolean use_priority, gint priority)
503 GPollFD *fd_array = g_new (GPollFD, n_poll_records);
509 pollrec = poll_records;
511 while (pollrec && (!use_priority || priority >= pollrec->priority))
513 fd_array[i].fd = pollrec->fd->fd;
514 fd_array[i].events = pollrec->fd->events;
515 fd_array[i].revents = 0;
517 pollrec = pollrec->next;
522 (*poll_func) (fd_array, npoll, timeout);
524 pollrec = poll_records;
528 pollrec->fd->revents = fd_array[i].revents;
529 pollrec = pollrec->next;
537 g_main_poll_add (gint priority,
540 GPollRec *lastrec, *pollrec, *newrec;
543 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
545 newrec = g_chunk_new (GPollRec, poll_chunk);
547 newrec->priority = priority;
550 pollrec = poll_records;
551 while (pollrec && priority >= pollrec->priority)
554 pollrec = pollrec->next;
558 lastrec->next = newrec;
560 poll_records = newrec;
562 newrec->next = pollrec;
568 g_main_poll_remove (GPollFD *fd)
570 GPollRec *pollrec, *lastrec;
573 pollrec = poll_records;
577 if (pollrec->fd == fd)
580 lastrec->next = pollrec->next;
582 poll_records = pollrec->next;
584 pollrec->next = poll_free_list;
585 poll_free_list = pollrec;
588 pollrec = pollrec->next;
595 g_main_set_poll_func (GPollFunc func)
600 poll_func = (GPollFunc)poll;
606 g_timeout_prepare (gpointer source_data,
607 GTimeVal *current_time,
611 GTimeoutData *data = source_data;
613 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
614 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
616 *timeout = (msec <= 0) ? 0 : msec;
622 g_timeout_check (gpointer source_data,
623 GTimeVal *current_time)
625 GTimeoutData *data = source_data;
627 return (data->expiration.tv_sec < current_time->tv_sec) ||
628 ((data->expiration.tv_sec == current_time->tv_sec) &&
629 (data->expiration.tv_usec <= current_time->tv_usec));
633 g_timeout_dispatch (gpointer source_data,
634 GTimeVal *current_time,
637 GTimeoutData *data = source_data;
639 if (data->callback(user_data))
641 data->expiration.tv_sec = current_time->tv_sec;
642 data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
643 if (data->expiration.tv_usec >= 1000000)
645 data->expiration.tv_usec -= 1000000;
646 data->expiration.tv_sec++;
655 g_timeout_add_full (gint priority,
657 GSourceFunc function,
659 GDestroyNotify notify)
661 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
663 timeout_data->interval = interval;
664 timeout_data->callback = function;
665 g_get_current_time (&timeout_data->expiration);
667 timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
668 if (timeout_data->expiration.tv_usec >= 1000000)
670 timeout_data->expiration.tv_usec -= 1000000;
671 timeout_data->expiration.tv_sec++;
674 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
678 g_timeout_add (guint32 interval,
679 GSourceFunc function,
682 return g_timeout_add_full (0, interval, function, data, NULL);
688 g_idle_prepare (gpointer source_data,
689 GTimeVal *current_time,
697 g_idle_check (gpointer source_data,
698 GTimeVal *current_time)
704 g_idle_dispatch (gpointer source_data,
705 GTimeVal *current_time,
708 GIdleData *data = source_data;
710 return (*data->callback)(user_data);
714 g_idle_add_full (gint priority,
715 GSourceFunc function,
717 GDestroyNotify notify)
719 GIdleData *idle_data = g_new (GIdleData, 1);
721 idle_data->callback = function;
723 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
727 g_idle_add (GSourceFunc function,
730 return g_idle_add_full (0, function, data, NULL);