Call the source-specific free function when the hook is freed not when it
[platform/upstream/glib.git] / gmain.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gmain.c: Main loop abstraction, timeouts, and idle functions
5  * Copyright 1998 Owen Taylor
6  *
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.
11  *
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.
16  *
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.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #include "glib.h"
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #ifdef  GLIB_HAVE_SYS_POLL_H
31 #  include <sys/poll.h>
32 #  undef events  /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
33 #  undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
34 #endif  /* GLIB_HAVE_SYS_POLL_H */
35 #include <unistd.h>
36 #include <errno.h>
37 #include "config.h"
38
39 /* Types */
40
41 typedef struct _GIdleData GIdleData;
42 typedef struct _GTimeoutData GTimeoutData;
43 typedef struct _GSource GSource;
44 typedef struct _GPollRec GPollRec;
45
46 typedef enum
47 {
48   G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
49   G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
50 } GSourceFlags;
51
52 struct _GSource
53 {
54   GHook hook;
55   gint priority;
56   gpointer source_data;
57 };
58
59 struct _GMainLoop
60 {
61   gboolean is_running;
62 };
63
64 struct _GIdleData
65 {
66   GSourceFunc callback;
67 };
68
69 struct _GTimeoutData
70 {
71   GTimeVal    expiration;
72   gint        interval;
73   GSourceFunc callback;
74 };
75
76 struct _GPollRec
77 {
78   gint priority;
79   GPollFD *fd;
80   GPollRec *next;
81 };
82
83 /* Forward declarations */
84
85 static gint     g_source_compare          (GHook      *a,
86                                            GHook      *b);
87 static void     g_source_free_func        (GHookList  *hook_list,
88                                            GHook      *hook);
89 static void     g_main_poll               (gint      timeout,
90                                            gboolean  use_priority, 
91                                            gint      priority);
92 static void     g_main_add_poll_unlocked  (gint      priority,
93                                            GPollFD  *fd);
94
95 static gboolean g_timeout_prepare      (gpointer  source_data, 
96                                         GTimeVal *current_time,
97                                         gint     *timeout);
98 static gboolean g_timeout_check        (gpointer  source_data,
99                                         GTimeVal *current_time);
100 static gboolean g_timeout_dispatch     (gpointer  source_data,
101                                         GTimeVal *current_time,
102                                         gpointer  user_data);
103 static gboolean g_idle_prepare         (gpointer  source_data, 
104                                         GTimeVal *current_time,
105                                         gint     *timeout);
106 static gboolean g_idle_check           (gpointer  source_data,
107                                         GTimeVal *current_time);
108 static gboolean g_idle_dispatch        (gpointer  source_data,
109                                         GTimeVal *current_time,
110                                         gpointer  user_data);
111
112 /* Data */
113
114 static GSList *pending_dispatches = NULL;
115 static GHookList source_list = { 0 };
116
117 /* The following lock is used for both the list of sources
118  * and the list of poll records
119  */
120 G_LOCK_DECLARE_STATIC (main_loop);
121
122 static GSourceFuncs timeout_funcs = {
123   g_timeout_prepare,
124   g_timeout_check,
125   g_timeout_dispatch,
126   (GDestroyNotify)g_free
127 };
128
129 static GSourceFuncs idle_funcs = {
130   g_idle_prepare,
131   g_idle_check,
132   g_idle_dispatch,
133   (GDestroyNotify)g_free
134 };
135
136 static GPollRec *poll_records = NULL;
137 static GPollRec *poll_free_list = NULL;
138 static GMemChunk *poll_chunk;
139 static guint n_poll_records = 0;
140
141 /* this pipe is used to wake up the main loop when a source is added.
142  */
143 static gint wake_up_pipe[2] = { -1, -1 };
144 static GPollFD wake_up_rec;
145 static gboolean poll_waiting = FALSE;
146
147 #ifdef HAVE_POLL
148 static GPollFunc poll_func = (GPollFunc) poll;
149 #else   /* !HAVE_POLL */
150
151 /* The following implementation of poll() comes from the GNU C Library.
152  * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
153  */
154
155 #include <string.h> /* for bzero on BSD systems */
156
157 #ifdef HAVE_SYS_SELECT_H
158 #include <sys/select.h>
159 #endif /* HAVE_SYS_SELECT_H_ */
160
161 #ifndef NO_FD_SET
162 #  define SELECT_MASK fd_set
163 #else   /* !NO_FD_SET */
164 #  ifndef _AIX
165 typedef long fd_mask;
166 #  endif
167 #  if defined(_IBMR2)
168 #    define SELECT_MASK void
169 #  else
170 #    define SELECT_MASK int
171 #  endif
172 #endif  /* !NO_FD_SET */
173
174 static gint 
175 g_poll (GPollFD *fds, guint nfds, gint timeout)
176 {
177   struct timeval tv;
178   SELECT_MASK rset, wset, xset;
179   GPollFD *f;
180   int ready;
181   int maxfd = 0;
182
183   FD_ZERO (&rset);
184   FD_ZERO (&wset);
185   FD_ZERO (&xset);
186
187   for (f = fds; f < &fds[nfds]; ++f)
188     if (f->fd >= 0)
189       {
190         if (f->events & G_IO_IN)
191           FD_SET (f->fd, &rset);
192         if (f->events & G_IO_OUT)
193           FD_SET (f->fd, &wset);
194         if (f->events & G_IO_PRI)
195           FD_SET (f->fd, &xset);
196         if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
197           maxfd = f->fd;
198       }
199
200   tv.tv_sec = timeout / 1000;
201   tv.tv_usec = (timeout % 1000) * 1000;
202
203   ready = select (maxfd + 1, &rset, &wset, &xset,
204                   timeout == -1 ? NULL : &tv);
205   if (ready > 0)
206     for (f = fds; f < &fds[nfds]; ++f)
207       {
208         f->revents = 0;
209         if (f->fd >= 0)
210           {
211             if (FD_ISSET (f->fd, &rset))
212               f->revents |= G_IO_IN;
213             if (FD_ISSET (f->fd, &wset))
214               f->revents |= G_IO_OUT;
215             if (FD_ISSET (f->fd, &xset))
216               f->revents |= G_IO_PRI;
217           }
218       }
219
220   return ready;
221 }
222 static GPollFunc poll_func = g_poll;
223 #endif  /* !HAVE_POLL */
224
225 /* Hooks for adding to the main loop */
226
227 /* Use knowledge of insert_sorted algorithm here to make
228  * sure we insert at the end of equal priority items
229  */
230 static gint
231 g_source_compare (GHook *a,
232                   GHook *b)
233 {
234   GSource *source_a = (GSource *)a;
235   GSource *source_b = (GSource *)b;
236
237   return (source_a->priority < source_b->priority) ? -1 : 1;
238 }
239
240 static void
241 g_source_free_func (GHookList      *hook_list,
242                     GHook          *hook)
243 {
244   GSource *source = (GSource *)hook;
245
246   ((GSourceFuncs *) hook->func)->destroy (source->source_data);
247 }
248
249 guint 
250 g_source_add (gint           priority,
251               gboolean       can_recurse,
252               GSourceFuncs  *funcs,
253               gpointer       source_data, 
254               gpointer       user_data,
255               GDestroyNotify notify)
256 {
257   guint return_val;
258   GSource *source;
259
260   G_LOCK (main_loop);
261
262   if (!source_list.is_setup)
263     g_hook_list_init (&source_list, sizeof(GSource));
264
265   source_list.hook_free = g_source_free_func;
266
267   source = (GSource *)g_hook_alloc (&source_list);
268   source->priority = priority;
269   source->source_data = source_data;
270   source->hook.func = funcs;
271   source->hook.data = user_data;
272   source->hook.destroy = notify;
273   
274   g_hook_insert_sorted (&source_list, 
275                         (GHook *)source, 
276                         g_source_compare);
277
278   if (can_recurse)
279     source->hook.flags |= G_SOURCE_CAN_RECURSE;
280
281   return_val = source->hook.hook_id;
282
283   /* Now wake up the main loop if it is waiting in the poll() */
284
285   if (poll_waiting)
286     {
287       poll_waiting = FALSE;
288       write (wake_up_pipe[1], "A", 1);
289     }
290
291   G_UNLOCK (main_loop);
292
293   return return_val;
294 }
295
296 void 
297 g_source_remove (guint tag)
298 {
299   GHook *hook;
300
301   G_LOCK (main_loop);
302
303   hook = g_hook_get (&source_list, tag);
304   if (hook)
305     g_hook_destroy_link (&source_list, hook);
306
307   G_UNLOCK (main_loop);
308 }
309
310 void 
311 g_source_remove_by_user_data (gpointer user_data)
312 {
313   GHook *hook;
314   
315   G_LOCK (main_loop);
316   
317   hook = g_hook_find_data (&source_list, TRUE, user_data);
318   if (hook)
319     g_hook_destroy_link (&source_list, hook);
320
321   G_UNLOCK (main_loop);
322 }
323
324 static gboolean
325 g_source_find_source_data (GHook        *hook,
326                            gpointer      data)
327 {
328   GSource *source = (GSource *)hook;
329
330   return (source->source_data == data);
331 }
332
333 void 
334 g_source_remove_by_source_data (gpointer source_data)
335 {
336   GHook *hook;
337
338   G_LOCK (main_loop);
339
340   hook = g_hook_find (&source_list, TRUE, 
341                       g_source_find_source_data, source_data);
342   if (hook)
343     g_hook_destroy_link (&source_list, hook);
344
345   G_UNLOCK (main_loop);
346 }
347
348 void
349 g_get_current_time (GTimeVal *result)
350 {
351   struct timeval r;
352   g_return_if_fail (result != NULL);
353
354   /*this is required on alpha, there the timeval structs are int's
355     not longs and a cast only would fail horribly*/
356   gettimeofday (&r, NULL);
357   result->tv_sec = r.tv_sec;
358   result->tv_usec = r.tv_usec;
359 }
360
361 /* Running the main loop */
362
363 /* HOLDS: main_loop_lock */
364 static void
365 g_main_dispatch (GTimeVal *current_time)
366 {
367   while (pending_dispatches != NULL)
368     {
369       gboolean need_destroy;
370       GSource *source = pending_dispatches->data;
371       GSList *tmp_list;
372
373       tmp_list = pending_dispatches;
374       pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
375       g_slist_free_1 (tmp_list);
376
377       if (G_HOOK_IS_VALID (source))
378         {
379           gboolean was_in_call;
380           gpointer hook_data = source->hook.data;
381           gpointer source_data = source->source_data;
382           gboolean (*dispatch) (gpointer,
383                                 GTimeVal *,
384                                 gpointer);
385
386           dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
387           
388           was_in_call = G_HOOK_IN_CALL (source);
389           source->hook.flags |= G_HOOK_FLAG_IN_CALL;
390
391           G_UNLOCK (main_loop);
392           need_destroy = ! dispatch (source_data,
393                                      current_time,
394                                      hook_data);
395           G_LOCK (main_loop);
396
397           if (!was_in_call)
398             source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
399           
400           if (need_destroy && G_HOOK_IS_VALID (source))
401             g_hook_destroy_link (&source_list, (GHook *) source);
402         }
403
404       g_hook_unref (&source_list, (GHook*) source);
405     }
406 }
407
408 /* g_main_iterate () runs a single iteration of the mainloop, or,
409  * if !dispatch checks to see if any sources need dispatching.
410  * basic algorithm for dispatch=TRUE:
411  *
412  * 1) while the list of currently pending sources is non-empty,
413  *    we call (*dispatch) on those that are !IN_CALL or can_recurse,
414  *    removing sources from the list after each returns.
415  *    the return value of (*dispatch) determines whether the source
416  *    itself is kept alive.
417  *
418  * 2) call (*prepare) for sources that are not yet SOURCE_READY and
419  *    are !IN_CALL or can_recurse. a return value of TRUE determines
420  *    that the source would like to be dispatched immediatedly, it
421  *    is then flagged as SOURCE_READY.
422  *
423  * 3) poll with the pollfds from all sources at the priority of the
424  *    first source flagged as SOURCE_READY. if there are any sources
425  *    flagged as SOURCE_READY, we use a timeout of 0 or the minimum
426  *    of all timouts otherwise.
427  *
428  * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
429  *    (*check) returns true, add the source to the pending list.
430  *    once one source returns true, stop after checking all sources
431  *    at that priority.
432  *
433  * 5) while the list of currently pending sources is non-empty,
434  *    call (*dispatch) on each source, removing the source
435  *    after the call.
436  *
437  */
438 static gboolean
439 g_main_iterate (gboolean block,
440                 gboolean dispatch)
441 {
442   GHook *hook;
443   GTimeVal current_time  ={ 0, 0 };
444   gint n_ready = 0;
445   gint current_priority = 0;
446   gint timeout;
447   gboolean retval = FALSE;
448
449   g_return_val_if_fail (!block || dispatch, FALSE);
450
451   g_get_current_time (&current_time);
452
453   G_LOCK (main_loop);
454   
455   /* If recursing, finish up current dispatch, before starting over */
456   if (pending_dispatches)
457     {
458       if (dispatch)
459         g_main_dispatch (&current_time);
460       
461       G_UNLOCK (main_loop);
462
463       return TRUE;
464     }
465
466   /* Prepare all sources */
467
468   timeout = block ? -1 : 0;
469   
470   hook = g_hook_first_valid (&source_list, TRUE);
471   while (hook)
472     {
473       GSource *source = (GSource *)hook;
474       gint source_timeout = -1;
475
476       if ((n_ready > 0) && (source->priority > current_priority))
477         {
478           g_hook_unref (&source_list, hook);
479           break;
480         }
481       if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
482         {
483           hook = g_hook_next_valid (&source_list, hook, TRUE);
484           continue;
485         }
486
487       if (hook->flags & G_SOURCE_READY ||
488           ((GSourceFuncs *) hook->func)->prepare (source->source_data,
489                                                   &current_time,
490                                                   &source_timeout))
491         {
492           if (!dispatch)
493             {
494               hook->flags |= G_SOURCE_READY;
495               g_hook_unref (&source_list, hook);
496               G_UNLOCK (main_loop);
497
498               return TRUE;
499             }
500           else
501             {
502               hook->flags |= G_SOURCE_READY;
503               n_ready++;
504               current_priority = source->priority;
505               timeout = 0;
506             }
507         }
508       
509       if (source_timeout >= 0)
510         {
511           if (timeout < 0)
512             timeout = source_timeout;
513           else
514             timeout = MIN (timeout, source_timeout);
515         }
516
517       hook = g_hook_next_valid (&source_list, hook, TRUE);
518     }
519
520   /* poll(), if necessary */
521
522   g_main_poll (timeout, n_ready > 0, current_priority);
523
524   /* Check to see what sources need to be dispatched */
525
526   n_ready = 0;
527   
528   hook = g_hook_first_valid (&source_list, TRUE);
529   while (hook)
530     {
531       GSource *source = (GSource *)hook;
532
533       if ((n_ready > 0) && (source->priority > current_priority))
534         {
535           g_hook_unref (&source_list, hook);
536           break;
537         }
538       if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
539         {
540           hook = g_hook_next_valid (&source_list, hook, TRUE);
541           continue;
542         }
543
544       if (hook->flags & G_SOURCE_READY ||
545           ((GSourceFuncs *) hook->func)->check (source->source_data,
546                                                 &current_time))
547         {
548           if (dispatch)
549             {
550               hook->flags &= ~G_SOURCE_READY;
551               g_hook_ref (&source_list, hook);
552               pending_dispatches = g_slist_prepend (pending_dispatches, source);
553               current_priority = source->priority;
554               n_ready++;
555             }
556           else
557             {
558               g_hook_unref (&source_list, hook);
559               G_UNLOCK (main_loop);
560
561               return TRUE;
562             }
563         }
564       
565       hook = g_hook_next_valid (&source_list, hook, TRUE);
566     }
567
568   /* Now invoke the callbacks */
569
570   if (pending_dispatches)
571     {
572       pending_dispatches = g_slist_reverse (pending_dispatches);
573       g_main_dispatch (&current_time);
574       retval = TRUE;
575     }
576
577   G_UNLOCK (main_loop);
578
579   return retval;
580 }
581
582 /* See if any events are pending
583  */
584 gboolean 
585 g_main_pending ()
586 {
587   return g_main_iterate (FALSE, FALSE);
588 }
589
590 /* Run a single iteration of the mainloop. If block is FALSE,
591  * will never block
592  */
593 gboolean
594 g_main_iteration (gboolean block)
595 {
596   return g_main_iterate (block, TRUE);
597 }
598
599 GMainLoop*
600 g_main_new (gboolean is_running)
601 {
602   GMainLoop *loop;
603
604   loop = g_new0 (GMainLoop, 1);
605   loop->is_running = is_running != FALSE;
606
607   return loop;
608 }
609
610 void 
611 g_main_run (GMainLoop *loop)
612 {
613   g_return_if_fail (loop != NULL);
614
615   loop->is_running = TRUE;
616   while (loop->is_running)
617     g_main_iterate (TRUE, TRUE);
618 }
619
620 void 
621 g_main_quit (GMainLoop *loop)
622 {
623   g_return_if_fail (loop != NULL);
624
625   loop->is_running = FALSE;
626 }
627
628 void 
629 g_main_destroy (GMainLoop *loop)
630 {
631   g_return_if_fail (loop != NULL);
632
633   g_free (loop);
634 }
635
636 gboolean
637 g_main_is_running (GMainLoop *loop)
638 {
639   g_return_val_if_fail (loop != NULL, FALSE);
640
641   return loop->is_running;
642 }
643
644 /* HOLDS: main_loop_lock */
645 static void
646 g_main_poll (gint     timeout,
647              gboolean use_priority,
648              gint     priority)
649 {
650   GPollFD *fd_array;
651   GPollRec *pollrec;
652
653   gint i;
654   gint npoll;
655
656   if (wake_up_pipe[0] < 0)
657     {
658       if (pipe (wake_up_pipe) < 0)
659         g_error ("Cannot create pipe main loop wake-up: %s\n",
660                  g_strerror (errno));
661
662       wake_up_rec.fd = wake_up_pipe[0];
663       wake_up_rec.events = G_IO_IN;
664       g_main_add_poll_unlocked (0, &wake_up_rec);
665     }
666   
667   fd_array = g_new (GPollFD, n_poll_records);
668  
669   pollrec = poll_records;
670   i = 0;
671   while (pollrec && (!use_priority || priority >= pollrec->priority))
672     {
673       fd_array[i].fd = pollrec->fd->fd;
674       fd_array[i].events = pollrec->fd->events;
675       fd_array[i].revents = 0;
676         
677       pollrec = pollrec->next;
678       i++;
679     }
680
681   poll_waiting = TRUE;
682   
683   G_UNLOCK (main_loop);
684   npoll = i;
685   (*poll_func) (fd_array, npoll, timeout);
686   G_LOCK (main_loop);
687
688   if (!poll_waiting)
689     {
690       gchar c;
691       read (wake_up_pipe[0], &c, 1);
692     }
693   else
694     poll_waiting = FALSE;
695
696   pollrec = poll_records;
697   i = 0;
698   while (i < npoll)
699     {
700       pollrec->fd->revents = fd_array[i].revents;
701       pollrec = pollrec->next;
702       i++;
703     }
704
705   g_free (fd_array);
706 }
707
708 void 
709 g_main_add_poll (GPollFD *fd,
710                  gint     priority)
711 {
712   G_LOCK (main_loop);
713   g_main_add_poll_unlocked (priority, fd);
714   G_UNLOCK (main_loop);
715 }
716
717 /* HOLDS: main_loop_lock */
718 static void 
719 g_main_add_poll_unlocked (gint     priority,
720                           GPollFD *fd)
721 {
722   GPollRec *lastrec, *pollrec, *newrec;
723
724   if (!poll_chunk)
725     poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
726
727   if (poll_free_list)
728     {
729       newrec = poll_free_list;
730       poll_free_list = newrec->next;
731     }
732   else
733     newrec = g_chunk_new (GPollRec, poll_chunk);
734
735   newrec->fd = fd;
736   newrec->priority = priority;
737
738   lastrec = NULL;
739   pollrec = poll_records;
740   while (pollrec && priority >= pollrec->priority)
741     {
742       lastrec = pollrec;
743       pollrec = pollrec->next;
744     }
745   
746   if (lastrec)
747     lastrec->next = newrec;
748   else
749     poll_records = newrec;
750
751   newrec->next = pollrec;
752
753   n_poll_records++;
754 }
755
756 void 
757 g_main_remove_poll (GPollFD *fd)
758 {
759   GPollRec *pollrec, *lastrec;
760
761   G_LOCK (main_loop);
762   
763   lastrec = NULL;
764   pollrec = poll_records;
765
766   while (pollrec)
767     {
768       if (pollrec->fd == fd)
769         {
770           if (lastrec != NULL)
771             lastrec->next = pollrec->next;
772           else
773             poll_records = pollrec->next;
774
775           pollrec->next = poll_free_list;
776           poll_free_list = pollrec;
777
778           n_poll_records--;
779           break;
780         }
781       lastrec = pollrec;
782       pollrec = pollrec->next;
783     }
784
785   G_UNLOCK (main_loop);
786 }
787
788 void 
789 g_main_set_poll_func (GPollFunc func)
790 {
791   if (func)
792     poll_func = func;
793   else
794 #ifdef HAVE_POLL
795     poll_func = (GPollFunc)poll;
796 #else
797     poll_func = (GPollFunc)g_poll;
798 #endif
799 }
800
801 /* Timeouts */
802
803 static gboolean 
804 g_timeout_prepare  (gpointer source_data, 
805                     GTimeVal *current_time,
806                     gint    *timeout)
807 {
808   glong msec;
809   GTimeoutData *data = source_data;
810
811   msec = (data->expiration.tv_sec  - current_time->tv_sec) * 1000 +
812          (data->expiration.tv_usec - current_time->tv_usec) / 1000;
813
814   *timeout = (msec <= 0) ? 0 : msec;
815
816   return (msec <= 0);
817 }
818
819 static gboolean 
820 g_timeout_check    (gpointer source_data,
821                     GTimeVal *current_time)
822 {
823   GTimeoutData *data = source_data;
824
825   return (data->expiration.tv_sec < current_time->tv_sec) ||
826          ((data->expiration.tv_sec == current_time->tv_sec) &&
827           (data->expiration.tv_usec <= current_time->tv_usec));
828 }
829
830 static gboolean
831 g_timeout_dispatch (gpointer source_data, 
832                     GTimeVal *current_time,
833                     gpointer user_data)
834 {
835   GTimeoutData *data = source_data;
836
837   if (data->callback(user_data))
838     {
839       guint seconds = data->interval / 1000;
840       guint msecs = data->interval - seconds * 1000;
841
842       data->expiration.tv_sec = current_time->tv_sec + seconds;
843       data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
844       if (data->expiration.tv_usec >= 1000000)
845         {
846           data->expiration.tv_usec -= 1000000;
847           data->expiration.tv_sec++;
848         }
849       return TRUE;
850     }
851   else
852     return FALSE;
853 }
854
855 guint 
856 g_timeout_add_full (gint           priority,
857                     guint          interval, 
858                     GSourceFunc    function,
859                     gpointer       data,
860                     GDestroyNotify notify)
861 {
862   guint seconds;
863   guint msecs;
864   GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
865
866   timeout_data->interval = interval;
867   timeout_data->callback = function;
868   g_get_current_time (&timeout_data->expiration);
869
870   seconds = timeout_data->interval / 1000;
871   msecs = timeout_data->interval - seconds * 1000;
872
873   timeout_data->expiration.tv_sec += seconds;
874   timeout_data->expiration.tv_usec += msecs * 1000;
875   if (timeout_data->expiration.tv_usec >= 1000000)
876     {
877       timeout_data->expiration.tv_usec -= 1000000;
878       timeout_data->expiration.tv_sec++;
879     }
880
881   return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
882 }
883
884 guint 
885 g_timeout_add (guint32        interval,
886                GSourceFunc    function,
887                gpointer       data)
888 {
889   return g_timeout_add_full (G_PRIORITY_DEFAULT, 
890                              interval, function, data, NULL);
891 }
892
893 /* Idle functions */
894
895 static gboolean 
896 g_idle_prepare  (gpointer source_data, 
897                  GTimeVal *current_time,
898                  gint     *timeout)
899 {
900   timeout = 0;
901   return TRUE;
902 }
903
904 static gboolean 
905 g_idle_check    (gpointer  source_data,
906                  GTimeVal *current_time)
907 {
908   return TRUE;
909 }
910
911 static gboolean
912 g_idle_dispatch (gpointer source_data, 
913                  GTimeVal *current_time,
914                  gpointer user_data)
915 {
916   GIdleData *data = source_data;
917
918   return (*data->callback)(user_data);
919 }
920
921 guint 
922 g_idle_add_full (gint          priority,
923                  GSourceFunc    function,
924                  gpointer       data,
925                  GDestroyNotify notify)
926 {
927   GIdleData *idle_data = g_new (GIdleData, 1);
928
929   idle_data->callback = function;
930
931   return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
932 }
933
934 guint 
935 g_idle_add (GSourceFunc    function,
936             gpointer       data)
937 {
938   return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
939 }