Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / glib / gmain.c
blob8ead8a017471ba087a6e5f2ae88357be0b9673c0
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/.
30 /*
31 * MT safe
34 #include "config.h"
36 /* uncomment the next line to get poll() debugging info */
37 /* #define G_MAIN_POLL_DEBUG */
41 #include "glib.h"
42 #include <sys/types.h>
43 #include <time.h>
44 #ifdef HAVE_SYS_TIME_H
45 #include <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 */
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif /* HAVE_UNISTD_H */
55 #include <errno.h>
57 #ifdef NATIVE_WIN32
58 #define STRICT
59 #include <windows.h>
60 #endif /* NATIVE_WIN32 */
62 #ifdef _MSC_VER
63 #include <fcntl.h>
64 #include <io.h>
65 #endif /* _MSC_VER */
67 /* Types */
69 typedef struct _GTimeoutData GTimeoutData;
70 typedef struct _GSource GSource;
71 typedef struct _GPollRec GPollRec;
73 typedef enum
75 G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
76 G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
77 } GSourceFlags;
79 struct _GSource
81 GHook hook;
82 gint priority;
83 gpointer source_data;
86 struct _GMainLoop
88 gboolean is_running;
91 struct _GTimeoutData
93 GTimeVal expiration;
94 gint interval;
95 GSourceFunc callback;
98 struct _GPollRec
100 gint priority;
101 GPollFD *fd;
102 GPollRec *next;
105 /* Forward declarations */
107 static gint g_source_compare (GHook *a,
108 GHook *b);
109 static void g_source_destroy_func (GHookList *hook_list,
110 GHook *hook);
111 static void g_main_poll (gint timeout,
112 gboolean use_priority,
113 gint priority);
114 static void g_main_add_poll_unlocked (gint priority,
115 GPollFD *fd);
116 static void g_main_wakeup (void);
118 static gboolean g_timeout_prepare (gpointer source_data,
119 GTimeVal *current_time,
120 gint *timeout,
121 gpointer user_data);
122 static gboolean g_timeout_check (gpointer source_data,
123 GTimeVal *current_time,
124 gpointer user_data);
125 static gboolean g_timeout_dispatch (gpointer source_data,
126 GTimeVal *dispatch_time,
127 gpointer user_data);
128 static gboolean g_idle_prepare (gpointer source_data,
129 GTimeVal *current_time,
130 gint *timeout,
131 gpointer user_data);
132 static gboolean g_idle_check (gpointer source_data,
133 GTimeVal *current_time,
134 gpointer user_data);
135 static gboolean g_idle_dispatch (gpointer source_data,
136 GTimeVal *dispatch_time,
137 gpointer user_data);
139 /* Data */
141 static GSList *pending_dispatches = NULL;
142 static GHookList source_list = { 0 };
143 static gint in_check_or_prepare = 0;
145 /* The following lock is used for both the list of sources
146 * and the list of poll records
148 G_LOCK_DEFINE_STATIC (main_loop);
150 static GSourceFuncs timeout_funcs =
152 g_timeout_prepare,
153 g_timeout_check,
154 g_timeout_dispatch,
155 g_free,
158 static GSourceFuncs idle_funcs =
160 g_idle_prepare,
161 g_idle_check,
162 g_idle_dispatch,
163 NULL,
166 static GPollRec *poll_records = NULL;
167 static GPollRec *poll_free_list = NULL;
168 static GMemChunk *poll_chunk;
169 static guint n_poll_records = 0;
171 #ifdef G_THREADS_ENABLED
172 #ifndef NATIVE_WIN32
173 /* this pipe is used to wake up the main loop when a source is added.
175 static gint wake_up_pipe[2] = { -1, -1 };
176 #else /* NATIVE_WIN32 */
177 static HANDLE wake_up_semaphore = NULL;
178 #endif /* NATIVE_WIN32 */
179 static GPollFD wake_up_rec;
180 static gboolean poll_waiting = FALSE;
182 /* Flag indicating whether the set of fd's changed during a poll */
183 static gboolean poll_changed = FALSE;
184 #endif /* G_THREADS_ENABLED */
186 #ifdef HAVE_POLL
187 /* SunOS has poll, but doesn't provide a prototype. */
188 # if defined (sun) && !defined (__SVR4)
189 extern gint poll (GPollFD *ufds, guint nfsd, gint timeout);
190 # endif /* !sun */
191 static GPollFunc poll_func = (GPollFunc) poll;
192 #else /* !HAVE_POLL */
193 #ifdef NATIVE_WIN32
195 static gint
196 g_poll (GPollFD *fds, guint nfds, gint timeout)
198 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
199 GPollFD *f;
200 DWORD ready;
201 MSG msg;
202 UINT timer;
203 LONG prevcnt;
204 gint poll_msgs = -1;
205 gint nhandles = 0;
207 for (f = fds; f < &fds[nfds]; ++f)
208 if (f->fd >= 0)
210 if (f->events & G_IO_IN)
211 if (f->fd == G_WIN32_MSG_HANDLE)
212 poll_msgs = f - fds;
213 else
215 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
216 handles[nhandles++] = (HANDLE) f->fd;
220 if (timeout == -1)
221 timeout = INFINITE;
223 if (poll_msgs >= 0)
225 /* Waiting for messages, and maybe events */
226 if (nhandles == 0)
228 if (timeout == INFINITE)
230 /* Waiting just for messages, infinite timeout
231 * -> Use PeekMessage, then WaitMessage
233 /* g_print ("WaitMessage, PeekMessage\n"); */
234 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
235 ready = WAIT_OBJECT_0;
236 else if (!WaitMessage ())
237 g_warning ("g_poll: WaitMessage failed");
238 ready = WAIT_OBJECT_0;
240 else if (timeout == 0)
242 /* Waiting just for messages, zero timeout
243 * -> Use PeekMessage
245 /* g_print ("PeekMessage\n"); */
246 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
247 ready = WAIT_OBJECT_0;
248 else
249 ready = WAIT_TIMEOUT;
251 else
253 /* Waiting just for messages, some timeout
254 * -> First try PeekMessage, then set a timer, wait for message,
255 * kill timer, use PeekMessage
257 /* g_print ("PeekMessage\n"); */
258 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
259 ready = WAIT_OBJECT_0;
260 else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
261 g_warning ("g_poll: SetTimer failed");
262 else
264 /* g_print ("WaitMessage\n"); */
265 WaitMessage ();
266 KillTimer (NULL, timer);
267 if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
268 ready = WAIT_OBJECT_0;
269 else
270 ready = WAIT_TIMEOUT;
274 else
276 /* Wait for either message or event
277 * -> Use MsgWaitForMultipleObjects
279 /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
280 ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
281 timeout, QS_ALLINPUT);
282 /* g_print("=%d\n", ready); */
283 if (ready == WAIT_FAILED)
284 g_warning ("g_poll: MsgWaitForMultipleObjects failed");
287 else if (nhandles == 0)
289 /* Wait for nothing (huh?) */
290 return 0;
292 else
294 /* Wait for just events
295 * -> Use WaitForMultipleObjects
297 /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
298 ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
299 /* g_print("=%d\n", ready); */
300 if (ready == WAIT_FAILED)
301 g_warning ("g_poll: WaitForMultipleObjects failed");
304 for (f = fds; f < &fds[nfds]; ++f)
305 f->revents = 0;
307 if (ready == WAIT_FAILED)
308 return -1;
309 else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
311 fds[poll_msgs].revents |= G_IO_IN;
313 else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
314 for (f = fds; f < &fds[nfds]; ++f)
316 if ((f->events & G_IO_IN)
317 && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
319 f->revents |= G_IO_IN;
320 /* g_print ("event %#x\n", f->fd); */
321 ResetEvent ((HANDLE) f->fd);
325 if (ready == WAIT_TIMEOUT)
326 return 0;
327 else
328 return 1;
331 #else /* !NATIVE_WIN32 */
333 /* The following implementation of poll() comes from the GNU C Library.
334 * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
337 #include <string.h> /* for bzero on BSD systems */
339 #ifdef HAVE_SYS_SELECT_H
340 #include <sys/select.h>
341 #endif /* HAVE_SYS_SELECT_H_ */
343 #ifndef NO_FD_SET
344 # define SELECT_MASK fd_set
345 #else /* !NO_FD_SET */
346 # ifndef _AIX
347 typedef long fd_mask;
348 # endif /* _AIX */
349 # ifdef _IBMR2
350 # define SELECT_MASK void
351 # else /* !_IBMR2 */
352 # define SELECT_MASK int
353 # endif /* !_IBMR2 */
354 #endif /* !NO_FD_SET */
356 static gint
357 g_poll (GPollFD *fds,
358 guint nfds,
359 gint timeout)
361 struct timeval tv;
362 SELECT_MASK rset, wset, xset;
363 GPollFD *f;
364 int ready;
365 int maxfd = 0;
367 FD_ZERO (&rset);
368 FD_ZERO (&wset);
369 FD_ZERO (&xset);
371 for (f = fds; f < &fds[nfds]; ++f)
372 if (f->fd >= 0)
374 if (f->events & G_IO_IN)
375 FD_SET (f->fd, &rset);
376 if (f->events & G_IO_OUT)
377 FD_SET (f->fd, &wset);
378 if (f->events & G_IO_PRI)
379 FD_SET (f->fd, &xset);
380 if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
381 maxfd = f->fd;
384 tv.tv_sec = timeout / 1000;
385 tv.tv_usec = (timeout % 1000) * 1000;
387 ready = select (maxfd + 1, &rset, &wset, &xset,
388 timeout == -1 ? NULL : &tv);
389 if (ready > 0)
390 for (f = fds; f < &fds[nfds]; ++f)
392 f->revents = 0;
393 if (f->fd >= 0)
395 if (FD_ISSET (f->fd, &rset))
396 f->revents |= G_IO_IN;
397 if (FD_ISSET (f->fd, &wset))
398 f->revents |= G_IO_OUT;
399 if (FD_ISSET (f->fd, &xset))
400 f->revents |= G_IO_PRI;
404 return ready;
407 #endif /* !NATIVE_WIN32 */
409 static GPollFunc poll_func = g_poll;
410 #endif /* !HAVE_POLL */
412 /* Hooks for adding to the main loop */
414 /* Use knowledge of insert_sorted algorithm here to make
415 * sure we insert at the end of equal priority items
417 static gint
418 g_source_compare (GHook *a,
419 GHook *b)
421 GSource *source_a = (GSource *)a;
422 GSource *source_b = (GSource *)b;
424 return (source_a->priority < source_b->priority) ? -1 : 1;
427 /* HOLDS: main_loop lock */
428 static void
429 g_source_destroy_func (GHookList *hook_list,
430 GHook *hook)
432 GSource *source = (GSource*) hook;
433 GDestroyNotify destroy;
435 G_UNLOCK (main_loop);
437 destroy = hook->destroy;
438 if (destroy)
439 destroy (hook->data);
441 destroy = ((GSourceFuncs*) hook->func)->destroy;
442 if (destroy)
443 destroy (source->source_data);
445 G_LOCK (main_loop);
448 guint
449 g_source_add (gint priority,
450 gboolean can_recurse,
451 GSourceFuncs *funcs,
452 gpointer source_data,
453 gpointer user_data,
454 GDestroyNotify notify)
456 guint return_val;
457 GSource *source;
459 G_LOCK (main_loop);
461 if (!source_list.is_setup)
463 g_hook_list_init (&source_list, sizeof (GSource));
465 source_list.hook_destroy = G_HOOK_DEFERRED_DESTROY;
466 source_list.hook_free = g_source_destroy_func;
469 source = (GSource*) g_hook_alloc (&source_list);
470 source->priority = priority;
471 source->source_data = source_data;
472 source->hook.func = funcs;
473 source->hook.data = user_data;
474 source->hook.destroy = notify;
476 g_hook_insert_sorted (&source_list,
477 (GHook *)source,
478 g_source_compare);
480 if (can_recurse)
481 source->hook.flags |= G_SOURCE_CAN_RECURSE;
483 return_val = source->hook.hook_id;
485 #ifdef G_THREADS_ENABLED
486 /* Now wake up the main loop if it is waiting in the poll() */
487 g_main_wakeup ();
488 #endif
490 G_UNLOCK (main_loop);
492 return return_val;
495 gboolean
496 g_source_remove (guint tag)
498 GHook *hook;
500 g_return_val_if_fail (tag > 0, FALSE);
502 G_LOCK (main_loop);
504 hook = g_hook_get (&source_list, tag);
505 if (hook)
506 g_hook_destroy_link (&source_list, hook);
508 G_UNLOCK (main_loop);
510 return hook != NULL;
513 gboolean
514 g_source_remove_by_user_data (gpointer user_data)
516 GHook *hook;
518 G_LOCK (main_loop);
520 hook = g_hook_find_data (&source_list, TRUE, user_data);
521 if (hook)
522 g_hook_destroy_link (&source_list, hook);
524 G_UNLOCK (main_loop);
526 return hook != NULL;
529 static gboolean
530 g_source_find_source_data (GHook *hook,
531 gpointer data)
533 GSource *source = (GSource *)hook;
535 return (source->source_data == data);
538 gboolean
539 g_source_remove_by_source_data (gpointer source_data)
541 GHook *hook;
543 G_LOCK (main_loop);
545 hook = g_hook_find (&source_list, TRUE,
546 g_source_find_source_data, source_data);
547 if (hook)
548 g_hook_destroy_link (&source_list, hook);
550 G_UNLOCK (main_loop);
552 return hook != NULL;
555 static gboolean
556 g_source_find_funcs_user_data (GHook *hook,
557 gpointer data)
559 gpointer *d = data;
561 return hook->func == d[0] && hook->data == d[1];
564 gboolean
565 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
566 gpointer user_data)
568 gpointer d[2];
569 GHook *hook;
571 g_return_val_if_fail (funcs != NULL, FALSE);
573 G_LOCK (main_loop);
575 d[0] = funcs;
576 d[1] = user_data;
578 hook = g_hook_find (&source_list, TRUE,
579 g_source_find_funcs_user_data, d);
580 if (hook)
581 g_hook_destroy_link (&source_list, hook);
583 G_UNLOCK (main_loop);
585 return hook != NULL;
588 void
589 g_get_current_time (GTimeVal *result)
591 #ifndef _MSC_VER
592 struct timeval r;
593 g_return_if_fail (result != NULL);
595 /*this is required on alpha, there the timeval structs are int's
596 not longs and a cast only would fail horribly*/
597 gettimeofday (&r, NULL);
598 result->tv_sec = r.tv_sec;
599 result->tv_usec = r.tv_usec;
600 #else
601 /* Avoid calling time() except for the first time.
602 * GetTickCount() should be pretty fast and low-level?
603 * I could also use ftime() but it seems unnecessarily overheady.
605 static DWORD start_tick = 0;
606 static time_t start_time;
607 DWORD tick;
608 time_t t;
610 g_return_if_fail (result != NULL);
612 if (start_tick == 0)
614 start_tick = GetTickCount ();
615 time (&start_time);
618 tick = GetTickCount ();
620 result->tv_sec = (tick - start_tick) / 1000 + start_time;
621 result->tv_usec = ((tick - start_tick) % 1000) * 1000;
622 #endif
625 /* Running the main loop */
627 /* HOLDS: main_loop_lock */
628 static void
629 g_main_dispatch (GTimeVal *dispatch_time)
631 while (pending_dispatches != NULL)
633 gboolean need_destroy;
634 GSource *source = pending_dispatches->data;
635 GSList *tmp_list;
637 tmp_list = pending_dispatches;
638 pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
639 g_slist_free_1 (tmp_list);
641 if (G_HOOK_IS_VALID (source))
643 gboolean was_in_call;
644 gpointer hook_data = source->hook.data;
645 gpointer source_data = source->source_data;
646 gboolean (*dispatch) (gpointer,
647 GTimeVal *,
648 gpointer);
650 dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
652 was_in_call = G_HOOK_IN_CALL (source);
653 source->hook.flags |= G_HOOK_FLAG_IN_CALL;
655 G_UNLOCK (main_loop);
656 need_destroy = ! dispatch (source_data,
657 dispatch_time,
658 hook_data);
659 G_LOCK (main_loop);
661 if (!was_in_call)
662 source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
664 if (need_destroy && G_HOOK_IS_VALID (source))
665 g_hook_destroy_link (&source_list, (GHook *) source);
668 g_hook_unref (&source_list, (GHook*) source);
672 /* g_main_iterate () runs a single iteration of the mainloop, or,
673 * if !dispatch checks to see if any sources need dispatching.
674 * basic algorithm for dispatch=TRUE:
676 * 1) while the list of currently pending sources is non-empty,
677 * we call (*dispatch) on those that are !IN_CALL or can_recurse,
678 * removing sources from the list after each returns.
679 * the return value of (*dispatch) determines whether the source
680 * itself is kept alive.
682 * 2) call (*prepare) for sources that are not yet SOURCE_READY and
683 * are !IN_CALL or can_recurse. a return value of TRUE determines
684 * that the source would like to be dispatched immediatedly, it
685 * is then flagged as SOURCE_READY.
687 * 3) poll with the pollfds from all sources at the priority of the
688 * first source flagged as SOURCE_READY. if there are any sources
689 * flagged as SOURCE_READY, we use a timeout of 0 or the minimum
690 * of all timouts otherwise.
692 * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
693 * (*check) returns true, add the source to the pending list.
694 * once one source returns true, stop after checking all sources
695 * at that priority.
697 * 5) while the list of currently pending sources is non-empty,
698 * call (*dispatch) on each source, removing the source
699 * after the call.
702 static gboolean
703 g_main_iterate (gboolean block,
704 gboolean dispatch)
706 GHook *hook;
707 GTimeVal current_time = { 0, 0 };
708 gint n_ready = 0;
709 gint current_priority = 0;
710 gint timeout;
711 gboolean retval = FALSE;
713 g_return_val_if_fail (!block || dispatch, FALSE);
715 g_get_current_time (&current_time);
717 G_LOCK (main_loop);
719 #ifdef G_THREADS_ENABLED
720 if (poll_waiting)
722 g_warning("g_main_iterate(): main loop already active in another thread");
723 G_UNLOCK (main_loop);
724 return FALSE;
726 #endif
728 /* If recursing, finish up current dispatch, before starting over */
729 if (pending_dispatches)
731 if (dispatch)
732 g_main_dispatch (&current_time);
734 G_UNLOCK (main_loop);
736 return TRUE;
739 /* Prepare all sources */
741 timeout = block ? -1 : 0;
743 hook = g_hook_first_valid (&source_list, TRUE);
744 while (hook)
746 GSource *source = (GSource*) hook;
747 gint source_timeout = -1;
749 if ((n_ready > 0) && (source->priority > current_priority))
751 g_hook_unref (&source_list, hook);
752 break;
754 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
756 hook = g_hook_next_valid (&source_list, hook, TRUE);
757 continue;
760 if (!(hook->flags & G_SOURCE_READY))
762 gboolean (*prepare) (gpointer source_data,
763 GTimeVal *current_time,
764 gint *timeout,
765 gpointer user_data);
767 prepare = ((GSourceFuncs *) hook->func)->prepare;
768 in_check_or_prepare++;
769 G_UNLOCK (main_loop);
771 if ((*prepare) (source->source_data, &current_time, &source_timeout, source->hook.data))
772 hook->flags |= G_SOURCE_READY;
774 G_LOCK (main_loop);
775 in_check_or_prepare--;
778 if (hook->flags & G_SOURCE_READY)
780 if (!dispatch)
782 g_hook_unref (&source_list, hook);
783 G_UNLOCK (main_loop);
785 return TRUE;
787 else
789 n_ready++;
790 current_priority = source->priority;
791 timeout = 0;
795 if (source_timeout >= 0)
797 if (timeout < 0)
798 timeout = source_timeout;
799 else
800 timeout = MIN (timeout, source_timeout);
803 hook = g_hook_next_valid (&source_list, hook, TRUE);
806 /* poll(), if necessary */
808 g_main_poll (timeout, n_ready > 0, current_priority);
810 if (timeout != 0)
811 g_get_current_time (&current_time);
813 /* Check to see what sources need to be dispatched */
815 n_ready = 0;
817 hook = g_hook_first_valid (&source_list, TRUE);
818 while (hook)
820 GSource *source = (GSource *)hook;
822 if ((n_ready > 0) && (source->priority > current_priority))
824 g_hook_unref (&source_list, hook);
825 break;
827 if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
829 hook = g_hook_next_valid (&source_list, hook, TRUE);
830 continue;
833 if (!(hook->flags & G_SOURCE_READY))
835 gboolean (*check) (gpointer source_data,
836 GTimeVal *current_time,
837 gpointer user_data);
839 check = ((GSourceFuncs *) hook->func)->check;
840 in_check_or_prepare++;
841 G_UNLOCK (main_loop);
843 if ((*check) (source->source_data, &current_time, source->hook.data))
844 hook->flags |= G_SOURCE_READY;
846 G_LOCK (main_loop);
847 in_check_or_prepare--;
850 if (hook->flags & G_SOURCE_READY)
852 if (dispatch)
854 hook->flags &= ~G_SOURCE_READY;
855 g_hook_ref (&source_list, hook);
856 pending_dispatches = g_slist_prepend (pending_dispatches, source);
857 current_priority = source->priority;
858 n_ready++;
860 else
862 g_hook_unref (&source_list, hook);
863 G_UNLOCK (main_loop);
865 return TRUE;
869 hook = g_hook_next_valid (&source_list, hook, TRUE);
872 /* Now invoke the callbacks */
874 if (pending_dispatches)
876 pending_dispatches = g_slist_reverse (pending_dispatches);
877 g_main_dispatch (&current_time);
878 retval = TRUE;
881 G_UNLOCK (main_loop);
883 return retval;
886 /* See if any events are pending
888 gboolean
889 g_main_pending (void)
891 return in_check_or_prepare ? FALSE : g_main_iterate (FALSE, FALSE);
894 /* Run a single iteration of the mainloop. If block is FALSE,
895 * will never block
897 gboolean
898 g_main_iteration (gboolean block)
900 if (in_check_or_prepare)
902 g_warning ("g_main_iteration(): called recursively from within a source's check() or "
903 "prepare() member or from a second thread, iteration not possible");
904 return FALSE;
906 else
907 return g_main_iterate (block, TRUE);
910 GMainLoop*
911 g_main_new (gboolean is_running)
913 GMainLoop *loop;
915 loop = g_new0 (GMainLoop, 1);
916 loop->is_running = is_running != FALSE;
918 return loop;
921 void
922 g_main_run (GMainLoop *loop)
924 g_return_if_fail (loop != NULL);
926 if (in_check_or_prepare)
928 g_warning ("g_main_run(): called recursively from within a source's check() or "
929 "prepare() member or from a second thread, iteration not possible");
930 return;
933 loop->is_running = TRUE;
934 while (loop->is_running)
935 g_main_iterate (TRUE, TRUE);
938 void
939 g_main_quit (GMainLoop *loop)
941 g_return_if_fail (loop != NULL);
943 loop->is_running = FALSE;
946 void
947 g_main_destroy (GMainLoop *loop)
949 g_return_if_fail (loop != NULL);
951 g_free (loop);
954 gboolean
955 g_main_is_running (GMainLoop *loop)
957 g_return_val_if_fail (loop != NULL, FALSE);
959 return loop->is_running;
962 /* HOLDS: main_loop_lock */
963 static void
964 g_main_poll (gint timeout,
965 gboolean use_priority,
966 gint priority)
968 #ifdef G_MAIN_POLL_DEBUG
969 GTimer *poll_timer;
970 #endif
971 GPollFD *fd_array;
972 GPollRec *pollrec;
973 gint i;
974 gint npoll;
976 #ifdef G_THREADS_ENABLED
977 #ifndef NATIVE_WIN32
978 if (wake_up_pipe[0] < 0)
980 if (pipe (wake_up_pipe) < 0)
981 g_error ("Cannot create pipe main loop wake-up: %s\n",
982 g_strerror (errno));
984 wake_up_rec.fd = wake_up_pipe[0];
985 wake_up_rec.events = G_IO_IN;
986 g_main_add_poll_unlocked (0, &wake_up_rec);
988 #else
989 if (wake_up_semaphore == NULL)
991 if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
992 g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
993 wake_up_rec.fd = (gint) wake_up_semaphore;
994 wake_up_rec.events = G_IO_IN;
995 g_main_add_poll_unlocked (0, &wake_up_rec);
997 #endif
998 #endif
999 fd_array = g_new (GPollFD, n_poll_records);
1001 pollrec = poll_records;
1002 i = 0;
1003 while (pollrec && (!use_priority || priority >= pollrec->priority))
1005 if (pollrec->fd->events)
1007 fd_array[i].fd = pollrec->fd->fd;
1008 /* In direct contradiction to the Unix98 spec, IRIX runs into
1009 * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
1010 * flags in the events field of the pollfd while it should
1011 * just ignoring them. So we mask them out here.
1013 fd_array[i].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
1014 fd_array[i].revents = 0;
1015 i++;
1018 pollrec = pollrec->next;
1020 #ifdef G_THREADS_ENABLED
1021 poll_waiting = TRUE;
1022 poll_changed = FALSE;
1023 #endif
1025 npoll = i;
1026 if (npoll || timeout != 0)
1028 #ifdef G_MAIN_POLL_DEBUG
1029 g_print ("g_main_poll(%d) timeout: %d\r", npoll, timeout);
1030 poll_timer = g_timer_new ();
1031 #endif
1033 G_UNLOCK (main_loop);
1034 (*poll_func) (fd_array, npoll, timeout);
1035 G_LOCK (main_loop);
1037 #ifdef G_MAIN_POLL_DEBUG
1038 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
1039 npoll,
1040 timeout,
1041 g_timer_elapsed (poll_timer, NULL));
1042 g_timer_destroy (poll_timer);
1043 pollrec = poll_records;
1044 i = 0;
1045 while (i < npoll)
1047 if (pollrec->fd->events)
1049 if (fd_array[i].revents)
1051 g_print (" [%d:", fd_array[i].fd);
1052 if (fd_array[i].revents & G_IO_IN)
1053 g_print ("i");
1054 if (fd_array[i].revents & G_IO_OUT)
1055 g_print ("o");
1056 if (fd_array[i].revents & G_IO_PRI)
1057 g_print ("p");
1058 if (fd_array[i].revents & G_IO_ERR)
1059 g_print ("e");
1060 if (fd_array[i].revents & G_IO_HUP)
1061 g_print ("h");
1062 if (fd_array[i].revents & G_IO_NVAL)
1063 g_print ("n");
1064 g_print ("]");
1066 i++;
1068 pollrec = pollrec->next;
1070 g_print ("\n");
1071 #endif
1072 } /* if (npoll || timeout != 0) */
1074 #ifdef G_THREADS_ENABLED
1075 if (!poll_waiting)
1077 #ifndef NATIVE_WIN32
1078 gchar c;
1079 read (wake_up_pipe[0], &c, 1);
1080 #endif
1082 else
1083 poll_waiting = FALSE;
1085 /* If the set of poll file descriptors changed, bail out
1086 * and let the main loop rerun
1088 if (poll_changed)
1090 g_free (fd_array);
1091 return;
1093 #endif
1095 pollrec = poll_records;
1096 i = 0;
1097 while (i < npoll)
1099 if (pollrec->fd->events)
1101 pollrec->fd->revents = fd_array[i].revents;
1102 i++;
1104 pollrec = pollrec->next;
1107 g_free (fd_array);
1110 void
1111 g_main_add_poll (GPollFD *fd,
1112 gint priority)
1114 G_LOCK (main_loop);
1115 g_main_add_poll_unlocked (priority, fd);
1116 G_UNLOCK (main_loop);
1119 /* HOLDS: main_loop_lock */
1120 static void
1121 g_main_add_poll_unlocked (gint priority,
1122 GPollFD *fd)
1124 GPollRec *lastrec, *pollrec, *newrec;
1126 if (!poll_chunk)
1127 poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
1129 if (poll_free_list)
1131 newrec = poll_free_list;
1132 poll_free_list = newrec->next;
1134 else
1135 newrec = g_chunk_new (GPollRec, poll_chunk);
1137 /* This file descriptor may be checked before we ever poll */
1138 fd->revents = 0;
1139 newrec->fd = fd;
1140 newrec->priority = priority;
1142 lastrec = NULL;
1143 pollrec = poll_records;
1144 while (pollrec && priority >= pollrec->priority)
1146 lastrec = pollrec;
1147 pollrec = pollrec->next;
1150 if (lastrec)
1151 lastrec->next = newrec;
1152 else
1153 poll_records = newrec;
1155 newrec->next = pollrec;
1157 n_poll_records++;
1159 #ifdef G_THREADS_ENABLED
1160 poll_changed = TRUE;
1162 /* Now wake up the main loop if it is waiting in the poll() */
1163 g_main_wakeup ();
1164 #endif
1167 void
1168 g_main_remove_poll (GPollFD *fd)
1170 GPollRec *pollrec, *lastrec;
1172 G_LOCK (main_loop);
1174 lastrec = NULL;
1175 pollrec = poll_records;
1177 while (pollrec)
1179 if (pollrec->fd == fd)
1181 if (lastrec != NULL)
1182 lastrec->next = pollrec->next;
1183 else
1184 poll_records = pollrec->next;
1186 pollrec->next = poll_free_list;
1187 poll_free_list = pollrec;
1189 n_poll_records--;
1190 break;
1192 lastrec = pollrec;
1193 pollrec = pollrec->next;
1196 #ifdef G_THREADS_ENABLED
1197 poll_changed = TRUE;
1199 /* Now wake up the main loop if it is waiting in the poll() */
1200 g_main_wakeup ();
1201 #endif
1203 G_UNLOCK (main_loop);
1206 void
1207 g_main_set_poll_func (GPollFunc func)
1209 if (func)
1210 poll_func = func;
1211 else
1212 #ifdef HAVE_POLL
1213 poll_func = (GPollFunc) poll;
1214 #else
1215 poll_func = (GPollFunc) g_poll;
1216 #endif
1219 /* Wake the main loop up from a poll() */
1220 static void
1221 g_main_wakeup (void)
1223 #ifdef G_THREADS_ENABLED
1224 if (poll_waiting)
1226 poll_waiting = FALSE;
1227 #ifndef NATIVE_WIN32
1228 write (wake_up_pipe[1], "A", 1);
1229 #else
1230 ReleaseSemaphore (wake_up_semaphore, 1, NULL);
1231 #endif
1233 #endif
1236 /* Timeouts */
1238 static void
1239 g_timeout_set_expiration (GTimeoutData *data,
1240 GTimeVal *current_time)
1242 guint seconds = data->interval / 1000;
1243 guint msecs = data->interval - seconds * 1000;
1245 data->expiration.tv_sec = current_time->tv_sec + seconds;
1246 data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1247 if (data->expiration.tv_usec >= 1000000)
1249 data->expiration.tv_usec -= 1000000;
1250 data->expiration.tv_sec++;
1254 static gboolean
1255 g_timeout_prepare (gpointer source_data,
1256 GTimeVal *current_time,
1257 gint *timeout,
1258 gpointer user_data)
1260 glong msec;
1261 GTimeoutData *data = source_data;
1263 msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
1264 (data->expiration.tv_usec - current_time->tv_usec) / 1000;
1266 if (msec < 0)
1267 msec = 0;
1268 else if (msec > data->interval)
1270 /* The system time has been set backwards, so we
1271 * reset the expiration time to now + data->interval;
1272 * this at least avoids hanging for long periods of time.
1274 g_timeout_set_expiration (data, current_time);
1275 msec = data->interval;
1278 *timeout = msec;
1280 return (msec == 0);
1283 static gboolean
1284 g_timeout_check (gpointer source_data,
1285 GTimeVal *current_time,
1286 gpointer user_data)
1288 GTimeoutData *data = source_data;
1290 return (data->expiration.tv_sec < current_time->tv_sec) ||
1291 ((data->expiration.tv_sec == current_time->tv_sec) &&
1292 (data->expiration.tv_usec <= current_time->tv_usec));
1295 static gboolean
1296 g_timeout_dispatch (gpointer source_data,
1297 GTimeVal *dispatch_time,
1298 gpointer user_data)
1300 GTimeoutData *data = source_data;
1302 if (data->callback (user_data))
1304 g_timeout_set_expiration (data, dispatch_time);
1305 return TRUE;
1307 else
1308 return FALSE;
1311 guint
1312 g_timeout_add_full (gint priority,
1313 guint interval,
1314 GSourceFunc function,
1315 gpointer data,
1316 GDestroyNotify notify)
1318 GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1319 GTimeVal current_time;
1321 timeout_data->interval = interval;
1322 timeout_data->callback = function;
1323 g_get_current_time (&current_time);
1325 g_timeout_set_expiration (timeout_data, &current_time);
1327 return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1330 guint
1331 g_timeout_add (guint32 interval,
1332 GSourceFunc function,
1333 gpointer data)
1335 return g_timeout_add_full (G_PRIORITY_DEFAULT,
1336 interval, function, data, NULL);
1339 /* Idle functions */
1341 static gboolean
1342 g_idle_prepare (gpointer source_data,
1343 GTimeVal *current_time,
1344 gint *timeout,
1345 gpointer user_data)
1347 *timeout = 0;
1349 return TRUE;
1352 static gboolean
1353 g_idle_check (gpointer source_data,
1354 GTimeVal *current_time,
1355 gpointer user_data)
1357 return TRUE;
1360 static gboolean
1361 g_idle_dispatch (gpointer source_data,
1362 GTimeVal *dispatch_time,
1363 gpointer user_data)
1365 GSourceFunc func = (GSourceFunc) source_data;
1367 return func (user_data);
1370 guint
1371 g_idle_add_full (gint priority,
1372 GSourceFunc function,
1373 gpointer data,
1374 GDestroyNotify notify)
1376 g_return_val_if_fail (function != NULL, 0);
1378 return g_source_add (priority, FALSE, &idle_funcs, (gpointer) function, data, notify);
1381 guint
1382 g_idle_add (GSourceFunc function,
1383 gpointer data)
1385 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1388 gboolean
1389 g_idle_remove_by_data (gpointer data)
1391 return g_source_remove_by_funcs_user_data (&idle_funcs, data);