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 #include <sys/types.h>
32 #ifdef HAVE_SYS_TIME_H
35 #ifdef GLIB_HAVE_SYS_POLL_H
36 # include <sys/poll.h>
37 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
38 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
39 #endif /* GLIB_HAVE_SYS_POLL_H */
57 typedef struct _GIdleData GIdleData;
58 typedef struct _GTimeoutData GTimeoutData;
59 typedef struct _GSource GSource;
60 typedef struct _GPollRec GPollRec;
64 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
65 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
99 /* Forward declarations */
101 static gint g_source_compare (GHook *a,
103 static void g_source_free_func (GHookList *hook_list,
105 static void g_main_poll (gint timeout,
106 gboolean use_priority,
108 static void g_main_add_poll_unlocked (gint priority,
111 static gboolean g_timeout_prepare (gpointer source_data,
112 GTimeVal *current_time,
114 static gboolean g_timeout_check (gpointer source_data,
115 GTimeVal *current_time);
116 static gboolean g_timeout_dispatch (gpointer source_data,
117 GTimeVal *current_time,
119 static gboolean g_idle_prepare (gpointer source_data,
120 GTimeVal *current_time,
122 static gboolean g_idle_check (gpointer source_data,
123 GTimeVal *current_time);
124 static gboolean g_idle_dispatch (gpointer source_data,
125 GTimeVal *current_time,
130 static GSList *pending_dispatches = NULL;
131 static GHookList source_list = { 0 };
133 /* The following lock is used for both the list of sources
134 * and the list of poll records
136 G_LOCK_DECLARE_STATIC (main_loop);
138 static GSourceFuncs timeout_funcs = {
142 (GDestroyNotify)g_free
145 static GSourceFuncs idle_funcs = {
149 (GDestroyNotify)g_free
152 static GPollRec *poll_records = NULL;
153 static GPollRec *poll_free_list = NULL;
154 static GMemChunk *poll_chunk;
155 static guint n_poll_records = 0;
157 #ifdef G_THREADS_ENABLED
159 /* this pipe is used to wake up the main loop when a source is added.
161 static gint wake_up_pipe[2] = { -1, -1 };
163 static HANDLE wake_up_semaphore = NULL;
165 static GPollFD wake_up_rec;
166 static gboolean poll_waiting = FALSE;
170 static GPollFunc poll_func = (GPollFunc) poll;
171 #else /* !HAVE_POLL */
175 g_poll (GPollFD *fds, guint nfds, gint timeout)
177 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
186 for (f = fds; f < &fds[nfds]; ++f)
189 if (f->events & G_IO_IN)
190 if (f->fd == G_WIN32_MSG_HANDLE)
194 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
195 handles[nhandles++] = (HANDLE) f->fd;
204 /* Waiting for messages, and maybe events */
207 if (timeout == INFINITE)
209 /* Waiting just for messages, infinite timeout
210 * -> Use PeekMessage, then WaitMessage
212 /* g_print ("WaitMessage, PeekMessage\n"); */
213 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
214 ready = WAIT_OBJECT_0;
215 else if (!WaitMessage ())
216 g_warning ("g_poll: WaitMessage failed");
217 ready = WAIT_OBJECT_0;
219 else if (timeout == 0)
221 /* Waiting just for messages, zero timeout
224 /* g_print ("PeekMessage\n"); */
225 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
226 ready = WAIT_OBJECT_0;
228 ready = WAIT_TIMEOUT;
232 /* Waiting just for messages, some timeout
233 * -> First try PeekMessage, then set a timer, wait for message,
234 * kill timer, use PeekMessage
236 /* g_print ("PeekMessage\n"); */
237 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
238 ready = WAIT_OBJECT_0;
239 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
240 g_warning ("g_poll: SetTimer failed");
243 /* g_print ("WaitMessage\n"); */
245 KillTimer (NULL, timer);
246 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
247 ready = WAIT_OBJECT_0;
249 ready = WAIT_TIMEOUT;
255 /* Wait for either message or event
256 * -> Use MsgWaitForMultipleObjects
258 /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
259 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
260 timeout, QS_ALLINPUT);
261 /* g_print("=%d\n", ready); */
262 if (ready == WAIT_FAILED)
263 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
266 else if (nhandles == 0)
268 /* Wait for nothing (huh?) */
273 /* Wait for just events
274 * -> Use WaitForMultipleObjects
276 /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
277 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
278 /* g_print("=%d\n", ready); */
279 if (ready == WAIT_FAILED)
280 g_warning ("g_poll: WaitForMultipleObjects failed");
283 for (f = fds; f < &fds[nfds]; ++f)
286 if (ready == WAIT_FAILED)
288 else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
290 fds[poll_msgs].revents |= G_IO_IN;
292 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
293 for (f = fds; f < &fds[nfds]; ++f)
295 if ((f->events & G_IO_IN)
296 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
298 f->revents |= G_IO_IN;
299 /* g_print ("event %#x\n", f->fd); */
300 ResetEvent ((HANDLE) f->fd);
304 if (ready == WAIT_TIMEOUT)
310 #else /* !NATIVE_WIN32 */
312 /* The following implementation of poll() comes from the GNU C Library.
313 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
316 #include <string.h> /* for bzero on BSD systems */
318 #ifdef HAVE_SYS_SELECT_H
319 #include <sys/select.h>
320 #endif /* HAVE_SYS_SELECT_H_ */
323 # define SELECT_MASK fd_set
324 #else /* !NO_FD_SET */
326 typedef long fd_mask;
329 # define SELECT_MASK void
331 # define SELECT_MASK int
333 #endif /* !NO_FD_SET */
336 g_poll (GPollFD *fds, guint nfds, gint timeout)
339 SELECT_MASK rset, wset, xset;
348 for (f = fds; f < &fds[nfds]; ++f)
351 if (f->events & G_IO_IN)
352 FD_SET (f->fd, &rset);
353 if (f->events & G_IO_OUT)
354 FD_SET (f->fd, &wset);
355 if (f->events & G_IO_PRI)
356 FD_SET (f->fd, &xset);
357 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
361 tv.tv_sec = timeout / 1000;
362 tv.tv_usec = (timeout % 1000) * 1000;
364 ready = select (maxfd + 1, &rset, &wset, &xset,
365 timeout == -1 ? NULL : &tv);
367 for (f = fds; f < &fds[nfds]; ++f)
372 if (FD_ISSET (f->fd, &rset))
373 f->revents |= G_IO_IN;
374 if (FD_ISSET (f->fd, &wset))
375 f->revents |= G_IO_OUT;
376 if (FD_ISSET (f->fd, &xset))
377 f->revents |= G_IO_PRI;
383 #endif /* !NATIVE_WIN32 */
385 static GPollFunc poll_func = g_poll;
386 #endif /* !HAVE_POLL */
388 /* Hooks for adding to the main loop */
390 /* Use knowledge of insert_sorted algorithm here to make
391 * sure we insert at the end of equal priority items
394 g_source_compare (GHook *a,
397 GSource *source_a = (GSource *)a;
398 GSource *source_b = (GSource *)b;
400 return (source_a->priority < source_b->priority) ? -1 : 1;
404 g_source_free_func (GHookList *hook_list,
407 GSource *source = (GSource *)hook;
409 ((GSourceFuncs *) hook->func)->destroy (source->source_data);
413 g_source_add (gint priority,
414 gboolean can_recurse,
416 gpointer source_data,
418 GDestroyNotify notify)
425 if (!source_list.is_setup)
426 g_hook_list_init (&source_list, sizeof(GSource));
428 source_list.hook_free = g_source_free_func;
430 source = (GSource *)g_hook_alloc (&source_list);
431 source->priority = priority;
432 source->source_data = source_data;
433 source->hook.func = funcs;
434 source->hook.data = user_data;
435 source->hook.destroy = notify;
437 g_hook_insert_sorted (&source_list,
442 source->hook.flags |= G_SOURCE_CAN_RECURSE;
444 return_val = source->hook.hook_id;
446 #ifdef G_THREADS_ENABLED
447 /* Now wake up the main loop if it is waiting in the poll() */
451 poll_waiting = FALSE;
453 write (wake_up_pipe[1], "A", 1);
455 ReleaseSemaphore (wake_up_semaphore, 1, NULL);
459 G_UNLOCK (main_loop);
465 g_source_remove (guint tag)
471 hook = g_hook_get (&source_list, tag);
473 g_hook_destroy_link (&source_list, hook);
475 G_UNLOCK (main_loop);
479 g_source_remove_by_user_data (gpointer user_data)
485 hook = g_hook_find_data (&source_list, TRUE, user_data);
487 g_hook_destroy_link (&source_list, hook);
489 G_UNLOCK (main_loop);
493 g_source_find_source_data (GHook *hook,
496 GSource *source = (GSource *)hook;
498 return (source->source_data == data);
502 g_source_remove_by_source_data (gpointer source_data)
508 hook = g_hook_find (&source_list, TRUE,
509 g_source_find_source_data, source_data);
511 g_hook_destroy_link (&source_list, hook);
513 G_UNLOCK (main_loop);
517 g_get_current_time (GTimeVal *result)
521 g_return_if_fail (result != NULL);
523 /*this is required on alpha, there the timeval structs are int's
524 not longs and a cast only would fail horribly*/
525 gettimeofday (&r, NULL);
526 result->tv_sec = r.tv_sec;
527 result->tv_usec = r.tv_usec;
529 /* Avoid calling time() except for the first time.
530 * GetTickCount() should be pretty fast and low-level?
531 * I could also use ftime() but it seems unnecessarily overheady.
533 static DWORD start_tick = 0;
534 static time_t start_time;
538 g_return_if_fail (result != NULL);
542 start_tick = GetTickCount ();
546 tick = GetTickCount ();
548 result->tv_sec = (tick - start_tick) / 1000 + start_time;
549 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
553 /* Running the main loop */
555 /* HOLDS: main_loop_lock */
557 g_main_dispatch (GTimeVal *current_time)
559 while (pending_dispatches != NULL)
561 gboolean need_destroy;
562 GSource *source = pending_dispatches->data;
565 tmp_list = pending_dispatches;
566 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
567 g_slist_free_1 (tmp_list);
569 if (G_HOOK_IS_VALID (source))
571 gboolean was_in_call;
572 gpointer hook_data = source->hook.data;
573 gpointer source_data = source->source_data;
574 gboolean (*dispatch) (gpointer,
578 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
580 was_in_call = G_HOOK_IN_CALL (source);
581 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
583 G_UNLOCK (main_loop);
584 need_destroy = ! dispatch (source_data,
590 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
592 if (need_destroy && G_HOOK_IS_VALID (source))
593 g_hook_destroy_link (&source_list, (GHook *) source);
596 g_hook_unref (&source_list, (GHook*) source);
600 /* g_main_iterate () runs a single iteration of the mainloop, or,
601 * if !dispatch checks to see if any sources need dispatching.
602 * basic algorithm for dispatch=TRUE:
604 * 1) while the list of currently pending sources is non-empty,
605 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
606 * removing sources from the list after each returns.
607 * the return value of (*dispatch) determines whether the source
608 * itself is kept alive.
610 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
611 * are !IN_CALL or can_recurse. a return value of TRUE determines
612 * that the source would like to be dispatched immediatedly, it
613 * is then flagged as SOURCE_READY.
615 * 3) poll with the pollfds from all sources at the priority of the
616 * first source flagged as SOURCE_READY. if there are any sources
617 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
618 * of all timouts otherwise.
620 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
621 * (*check) returns true, add the source to the pending list.
622 * once one source returns true, stop after checking all sources
625 * 5) while the list of currently pending sources is non-empty,
626 * call (*dispatch) on each source, removing the source
631 g_main_iterate (gboolean block,
635 GTimeVal current_time ={ 0, 0 };
637 gint current_priority = 0;
639 gboolean retval = FALSE;
641 g_return_val_if_fail (!block || dispatch, FALSE);
643 g_get_current_time (¤t_time);
647 /* If recursing, finish up current dispatch, before starting over */
648 if (pending_dispatches)
651 g_main_dispatch (¤t_time);
653 G_UNLOCK (main_loop);
658 /* Prepare all sources */
660 timeout = block ? -1 : 0;
662 hook = g_hook_first_valid (&source_list, TRUE);
665 GSource *source = (GSource *)hook;
666 gint source_timeout = -1;
668 if ((n_ready > 0) && (source->priority > current_priority))
670 g_hook_unref (&source_list, hook);
673 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
675 hook = g_hook_next_valid (&source_list, hook, TRUE);
679 if (hook->flags & G_SOURCE_READY ||
680 ((GSourceFuncs *) hook->func)->prepare (source->source_data,
686 hook->flags |= G_SOURCE_READY;
687 g_hook_unref (&source_list, hook);
688 G_UNLOCK (main_loop);
694 hook->flags |= G_SOURCE_READY;
696 current_priority = source->priority;
701 if (source_timeout >= 0)
704 timeout = source_timeout;
706 timeout = MIN (timeout, source_timeout);
709 hook = g_hook_next_valid (&source_list, hook, TRUE);
712 /* poll(), if necessary */
714 g_main_poll (timeout, n_ready > 0, current_priority);
716 /* Check to see what sources need to be dispatched */
720 hook = g_hook_first_valid (&source_list, TRUE);
723 GSource *source = (GSource *)hook;
725 if ((n_ready > 0) && (source->priority > current_priority))
727 g_hook_unref (&source_list, hook);
730 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
732 hook = g_hook_next_valid (&source_list, hook, TRUE);
736 if (hook->flags & G_SOURCE_READY ||
737 ((GSourceFuncs *) hook->func)->check (source->source_data,
742 hook->flags &= ~G_SOURCE_READY;
743 g_hook_ref (&source_list, hook);
744 pending_dispatches = g_slist_prepend (pending_dispatches, source);
745 current_priority = source->priority;
750 g_hook_unref (&source_list, hook);
751 G_UNLOCK (main_loop);
757 hook = g_hook_next_valid (&source_list, hook, TRUE);
760 /* Now invoke the callbacks */
762 if (pending_dispatches)
764 pending_dispatches = g_slist_reverse (pending_dispatches);
765 g_main_dispatch (¤t_time);
769 G_UNLOCK (main_loop);
774 /* See if any events are pending
779 return g_main_iterate (FALSE, FALSE);
782 /* Run a single iteration of the mainloop. If block is FALSE,
786 g_main_iteration (gboolean block)
788 return g_main_iterate (block, TRUE);
792 g_main_new (gboolean is_running)
796 loop = g_new0 (GMainLoop, 1);
797 loop->is_running = is_running != FALSE;
803 g_main_run (GMainLoop *loop)
805 g_return_if_fail (loop != NULL);
807 loop->is_running = TRUE;
808 while (loop->is_running)
809 g_main_iterate (TRUE, TRUE);
813 g_main_quit (GMainLoop *loop)
815 g_return_if_fail (loop != NULL);
817 loop->is_running = FALSE;
821 g_main_destroy (GMainLoop *loop)
823 g_return_if_fail (loop != NULL);
829 g_main_is_running (GMainLoop *loop)
831 g_return_val_if_fail (loop != NULL, FALSE);
833 return loop->is_running;
836 /* HOLDS: main_loop_lock */
838 g_main_poll (gint timeout,
839 gboolean use_priority,
847 #ifdef G_THREADS_ENABLED
849 if (wake_up_pipe[0] < 0)
851 if (pipe (wake_up_pipe) < 0)
852 g_error ("Cannot create pipe main loop wake-up: %s\n",
855 wake_up_rec.fd = wake_up_pipe[0];
856 wake_up_rec.events = G_IO_IN;
857 g_main_add_poll_unlocked (0, &wake_up_rec);
860 if (wake_up_semaphore == NULL)
862 if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
863 g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
864 wake_up_rec.fd = (gint) wake_up_semaphore;
865 wake_up_rec.events = G_IO_IN;
866 g_main_add_poll_unlocked (0, &wake_up_rec);
870 fd_array = g_new (GPollFD, n_poll_records);
872 pollrec = poll_records;
874 while (pollrec && (!use_priority || priority >= pollrec->priority))
876 fd_array[i].fd = pollrec->fd->fd;
877 fd_array[i].events = pollrec->fd->events;
878 fd_array[i].revents = 0;
880 pollrec = pollrec->next;
883 #ifdef G_THREADS_ENABLED
886 G_UNLOCK (main_loop);
888 (*poll_func) (fd_array, npoll, timeout);
891 #ifdef G_THREADS_ENABLED
896 read (wake_up_pipe[0], &c, 1);
900 poll_waiting = FALSE;
903 pollrec = poll_records;
907 pollrec->fd->revents = fd_array[i].revents;
908 pollrec = pollrec->next;
916 g_main_add_poll (GPollFD *fd,
920 g_main_add_poll_unlocked (priority, fd);
921 G_UNLOCK (main_loop);
924 /* HOLDS: main_loop_lock */
926 g_main_add_poll_unlocked (gint priority,
929 GPollRec *lastrec, *pollrec, *newrec;
932 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
936 newrec = poll_free_list;
937 poll_free_list = newrec->next;
940 newrec = g_chunk_new (GPollRec, poll_chunk);
943 newrec->priority = priority;
946 pollrec = poll_records;
947 while (pollrec && priority >= pollrec->priority)
950 pollrec = pollrec->next;
954 lastrec->next = newrec;
956 poll_records = newrec;
958 newrec->next = pollrec;
964 g_main_remove_poll (GPollFD *fd)
966 GPollRec *pollrec, *lastrec;
971 pollrec = poll_records;
975 if (pollrec->fd == fd)
978 lastrec->next = pollrec->next;
980 poll_records = pollrec->next;
982 pollrec->next = poll_free_list;
983 poll_free_list = pollrec;
989 pollrec = pollrec->next;
992 G_UNLOCK (main_loop);
996 g_main_set_poll_func (GPollFunc func)
1002 poll_func = (GPollFunc)poll;
1004 poll_func = (GPollFunc)g_poll;
1011 g_timeout_prepare (gpointer source_data,
1012 GTimeVal *current_time,
1016 GTimeoutData *data = source_data;
1018 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
1019 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
1021 *timeout = (msec <= 0) ? 0 : msec;
1027 g_timeout_check (gpointer source_data,
1028 GTimeVal *current_time)
1030 GTimeoutData *data = source_data;
1032 return (data->expiration.tv_sec < current_time->tv_sec) ||
1033 ((data->expiration.tv_sec == current_time->tv_sec) &&
1034 (data->expiration.tv_usec <= current_time->tv_usec));
1038 g_timeout_dispatch (gpointer source_data,
1039 GTimeVal *current_time,
1042 GTimeoutData *data = source_data;
1044 if (data->callback(user_data))
1046 guint seconds = data->interval / 1000;
1047 guint msecs = data->interval - seconds * 1000;
1049 data->expiration.tv_sec = current_time->tv_sec + seconds;
1050 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1051 if (data->expiration.tv_usec >= 1000000)
1053 data->expiration.tv_usec -= 1000000;
1054 data->expiration.tv_sec++;
1063 g_timeout_add_full (gint priority,
1065 GSourceFunc function,
1067 GDestroyNotify notify)
1071 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1073 timeout_data->interval = interval;
1074 timeout_data->callback = function;
1075 g_get_current_time (&timeout_data->expiration);
1077 seconds = timeout_data->interval / 1000;
1078 msecs = timeout_data->interval - seconds * 1000;
1080 timeout_data->expiration.tv_sec += seconds;
1081 timeout_data->expiration.tv_usec += msecs * 1000;
1082 if (timeout_data->expiration.tv_usec >= 1000000)
1084 timeout_data->expiration.tv_usec -= 1000000;
1085 timeout_data->expiration.tv_sec++;
1088 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1092 g_timeout_add (guint32 interval,
1093 GSourceFunc function,
1096 return g_timeout_add_full (G_PRIORITY_DEFAULT,
1097 interval, function, data, NULL);
1100 /* Idle functions */
1103 g_idle_prepare (gpointer source_data,
1104 GTimeVal *current_time,
1112 g_idle_check (gpointer source_data,
1113 GTimeVal *current_time)
1119 g_idle_dispatch (gpointer source_data,
1120 GTimeVal *current_time,
1123 GIdleData *data = source_data;
1125 return (*data->callback)(user_data);
1129 g_idle_add_full (gint priority,
1130 GSourceFunc function,
1132 GDestroyNotify notify)
1134 GIdleData *idle_data = g_new (GIdleData, 1);
1136 idle_data->callback = function;
1138 return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
1142 g_idle_add (GSourceFunc function,
1145 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);