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 /* NATIVE_WIN32 */
69 typedef struct _GTimeoutData GTimeoutData
;
70 typedef struct _GSource GSource
;
71 typedef struct _GPollRec GPollRec
;
75 G_SOURCE_READY
= 1 << G_HOOK_FLAG_USER_SHIFT
,
76 G_SOURCE_CAN_RECURSE
= 1 << (G_HOOK_FLAG_USER_SHIFT
+ 1)
105 /* Forward declarations */
107 static gint
g_source_compare (GHook
*a
,
109 static void g_source_destroy_func (GHookList
*hook_list
,
111 static void g_main_poll (gint timeout
,
112 gboolean use_priority
,
114 static void g_main_add_poll_unlocked (gint priority
,
116 static void g_main_wakeup (void);
118 static gboolean
g_timeout_prepare (gpointer source_data
,
119 GTimeVal
*current_time
,
122 static gboolean
g_timeout_check (gpointer source_data
,
123 GTimeVal
*current_time
,
125 static gboolean
g_timeout_dispatch (gpointer source_data
,
126 GTimeVal
*dispatch_time
,
128 static gboolean
g_idle_prepare (gpointer source_data
,
129 GTimeVal
*current_time
,
132 static gboolean
g_idle_check (gpointer source_data
,
133 GTimeVal
*current_time
,
135 static gboolean
g_idle_dispatch (gpointer source_data
,
136 GTimeVal
*dispatch_time
,
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
=
158 static GSourceFuncs idle_funcs
=
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
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 */
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
);
191 static GPollFunc poll_func
= (GPollFunc
) poll
;
192 #else /* !HAVE_POLL */
196 g_poll (GPollFD
*fds
, guint nfds
, gint timeout
)
198 HANDLE handles
[MAXIMUM_WAIT_OBJECTS
];
207 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
210 if (f
->events
& G_IO_IN
)
211 if (f
->fd
== G_WIN32_MSG_HANDLE
)
215 /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
216 handles
[nhandles
++] = (HANDLE
) f
->fd
;
225 /* Waiting for messages, and maybe events */
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
245 /* g_print ("PeekMessage\n"); */
246 if (PeekMessage (&msg
, NULL
, 0, 0, PM_NOREMOVE
))
247 ready
= WAIT_OBJECT_0
;
249 ready
= WAIT_TIMEOUT
;
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");
264 /* g_print ("WaitMessage\n"); */
266 KillTimer (NULL
, timer
);
267 if (PeekMessage (&msg
, NULL
, 0, 0, PM_NOREMOVE
))
268 ready
= WAIT_OBJECT_0
;
270 ready
= WAIT_TIMEOUT
;
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?) */
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
)
307 if (ready
== WAIT_FAILED
)
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
)
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_ */
344 # define SELECT_MASK fd_set
345 #else /* !NO_FD_SET */
347 typedef long fd_mask
;
350 # define SELECT_MASK void
352 # define SELECT_MASK int
353 # endif /* !_IBMR2 */
354 #endif /* !NO_FD_SET */
357 g_poll (GPollFD
*fds
,
362 SELECT_MASK rset
, wset
, xset
;
371 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
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
)))
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
);
390 for (f
= fds
; f
< &fds
[nfds
]; ++f
)
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
;
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
418 g_source_compare (GHook
*a
,
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 */
429 g_source_destroy_func (GHookList
*hook_list
,
432 GSource
*source
= (GSource
*) hook
;
433 GDestroyNotify destroy
;
435 G_UNLOCK (main_loop
);
437 destroy
= hook
->destroy
;
439 destroy (hook
->data
);
441 destroy
= ((GSourceFuncs
*) hook
->func
)->destroy
;
443 destroy (source
->source_data
);
449 g_source_add (gint priority
,
450 gboolean can_recurse
,
452 gpointer source_data
,
454 GDestroyNotify notify
)
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
,
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() */
490 G_UNLOCK (main_loop
);
496 g_source_remove (guint tag
)
500 g_return_val_if_fail (tag
> 0, FALSE
);
504 hook
= g_hook_get (&source_list
, tag
);
506 g_hook_destroy_link (&source_list
, hook
);
508 G_UNLOCK (main_loop
);
514 g_source_remove_by_user_data (gpointer user_data
)
520 hook
= g_hook_find_data (&source_list
, TRUE
, user_data
);
522 g_hook_destroy_link (&source_list
, hook
);
524 G_UNLOCK (main_loop
);
530 g_source_find_source_data (GHook
*hook
,
533 GSource
*source
= (GSource
*)hook
;
535 return (source
->source_data
== data
);
539 g_source_remove_by_source_data (gpointer source_data
)
545 hook
= g_hook_find (&source_list
, TRUE
,
546 g_source_find_source_data
, source_data
);
548 g_hook_destroy_link (&source_list
, hook
);
550 G_UNLOCK (main_loop
);
556 g_source_find_funcs_user_data (GHook
*hook
,
561 return hook
->func
== d
[0] && hook
->data
== d
[1];
565 g_source_remove_by_funcs_user_data (GSourceFuncs
*funcs
,
571 g_return_val_if_fail (funcs
!= NULL
, FALSE
);
578 hook
= g_hook_find (&source_list
, TRUE
,
579 g_source_find_funcs_user_data
, d
);
581 g_hook_destroy_link (&source_list
, hook
);
583 G_UNLOCK (main_loop
);
589 g_get_current_time (GTimeVal
*result
)
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
;
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
;
610 g_return_if_fail (result
!= NULL
);
614 start_tick
= GetTickCount ();
618 tick
= GetTickCount ();
620 result
->tv_sec
= (tick
- start_tick
) / 1000 + start_time
;
621 result
->tv_usec
= ((tick
- start_tick
) % 1000) * 1000;
625 /* Running the main loop */
627 /* HOLDS: main_loop_lock */
629 g_main_dispatch (GTimeVal
*dispatch_time
)
631 while (pending_dispatches
!= NULL
)
633 gboolean need_destroy
;
634 GSource
*source
= pending_dispatches
->data
;
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
,
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
,
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
697 * 5) while the list of currently pending sources is non-empty,
698 * call (*dispatch) on each source, removing the source
703 g_main_iterate (gboolean block
,
707 GTimeVal current_time
= { 0, 0 };
709 gint current_priority
= 0;
711 gboolean retval
= FALSE
;
713 g_return_val_if_fail (!block
|| dispatch
, FALSE
);
715 g_get_current_time (¤t_time
);
719 #ifdef G_THREADS_ENABLED
722 g_warning("g_main_iterate(): main loop already active in another thread");
723 G_UNLOCK (main_loop
);
728 /* If recursing, finish up current dispatch, before starting over */
729 if (pending_dispatches
)
732 g_main_dispatch (¤t_time
);
734 G_UNLOCK (main_loop
);
739 /* Prepare all sources */
741 timeout
= block
? -1 : 0;
743 hook
= g_hook_first_valid (&source_list
, TRUE
);
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
);
754 if (G_HOOK_IN_CALL (hook
) && !(hook
->flags
& G_SOURCE_CAN_RECURSE
))
756 hook
= g_hook_next_valid (&source_list
, hook
, TRUE
);
760 if (!(hook
->flags
& G_SOURCE_READY
))
762 gboolean (*prepare
) (gpointer source_data
,
763 GTimeVal
*current_time
,
767 prepare
= ((GSourceFuncs
*) hook
->func
)->prepare
;
768 in_check_or_prepare
++;
769 G_UNLOCK (main_loop
);
771 if ((*prepare
) (source
->source_data
, ¤t_time
, &source_timeout
, source
->hook
.data
))
772 hook
->flags
|= G_SOURCE_READY
;
775 in_check_or_prepare
--;
778 if (hook
->flags
& G_SOURCE_READY
)
782 g_hook_unref (&source_list
, hook
);
783 G_UNLOCK (main_loop
);
790 current_priority
= source
->priority
;
795 if (source_timeout
>= 0)
798 timeout
= source_timeout
;
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
);
811 g_get_current_time (¤t_time
);
813 /* Check to see what sources need to be dispatched */
817 hook
= g_hook_first_valid (&source_list
, TRUE
);
820 GSource
*source
= (GSource
*)hook
;
822 if ((n_ready
> 0) && (source
->priority
> current_priority
))
824 g_hook_unref (&source_list
, hook
);
827 if (G_HOOK_IN_CALL (hook
) && !(hook
->flags
& G_SOURCE_CAN_RECURSE
))
829 hook
= g_hook_next_valid (&source_list
, hook
, TRUE
);
833 if (!(hook
->flags
& G_SOURCE_READY
))
835 gboolean (*check
) (gpointer source_data
,
836 GTimeVal
*current_time
,
839 check
= ((GSourceFuncs
*) hook
->func
)->check
;
840 in_check_or_prepare
++;
841 G_UNLOCK (main_loop
);
843 if ((*check
) (source
->source_data
, ¤t_time
, source
->hook
.data
))
844 hook
->flags
|= G_SOURCE_READY
;
847 in_check_or_prepare
--;
850 if (hook
->flags
& G_SOURCE_READY
)
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
;
862 g_hook_unref (&source_list
, hook
);
863 G_UNLOCK (main_loop
);
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 (¤t_time
);
881 G_UNLOCK (main_loop
);
886 /* See if any events are pending
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,
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");
907 return g_main_iterate (block
, TRUE
);
911 g_main_new (gboolean is_running
)
915 loop
= g_new0 (GMainLoop
, 1);
916 loop
->is_running
= is_running
!= FALSE
;
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");
933 loop
->is_running
= TRUE
;
934 while (loop
->is_running
)
935 g_main_iterate (TRUE
, TRUE
);
939 g_main_quit (GMainLoop
*loop
)
941 g_return_if_fail (loop
!= NULL
);
943 loop
->is_running
= FALSE
;
947 g_main_destroy (GMainLoop
*loop
)
949 g_return_if_fail (loop
!= NULL
);
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 */
964 g_main_poll (gint timeout
,
965 gboolean use_priority
,
968 #ifdef G_MAIN_POLL_DEBUG
976 #ifdef G_THREADS_ENABLED
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",
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
);
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
);
999 fd_array
= g_new (GPollFD
, n_poll_records
);
1001 pollrec
= poll_records
;
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;
1018 pollrec
= pollrec
->next
;
1020 #ifdef G_THREADS_ENABLED
1021 poll_waiting
= TRUE
;
1022 poll_changed
= FALSE
;
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 ();
1033 G_UNLOCK (main_loop
);
1034 (*poll_func
) (fd_array
, npoll
, timeout
);
1037 #ifdef G_MAIN_POLL_DEBUG
1038 g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
1041 g_timer_elapsed (poll_timer
, NULL
));
1042 g_timer_destroy (poll_timer
);
1043 pollrec
= poll_records
;
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
)
1054 if (fd_array
[i
].revents
& G_IO_OUT
)
1056 if (fd_array
[i
].revents
& G_IO_PRI
)
1058 if (fd_array
[i
].revents
& G_IO_ERR
)
1060 if (fd_array
[i
].revents
& G_IO_HUP
)
1062 if (fd_array
[i
].revents
& G_IO_NVAL
)
1068 pollrec
= pollrec
->next
;
1072 } /* if (npoll || timeout != 0) */
1074 #ifdef G_THREADS_ENABLED
1077 #ifndef NATIVE_WIN32
1079 read (wake_up_pipe
[0], &c
, 1);
1083 poll_waiting
= FALSE
;
1085 /* If the set of poll file descriptors changed, bail out
1086 * and let the main loop rerun
1095 pollrec
= poll_records
;
1099 if (pollrec
->fd
->events
)
1101 pollrec
->fd
->revents
= fd_array
[i
].revents
;
1104 pollrec
= pollrec
->next
;
1111 g_main_add_poll (GPollFD
*fd
,
1115 g_main_add_poll_unlocked (priority
, fd
);
1116 G_UNLOCK (main_loop
);
1119 /* HOLDS: main_loop_lock */
1121 g_main_add_poll_unlocked (gint priority
,
1124 GPollRec
*lastrec
, *pollrec
, *newrec
;
1127 poll_chunk
= g_mem_chunk_create (GPollRec
, 32, G_ALLOC_ONLY
);
1131 newrec
= poll_free_list
;
1132 poll_free_list
= newrec
->next
;
1135 newrec
= g_chunk_new (GPollRec
, poll_chunk
);
1137 /* This file descriptor may be checked before we ever poll */
1140 newrec
->priority
= priority
;
1143 pollrec
= poll_records
;
1144 while (pollrec
&& priority
>= pollrec
->priority
)
1147 pollrec
= pollrec
->next
;
1151 lastrec
->next
= newrec
;
1153 poll_records
= newrec
;
1155 newrec
->next
= pollrec
;
1159 #ifdef G_THREADS_ENABLED
1160 poll_changed
= TRUE
;
1162 /* Now wake up the main loop if it is waiting in the poll() */
1168 g_main_remove_poll (GPollFD
*fd
)
1170 GPollRec
*pollrec
, *lastrec
;
1175 pollrec
= poll_records
;
1179 if (pollrec
->fd
== fd
)
1181 if (lastrec
!= NULL
)
1182 lastrec
->next
= pollrec
->next
;
1184 poll_records
= pollrec
->next
;
1186 pollrec
->next
= poll_free_list
;
1187 poll_free_list
= 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() */
1203 G_UNLOCK (main_loop
);
1207 g_main_set_poll_func (GPollFunc func
)
1213 poll_func
= (GPollFunc
) poll
;
1215 poll_func
= (GPollFunc
) g_poll
;
1219 /* Wake the main loop up from a poll() */
1221 g_main_wakeup (void)
1223 #ifdef G_THREADS_ENABLED
1226 poll_waiting
= FALSE
;
1227 #ifndef NATIVE_WIN32
1228 write (wake_up_pipe
[1], "A", 1);
1230 ReleaseSemaphore (wake_up_semaphore
, 1, NULL
);
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
++;
1255 g_timeout_prepare (gpointer source_data
,
1256 GTimeVal
*current_time
,
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;
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
;
1284 g_timeout_check (gpointer source_data
,
1285 GTimeVal
*current_time
,
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
));
1296 g_timeout_dispatch (gpointer source_data
,
1297 GTimeVal
*dispatch_time
,
1300 GTimeoutData
*data
= source_data
;
1302 if (data
->callback (user_data
))
1304 g_timeout_set_expiration (data
, dispatch_time
);
1312 g_timeout_add_full (gint priority
,
1314 GSourceFunc function
,
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 (¤t_time
);
1325 g_timeout_set_expiration (timeout_data
, ¤t_time
);
1327 return g_source_add (priority
, FALSE
, &timeout_funcs
, timeout_data
, data
, notify
);
1331 g_timeout_add (guint32 interval
,
1332 GSourceFunc function
,
1335 return g_timeout_add_full (G_PRIORITY_DEFAULT
,
1336 interval
, function
, data
, NULL
);
1339 /* Idle functions */
1342 g_idle_prepare (gpointer source_data
,
1343 GTimeVal
*current_time
,
1353 g_idle_check (gpointer source_data
,
1354 GTimeVal
*current_time
,
1361 g_idle_dispatch (gpointer source_data
,
1362 GTimeVal
*dispatch_time
,
1365 GSourceFunc func
= (GSourceFunc
) source_data
;
1367 return func (user_data
);
1371 g_idle_add_full (gint priority
,
1372 GSourceFunc function
,
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
);
1382 g_idle_add (GSourceFunc function
,
1385 return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE
, function
, data
, NULL
);
1389 g_idle_remove_by_data (gpointer data
)
1391 return g_source_remove_by_funcs_user_data (&idle_funcs
, data
);