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.
24 * Modified by the GLib Team and others 1997-1999. See the AUTHORS
25 * file for a list of people on the GLib Team. See the ChangeLog
26 * files for a list of changes. These files are distributed with
27 * GLib at ftp://ftp.gtk.org/pub/gtk/.
36 /* uncomment the next line to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
42 #include <sys/types.h>
44 #ifdef HAVE_SYS_TIME_H
46 #endif /* HAVE_SYS_TIME_H */
47 #ifdef GLIB_HAVE_SYS_POLL_H
48 # include <sys/poll.h>
49 # undef events /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
50 # undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
51 #endif /* GLIB_HAVE_SYS_POLL_H */
54 #endif /* HAVE_UNISTD_H */
60 #endif /* G_OS_WIN32 */
63 #include <net/socket.h>
64 #endif /* G_OS_BEOS */
68 typedef struct _GTimeoutData GTimeoutData;
69 typedef struct _GSource GSource;
70 typedef struct _GPollRec GPollRec;
74 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
75 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
104 /* Forward declarations */
106 static gint g_source_compare (GHook *a,
108 static void g_source_destroy_func (GHookList *hook_list,
110 static void g_main_poll (gint timeout,
111 gboolean use_priority,
113 static void g_main_add_poll_unlocked (gint priority,
115 static void g_main_wakeup (void);
117 static gboolean g_timeout_prepare (gpointer source_data,
118 GTimeVal *current_time,
121 static gboolean g_timeout_check (gpointer source_data,
122 GTimeVal *current_time,
124 static gboolean g_timeout_dispatch (gpointer source_data,
125 GTimeVal *dispatch_time,
127 static gboolean g_idle_prepare (gpointer source_data,
128 GTimeVal *current_time,
131 static gboolean g_idle_check (gpointer source_data,
132 GTimeVal *current_time,
134 static gboolean g_idle_dispatch (gpointer source_data,
135 GTimeVal *dispatch_time,
140 static GSList *pending_dispatches = NULL;
141 static GHookList source_list = { 0 };
142 static gint in_check_or_prepare = 0;
144 /* The following lock is used for both the list of sources
145 * and the list of poll records
147 G_LOCK_DEFINE_STATIC (main_loop);
149 static GSourceFuncs timeout_funcs =
157 static GSourceFuncs idle_funcs =
165 static GPollRec *poll_records = NULL;
166 static GPollRec *poll_free_list = NULL;
167 static GMemChunk *poll_chunk;
168 static guint n_poll_records = 0;
170 #ifdef G_THREADS_ENABLED
172 /* this pipe is used to wake up the main loop when a source is added.
174 static gint wake_up_pipe[2] = { -1, -1 };
175 #else /* G_OS_WIN32 */
176 static HANDLE wake_up_semaphore = NULL;
177 #endif /* G_OS_WIN32 */
178 static GPollFD wake_up_rec;
179 static gboolean poll_waiting = FALSE;
181 /* Flag indicating whether the set of fd's changed during a poll */
182 static gboolean poll_changed = FALSE;
183 #endif /* G_THREADS_ENABLED */
186 /* SunOS has poll, but doesn't provide a prototype. */
187 # if defined (sun) && !defined (__SVR4)
188 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
190 static GPollFunc poll_func = (GPollFunc) poll;
191 #else /* !HAVE_POLL */
195 g_poll (GPollFD *fds, guint nfds, gint timeout)
197 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
206 for (f = fds; f < &fds[nfds]; ++f)
209 if (f->events & G_IO_IN)
210 if (f->fd == G_WIN32_MSG_HANDLE)
214 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
215 handles[nhandles++] = (HANDLE) f->fd;
224 /* Waiting for messages, and maybe events */
227 if (timeout == INFINITE)
229 /* Waiting just for messages, infinite timeout
230 * -> Use PeekMessage, then WaitMessage
232 /* g_print ("WaitMessage, PeekMessage\n"); */
233 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
234 ready = WAIT_OBJECT_0;
235 else if (!WaitMessage ())
236 g_warning ("g_poll: WaitMessage failed");
237 ready = WAIT_OBJECT_0;
239 else if (timeout == 0)
241 /* Waiting just for messages, zero timeout
244 /* g_print ("PeekMessage\n"); */
245 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
246 ready = WAIT_OBJECT_0;
248 ready = WAIT_TIMEOUT;
252 /* Waiting just for messages, some timeout
253 * -> First try PeekMessage, then set a timer, wait for message,
254 * kill timer, use PeekMessage
256 /* g_print ("PeekMessage\n"); */
257 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
258 ready = WAIT_OBJECT_0;
259 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
260 g_warning ("g_poll: SetTimer failed");
263 /* g_print ("WaitMessage\n"); */
265 KillTimer (NULL, timer);
266 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
267 ready = WAIT_OBJECT_0;
269 ready = WAIT_TIMEOUT;
275 /* Wait for either message or event
276 * -> Use MsgWaitForMultipleObjects
278 /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
279 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
280 timeout, QS_ALLINPUT);
281 /* g_print("=%d\n", ready); */
282 if (ready == WAIT_FAILED)
283 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
286 else if (nhandles == 0)
288 /* Wait for nothing (huh?) */
293 /* Wait for just events
294 * -> Use WaitForMultipleObjects
296 /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
297 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
298 /* g_print("=%d\n", ready); */
299 if (ready == WAIT_FAILED)
300 g_warning ("g_poll: WaitForMultipleObjects failed");
303 for (f = fds; f < &fds[nfds]; ++f)
306 if (ready == WAIT_FAILED)
308 else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
310 fds[poll_msgs].revents |= G_IO_IN;
312 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
313 for (f = fds; f < &fds[nfds]; ++f)
315 if ((f->events & G_IO_IN)
316 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
318 f->revents |= G_IO_IN;
319 /* g_print ("event %#x\n", f->fd); */
320 ResetEvent ((HANDLE) f->fd);
324 if (ready == WAIT_TIMEOUT)
330 #else /* !G_OS_WIN32 */
332 /* The following implementation of poll() comes from the GNU C Library.
333 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
336 #include <string.h> /* for bzero on BSD systems */
338 #ifdef HAVE_SYS_SELECT_H
339 #include <sys/select.h>
340 #endif /* HAVE_SYS_SELECT_H */
344 #endif /* G_OS_BEOS */
347 # define SELECT_MASK fd_set
348 #else /* !NO_FD_SET */
350 typedef long fd_mask;
353 # define SELECT_MASK void
355 # define SELECT_MASK int
356 # endif /* !_IBMR2 */
357 #endif /* !NO_FD_SET */
360 g_poll (GPollFD *fds,
365 SELECT_MASK rset, wset, xset;
374 for (f = fds; f < &fds[nfds]; ++f)
377 if (f->events & G_IO_IN)
378 FD_SET (f->fd, &rset);
379 if (f->events & G_IO_OUT)
380 FD_SET (f->fd, &wset);
381 if (f->events & G_IO_PRI)
382 FD_SET (f->fd, &xset);
383 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
387 tv.tv_sec = timeout / 1000;
388 tv.tv_usec = (timeout % 1000) * 1000;
390 ready = select (maxfd + 1, &rset, &wset, &xset,
391 timeout == -1 ? NULL : &tv);
393 for (f = fds; f < &fds[nfds]; ++f)
398 if (FD_ISSET (f->fd, &rset))
399 f->revents |= G_IO_IN;
400 if (FD_ISSET (f->fd, &wset))
401 f->revents |= G_IO_OUT;
402 if (FD_ISSET (f->fd, &xset))
403 f->revents |= G_IO_PRI;
410 #endif /* !G_OS_WIN32 */
412 static GPollFunc poll_func = g_poll;
413 #endif /* !HAVE_POLL */
415 /* Hooks for adding to the main loop */
417 /* Use knowledge of insert_sorted algorithm here to make
418 * sure we insert at the end of equal priority items
421 g_source_compare (GHook *a,
424 GSource *source_a = (GSource *)a;
425 GSource *source_b = (GSource *)b;
427 return (source_a->priority < source_b->priority) ? -1 : 1;
430 /* HOLDS: main_loop lock */
432 g_source_destroy_func (GHookList *hook_list,
435 GSource *source = (GSource*) hook;
436 GDestroyNotify destroy;
438 G_UNLOCK (main_loop);
440 destroy = hook->destroy;
442 destroy (hook->data);
444 destroy = ((GSourceFuncs*) hook->func)->destroy;
446 destroy (source->source_data);
452 g_source_add (gint priority,
453 gboolean can_recurse,
455 gpointer source_data,
457 GDestroyNotify notify)
464 if (!source_list.is_setup)
466 g_hook_list_init (&source_list, sizeof (GSource));
468 source_list.hook_destroy = G_HOOK_DEFERRED_DESTROY;
469 source_list.hook_free = g_source_destroy_func;
472 source = (GSource*) g_hook_alloc (&source_list);
473 source->priority = priority;
474 source->source_data = source_data;
475 source->hook.func = funcs;
476 source->hook.data = user_data;
477 source->hook.destroy = notify;
479 g_hook_insert_sorted (&source_list,
484 source->hook.flags |= G_SOURCE_CAN_RECURSE;
486 return_val = source->hook.hook_id;
488 #ifdef G_THREADS_ENABLED
489 /* Now wake up the main loop if it is waiting in the poll() */
493 G_UNLOCK (main_loop);
499 g_source_remove (guint tag)
503 g_return_val_if_fail (tag > 0, FALSE);
507 hook = g_hook_get (&source_list, tag);
509 g_hook_destroy_link (&source_list, hook);
511 G_UNLOCK (main_loop);
517 g_source_remove_by_user_data (gpointer user_data)
523 hook = g_hook_find_data (&source_list, TRUE, user_data);
525 g_hook_destroy_link (&source_list, hook);
527 G_UNLOCK (main_loop);
533 g_source_find_source_data (GHook *hook,
536 GSource *source = (GSource *)hook;
538 return (source->source_data == data);
542 g_source_remove_by_source_data (gpointer source_data)
548 hook = g_hook_find (&source_list, TRUE,
549 g_source_find_source_data, source_data);
551 g_hook_destroy_link (&source_list, hook);
553 G_UNLOCK (main_loop);
559 g_source_find_funcs_user_data (GHook *hook,
564 return hook->func == d[0] && hook->data == d[1];
568 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
574 g_return_val_if_fail (funcs != NULL, FALSE);
581 hook = g_hook_find (&source_list, TRUE,
582 g_source_find_funcs_user_data, d);
584 g_hook_destroy_link (&source_list, hook);
586 G_UNLOCK (main_loop);
592 g_get_current_time (GTimeVal *result)
596 g_return_if_fail (result != NULL);
598 /*this is required on alpha, there the timeval structs are int's
599 not longs and a cast only would fail horribly*/
600 gettimeofday (&r, NULL);
601 result->tv_sec = r.tv_sec;
602 result->tv_usec = r.tv_usec;
604 /* Avoid calling time() except for the first time.
605 * GetTickCount() should be pretty fast and low-level?
606 * I could also use ftime() but it seems unnecessarily overheady.
608 static DWORD start_tick = 0;
609 static time_t start_time;
613 g_return_if_fail (result != NULL);
617 start_tick = GetTickCount ();
621 tick = GetTickCount ();
623 result->tv_sec = (tick - start_tick) / 1000 + start_time;
624 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
628 /* Running the main loop */
630 /* HOLDS: main_loop_lock */
632 g_main_dispatch (GTimeVal *dispatch_time)
634 while (pending_dispatches != NULL)
636 gboolean need_destroy;
637 GSource *source = pending_dispatches->data;
640 tmp_list = pending_dispatches;
641 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
642 g_slist_free_1 (tmp_list);
644 if (G_HOOK_IS_VALID (source))
646 gboolean was_in_call;
647 gpointer hook_data = source->hook.data;
648 gpointer source_data = source->source_data;
649 gboolean (*dispatch) (gpointer,
653 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
655 was_in_call = G_HOOK_IN_CALL (source);
656 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
658 G_UNLOCK (main_loop);
659 need_destroy = ! dispatch (source_data,
665 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
667 if (need_destroy && G_HOOK_IS_VALID (source))
668 g_hook_destroy_link (&source_list, (GHook *) source);
671 g_hook_unref (&source_list, (GHook*) source);
675 /* g_main_iterate () runs a single iteration of the mainloop, or,
676 * if !dispatch checks to see if any sources need dispatching.
677 * basic algorithm for dispatch=TRUE:
679 * 1) while the list of currently pending sources is non-empty,
680 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
681 * removing sources from the list after each returns.
682 * the return value of (*dispatch) determines whether the source
683 * itself is kept alive.
685 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
686 * are !IN_CALL or can_recurse. a return value of TRUE determines
687 * that the source would like to be dispatched immediatedly, it
688 * is then flagged as SOURCE_READY.
690 * 3) poll with the pollfds from all sources at the priority of the
691 * first source flagged as SOURCE_READY. if there are any sources
692 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
693 * of all timouts otherwise.
695 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
696 * (*check) returns true, add the source to the pending list.
697 * once one source returns true, stop after checking all sources
700 * 5) while the list of currently pending sources is non-empty,
701 * call (*dispatch) on each source, removing the source
706 g_main_iterate (gboolean block,
710 GTimeVal current_time = { 0, 0 };
712 gint current_priority = 0;
714 gboolean retval = FALSE;
716 g_return_val_if_fail (!block || dispatch, FALSE);
718 g_get_current_time (¤t_time);
722 #ifdef G_THREADS_ENABLED
725 g_warning("g_main_iterate(): main loop already active in another thread");
726 G_UNLOCK (main_loop);
729 #endif G_THREADS_ENABLED
731 /* If recursing, finish up current dispatch, before starting over */
732 if (pending_dispatches)
735 g_main_dispatch (¤t_time);
737 G_UNLOCK (main_loop);
742 /* Prepare all sources */
744 timeout = block ? -1 : 0;
746 hook = g_hook_first_valid (&source_list, TRUE);
749 GSource *source = (GSource*) hook;
750 gint source_timeout = -1;
752 if ((n_ready > 0) && (source->priority > current_priority))
754 g_hook_unref (&source_list, hook);
757 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
759 hook = g_hook_next_valid (&source_list, hook, TRUE);
763 if (!(hook->flags & G_SOURCE_READY))
765 gboolean (*prepare) (gpointer source_data,
766 GTimeVal *current_time,
770 prepare = ((GSourceFuncs *) hook->func)->prepare;
771 in_check_or_prepare++;
772 G_UNLOCK (main_loop);
774 if ((*prepare) (source->source_data, ¤t_time, &source_timeout, source->hook.data))
775 hook->flags |= G_SOURCE_READY;
778 in_check_or_prepare--;
781 if (hook->flags & G_SOURCE_READY)
785 g_hook_unref (&source_list, hook);
786 G_UNLOCK (main_loop);
793 current_priority = source->priority;
798 if (source_timeout >= 0)
801 timeout = source_timeout;
803 timeout = MIN (timeout, source_timeout);
806 hook = g_hook_next_valid (&source_list, hook, TRUE);
809 /* poll(), if necessary */
811 g_main_poll (timeout, n_ready > 0, current_priority);
814 g_get_current_time (¤t_time);
816 /* Check to see what sources need to be dispatched */
820 hook = g_hook_first_valid (&source_list, TRUE);
823 GSource *source = (GSource *)hook;
825 if ((n_ready > 0) && (source->priority > current_priority))
827 g_hook_unref (&source_list, hook);
830 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
832 hook = g_hook_next_valid (&source_list, hook, TRUE);
836 if (!(hook->flags & G_SOURCE_READY))
838 gboolean (*check) (gpointer source_data,
839 GTimeVal *current_time,
842 check = ((GSourceFuncs *) hook->func)->check;
843 in_check_or_prepare++;
844 G_UNLOCK (main_loop);
846 if ((*check) (source->source_data, ¤t_time, source->hook.data))
847 hook->flags |= G_SOURCE_READY;
850 in_check_or_prepare--;
853 if (hook->flags & G_SOURCE_READY)
857 hook->flags &= ~G_SOURCE_READY;
858 g_hook_ref (&source_list, hook);
859 pending_dispatches = g_slist_prepend (pending_dispatches, source);
860 current_priority = source->priority;
865 g_hook_unref (&source_list, hook);
866 G_UNLOCK (main_loop);
872 hook = g_hook_next_valid (&source_list, hook, TRUE);
875 /* Now invoke the callbacks */
877 if (pending_dispatches)
879 pending_dispatches = g_slist_reverse (pending_dispatches);
880 g_main_dispatch (¤t_time);
884 G_UNLOCK (main_loop);
889 /* See if any events are pending
892 g_main_pending (void)
894 return in_check_or_prepare ? FALSE : g_main_iterate (FALSE, FALSE);
897 /* Run a single iteration of the mainloop. If block is FALSE,
901 g_main_iteration (gboolean block)
903 if (in_check_or_prepare)
905 g_warning ("g_main_iteration(): called recursively from within a source's check() or "
906 "prepare() member or from a second thread, iteration not possible");
910 return g_main_iterate (block, TRUE);
914 g_main_new (gboolean is_running)
918 loop = g_new0 (GMainLoop, 1);
919 loop->is_running = is_running != FALSE;
925 g_main_run (GMainLoop *loop)
927 g_return_if_fail (loop != NULL);
929 if (in_check_or_prepare)
931 g_warning ("g_main_run(): called recursively from within a source's check() or "
932 "prepare() member or from a second thread, iteration not possible");
936 loop->is_running = TRUE;
937 while (loop->is_running)
938 g_main_iterate (TRUE, TRUE);
942 g_main_quit (GMainLoop *loop)
944 g_return_if_fail (loop != NULL);
946 loop->is_running = FALSE;
950 g_main_destroy (GMainLoop *loop)
952 g_return_if_fail (loop != NULL);
958 g_main_is_running (GMainLoop *loop)
960 g_return_val_if_fail (loop != NULL, FALSE);
962 return loop->is_running;
965 /* HOLDS: main_loop_lock */
967 g_main_poll (gint timeout,
968 gboolean use_priority,
971 #ifdef G_MAIN_POLL_DEBUG
979 #ifdef G_THREADS_ENABLED
981 if (wake_up_pipe[0] < 0)
983 if (pipe (wake_up_pipe) < 0)
984 g_error ("Cannot create pipe main loop wake-up: %s\n",
987 wake_up_rec.fd = wake_up_pipe[0];
988 wake_up_rec.events = G_IO_IN;
989 g_main_add_poll_unlocked (0, &wake_up_rec);
992 if (wake_up_semaphore == NULL)
994 if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
995 g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
996 wake_up_rec.fd = (gint) wake_up_semaphore;
997 wake_up_rec.events = G_IO_IN;
998 g_main_add_poll_unlocked (0, &wake_up_rec);
1002 fd_array = g_new (GPollFD, n_poll_records);
1004 pollrec = poll_records;
1006 while (pollrec && (!use_priority || priority >= pollrec->priority))
1008 if (pollrec->fd->events)
1010 fd_array[i].fd = pollrec->fd->fd;
1011 /* In direct contradiction to the Unix98 spec, IRIX runs into
1012 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
1013 * flags in the events field of the pollfd while it should
1014 * just ignoring them. So we mask them out here.
1016 fd_array[i].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
1017 fd_array[i].revents = 0;
1021 pollrec = pollrec->next;
1023 #ifdef G_THREADS_ENABLED
1024 poll_waiting = TRUE;
1025 poll_changed = FALSE;
1029 if (npoll || timeout != 0)
1031 #ifdef G_MAIN_POLL_DEBUG
1032 g_print ("g_main_poll(%d) timeout: %d\r", npoll, timeout);
1033 poll_timer = g_timer_new ();
1036 G_UNLOCK (main_loop);
1037 (*poll_func) (fd_array, npoll, timeout);
1040 #ifdef G_MAIN_POLL_DEBUG
1041 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
1044 g_timer_elapsed (poll_timer, NULL));
1045 g_timer_destroy (poll_timer);
1046 pollrec = poll_records;
1050 if (pollrec->fd->events)
1052 if (fd_array[i].revents)
1054 g_print (" [%d:", fd_array[i].fd);
1055 if (fd_array[i].revents & G_IO_IN)
1057 if (fd_array[i].revents & G_IO_OUT)
1059 if (fd_array[i].revents & G_IO_PRI)
1061 if (fd_array[i].revents & G_IO_ERR)
1063 if (fd_array[i].revents & G_IO_HUP)
1065 if (fd_array[i].revents & G_IO_NVAL)
1071 pollrec = pollrec->next;
1075 } /* if (npoll || timeout != 0) */
1077 #ifdef G_THREADS_ENABLED
1082 read (wake_up_pipe[0], &c, 1);
1086 poll_waiting = FALSE;
1088 /* If the set of poll file descriptors changed, bail out
1089 * and let the main loop rerun
1098 pollrec = poll_records;
1102 if (pollrec->fd->events)
1104 pollrec->fd->revents = fd_array[i].revents;
1107 pollrec = pollrec->next;
1114 g_main_add_poll (GPollFD *fd,
1118 g_main_add_poll_unlocked (priority, fd);
1119 G_UNLOCK (main_loop);
1122 /* HOLDS: main_loop_lock */
1124 g_main_add_poll_unlocked (gint priority,
1127 GPollRec *lastrec, *pollrec, *newrec;
1130 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
1134 newrec = poll_free_list;
1135 poll_free_list = newrec->next;
1138 newrec = g_chunk_new (GPollRec, poll_chunk);
1141 newrec->priority = priority;
1144 pollrec = poll_records;
1145 while (pollrec && priority >= pollrec->priority)
1148 pollrec = pollrec->next;
1152 lastrec->next = newrec;
1154 poll_records = newrec;
1156 newrec->next = pollrec;
1160 #ifdef G_THREADS_ENABLED
1161 poll_changed = TRUE;
1163 /* Now wake up the main loop if it is waiting in the poll() */
1169 g_main_remove_poll (GPollFD *fd)
1171 GPollRec *pollrec, *lastrec;
1176 pollrec = poll_records;
1180 if (pollrec->fd == fd)
1182 if (lastrec != NULL)
1183 lastrec->next = pollrec->next;
1185 poll_records = pollrec->next;
1187 pollrec->next = poll_free_list;
1188 poll_free_list = pollrec;
1194 pollrec = pollrec->next;
1197 #ifdef G_THREADS_ENABLED
1198 poll_changed = TRUE;
1200 /* Now wake up the main loop if it is waiting in the poll() */
1204 G_UNLOCK (main_loop);
1208 g_main_set_poll_func (GPollFunc func)
1214 poll_func = (GPollFunc) poll;
1216 poll_func = (GPollFunc) g_poll;
1220 /* Wake the main loop up from a poll() */
1222 g_main_wakeup (void)
1224 #ifdef G_THREADS_ENABLED
1227 poll_waiting = FALSE;
1229 write (wake_up_pipe[1], "A", 1);
1231 ReleaseSemaphore (wake_up_semaphore, 1, NULL);
1240 g_timeout_set_expiration (GTimeoutData *data,
1241 GTimeVal *current_time)
1243 guint seconds = data->interval / 1000;
1244 guint msecs = data->interval - seconds * 1000;
1246 data->expiration.tv_sec = current_time->tv_sec + seconds;
1247 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1248 if (data->expiration.tv_usec >= 1000000)
1250 data->expiration.tv_usec -= 1000000;
1251 data->expiration.tv_sec++;
1256 g_timeout_prepare (gpointer source_data,
1257 GTimeVal *current_time,
1262 GTimeoutData *data = source_data;
1264 msec = ((data->expiration.tv_sec - current_time->tv_sec) * 1000 +
1265 (data->expiration.tv_usec - current_time->tv_usec) / 1000);
1269 else if (msec > data->interval)
1271 /* The system time has been set backwards, so we
1272 * reset the expiration time to now + data->interval;
1273 * this at least avoids hanging for long periods of time.
1275 g_timeout_set_expiration (data, current_time);
1276 msec = data->interval;
1285 g_timeout_check (gpointer source_data,
1286 GTimeVal *current_time,
1289 GTimeoutData *data = source_data;
1291 return ((data->expiration.tv_sec < current_time->tv_sec) ||
1292 ((data->expiration.tv_sec == current_time->tv_sec) &&
1293 (data->expiration.tv_usec <= current_time->tv_usec)));
1297 g_timeout_dispatch (gpointer source_data,
1298 GTimeVal *dispatch_time,
1301 GTimeoutData *data = source_data;
1303 if (data->callback (user_data))
1305 g_timeout_set_expiration (data, dispatch_time);
1314 g_timeout_add_full (gint priority,
1316 GSourceFunc function,
1318 GDestroyNotify notify)
1320 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1321 GTimeVal current_time;
1323 timeout_data->interval = interval;
1324 timeout_data->callback = function;
1325 g_get_current_time (¤t_time);
1327 g_timeout_set_expiration (timeout_data, ¤t_time);
1329 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1333 g_timeout_add (guint32 interval,
1334 GSourceFunc function,
1337 return g_timeout_add_full (G_PRIORITY_DEFAULT,
1338 interval, function, data, NULL);
1341 /* Idle functions */
1344 g_idle_prepare (gpointer source_data,
1345 GTimeVal *current_time,
1354 g_idle_check (gpointer source_data,
1355 GTimeVal *current_time,
1362 g_idle_dispatch (gpointer source_data,
1363 GTimeVal *dispatch_time,
1366 GSourceFunc func = source_data;
1368 return func (user_data);
1372 g_idle_add_full (gint priority,
1373 GSourceFunc function,
1375 GDestroyNotify notify)
1377 g_return_val_if_fail (function != NULL, 0);
1379 return g_source_add (priority, FALSE, &idle_funcs, function, data, notify);
1383 g_idle_add (GSourceFunc function,
1386 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1390 g_idle_remove_by_data (gpointer data)
1392 return g_source_remove_by_funcs_user_data (&idle_funcs, data);