Add more docs
[platform/upstream/glib.git] / glib / 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 Lesser 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser 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  * Modified by the GLib Team and others 1997-2000.  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/. 
28  */
29
30 /* 
31  * MT safe
32  */
33
34 #include "config.h"
35
36 /* Uncomment the next line (and the corresponding line in gpoll.c) to
37  * enable debugging printouts if the environment variable
38  * G_MAIN_POLL_DEBUG is set to some value.
39  */
40 /* #define G_MAIN_POLL_DEBUG */
41
42 #ifdef _WIN32
43 /* Always enable debugging printout on Windows, as it is more often
44  * needed there...
45  */
46 #define G_MAIN_POLL_DEBUG
47 #endif
48
49 #include "glib.h"
50 #include "gthreadprivate.h"
51 #include <signal.h>
52 #include <sys/types.h>
53 #include <time.h>
54 #include <stdlib.h>
55 #ifdef HAVE_SYS_TIME_H
56 #include <sys/time.h>
57 #endif /* HAVE_SYS_TIME_H */
58 #ifdef HAVE_UNISTD_H
59 #include <unistd.h>
60 #endif /* HAVE_UNISTD_H */
61 #include <errno.h>
62
63 #ifdef G_OS_WIN32
64 #define STRICT
65 #include <windows.h>
66 #endif /* G_OS_WIN32 */
67
68 #ifdef G_OS_BEOS
69 #include <sys/socket.h>
70 #include <sys/wait.h>
71 #endif /* G_OS_BEOS */
72
73 #ifdef G_OS_UNIX
74 #include <fcntl.h>
75 #include <sys/wait.h>
76 #endif
77
78 #include "galias.h"
79
80 /* Types */
81
82 typedef struct _GTimeoutSource GTimeoutSource;
83 typedef struct _GChildWatchSource GChildWatchSource;
84 typedef struct _GPollRec GPollRec;
85 typedef struct _GSourceCallback GSourceCallback;
86
87 typedef enum
88 {
89   G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
90   G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
91 } GSourceFlags;
92
93 #ifdef G_THREADS_ENABLED
94 typedef struct _GMainWaiter GMainWaiter;
95
96 struct _GMainWaiter
97 {
98   GCond *cond;
99   GMutex *mutex;
100 };
101 #endif  
102
103 typedef struct _GMainDispatch GMainDispatch;
104
105 struct _GMainDispatch
106 {
107   gint depth;
108   GSList *dispatching_sources; /* stack of current sources */
109 };
110
111 #ifdef G_MAIN_POLL_DEBUG
112 gboolean _g_main_poll_debug = FALSE;
113 #endif
114
115 struct _GMainContext
116 {
117 #ifdef G_THREADS_ENABLED
118   /* The following lock is used for both the list of sources
119    * and the list of poll records
120    */
121   GStaticMutex mutex;
122   GCond *cond;
123   GThread *owner;
124   guint owner_count;
125   GSList *waiters;
126 #endif  
127
128   gint ref_count;
129
130   GPtrArray *pending_dispatches;
131   gint timeout;                 /* Timeout for current iteration */
132
133   guint next_id;
134   GSource *source_list;
135   gint in_check_or_prepare;
136
137   GPollRec *poll_records;
138   guint n_poll_records;
139   GPollFD *cached_poll_array;
140   guint cached_poll_array_size;
141
142 #ifdef G_THREADS_ENABLED  
143 #ifndef G_OS_WIN32
144 /* this pipe is used to wake up the main loop when a source is added.
145  */
146   gint wake_up_pipe[2];
147 #else /* G_OS_WIN32 */
148   HANDLE wake_up_semaphore;
149 #endif /* G_OS_WIN32 */
150
151   GPollFD wake_up_rec;
152   gboolean poll_waiting;
153
154 /* Flag indicating whether the set of fd's changed during a poll */
155   gboolean poll_changed;
156 #endif /* G_THREADS_ENABLED */
157
158   GPollFunc poll_func;
159
160   GTimeVal current_time;
161   gboolean time_is_current;
162 };
163
164 struct _GSourceCallback
165 {
166   guint ref_count;
167   GSourceFunc func;
168   gpointer    data;
169   GDestroyNotify notify;
170 };
171
172 struct _GMainLoop
173 {
174   GMainContext *context;
175   gboolean is_running;
176   gint ref_count;
177 };
178
179 struct _GTimeoutSource
180 {
181   GSource     source;
182   GTimeVal    expiration;
183   guint       interval;
184   guint       granularity;
185 };
186
187 struct _GChildWatchSource
188 {
189   GSource     source;
190   GPid        pid;
191   gint        child_status;
192 #ifdef G_OS_WIN32
193   GPollFD     poll;
194 #else /* G_OS_WIN32 */
195   gint        count;
196   gboolean    child_exited;
197 #endif /* G_OS_WIN32 */
198 };
199
200 struct _GPollRec
201 {
202   GPollFD *fd;
203   GPollRec *next;
204   gint priority;
205 };
206
207 #ifdef G_THREADS_ENABLED
208 #define LOCK_CONTEXT(context) g_static_mutex_lock (&context->mutex)
209 #define UNLOCK_CONTEXT(context) g_static_mutex_unlock (&context->mutex)
210 #define G_THREAD_SELF g_thread_self ()
211 #else
212 #define LOCK_CONTEXT(context) (void)0
213 #define UNLOCK_CONTEXT(context) (void)0
214 #define G_THREAD_SELF NULL
215 #endif
216
217 #define SOURCE_DESTROYED(source) (((source)->flags & G_HOOK_FLAG_ACTIVE) == 0)
218 #define SOURCE_BLOCKED(source) (((source)->flags & G_HOOK_FLAG_IN_CALL) != 0 && \
219                                 ((source)->flags & G_SOURCE_CAN_RECURSE) == 0)
220
221 #define SOURCE_UNREF(source, context)                       \
222    G_STMT_START {                                           \
223     if ((source)->ref_count > 1)                            \
224       (source)->ref_count--;                                \
225     else                                                    \
226       g_source_unref_internal ((source), (context), TRUE);  \
227    } G_STMT_END
228
229
230 /* Forward declarations */
231
232 static void g_source_unref_internal             (GSource      *source,
233                                                  GMainContext *context,
234                                                  gboolean      have_lock);
235 static void g_source_destroy_internal           (GSource      *source,
236                                                  GMainContext *context,
237                                                  gboolean      have_lock);
238 static void g_main_context_poll                 (GMainContext *context,
239                                                  gint          timeout,
240                                                  gint          priority,
241                                                  GPollFD      *fds,
242                                                  gint          n_fds);
243 static void g_main_context_add_poll_unlocked    (GMainContext *context,
244                                                  gint          priority,
245                                                  GPollFD      *fd);
246 static void g_main_context_remove_poll_unlocked (GMainContext *context,
247                                                  GPollFD      *fd);
248 static void g_main_context_wakeup_unlocked      (GMainContext *context);
249
250 static gboolean g_timeout_prepare  (GSource     *source,
251                                     gint        *timeout);
252 static gboolean g_timeout_check    (GSource     *source);
253 static gboolean g_timeout_dispatch (GSource     *source,
254                                     GSourceFunc  callback,
255                                     gpointer     user_data);
256 static gboolean g_child_watch_prepare  (GSource     *source,
257                                         gint        *timeout);
258 static gboolean g_child_watch_check    (GSource     *source);
259 static gboolean g_child_watch_dispatch (GSource     *source,
260                                         GSourceFunc  callback,
261                                         gpointer     user_data);
262 static gboolean g_idle_prepare     (GSource     *source,
263                                     gint        *timeout);
264 static gboolean g_idle_check       (GSource     *source);
265 static gboolean g_idle_dispatch    (GSource     *source,
266                                     GSourceFunc  callback,
267                                     gpointer     user_data);
268
269 G_LOCK_DEFINE_STATIC (main_loop);
270 static GMainContext *default_main_context;
271 static GSList *main_contexts_without_pipe = NULL;
272
273 #ifndef G_OS_WIN32
274 /* Child status monitoring code */
275 enum {
276   CHILD_WATCH_UNINITIALIZED,
277   CHILD_WATCH_INITIALIZED_SINGLE,
278   CHILD_WATCH_INITIALIZED_THREADED
279 };
280 static gint child_watch_init_state = CHILD_WATCH_UNINITIALIZED;
281 static gint child_watch_count = 1;
282 static gint child_watch_wake_up_pipe[2] = {0, 0};
283 #endif /* !G_OS_WIN32 */
284 G_LOCK_DEFINE_STATIC (main_context_list);
285 static GSList *main_context_list = NULL;
286
287 static gint timer_perturb = -1;
288
289 GSourceFuncs g_timeout_funcs =
290 {
291   g_timeout_prepare,
292   g_timeout_check,
293   g_timeout_dispatch,
294   NULL
295 };
296
297 GSourceFuncs g_child_watch_funcs =
298 {
299   g_child_watch_prepare,
300   g_child_watch_check,
301   g_child_watch_dispatch,
302   NULL
303 };
304
305 GSourceFuncs g_idle_funcs =
306 {
307   g_idle_prepare,
308   g_idle_check,
309   g_idle_dispatch,
310   NULL
311 };
312
313 /**
314  * g_main_context_ref:
315  * @context: a #GMainContext
316  * 
317  * Increases the reference count on a #GMainContext object by one.
318  *
319  * Returns: the @context that was passed in (since 2.6)
320  **/
321 GMainContext *
322 g_main_context_ref (GMainContext *context)
323 {
324   g_return_val_if_fail (context != NULL, NULL);
325   g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL); 
326
327   g_atomic_int_inc (&context->ref_count);
328
329   return context;
330 }
331
332 static inline void
333 poll_rec_list_free (GMainContext *context,
334                     GPollRec     *list)
335 {
336   g_slice_free_chain (GPollRec, list, next);
337 }
338
339 /**
340  * g_main_context_unref:
341  * @context: a #GMainContext
342  * 
343  * Decreases the reference count on a #GMainContext object by one. If
344  * the result is zero, free the context and free all associated memory.
345  **/
346 void
347 g_main_context_unref (GMainContext *context)
348 {
349   GSource *source;
350   g_return_if_fail (context != NULL);
351   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0); 
352
353   if (!g_atomic_int_dec_and_test (&context->ref_count))
354     return;
355
356   G_LOCK (main_context_list);
357   main_context_list = g_slist_remove (main_context_list, context);
358   G_UNLOCK (main_context_list);
359
360   source = context->source_list;
361   while (source)
362     {
363       GSource *next = source->next;
364       g_source_destroy_internal (source, context, FALSE);
365       source = next;
366     }
367
368 #ifdef G_THREADS_ENABLED  
369   g_static_mutex_free (&context->mutex);
370 #endif
371
372   g_ptr_array_free (context->pending_dispatches, TRUE);
373   g_free (context->cached_poll_array);
374
375   poll_rec_list_free (context, context->poll_records);
376   
377 #ifdef G_THREADS_ENABLED
378   if (g_thread_supported())
379     {
380 #ifndef G_OS_WIN32
381       close (context->wake_up_pipe[0]);
382       close (context->wake_up_pipe[1]);
383 #else
384       CloseHandle (context->wake_up_semaphore);
385 #endif
386     } 
387   else
388     main_contexts_without_pipe = g_slist_remove (main_contexts_without_pipe, 
389                                                  context);
390
391   if (context->cond != NULL)
392     g_cond_free (context->cond);
393 #endif
394   
395   g_free (context);
396 }
397
398 #ifdef G_THREADS_ENABLED
399 static void 
400 g_main_context_init_pipe (GMainContext *context)
401 {
402 # ifndef G_OS_WIN32
403   if (context->wake_up_pipe[0] != -1)
404     return;
405   if (pipe (context->wake_up_pipe) < 0)
406     g_error ("Cannot create pipe main loop wake-up: %s\n",
407              g_strerror (errno));
408  
409   fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC);
410   fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC);
411
412   context->wake_up_rec.fd = context->wake_up_pipe[0];
413   context->wake_up_rec.events = G_IO_IN;
414 # else
415   if (context->wake_up_semaphore != NULL)
416     return;
417   context->wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL);
418   if (context->wake_up_semaphore == NULL)
419     g_error ("Cannot create wake-up semaphore: %s",
420              g_win32_error_message (GetLastError ()));
421   context->wake_up_rec.fd = (gintptr) context->wake_up_semaphore;
422   context->wake_up_rec.events = G_IO_IN;
423
424   if (_g_main_poll_debug)
425     g_print ("wake-up semaphore: %p\n", context->wake_up_semaphore);
426 # endif
427   g_main_context_add_poll_unlocked (context, 0, &context->wake_up_rec);
428 }
429
430 void
431 _g_main_thread_init (void)
432 {
433   GSList *curr = main_contexts_without_pipe;
434   while (curr)
435     {
436       g_main_context_init_pipe ((GMainContext *)curr->data);
437       curr = curr->next;
438     }
439   g_slist_free (main_contexts_without_pipe);
440   main_contexts_without_pipe = NULL;  
441 }
442 #endif /* G_THREADS_ENABLED */
443
444 /**
445  * g_main_context_new:
446  * 
447  * Creates a new #GMainContext structure.
448  * 
449  * Return value: the new #GMainContext
450  **/
451 GMainContext *
452 g_main_context_new (void)
453 {
454   GMainContext *context = g_new0 (GMainContext, 1);
455
456 #ifdef G_MAIN_POLL_DEBUG
457   {
458     static gboolean beenhere = FALSE;
459
460     if (!beenhere)
461       {
462         if (getenv ("G_MAIN_POLL_DEBUG") != NULL)
463           _g_main_poll_debug = TRUE;
464         beenhere = TRUE;
465       }
466   }
467 #endif
468
469 #ifdef G_THREADS_ENABLED
470   g_static_mutex_init (&context->mutex);
471
472   context->owner = NULL;
473   context->waiters = NULL;
474
475 # ifndef G_OS_WIN32
476   context->wake_up_pipe[0] = -1;
477   context->wake_up_pipe[1] = -1;
478 # else
479   context->wake_up_semaphore = NULL;
480 # endif
481 #endif
482
483   context->ref_count = 1;
484
485   context->next_id = 1;
486   
487   context->source_list = NULL;
488   
489   context->poll_func = g_poll;
490   
491   context->cached_poll_array = NULL;
492   context->cached_poll_array_size = 0;
493   
494   context->pending_dispatches = g_ptr_array_new ();
495   
496   context->time_is_current = FALSE;
497   
498 #ifdef G_THREADS_ENABLED
499   if (g_thread_supported ())
500     g_main_context_init_pipe (context);
501   else
502     main_contexts_without_pipe = g_slist_prepend (main_contexts_without_pipe, 
503                                                   context);
504 #endif
505
506   G_LOCK (main_context_list);
507   main_context_list = g_slist_append (main_context_list, context);
508
509 #ifdef G_MAIN_POLL_DEBUG
510   if (_g_main_poll_debug)
511     g_print ("created context=%p\n", context);
512 #endif
513
514   G_UNLOCK (main_context_list);
515
516   return context;
517 }
518
519 /**
520  * g_main_context_default:
521  * 
522  * Returns the default main context. This is the main context used
523  * for main loop functions when a main loop is not explicitly
524  * specified.
525  * 
526  * Return value: the default main context.
527  **/
528 GMainContext *
529 g_main_context_default (void)
530 {
531   /* Slow, but safe */
532   
533   G_LOCK (main_loop);
534
535   if (!default_main_context)
536     {
537       default_main_context = g_main_context_new ();
538 #ifdef G_MAIN_POLL_DEBUG
539       if (_g_main_poll_debug)
540         g_print ("default context=%p\n", default_main_context);
541 #endif
542     }
543
544   G_UNLOCK (main_loop);
545
546   return default_main_context;
547 }
548
549 /* Hooks for adding to the main loop */
550
551 /**
552  * g_source_new:
553  * @source_funcs: structure containing functions that implement
554  *                the sources behavior.
555  * @struct_size: size of the #GSource structure to create.
556  * 
557  * Creates a new #GSource structure. The size is specified to
558  * allow creating structures derived from #GSource that contain
559  * additional data. The size passed in must be at least
560  * <literal>sizeof (GSource)</literal>.
561  * 
562  * The source will not initially be associated with any #GMainContext
563  * and must be added to one with g_source_attach() before it will be
564  * executed.
565  * 
566  * Return value: the newly-created #GSource.
567  **/
568 GSource *
569 g_source_new (GSourceFuncs *source_funcs,
570               guint         struct_size)
571 {
572   GSource *source;
573
574   g_return_val_if_fail (source_funcs != NULL, NULL);
575   g_return_val_if_fail (struct_size >= sizeof (GSource), NULL);
576   
577   source = (GSource*) g_malloc0 (struct_size);
578
579   source->source_funcs = source_funcs;
580   source->ref_count = 1;
581   
582   source->priority = G_PRIORITY_DEFAULT;
583
584   source->flags = G_HOOK_FLAG_ACTIVE;
585
586   /* NULL/0 initialization for all other fields */
587   
588   return source;
589 }
590
591 /* Holds context's lock
592  */
593 static void
594 g_source_list_add (GSource      *source,
595                    GMainContext *context)
596 {
597   GSource *tmp_source, *last_source;
598   
599   last_source = NULL;
600   tmp_source = context->source_list;
601   while (tmp_source && tmp_source->priority <= source->priority)
602     {
603       last_source = tmp_source;
604       tmp_source = tmp_source->next;
605     }
606
607   source->next = tmp_source;
608   if (tmp_source)
609     tmp_source->prev = source;
610   
611   source->prev = last_source;
612   if (last_source)
613     last_source->next = source;
614   else
615     context->source_list = source;
616 }
617
618 /* Holds context's lock
619  */
620 static void
621 g_source_list_remove (GSource      *source,
622                       GMainContext *context)
623 {
624   if (source->prev)
625     source->prev->next = source->next;
626   else
627     context->source_list = source->next;
628
629   if (source->next)
630     source->next->prev = source->prev;
631
632   source->prev = NULL;
633   source->next = NULL;
634 }
635
636 /**
637  * g_source_attach:
638  * @source: a #GSource
639  * @context: a #GMainContext (if %NULL, the default context will be used)
640  * 
641  * Adds a #GSource to a @context so that it will be executed within
642  * that context. Remove it by calling g_source_destroy().
643  *
644  * Return value: the ID (greater than 0) for the source within the 
645  *   #GMainContext. 
646  **/
647 guint
648 g_source_attach (GSource      *source,
649                  GMainContext *context)
650 {
651   guint result = 0;
652   GSList *tmp_list;
653
654   g_return_val_if_fail (source->context == NULL, 0);
655   g_return_val_if_fail (!SOURCE_DESTROYED (source), 0);
656   
657   if (!context)
658     context = g_main_context_default ();
659
660   LOCK_CONTEXT (context);
661
662   source->context = context;
663   result = source->source_id = context->next_id++;
664
665   source->ref_count++;
666   g_source_list_add (source, context);
667
668   tmp_list = source->poll_fds;
669   while (tmp_list)
670     {
671       g_main_context_add_poll_unlocked (context, source->priority, tmp_list->data);
672       tmp_list = tmp_list->next;
673     }
674
675 #ifdef G_THREADS_ENABLED
676   /* Now wake up the main loop if it is waiting in the poll() */
677   g_main_context_wakeup_unlocked (context);
678 #endif
679
680   UNLOCK_CONTEXT (context);
681
682   return result;
683 }
684
685 static void
686 g_source_destroy_internal (GSource      *source,
687                            GMainContext *context,
688                            gboolean      have_lock)
689 {
690   if (!have_lock)
691     LOCK_CONTEXT (context);
692   
693   if (!SOURCE_DESTROYED (source))
694     {
695       GSList *tmp_list;
696       gpointer old_cb_data;
697       GSourceCallbackFuncs *old_cb_funcs;
698       
699       source->flags &= ~G_HOOK_FLAG_ACTIVE;
700
701       old_cb_data = source->callback_data;
702       old_cb_funcs = source->callback_funcs;
703
704       source->callback_data = NULL;
705       source->callback_funcs = NULL;
706
707       if (old_cb_funcs)
708         {
709           UNLOCK_CONTEXT (context);
710           old_cb_funcs->unref (old_cb_data);
711           LOCK_CONTEXT (context);
712         }
713
714       if (!SOURCE_BLOCKED (source))
715         {
716           tmp_list = source->poll_fds;
717           while (tmp_list)
718             {
719               g_main_context_remove_poll_unlocked (context, tmp_list->data);
720               tmp_list = tmp_list->next;
721             }
722         }
723           
724       g_source_unref_internal (source, context, TRUE);
725     }
726
727   if (!have_lock)
728     UNLOCK_CONTEXT (context);
729 }
730
731 /**
732  * g_source_destroy:
733  * @source: a #GSource
734  * 
735  * Removes a source from its #GMainContext, if any, and mark it as
736  * destroyed.  The source cannot be subsequently added to another
737  * context.
738  **/
739 void
740 g_source_destroy (GSource *source)
741 {
742   GMainContext *context;
743   
744   g_return_if_fail (source != NULL);
745   
746   context = source->context;
747   
748   if (context)
749     g_source_destroy_internal (source, context, FALSE);
750   else
751     source->flags &= ~G_HOOK_FLAG_ACTIVE;
752 }
753
754 /**
755  * g_source_get_id:
756  * @source: a #GSource
757  * 
758  * Returns the numeric ID for a particular source. The ID of a source
759  * is a positive integer which is unique within a particular main loop 
760  * context. The reverse
761  * mapping from ID to source is done by g_main_context_find_source_by_id().
762  *
763  * Return value: the ID (greater than 0) for the source
764  **/
765 guint
766 g_source_get_id (GSource *source)
767 {
768   guint result;
769   
770   g_return_val_if_fail (source != NULL, 0);
771   g_return_val_if_fail (source->context != NULL, 0);
772
773   LOCK_CONTEXT (source->context);
774   result = source->source_id;
775   UNLOCK_CONTEXT (source->context);
776   
777   return result;
778 }
779
780 /**
781  * g_source_get_context:
782  * @source: a #GSource
783  * 
784  * Gets the #GMainContext with which the source is associated.
785  * Calling this function on a destroyed source is an error.
786  * 
787  * Return value: the #GMainContext with which the source is associated,
788  *               or %NULL if the context has not yet been added
789  *               to a source.
790  **/
791 GMainContext *
792 g_source_get_context (GSource *source)
793 {
794   g_return_val_if_fail (!SOURCE_DESTROYED (source), NULL);
795
796   return source->context;
797 }
798
799 /**
800  * g_source_add_poll:
801  * @source:a #GSource 
802  * @fd: a #GPollFD structure holding information about a file
803  *      descriptor to watch.
804  * 
805  * Adds a file descriptor to the set of file descriptors polled for
806  * this source. This is usually combined with g_source_new() to add an
807  * event source. The event source's check function will typically test
808  * the @revents field in the #GPollFD struct and return %TRUE if events need
809  * to be processed.
810  **/
811 void
812 g_source_add_poll (GSource *source,
813                    GPollFD *fd)
814 {
815   GMainContext *context;
816   
817   g_return_if_fail (source != NULL);
818   g_return_if_fail (fd != NULL);
819   g_return_if_fail (!SOURCE_DESTROYED (source));
820   
821   context = source->context;
822
823   if (context)
824     LOCK_CONTEXT (context);
825   
826   source->poll_fds = g_slist_prepend (source->poll_fds, fd);
827
828   if (context)
829     {
830       if (!SOURCE_BLOCKED (source))
831         g_main_context_add_poll_unlocked (context, source->priority, fd);
832       UNLOCK_CONTEXT (context);
833     }
834 }
835
836 /**
837  * g_source_remove_poll:
838  * @source:a #GSource 
839  * @fd: a #GPollFD structure previously passed to g_source_add_poll().
840  * 
841  * Removes a file descriptor from the set of file descriptors polled for
842  * this source. 
843  **/
844 void
845 g_source_remove_poll (GSource *source,
846                       GPollFD *fd)
847 {
848   GMainContext *context;
849   
850   g_return_if_fail (source != NULL);
851   g_return_if_fail (fd != NULL);
852   g_return_if_fail (!SOURCE_DESTROYED (source));
853   
854   context = source->context;
855
856   if (context)
857     LOCK_CONTEXT (context);
858   
859   source->poll_fds = g_slist_remove (source->poll_fds, fd);
860
861   if (context)
862     {
863       if (!SOURCE_BLOCKED (source))
864         g_main_context_remove_poll_unlocked (context, fd);
865       UNLOCK_CONTEXT (context);
866     }
867 }
868
869 /**
870  * g_source_set_callback_indirect:
871  * @source: the source
872  * @callback_data: pointer to callback data "object"
873  * @callback_funcs: functions for reference counting @callback_data
874  *                  and getting the callback and data
875  * 
876  * Sets the callback function storing the data as a refcounted callback
877  * "object". This is used internally. Note that calling 
878  * g_source_set_callback_indirect() assumes
879  * an initial reference count on @callback_data, and thus
880  * @callback_funcs->unref will eventually be called once more
881  * than @callback_funcs->ref.
882  **/
883 void
884 g_source_set_callback_indirect (GSource              *source,
885                                 gpointer              callback_data,
886                                 GSourceCallbackFuncs *callback_funcs)
887 {
888   GMainContext *context;
889   gpointer old_cb_data;
890   GSourceCallbackFuncs *old_cb_funcs;
891   
892   g_return_if_fail (source != NULL);
893   g_return_if_fail (callback_funcs != NULL || callback_data == NULL);
894
895   context = source->context;
896
897   if (context)
898     LOCK_CONTEXT (context);
899
900   old_cb_data = source->callback_data;
901   old_cb_funcs = source->callback_funcs;
902
903   source->callback_data = callback_data;
904   source->callback_funcs = callback_funcs;
905   
906   if (context)
907     UNLOCK_CONTEXT (context);
908   
909   if (old_cb_funcs)
910     old_cb_funcs->unref (old_cb_data);
911 }
912
913 static void
914 g_source_callback_ref (gpointer cb_data)
915 {
916   GSourceCallback *callback = cb_data;
917
918   callback->ref_count++;
919 }
920
921
922 static void
923 g_source_callback_unref (gpointer cb_data)
924 {
925   GSourceCallback *callback = cb_data;
926
927   callback->ref_count--;
928   if (callback->ref_count == 0)
929     {
930       if (callback->notify)
931         callback->notify (callback->data);
932       g_free (callback);
933     }
934 }
935
936 static void
937 g_source_callback_get (gpointer     cb_data,
938                        GSource     *source, 
939                        GSourceFunc *func,
940                        gpointer    *data)
941 {
942   GSourceCallback *callback = cb_data;
943
944   *func = callback->func;
945   *data = callback->data;
946 }
947
948 static GSourceCallbackFuncs g_source_callback_funcs = {
949   g_source_callback_ref,
950   g_source_callback_unref,
951   g_source_callback_get,
952 };
953
954 /**
955  * g_source_set_callback:
956  * @source: the source
957  * @func: a callback function
958  * @data: the data to pass to callback function
959  * @notify: a function to call when @data is no longer in use, or %NULL.
960  * 
961  * Sets the callback function for a source. The callback for a source is
962  * called from the source's dispatch function.
963  *
964  * The exact type of @func depends on the type of source; ie. you
965  * should not count on @func being called with @data as its first
966  * parameter.
967  * 
968  * Typically, you won't use this function. Instead use functions specific
969  * to the type of source you are using.
970  **/
971 void
972 g_source_set_callback (GSource        *source,
973                        GSourceFunc     func,
974                        gpointer        data,
975                        GDestroyNotify  notify)
976 {
977   GSourceCallback *new_callback;
978
979   g_return_if_fail (source != NULL);
980
981   new_callback = g_new (GSourceCallback, 1);
982
983   new_callback->ref_count = 1;
984   new_callback->func = func;
985   new_callback->data = data;
986   new_callback->notify = notify;
987
988   g_source_set_callback_indirect (source, new_callback, &g_source_callback_funcs);
989 }
990
991
992 /**
993  * g_source_set_funcs:
994  * @source: a #GSource
995  * @funcs: the new #GSourceFuncs
996  * 
997  * Sets the source functions (can be used to override 
998  * default implementations) of an unattached source.
999  * 
1000  * Since: 2.12
1001  */
1002 void
1003 g_source_set_funcs (GSource     *source,
1004                    GSourceFuncs *funcs)
1005 {
1006   g_return_if_fail (source != NULL);
1007   g_return_if_fail (source->context == NULL);
1008   g_return_if_fail (source->ref_count > 0);
1009   g_return_if_fail (funcs != NULL);
1010
1011   source->source_funcs = funcs;
1012 }
1013
1014 /**
1015  * g_source_set_priority:
1016  * @source: a #GSource
1017  * @priority: the new priority.
1018  * 
1019  * Sets the priority of a source. While the main loop is being
1020  * run, a source will be dispatched if it is ready to be dispatched and no sources 
1021  * at a higher (numerically smaller) priority are ready to be dispatched.
1022  **/
1023 void
1024 g_source_set_priority (GSource  *source,
1025                        gint      priority)
1026 {
1027   GSList *tmp_list;
1028   GMainContext *context;
1029   
1030   g_return_if_fail (source != NULL);
1031
1032   context = source->context;
1033
1034   if (context)
1035     LOCK_CONTEXT (context);
1036   
1037   source->priority = priority;
1038
1039   if (context)
1040     {
1041       /* Remove the source from the context's source and then
1042        * add it back so it is sorted in the correct plcae
1043        */
1044       g_source_list_remove (source, source->context);
1045       g_source_list_add (source, source->context);
1046
1047       if (!SOURCE_BLOCKED (source))
1048         {
1049           tmp_list = source->poll_fds;
1050           while (tmp_list)
1051             {
1052               g_main_context_remove_poll_unlocked (context, tmp_list->data);
1053               g_main_context_add_poll_unlocked (context, priority, tmp_list->data);
1054               
1055               tmp_list = tmp_list->next;
1056             }
1057         }
1058       
1059       UNLOCK_CONTEXT (source->context);
1060     }
1061 }
1062
1063 /**
1064  * g_source_get_priority:
1065  * @source: a #GSource
1066  * 
1067  * Gets the priority of a source.
1068  * 
1069  * Return value: the priority of the source
1070  **/
1071 gint
1072 g_source_get_priority (GSource *source)
1073 {
1074   g_return_val_if_fail (source != NULL, 0);
1075
1076   return source->priority;
1077 }
1078
1079 /**
1080  * g_source_set_can_recurse:
1081  * @source: a #GSource
1082  * @can_recurse: whether recursion is allowed for this source
1083  * 
1084  * Sets whether a source can be called recursively. If @can_recurse is
1085  * %TRUE, then while the source is being dispatched then this source
1086  * will be processed normally. Otherwise, all processing of this
1087  * source is blocked until the dispatch function returns.
1088  **/
1089 void
1090 g_source_set_can_recurse (GSource  *source,
1091                           gboolean  can_recurse)
1092 {
1093   GMainContext *context;
1094   
1095   g_return_if_fail (source != NULL);
1096
1097   context = source->context;
1098
1099   if (context)
1100     LOCK_CONTEXT (context);
1101   
1102   if (can_recurse)
1103     source->flags |= G_SOURCE_CAN_RECURSE;
1104   else
1105     source->flags &= ~G_SOURCE_CAN_RECURSE;
1106
1107   if (context)
1108     UNLOCK_CONTEXT (context);
1109 }
1110
1111 /**
1112  * g_source_get_can_recurse:
1113  * @source: a #GSource
1114  * 
1115  * Checks whether a source is allowed to be called recursively.
1116  * see g_source_set_can_recurse().
1117  * 
1118  * Return value: whether recursion is allowed.
1119  **/
1120 gboolean
1121 g_source_get_can_recurse (GSource  *source)
1122 {
1123   g_return_val_if_fail (source != NULL, FALSE);
1124   
1125   return (source->flags & G_SOURCE_CAN_RECURSE) != 0;
1126 }
1127
1128 /**
1129  * g_source_ref:
1130  * @source: a #GSource
1131  * 
1132  * Increases the reference count on a source by one.
1133  * 
1134  * Return value: @source
1135  **/
1136 GSource *
1137 g_source_ref (GSource *source)
1138 {
1139   GMainContext *context;
1140   
1141   g_return_val_if_fail (source != NULL, NULL);
1142
1143   context = source->context;
1144
1145   if (context)
1146     LOCK_CONTEXT (context);
1147
1148   source->ref_count++;
1149
1150   if (context)
1151     UNLOCK_CONTEXT (context);
1152
1153   return source;
1154 }
1155
1156 /* g_source_unref() but possible to call within context lock
1157  */
1158 static void
1159 g_source_unref_internal (GSource      *source,
1160                          GMainContext *context,
1161                          gboolean      have_lock)
1162 {
1163   gpointer old_cb_data = NULL;
1164   GSourceCallbackFuncs *old_cb_funcs = NULL;
1165
1166   g_return_if_fail (source != NULL);
1167   
1168   if (!have_lock && context)
1169     LOCK_CONTEXT (context);
1170
1171   source->ref_count--;
1172   if (source->ref_count == 0)
1173     {
1174       old_cb_data = source->callback_data;
1175       old_cb_funcs = source->callback_funcs;
1176
1177       source->callback_data = NULL;
1178       source->callback_funcs = NULL;
1179
1180       if (context && !SOURCE_DESTROYED (source))
1181         {
1182           g_warning (G_STRLOC ": ref_count == 0, but source is still attached to a context!");
1183           source->ref_count++;
1184         }
1185       else if (context)
1186         g_source_list_remove (source, context);
1187
1188       if (source->source_funcs->finalize)
1189         source->source_funcs->finalize (source);
1190       
1191       g_slist_free (source->poll_fds);
1192       source->poll_fds = NULL;
1193       g_free (source);
1194     }
1195   
1196   if (!have_lock && context)
1197     UNLOCK_CONTEXT (context);
1198
1199   if (old_cb_funcs)
1200     {
1201       if (have_lock)
1202         UNLOCK_CONTEXT (context);
1203       
1204       old_cb_funcs->unref (old_cb_data);
1205
1206       if (have_lock)
1207         LOCK_CONTEXT (context);
1208     }
1209 }
1210
1211 /**
1212  * g_source_unref:
1213  * @source: a #GSource
1214  * 
1215  * Decreases the reference count of a source by one. If the
1216  * resulting reference count is zero the source and associated
1217  * memory will be destroyed. 
1218  **/
1219 void
1220 g_source_unref (GSource *source)
1221 {
1222   g_return_if_fail (source != NULL);
1223
1224   g_source_unref_internal (source, source->context, FALSE);
1225 }
1226
1227 /**
1228  * g_main_context_find_source_by_id:
1229  * @context: a #GMainContext (if %NULL, the default context will be used)
1230  * @source_id: the source ID, as returned by g_source_get_id(). 
1231  * 
1232  * Finds a #GSource given a pair of context and ID.
1233  * 
1234  * Return value: the #GSource if found, otherwise, %NULL
1235  **/
1236 GSource *
1237 g_main_context_find_source_by_id (GMainContext *context,
1238                                   guint         source_id)
1239 {
1240   GSource *source;
1241   
1242   g_return_val_if_fail (source_id > 0, NULL);
1243
1244   if (context == NULL)
1245     context = g_main_context_default ();
1246   
1247   LOCK_CONTEXT (context);
1248   
1249   source = context->source_list;
1250   while (source)
1251     {
1252       if (!SOURCE_DESTROYED (source) &&
1253           source->source_id == source_id)
1254         break;
1255       source = source->next;
1256     }
1257
1258   UNLOCK_CONTEXT (context);
1259
1260   return source;
1261 }
1262
1263 /**
1264  * g_main_context_find_source_by_funcs_user_data:
1265  * @context: a #GMainContext (if %NULL, the default context will be used).
1266  * @funcs: the @source_funcs passed to g_source_new().
1267  * @user_data: the user data from the callback.
1268  * 
1269  * Finds a source with the given source functions and user data.  If
1270  * multiple sources exist with the same source function and user data,
1271  * the first one found will be returned.
1272  * 
1273  * Return value: the source, if one was found, otherwise %NULL
1274  **/
1275 GSource *
1276 g_main_context_find_source_by_funcs_user_data (GMainContext *context,
1277                                                GSourceFuncs *funcs,
1278                                                gpointer      user_data)
1279 {
1280   GSource *source;
1281   
1282   g_return_val_if_fail (funcs != NULL, NULL);
1283
1284   if (context == NULL)
1285     context = g_main_context_default ();
1286   
1287   LOCK_CONTEXT (context);
1288
1289   source = context->source_list;
1290   while (source)
1291     {
1292       if (!SOURCE_DESTROYED (source) &&
1293           source->source_funcs == funcs &&
1294           source->callback_funcs)
1295         {
1296           GSourceFunc callback;
1297           gpointer callback_data;
1298
1299           source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1300           
1301           if (callback_data == user_data)
1302             break;
1303         }
1304       source = source->next;
1305     }
1306
1307   UNLOCK_CONTEXT (context);
1308
1309   return source;
1310 }
1311
1312 /**
1313  * g_main_context_find_source_by_user_data:
1314  * @context: a #GMainContext
1315  * @user_data: the user_data for the callback.
1316  * 
1317  * Finds a source with the given user data for the callback.  If
1318  * multiple sources exist with the same user data, the first
1319  * one found will be returned.
1320  * 
1321  * Return value: the source, if one was found, otherwise %NULL
1322  **/
1323 GSource *
1324 g_main_context_find_source_by_user_data (GMainContext *context,
1325                                          gpointer      user_data)
1326 {
1327   GSource *source;
1328   
1329   if (context == NULL)
1330     context = g_main_context_default ();
1331   
1332   LOCK_CONTEXT (context);
1333
1334   source = context->source_list;
1335   while (source)
1336     {
1337       if (!SOURCE_DESTROYED (source) &&
1338           source->callback_funcs)
1339         {
1340           GSourceFunc callback;
1341           gpointer callback_data = NULL;
1342
1343           source->callback_funcs->get (source->callback_data, source, &callback, &callback_data);
1344
1345           if (callback_data == user_data)
1346             break;
1347         }
1348       source = source->next;
1349     }
1350
1351   UNLOCK_CONTEXT (context);
1352
1353   return source;
1354 }
1355
1356 /**
1357  * g_source_remove:
1358  * @tag: the ID of the source to remove.
1359  * 
1360  * Removes the source with the given id from the default main context. 
1361  * The id of
1362  * a #GSource is given by g_source_get_id(), or will be returned by the
1363  * functions g_source_attach(), g_idle_add(), g_idle_add_full(),
1364  * g_timeout_add(), g_timeout_add_full(), g_child_watch_add(),
1365  * g_child_watch_add_full(), g_io_add_watch(), and g_io_add_watch_full().
1366  *
1367  * See also g_source_destroy(). You must use g_source_destroy() for sources
1368  * added to a non-default main context.
1369  *
1370  * Return value: %TRUE if the source was found and removed.
1371  **/
1372 gboolean
1373 g_source_remove (guint tag)
1374 {
1375   GSource *source;
1376   
1377   g_return_val_if_fail (tag > 0, FALSE);
1378
1379   source = g_main_context_find_source_by_id (NULL, tag);
1380   if (source)
1381     g_source_destroy (source);
1382
1383   return source != NULL;
1384 }
1385
1386 /**
1387  * g_source_remove_by_user_data:
1388  * @user_data: the user_data for the callback.
1389  * 
1390  * Removes a source from the default main loop context given the user
1391  * data for the callback. If multiple sources exist with the same user
1392  * data, only one will be destroyed.
1393  * 
1394  * Return value: %TRUE if a source was found and removed. 
1395  **/
1396 gboolean
1397 g_source_remove_by_user_data (gpointer user_data)
1398 {
1399   GSource *source;
1400   
1401   source = g_main_context_find_source_by_user_data (NULL, user_data);
1402   if (source)
1403     {
1404       g_source_destroy (source);
1405       return TRUE;
1406     }
1407   else
1408     return FALSE;
1409 }
1410
1411 /**
1412  * g_source_remove_by_funcs_user_data:
1413  * @funcs: The @source_funcs passed to g_source_new()
1414  * @user_data: the user data for the callback
1415  * 
1416  * Removes a source from the default main loop context given the
1417  * source functions and user data. If multiple sources exist with the
1418  * same source functions and user data, only one will be destroyed.
1419  * 
1420  * Return value: %TRUE if a source was found and removed. 
1421  **/
1422 gboolean
1423 g_source_remove_by_funcs_user_data (GSourceFuncs *funcs,
1424                                     gpointer      user_data)
1425 {
1426   GSource *source;
1427
1428   g_return_val_if_fail (funcs != NULL, FALSE);
1429
1430   source = g_main_context_find_source_by_funcs_user_data (NULL, funcs, user_data);
1431   if (source)
1432     {
1433       g_source_destroy (source);
1434       return TRUE;
1435     }
1436   else
1437     return FALSE;
1438 }
1439
1440 /**
1441  * g_get_current_time:
1442  * @result: #GTimeVal structure in which to store current time.
1443  * 
1444  * Equivalent to the UNIX gettimeofday() function, but portable.
1445  **/
1446 void
1447 g_get_current_time (GTimeVal *result)
1448 {
1449 #ifndef G_OS_WIN32
1450   struct timeval r;
1451
1452   g_return_if_fail (result != NULL);
1453
1454   /*this is required on alpha, there the timeval structs are int's
1455     not longs and a cast only would fail horribly*/
1456   gettimeofday (&r, NULL);
1457   result->tv_sec = r.tv_sec;
1458   result->tv_usec = r.tv_usec;
1459 #else
1460   FILETIME ft;
1461   guint64 time64;
1462
1463   g_return_if_fail (result != NULL);
1464
1465   GetSystemTimeAsFileTime (&ft);
1466   memmove (&time64, &ft, sizeof (FILETIME));
1467
1468   /* Convert from 100s of nanoseconds since 1601-01-01
1469    * to Unix epoch. Yes, this is Y2038 unsafe.
1470    */
1471   time64 -= G_GINT64_CONSTANT (116444736000000000);
1472   time64 /= 10;
1473
1474   result->tv_sec = time64 / 1000000;
1475   result->tv_usec = time64 % 1000000;
1476 #endif
1477 }
1478
1479 static void
1480 g_main_dispatch_free (gpointer dispatch)
1481 {
1482   g_slice_free (GMainDispatch, dispatch);
1483 }
1484
1485 /* Running the main loop */
1486
1487 static GMainDispatch *
1488 get_dispatch (void)
1489 {
1490   static GStaticPrivate depth_private = G_STATIC_PRIVATE_INIT;
1491   GMainDispatch *dispatch = g_static_private_get (&depth_private);
1492   if (!dispatch)
1493     {
1494       dispatch = g_slice_new0 (GMainDispatch);
1495       g_static_private_set (&depth_private, dispatch, g_main_dispatch_free);
1496     }
1497
1498   return dispatch;
1499 }
1500
1501 /**
1502  * g_main_depth:
1503  *
1504  * Returns the depth of the stack of calls to
1505  * g_main_context_dispatch() on any #GMainContext in the current thread.
1506  *  That is, when called from the toplevel, it gives 0. When
1507  * called from within a callback from g_main_context_iteration()
1508  * (or g_main_loop_run(), etc.) it returns 1. When called from within 
1509  * a callback to a recursive call to g_main_context_iterate(),
1510  * it returns 2. And so forth.
1511  *
1512  * This function is useful in a situation like the following:
1513  * Imagine an extremely simple "garbage collected" system.
1514  *
1515  * |[
1516  * static GList *free_list;
1517  * 
1518  * gpointer
1519  * allocate_memory (gsize size)
1520  * { 
1521  *   gpointer result = g_malloc (size);
1522  *   free_list = g_list_prepend (free_list, result);
1523  *   return result;
1524  * }
1525  * 
1526  * void
1527  * free_allocated_memory (void)
1528  * {
1529  *   GList *l;
1530  *   for (l = free_list; l; l = l->next);
1531  *     g_free (l->data);
1532  *   g_list_free (free_list);
1533  *   free_list = NULL;
1534  *  }
1535  * 
1536  * [...]
1537  * 
1538  * while (TRUE); 
1539  *  {
1540  *    g_main_context_iteration (NULL, TRUE);
1541  *    free_allocated_memory();
1542  *   }
1543  * ]|
1544  *
1545  * This works from an application, however, if you want to do the same
1546  * thing from a library, it gets more difficult, since you no longer
1547  * control the main loop. You might think you can simply use an idle
1548  * function to make the call to free_allocated_memory(), but that
1549  * doesn't work, since the idle function could be called from a
1550  * recursive callback. This can be fixed by using g_main_depth()
1551  *
1552  * |[
1553  * gpointer
1554  * allocate_memory (gsize size)
1555  * { 
1556  *   FreeListBlock *block = g_new (FreeListBlock, 1);
1557  *   block->mem = g_malloc (size);
1558  *   block->depth = g_main_depth ();   
1559  *   free_list = g_list_prepend (free_list, block);
1560  *   return block->mem;
1561  * }
1562  * 
1563  * void
1564  * free_allocated_memory (void)
1565  * {
1566  *   GList *l;
1567  *   
1568  *   int depth = g_main_depth ();
1569  *   for (l = free_list; l; );
1570  *     {
1571  *       GList *next = l->next;
1572  *       FreeListBlock *block = l->data;
1573  *       if (block->depth > depth)
1574  *         {
1575  *           g_free (block->mem);
1576  *           g_free (block);
1577  *           free_list = g_list_delete_link (free_list, l);
1578  *         }
1579  *               
1580  *       l = next;
1581  *     }
1582  *   }
1583  * ]|
1584  *
1585  * There is a temptation to use g_main_depth() to solve
1586  * problems with reentrancy. For instance, while waiting for data
1587  * to be received from the network in response to a menu item,
1588  * the menu item might be selected again. It might seem that
1589  * one could make the menu item's callback return immediately
1590  * and do nothing if g_main_depth() returns a value greater than 1.
1591  * However, this should be avoided since the user then sees selecting
1592  * the menu item do nothing. Furthermore, you'll find yourself adding
1593  * these checks all over your code, since there are doubtless many,
1594  * many things that the user could do. Instead, you can use the
1595  * following techniques:
1596  *
1597  * <orderedlist>
1598  *  <listitem>
1599  *   <para>
1600  *     Use gtk_widget_set_sensitive() or modal dialogs to prevent
1601  *     the user from interacting with elements while the main
1602  *     loop is recursing.
1603  *   </para>
1604  *  </listitem>
1605  *  <listitem>
1606  *   <para>
1607  *     Avoid main loop recursion in situations where you can't handle
1608  *     arbitrary  callbacks. Instead, structure your code so that you
1609  *     simply return to the main loop and then get called again when
1610  *     there is more work to do.
1611  *   </para>
1612  *  </listitem>
1613  * </orderedlist>
1614  * 
1615  * Return value: The main loop recursion level in the current thread
1616  **/
1617 int
1618 g_main_depth (void)
1619 {
1620   GMainDispatch *dispatch = get_dispatch ();
1621   return dispatch->depth;
1622 }
1623
1624 /**
1625  * g_main_current_source:
1626  *
1627  * Returns the currently firing source for this thread.
1628  * 
1629  * Return value: The currently firing source or %NULL.
1630  *
1631  * Since: 2.12
1632  */
1633 GSource *
1634 g_main_current_source (void)
1635 {
1636   GMainDispatch *dispatch = get_dispatch ();
1637   return dispatch->dispatching_sources ? dispatch->dispatching_sources->data : NULL;
1638 }
1639
1640 /**
1641  * g_source_is_destroyed:
1642  * @source: a #GSource
1643  *
1644  * Returns whether @source has been destroyed.
1645  *
1646  * This is important when you operate upon your objects 
1647  * from within idle handlers, but may have freed the object 
1648  * before the dispatch of your idle handler.
1649  *
1650  * |[
1651  * static gboolean 
1652  * idle_callback (gpointer data)
1653  * {
1654  *   SomeWidget *self = data;
1655  *    
1656  *   GDK_THREADS_ENTER (<!-- -->);
1657  *   /<!-- -->* do stuff with self *<!-- -->/
1658  *   self->idle_id = 0;
1659  *   GDK_THREADS_LEAVE (<!-- -->);
1660  *    
1661  *   return FALSE;
1662  * }
1663  *  
1664  * static void 
1665  * some_widget_do_stuff_later (SomeWidget *self)
1666  * {
1667  *   self->idle_id = g_idle_add (idle_callback, self);
1668  * }
1669  *  
1670  * static void 
1671  * some_widget_finalize (GObject *object)
1672  * {
1673  *   SomeWidget *self = SOME_WIDGET (object);
1674  *    
1675  *   if (self->idle_id)
1676  *     g_source_remove (self->idle_id);
1677  *    
1678  *   G_OBJECT_CLASS (parent_class)->finalize (object);
1679  * }
1680  * ]|
1681  *
1682  * This will fail in a multi-threaded application if the 
1683  * widget is destroyed before the idle handler fires due 
1684  * to the use after free in the callback. A solution, to 
1685  * this particular problem, is to check to if the source
1686  * has already been destroy within the callback.
1687  *
1688  * |[
1689  * static gboolean 
1690  * idle_callback (gpointer data)
1691  * {
1692  *   SomeWidget *self = data;
1693  *   
1694  *   GDK_THREADS_ENTER ();
1695  *   if (!g_source_is_destroyed (g_main_current_source ()))
1696  *     {
1697  *       /<!-- -->* do stuff with self *<!-- -->/
1698  *     }
1699  *   GDK_THREADS_LEAVE ();
1700  *   
1701  *   return FALSE;
1702  * }
1703  * ]|
1704  *
1705  * Return value: %TRUE if the source has been destroyed
1706  *
1707  * Since: 2.12
1708  */
1709 gboolean
1710 g_source_is_destroyed (GSource *source)
1711 {
1712   return SOURCE_DESTROYED (source);
1713 }
1714
1715
1716 /* Temporarily remove all this source's file descriptors from the
1717  * poll(), so that if data comes available for one of the file descriptors
1718  * we don't continually spin in the poll()
1719  */
1720 /* HOLDS: source->context's lock */
1721 static void
1722 block_source (GSource *source)
1723 {
1724   GSList *tmp_list;
1725
1726   g_return_if_fail (!SOURCE_BLOCKED (source));
1727
1728   tmp_list = source->poll_fds;
1729   while (tmp_list)
1730     {
1731       g_main_context_remove_poll_unlocked (source->context, tmp_list->data);
1732       tmp_list = tmp_list->next;
1733     }
1734 }
1735
1736 /* HOLDS: source->context's lock */
1737 static void
1738 unblock_source (GSource *source)
1739 {
1740   GSList *tmp_list;
1741   
1742   g_return_if_fail (!SOURCE_BLOCKED (source)); /* Source already unblocked */
1743   g_return_if_fail (!SOURCE_DESTROYED (source));
1744   
1745   tmp_list = source->poll_fds;
1746   while (tmp_list)
1747     {
1748       g_main_context_add_poll_unlocked (source->context, source->priority, tmp_list->data);
1749       tmp_list = tmp_list->next;
1750     }
1751 }
1752
1753 /* HOLDS: context's lock */
1754 static void
1755 g_main_dispatch (GMainContext *context)
1756 {
1757   GMainDispatch *current = get_dispatch ();
1758   guint i;
1759
1760   for (i = 0; i < context->pending_dispatches->len; i++)
1761     {
1762       GSource *source = context->pending_dispatches->pdata[i];
1763
1764       context->pending_dispatches->pdata[i] = NULL;
1765       g_assert (source);
1766
1767       source->flags &= ~G_SOURCE_READY;
1768
1769       if (!SOURCE_DESTROYED (source))
1770         {
1771           gboolean was_in_call;
1772           gpointer user_data = NULL;
1773           GSourceFunc callback = NULL;
1774           GSourceCallbackFuncs *cb_funcs;
1775           gpointer cb_data;
1776           gboolean need_destroy;
1777
1778           gboolean (*dispatch) (GSource *,
1779                                 GSourceFunc,
1780                                 gpointer);
1781           GSList current_source_link;
1782
1783           dispatch = source->source_funcs->dispatch;
1784           cb_funcs = source->callback_funcs;
1785           cb_data = source->callback_data;
1786
1787           if (cb_funcs)
1788             cb_funcs->ref (cb_data);
1789           
1790           if ((source->flags & G_SOURCE_CAN_RECURSE) == 0)
1791             block_source (source);
1792           
1793           was_in_call = source->flags & G_HOOK_FLAG_IN_CALL;
1794           source->flags |= G_HOOK_FLAG_IN_CALL;
1795
1796           if (cb_funcs)
1797             cb_funcs->get (cb_data, source, &callback, &user_data);
1798
1799           UNLOCK_CONTEXT (context);
1800
1801           current->depth++;
1802           /* The on-stack allocation of the GSList is unconventional, but
1803            * we know that the lifetime of the link is bounded to this
1804            * function as the link is kept in a thread specific list and
1805            * not manipulated outside of this function and its descendants.
1806            * Avoiding the overhead of a g_slist_alloc() is useful as many
1807            * applications do little more than dispatch events.
1808            *
1809            * This is a performance hack - do not revert to g_slist_prepend()!
1810            */
1811           current_source_link.data = source;
1812           current_source_link.next = current->dispatching_sources;
1813           current->dispatching_sources = &current_source_link;
1814           need_destroy = ! dispatch (source,
1815                                      callback,
1816                                      user_data);
1817           g_assert (current->dispatching_sources == &current_source_link);
1818           current->dispatching_sources = current_source_link.next;
1819           current->depth--;
1820           
1821           if (cb_funcs)
1822             cb_funcs->unref (cb_data);
1823
1824           LOCK_CONTEXT (context);
1825           
1826           if (!was_in_call)
1827             source->flags &= ~G_HOOK_FLAG_IN_CALL;
1828
1829           if ((source->flags & G_SOURCE_CAN_RECURSE) == 0 &&
1830               !SOURCE_DESTROYED (source))
1831             unblock_source (source);
1832           
1833           /* Note: this depends on the fact that we can't switch
1834            * sources from one main context to another
1835            */
1836           if (need_destroy && !SOURCE_DESTROYED (source))
1837             {
1838               g_assert (source->context == context);
1839               g_source_destroy_internal (source, context, TRUE);
1840             }
1841         }
1842       
1843       SOURCE_UNREF (source, context);
1844     }
1845
1846   g_ptr_array_set_size (context->pending_dispatches, 0);
1847 }
1848
1849 /* Holds context's lock */
1850 static inline GSource *
1851 next_valid_source (GMainContext *context,
1852                    GSource      *source)
1853 {
1854   GSource *new_source = source ? source->next : context->source_list;
1855
1856   while (new_source)
1857     {
1858       if (!SOURCE_DESTROYED (new_source))
1859         {
1860           new_source->ref_count++;
1861           break;
1862         }
1863       
1864       new_source = new_source->next;
1865     }
1866
1867   if (source)
1868     SOURCE_UNREF (source, context);
1869           
1870   return new_source;
1871 }
1872
1873 /**
1874  * g_main_context_acquire:
1875  * @context: a #GMainContext
1876  * 
1877  * Tries to become the owner of the specified context.
1878  * If some other thread is the owner of the context,
1879  * returns %FALSE immediately. Ownership is properly
1880  * recursive: the owner can require ownership again
1881  * and will release ownership when g_main_context_release()
1882  * is called as many times as g_main_context_acquire().
1883  *
1884  * You must be the owner of a context before you
1885  * can call g_main_context_prepare(), g_main_context_query(),
1886  * g_main_context_check(), g_main_context_dispatch().
1887  * 
1888  * Return value: %TRUE if the operation succeeded, and
1889  *   this thread is now the owner of @context.
1890  **/
1891 gboolean 
1892 g_main_context_acquire (GMainContext *context)
1893 {
1894 #ifdef G_THREADS_ENABLED
1895   gboolean result = FALSE;
1896   GThread *self = G_THREAD_SELF;
1897
1898   if (context == NULL)
1899     context = g_main_context_default ();
1900   
1901   LOCK_CONTEXT (context);
1902
1903   if (!context->owner)
1904     {
1905       context->owner = self;
1906       g_assert (context->owner_count == 0);
1907     }
1908
1909   if (context->owner == self)
1910     {
1911       context->owner_count++;
1912       result = TRUE;
1913     }
1914
1915   UNLOCK_CONTEXT (context); 
1916   
1917   return result;
1918 #else /* !G_THREADS_ENABLED */
1919   return TRUE;
1920 #endif /* G_THREADS_ENABLED */
1921 }
1922
1923 /**
1924  * g_main_context_release:
1925  * @context: a #GMainContext
1926  * 
1927  * Releases ownership of a context previously acquired by this thread
1928  * with g_main_context_acquire(). If the context was acquired multiple
1929  * times, the ownership will be released only when g_main_context_release()
1930  * is called as many times as it was acquired.
1931  **/
1932 void
1933 g_main_context_release (GMainContext *context)
1934 {
1935 #ifdef G_THREADS_ENABLED
1936   if (context == NULL)
1937     context = g_main_context_default ();
1938   
1939   LOCK_CONTEXT (context);
1940
1941   context->owner_count--;
1942   if (context->owner_count == 0)
1943     {
1944       context->owner = NULL;
1945
1946       if (context->waiters)
1947         {
1948           GMainWaiter *waiter = context->waiters->data;
1949           gboolean loop_internal_waiter =
1950             (waiter->mutex == g_static_mutex_get_mutex (&context->mutex));
1951           context->waiters = g_slist_delete_link (context->waiters,
1952                                                   context->waiters);
1953           if (!loop_internal_waiter)
1954             g_mutex_lock (waiter->mutex);
1955           
1956           g_cond_signal (waiter->cond);
1957           
1958           if (!loop_internal_waiter)
1959             g_mutex_unlock (waiter->mutex);
1960         }
1961     }
1962
1963   UNLOCK_CONTEXT (context); 
1964 #endif /* G_THREADS_ENABLED */
1965 }
1966
1967 /**
1968  * g_main_context_wait:
1969  * @context: a #GMainContext
1970  * @cond: a condition variable
1971  * @mutex: a mutex, currently held
1972  * 
1973  * Tries to become the owner of the specified context,
1974  * as with g_main_context_acquire(). But if another thread
1975  * is the owner, atomically drop @mutex and wait on @cond until 
1976  * that owner releases ownership or until @cond is signaled, then
1977  * try again (once) to become the owner.
1978  * 
1979  * Return value: %TRUE if the operation succeeded, and
1980  *   this thread is now the owner of @context.
1981  **/
1982 gboolean
1983 g_main_context_wait (GMainContext *context,
1984                      GCond        *cond,
1985                      GMutex       *mutex)
1986 {
1987 #ifdef G_THREADS_ENABLED
1988   gboolean result = FALSE;
1989   GThread *self = G_THREAD_SELF;
1990   gboolean loop_internal_waiter;
1991   
1992   if (context == NULL)
1993     context = g_main_context_default ();
1994
1995   loop_internal_waiter = (mutex == g_static_mutex_get_mutex (&context->mutex));
1996   
1997   if (!loop_internal_waiter)
1998     LOCK_CONTEXT (context);
1999
2000   if (context->owner && context->owner != self)
2001     {
2002       GMainWaiter waiter;
2003
2004       waiter.cond = cond;
2005       waiter.mutex = mutex;
2006
2007       context->waiters = g_slist_append (context->waiters, &waiter);
2008       
2009       if (!loop_internal_waiter)
2010         UNLOCK_CONTEXT (context);
2011       g_cond_wait (cond, mutex);
2012       if (!loop_internal_waiter)      
2013         LOCK_CONTEXT (context);
2014
2015       context->waiters = g_slist_remove (context->waiters, &waiter);
2016     }
2017
2018   if (!context->owner)
2019     {
2020       context->owner = self;
2021       g_assert (context->owner_count == 0);
2022     }
2023
2024   if (context->owner == self)
2025     {
2026       context->owner_count++;
2027       result = TRUE;
2028     }
2029
2030   if (!loop_internal_waiter)
2031     UNLOCK_CONTEXT (context); 
2032   
2033   return result;
2034 #else /* !G_THREADS_ENABLED */
2035   return TRUE;
2036 #endif /* G_THREADS_ENABLED */
2037 }
2038
2039 /**
2040  * g_main_context_prepare:
2041  * @context: a #GMainContext
2042  * @priority: location to store priority of highest priority
2043  *            source already ready.
2044  * 
2045  * Prepares to poll sources within a main loop. The resulting information
2046  * for polling is determined by calling g_main_context_query ().
2047  * 
2048  * Return value: %TRUE if some source is ready to be dispatched
2049  *               prior to polling.
2050  **/
2051 gboolean
2052 g_main_context_prepare (GMainContext *context,
2053                         gint         *priority)
2054 {
2055   gint i;
2056   gint n_ready = 0;
2057   gint current_priority = G_MAXINT;
2058   GSource *source;
2059
2060   if (context == NULL)
2061     context = g_main_context_default ();
2062   
2063   LOCK_CONTEXT (context);
2064
2065   context->time_is_current = FALSE;
2066
2067   if (context->in_check_or_prepare)
2068     {
2069       g_warning ("g_main_context_prepare() called recursively from within a source's check() or "
2070                  "prepare() member.");
2071       UNLOCK_CONTEXT (context);
2072       return FALSE;
2073     }
2074
2075 #ifdef G_THREADS_ENABLED
2076   if (context->poll_waiting)
2077     {
2078       g_warning("g_main_context_prepare(): main loop already active in another thread");
2079       UNLOCK_CONTEXT (context);
2080       return FALSE;
2081     }
2082   
2083   context->poll_waiting = TRUE;
2084 #endif /* G_THREADS_ENABLED */
2085
2086 #if 0
2087   /* If recursing, finish up current dispatch, before starting over */
2088   if (context->pending_dispatches)
2089     {
2090       if (dispatch)
2091         g_main_dispatch (context, &current_time);
2092       
2093       UNLOCK_CONTEXT (context);
2094       return TRUE;
2095     }
2096 #endif
2097
2098   /* If recursing, clear list of pending dispatches */
2099
2100   for (i = 0; i < context->pending_dispatches->len; i++)
2101     {
2102       if (context->pending_dispatches->pdata[i])
2103         SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
2104     }
2105   g_ptr_array_set_size (context->pending_dispatches, 0);
2106   
2107   /* Prepare all sources */
2108
2109   context->timeout = -1;
2110   
2111   source = next_valid_source (context, NULL);
2112   while (source)
2113     {
2114       gint source_timeout = -1;
2115
2116       if ((n_ready > 0) && (source->priority > current_priority))
2117         {
2118           SOURCE_UNREF (source, context);
2119           break;
2120         }
2121       if (SOURCE_BLOCKED (source))
2122         goto next;
2123
2124       if (!(source->flags & G_SOURCE_READY))
2125         {
2126           gboolean result;
2127           gboolean (*prepare)  (GSource  *source, 
2128                                 gint     *timeout);
2129
2130           prepare = source->source_funcs->prepare;
2131           context->in_check_or_prepare++;
2132           UNLOCK_CONTEXT (context);
2133
2134           result = (*prepare) (source, &source_timeout);
2135
2136           LOCK_CONTEXT (context);
2137           context->in_check_or_prepare--;
2138
2139           if (result)
2140             source->flags |= G_SOURCE_READY;
2141         }
2142
2143       if (source->flags & G_SOURCE_READY)
2144         {
2145           n_ready++;
2146           current_priority = source->priority;
2147           context->timeout = 0;
2148         }
2149       
2150       if (source_timeout >= 0)
2151         {
2152           if (context->timeout < 0)
2153             context->timeout = source_timeout;
2154           else
2155             context->timeout = MIN (context->timeout, source_timeout);
2156         }
2157
2158     next:
2159       source = next_valid_source (context, source);
2160     }
2161
2162   UNLOCK_CONTEXT (context);
2163   
2164   if (priority)
2165     *priority = current_priority;
2166   
2167   return (n_ready > 0);
2168 }
2169
2170 /**
2171  * g_main_context_query:
2172  * @context: a #GMainContext
2173  * @max_priority: maximum priority source to check
2174  * @timeout_: location to store timeout to be used in polling
2175  * @fds: location to store #GPollFD records that need to be polled.
2176  * @n_fds: length of @fds.
2177  * 
2178  * Determines information necessary to poll this main loop.
2179  * 
2180  * Return value: the number of records actually stored in @fds,
2181  *   or, if more than @n_fds records need to be stored, the number
2182  *   of records that need to be stored.
2183  **/
2184 gint
2185 g_main_context_query (GMainContext *context,
2186                       gint          max_priority,
2187                       gint         *timeout,
2188                       GPollFD      *fds,
2189                       gint          n_fds)
2190 {
2191   gint n_poll;
2192   GPollRec *pollrec;
2193   
2194   LOCK_CONTEXT (context);
2195
2196   pollrec = context->poll_records;
2197   n_poll = 0;
2198   while (pollrec && max_priority >= pollrec->priority)
2199     {
2200       /* We need to include entries with fd->events == 0 in the array because
2201        * otherwise if the application changes fd->events behind our back and 
2202        * makes it non-zero, we'll be out of sync when we check the fds[] array.
2203        * (Changing fd->events after adding an FD wasn't an anticipated use of 
2204        * this API, but it occurs in practice.) */
2205       if (n_poll < n_fds)
2206         {
2207           fds[n_poll].fd = pollrec->fd->fd;
2208           /* In direct contradiction to the Unix98 spec, IRIX runs into
2209            * difficulty if you pass in POLLERR, POLLHUP or POLLNVAL
2210            * flags in the events field of the pollfd while it should
2211            * just ignoring them. So we mask them out here.
2212            */
2213           fds[n_poll].events = pollrec->fd->events & ~(G_IO_ERR|G_IO_HUP|G_IO_NVAL);
2214           fds[n_poll].revents = 0;
2215         }
2216
2217       pollrec = pollrec->next;
2218       n_poll++;
2219     }
2220
2221 #ifdef G_THREADS_ENABLED
2222   context->poll_changed = FALSE;
2223 #endif
2224   
2225   if (timeout)
2226     {
2227       *timeout = context->timeout;
2228       if (*timeout != 0)
2229         context->time_is_current = FALSE;
2230     }
2231   
2232   UNLOCK_CONTEXT (context);
2233
2234   return n_poll;
2235 }
2236
2237 /**
2238  * g_main_context_check:
2239  * @context: a #GMainContext
2240  * @max_priority: the maximum numerical priority of sources to check
2241  * @fds: array of #GPollFD's that was passed to the last call to
2242  *       g_main_context_query()
2243  * @n_fds: return value of g_main_context_query()
2244  * 
2245  * Passes the results of polling back to the main loop.
2246  * 
2247  * Return value: %TRUE if some sources are ready to be dispatched.
2248  **/
2249 gboolean
2250 g_main_context_check (GMainContext *context,
2251                       gint          max_priority,
2252                       GPollFD      *fds,
2253                       gint          n_fds)
2254 {
2255   GSource *source;
2256   GPollRec *pollrec;
2257   gint n_ready = 0;
2258   gint i;
2259    
2260   LOCK_CONTEXT (context);
2261
2262   if (context->in_check_or_prepare)
2263     {
2264       g_warning ("g_main_context_check() called recursively from within a source's check() or "
2265                  "prepare() member.");
2266       UNLOCK_CONTEXT (context);
2267       return FALSE;
2268     }
2269   
2270 #ifdef G_THREADS_ENABLED
2271   if (!context->poll_waiting)
2272     {
2273 #ifndef G_OS_WIN32
2274       gchar a;
2275       read (context->wake_up_pipe[0], &a, 1);
2276 #endif
2277     }
2278   else
2279     context->poll_waiting = FALSE;
2280
2281   /* If the set of poll file descriptors changed, bail out
2282    * and let the main loop rerun
2283    */
2284   if (context->poll_changed)
2285     {
2286       UNLOCK_CONTEXT (context);
2287       return FALSE;
2288     }
2289 #endif /* G_THREADS_ENABLED */
2290   
2291   pollrec = context->poll_records;
2292   i = 0;
2293   while (i < n_fds)
2294     {
2295       if (pollrec->fd->events)
2296         pollrec->fd->revents = fds[i].revents;
2297
2298       pollrec = pollrec->next;
2299       i++;
2300     }
2301
2302   source = next_valid_source (context, NULL);
2303   while (source)
2304     {
2305       if ((n_ready > 0) && (source->priority > max_priority))
2306         {
2307           SOURCE_UNREF (source, context);
2308           break;
2309         }
2310       if (SOURCE_BLOCKED (source))
2311         goto next;
2312
2313       if (!(source->flags & G_SOURCE_READY))
2314         {
2315           gboolean result;
2316           gboolean (*check) (GSource  *source);
2317
2318           check = source->source_funcs->check;
2319           
2320           context->in_check_or_prepare++;
2321           UNLOCK_CONTEXT (context);
2322           
2323           result = (*check) (source);
2324           
2325           LOCK_CONTEXT (context);
2326           context->in_check_or_prepare--;
2327           
2328           if (result)
2329             source->flags |= G_SOURCE_READY;
2330         }
2331
2332       if (source->flags & G_SOURCE_READY)
2333         {
2334           source->ref_count++;
2335           g_ptr_array_add (context->pending_dispatches, source);
2336
2337           n_ready++;
2338
2339           /* never dispatch sources with less priority than the first
2340            * one we choose to dispatch
2341            */
2342           max_priority = source->priority;
2343         }
2344
2345     next:
2346       source = next_valid_source (context, source);
2347     }
2348
2349   UNLOCK_CONTEXT (context);
2350
2351   return n_ready > 0;
2352 }
2353
2354 /**
2355  * g_main_context_dispatch:
2356  * @context: a #GMainContext
2357  * 
2358  * Dispatches all pending sources.
2359  **/
2360 void
2361 g_main_context_dispatch (GMainContext *context)
2362 {
2363   LOCK_CONTEXT (context);
2364
2365   if (context->pending_dispatches->len > 0)
2366     {
2367       g_main_dispatch (context);
2368     }
2369
2370   UNLOCK_CONTEXT (context);
2371 }
2372
2373 /* HOLDS context lock */
2374 static gboolean
2375 g_main_context_iterate (GMainContext *context,
2376                         gboolean      block,
2377                         gboolean      dispatch,
2378                         GThread      *self)
2379 {
2380   gint max_priority;
2381   gint timeout;
2382   gboolean some_ready;
2383   gint nfds, allocated_nfds;
2384   GPollFD *fds = NULL;
2385   
2386   UNLOCK_CONTEXT (context);
2387
2388 #ifdef G_THREADS_ENABLED
2389   if (!g_main_context_acquire (context))
2390     {
2391       gboolean got_ownership;
2392       
2393       g_return_val_if_fail (g_thread_supported (), FALSE);
2394
2395       if (!block)
2396         return FALSE;
2397
2398       LOCK_CONTEXT (context);
2399       
2400       if (!context->cond)
2401         context->cond = g_cond_new ();
2402           
2403       got_ownership = g_main_context_wait (context,
2404                                            context->cond,
2405                                            g_static_mutex_get_mutex (&context->mutex));
2406
2407       if (!got_ownership)
2408         {
2409           UNLOCK_CONTEXT (context);
2410           return FALSE;
2411         }
2412     }
2413   else
2414     LOCK_CONTEXT (context);
2415 #endif /* G_THREADS_ENABLED */
2416   
2417   if (!context->cached_poll_array)
2418     {
2419       context->cached_poll_array_size = context->n_poll_records;
2420       context->cached_poll_array = g_new (GPollFD, context->n_poll_records);
2421     }
2422
2423   allocated_nfds = context->cached_poll_array_size;
2424   fds = context->cached_poll_array;
2425   
2426   UNLOCK_CONTEXT (context);
2427
2428   g_main_context_prepare (context, &max_priority); 
2429   
2430   while ((nfds = g_main_context_query (context, max_priority, &timeout, fds, 
2431                                        allocated_nfds)) > allocated_nfds)
2432     {
2433       LOCK_CONTEXT (context);
2434       g_free (fds);
2435       context->cached_poll_array_size = allocated_nfds = nfds;
2436       context->cached_poll_array = fds = g_new (GPollFD, nfds);
2437       UNLOCK_CONTEXT (context);
2438     }
2439
2440   if (!block)
2441     timeout = 0;
2442   
2443   g_main_context_poll (context, timeout, max_priority, fds, nfds);
2444   
2445   some_ready = g_main_context_check (context, max_priority, fds, nfds);
2446   
2447   if (dispatch)
2448     g_main_context_dispatch (context);
2449   
2450 #ifdef G_THREADS_ENABLED
2451   g_main_context_release (context);
2452 #endif /* G_THREADS_ENABLED */    
2453
2454   LOCK_CONTEXT (context);
2455
2456   return some_ready;
2457 }
2458
2459 /**
2460  * g_main_context_pending:
2461  * @context: a #GMainContext (if %NULL, the default context will be used)
2462  *
2463  * Checks if any sources have pending events for the given context.
2464  * 
2465  * Return value: %TRUE if events are pending.
2466  **/
2467 gboolean 
2468 g_main_context_pending (GMainContext *context)
2469 {
2470   gboolean retval;
2471
2472   if (!context)
2473     context = g_main_context_default();
2474
2475   LOCK_CONTEXT (context);
2476   retval = g_main_context_iterate (context, FALSE, FALSE, G_THREAD_SELF);
2477   UNLOCK_CONTEXT (context);
2478   
2479   return retval;
2480 }
2481
2482 /**
2483  * g_main_context_iteration:
2484  * @context: a #GMainContext (if %NULL, the default context will be used) 
2485  * @may_block: whether the call may block.
2486  * 
2487  * Runs a single iteration for the given main loop. This involves
2488  * checking to see if any event sources are ready to be processed,
2489  * then if no events sources are ready and @may_block is %TRUE, waiting
2490  * for a source to become ready, then dispatching the highest priority
2491  * events sources that are ready. Otherwise, if @may_block is %FALSE 
2492  * sources are not waited to become ready, only those highest priority 
2493  * events sources will be dispatched (if any), that are ready at this 
2494  * given moment without further waiting.
2495  *
2496  * Note that even when @may_block is %TRUE, it is still possible for 
2497  * g_main_context_iteration() to return %FALSE, since the the wait may 
2498  * be interrupted for other reasons than an event source becoming ready.
2499  * 
2500  * Return value: %TRUE if events were dispatched.
2501  **/
2502 gboolean
2503 g_main_context_iteration (GMainContext *context, gboolean may_block)
2504 {
2505   gboolean retval;
2506
2507   if (!context)
2508     context = g_main_context_default();
2509   
2510   LOCK_CONTEXT (context);
2511   retval = g_main_context_iterate (context, may_block, TRUE, G_THREAD_SELF);
2512   UNLOCK_CONTEXT (context);
2513   
2514   return retval;
2515 }
2516
2517 /**
2518  * g_main_loop_new:
2519  * @context: a #GMainContext  (if %NULL, the default context will be used).
2520  * @is_running: set to %TRUE to indicate that the loop is running. This
2521  * is not very important since calling g_main_loop_run() will set this to
2522  * %TRUE anyway.
2523  * 
2524  * Creates a new #GMainLoop structure.
2525  * 
2526  * Return value: a new #GMainLoop.
2527  **/
2528 GMainLoop *
2529 g_main_loop_new (GMainContext *context,
2530                  gboolean      is_running)
2531 {
2532   GMainLoop *loop;
2533
2534   if (!context)
2535     context = g_main_context_default();
2536   
2537   g_main_context_ref (context);
2538
2539   loop = g_new0 (GMainLoop, 1);
2540   loop->context = context;
2541   loop->is_running = is_running != FALSE;
2542   loop->ref_count = 1;
2543   
2544   return loop;
2545 }
2546
2547 /**
2548  * g_main_loop_ref:
2549  * @loop: a #GMainLoop
2550  * 
2551  * Increases the reference count on a #GMainLoop object by one.
2552  * 
2553  * Return value: @loop
2554  **/
2555 GMainLoop *
2556 g_main_loop_ref (GMainLoop *loop)
2557 {
2558   g_return_val_if_fail (loop != NULL, NULL);
2559   g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2560
2561   g_atomic_int_inc (&loop->ref_count);
2562
2563   return loop;
2564 }
2565
2566 /**
2567  * g_main_loop_unref:
2568  * @loop: a #GMainLoop
2569  * 
2570  * Decreases the reference count on a #GMainLoop object by one. If
2571  * the result is zero, free the loop and free all associated memory.
2572  **/
2573 void
2574 g_main_loop_unref (GMainLoop *loop)
2575 {
2576   g_return_if_fail (loop != NULL);
2577   g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2578
2579   if (!g_atomic_int_dec_and_test (&loop->ref_count))
2580     return;
2581
2582   g_main_context_unref (loop->context);
2583   g_free (loop);
2584 }
2585
2586 /**
2587  * g_main_loop_run:
2588  * @loop: a #GMainLoop
2589  * 
2590  * Runs a main loop until g_main_loop_quit() is called on the loop.
2591  * If this is called for the thread of the loop's #GMainContext,
2592  * it will process events from the loop, otherwise it will
2593  * simply wait.
2594  **/
2595 void 
2596 g_main_loop_run (GMainLoop *loop)
2597 {
2598   GThread *self = G_THREAD_SELF;
2599
2600   g_return_if_fail (loop != NULL);
2601   g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2602
2603 #ifdef G_THREADS_ENABLED
2604   if (!g_main_context_acquire (loop->context))
2605     {
2606       gboolean got_ownership = FALSE;
2607       
2608       /* Another thread owns this context */
2609       if (!g_thread_supported ())
2610         {
2611           g_warning ("g_main_loop_run() was called from second thread but "
2612                      "g_thread_init() was never called.");
2613           return;
2614         }
2615       
2616       LOCK_CONTEXT (loop->context);
2617
2618       g_atomic_int_inc (&loop->ref_count);
2619
2620       if (!loop->is_running)
2621         loop->is_running = TRUE;
2622
2623       if (!loop->context->cond)
2624         loop->context->cond = g_cond_new ();
2625           
2626       while (loop->is_running && !got_ownership)
2627         got_ownership = g_main_context_wait (loop->context,
2628                                              loop->context->cond,
2629                                              g_static_mutex_get_mutex (&loop->context->mutex));
2630       
2631       if (!loop->is_running)
2632         {
2633           UNLOCK_CONTEXT (loop->context);
2634           if (got_ownership)
2635             g_main_context_release (loop->context);
2636           g_main_loop_unref (loop);
2637           return;
2638         }
2639
2640       g_assert (got_ownership);
2641     }
2642   else
2643     LOCK_CONTEXT (loop->context);
2644 #endif /* G_THREADS_ENABLED */ 
2645
2646   if (loop->context->in_check_or_prepare)
2647     {
2648       g_warning ("g_main_loop_run(): called recursively from within a source's "
2649                  "check() or prepare() member, iteration not possible.");
2650       return;
2651     }
2652
2653   g_atomic_int_inc (&loop->ref_count);
2654   loop->is_running = TRUE;
2655   while (loop->is_running)
2656     g_main_context_iterate (loop->context, TRUE, TRUE, self);
2657
2658   UNLOCK_CONTEXT (loop->context);
2659   
2660 #ifdef G_THREADS_ENABLED
2661   g_main_context_release (loop->context);
2662 #endif /* G_THREADS_ENABLED */    
2663   
2664   g_main_loop_unref (loop);
2665 }
2666
2667 /**
2668  * g_main_loop_quit:
2669  * @loop: a #GMainLoop
2670  * 
2671  * Stops a #GMainLoop from running. Any calls to g_main_loop_run()
2672  * for the loop will return. 
2673  *
2674  * Note that sources that have already been dispatched when 
2675  * g_main_loop_quit() is called will still be executed.
2676  **/
2677 void 
2678 g_main_loop_quit (GMainLoop *loop)
2679 {
2680   g_return_if_fail (loop != NULL);
2681   g_return_if_fail (g_atomic_int_get (&loop->ref_count) > 0);
2682
2683   LOCK_CONTEXT (loop->context);
2684   loop->is_running = FALSE;
2685   g_main_context_wakeup_unlocked (loop->context);
2686
2687 #ifdef G_THREADS_ENABLED
2688   if (loop->context->cond)
2689     g_cond_broadcast (loop->context->cond);
2690 #endif /* G_THREADS_ENABLED */
2691
2692   UNLOCK_CONTEXT (loop->context);
2693 }
2694
2695 /**
2696  * g_main_loop_is_running:
2697  * @loop: a #GMainLoop.
2698  * 
2699  * Checks to see if the main loop is currently being run via g_main_loop_run().
2700  * 
2701  * Return value: %TRUE if the mainloop is currently being run.
2702  **/
2703 gboolean
2704 g_main_loop_is_running (GMainLoop *loop)
2705 {
2706   g_return_val_if_fail (loop != NULL, FALSE);
2707   g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, FALSE);
2708
2709   return loop->is_running;
2710 }
2711
2712 /**
2713  * g_main_loop_get_context:
2714  * @loop: a #GMainLoop.
2715  * 
2716  * Returns the #GMainContext of @loop.
2717  * 
2718  * Return value: the #GMainContext of @loop
2719  **/
2720 GMainContext *
2721 g_main_loop_get_context (GMainLoop *loop)
2722 {
2723   g_return_val_if_fail (loop != NULL, NULL);
2724   g_return_val_if_fail (g_atomic_int_get (&loop->ref_count) > 0, NULL);
2725  
2726   return loop->context;
2727 }
2728
2729 /* HOLDS: context's lock */
2730 static void
2731 g_main_context_poll (GMainContext *context,
2732                      gint          timeout,
2733                      gint          priority,
2734                      GPollFD      *fds,
2735                      gint          n_fds)
2736 {
2737 #ifdef  G_MAIN_POLL_DEBUG
2738   GTimer *poll_timer;
2739   GPollRec *pollrec;
2740   gint i;
2741 #endif
2742
2743   GPollFunc poll_func;
2744
2745   if (n_fds || timeout != 0)
2746     {
2747 #ifdef  G_MAIN_POLL_DEBUG
2748       if (_g_main_poll_debug)
2749         {
2750           g_print ("polling context=%p n=%d timeout=%d\n",
2751                    context, n_fds, timeout);
2752           poll_timer = g_timer_new ();
2753         }
2754 #endif
2755
2756       LOCK_CONTEXT (context);
2757
2758       poll_func = context->poll_func;
2759       
2760       UNLOCK_CONTEXT (context);
2761       if ((*poll_func) (fds, n_fds, timeout) < 0 && errno != EINTR)
2762         {
2763 #ifndef G_OS_WIN32
2764           g_warning ("poll(2) failed due to: %s.",
2765                      g_strerror (errno));
2766 #else
2767           /* If g_poll () returns -1, it has already called g_warning() */
2768 #endif
2769         }
2770       
2771 #ifdef  G_MAIN_POLL_DEBUG
2772       if (_g_main_poll_debug)
2773         {
2774           LOCK_CONTEXT (context);
2775
2776           g_print ("g_main_poll(%d) timeout: %d - elapsed %12.10f seconds",
2777                    n_fds,
2778                    timeout,
2779                    g_timer_elapsed (poll_timer, NULL));
2780           g_timer_destroy (poll_timer);
2781           pollrec = context->poll_records;
2782
2783           while (pollrec != NULL)
2784             {
2785               i = 0;
2786               while (i < n_fds)
2787                 {
2788                   if (fds[i].fd == pollrec->fd->fd &&
2789                       pollrec->fd->events &&
2790                       fds[i].revents)
2791                     {
2792                       g_print (" [" G_POLLFD_FORMAT " :", fds[i].fd);
2793                       if (fds[i].revents & G_IO_IN)
2794                         g_print ("i");
2795                       if (fds[i].revents & G_IO_OUT)
2796                         g_print ("o");
2797                       if (fds[i].revents & G_IO_PRI)
2798                         g_print ("p");
2799                       if (fds[i].revents & G_IO_ERR)
2800                         g_print ("e");
2801                       if (fds[i].revents & G_IO_HUP)
2802                         g_print ("h");
2803                       if (fds[i].revents & G_IO_NVAL)
2804                         g_print ("n");
2805                       g_print ("]");
2806                     }
2807                   i++;
2808                 }
2809               pollrec = pollrec->next;
2810             }
2811           g_print ("\n");
2812
2813           UNLOCK_CONTEXT (context);
2814         }
2815 #endif
2816     } /* if (n_fds || timeout != 0) */
2817 }
2818
2819 /**
2820  * g_main_context_add_poll:
2821  * @context: a #GMainContext (or %NULL for the default context)
2822  * @fd: a #GPollFD structure holding information about a file
2823  *      descriptor to watch.
2824  * @priority: the priority for this file descriptor which should be
2825  *      the same as the priority used for g_source_attach() to ensure that the
2826  *      file descriptor is polled whenever the results may be needed.
2827  * 
2828  * Adds a file descriptor to the set of file descriptors polled for
2829  * this context. This will very seldomly be used directly. Instead
2830  * a typical event source will use g_source_add_poll() instead.
2831  **/
2832 void
2833 g_main_context_add_poll (GMainContext *context,
2834                          GPollFD      *fd,
2835                          gint          priority)
2836 {
2837   if (!context)
2838     context = g_main_context_default ();
2839   
2840   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
2841   g_return_if_fail (fd);
2842
2843   LOCK_CONTEXT (context);
2844   g_main_context_add_poll_unlocked (context, priority, fd);
2845   UNLOCK_CONTEXT (context);
2846 }
2847
2848 /* HOLDS: main_loop_lock */
2849 static void 
2850 g_main_context_add_poll_unlocked (GMainContext *context,
2851                                   gint          priority,
2852                                   GPollFD      *fd)
2853 {
2854   GPollRec *lastrec, *pollrec;
2855   GPollRec *newrec = g_slice_new (GPollRec);
2856
2857   /* This file descriptor may be checked before we ever poll */
2858   fd->revents = 0;
2859   newrec->fd = fd;
2860   newrec->priority = priority;
2861
2862   lastrec = NULL;
2863   pollrec = context->poll_records;
2864   while (pollrec && priority >= pollrec->priority)
2865     {
2866       lastrec = pollrec;
2867       pollrec = pollrec->next;
2868     }
2869   
2870   if (lastrec)
2871     lastrec->next = newrec;
2872   else
2873     context->poll_records = newrec;
2874
2875   newrec->next = pollrec;
2876
2877   context->n_poll_records++;
2878
2879 #ifdef G_THREADS_ENABLED
2880   context->poll_changed = TRUE;
2881
2882   /* Now wake up the main loop if it is waiting in the poll() */
2883   g_main_context_wakeup_unlocked (context);
2884 #endif
2885 }
2886
2887 /**
2888  * g_main_context_remove_poll:
2889  * @context:a #GMainContext 
2890  * @fd: a #GPollFD descriptor previously added with g_main_context_add_poll()
2891  * 
2892  * Removes file descriptor from the set of file descriptors to be
2893  * polled for a particular context.
2894  **/
2895 void
2896 g_main_context_remove_poll (GMainContext *context,
2897                             GPollFD      *fd)
2898 {
2899   if (!context)
2900     context = g_main_context_default ();
2901   
2902   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
2903   g_return_if_fail (fd);
2904
2905   LOCK_CONTEXT (context);
2906   g_main_context_remove_poll_unlocked (context, fd);
2907   UNLOCK_CONTEXT (context);
2908 }
2909
2910 static void
2911 g_main_context_remove_poll_unlocked (GMainContext *context,
2912                                      GPollFD      *fd)
2913 {
2914   GPollRec *pollrec, *lastrec;
2915
2916   lastrec = NULL;
2917   pollrec = context->poll_records;
2918
2919   while (pollrec)
2920     {
2921       if (pollrec->fd == fd)
2922         {
2923           if (lastrec != NULL)
2924             lastrec->next = pollrec->next;
2925           else
2926             context->poll_records = pollrec->next;
2927
2928           g_slice_free (GPollRec, pollrec);
2929
2930           context->n_poll_records--;
2931           break;
2932         }
2933       lastrec = pollrec;
2934       pollrec = pollrec->next;
2935     }
2936
2937 #ifdef G_THREADS_ENABLED
2938   context->poll_changed = TRUE;
2939   
2940   /* Now wake up the main loop if it is waiting in the poll() */
2941   g_main_context_wakeup_unlocked (context);
2942 #endif
2943 }
2944
2945 /**
2946  * g_source_get_current_time:
2947  * @source:  a #GSource
2948  * @timeval: #GTimeVal structure in which to store current time.
2949  * 
2950  * Gets the "current time" to be used when checking 
2951  * this source. The advantage of calling this function over
2952  * calling g_get_current_time() directly is that when 
2953  * checking multiple sources, GLib can cache a single value
2954  * instead of having to repeatedly get the system time.
2955  **/
2956 void
2957 g_source_get_current_time (GSource  *source,
2958                            GTimeVal *timeval)
2959 {
2960   GMainContext *context;
2961   
2962   g_return_if_fail (source->context != NULL);
2963  
2964   context = source->context;
2965
2966   LOCK_CONTEXT (context);
2967
2968   if (!context->time_is_current)
2969     {
2970       g_get_current_time (&context->current_time);
2971       context->time_is_current = TRUE;
2972     }
2973   
2974   *timeval = context->current_time;
2975   
2976   UNLOCK_CONTEXT (context);
2977 }
2978
2979 /**
2980  * g_main_context_set_poll_func:
2981  * @context: a #GMainContext
2982  * @func: the function to call to poll all file descriptors
2983  * 
2984  * Sets the function to use to handle polling of file descriptors. It
2985  * will be used instead of the poll() system call 
2986  * (or GLib's replacement function, which is used where 
2987  * poll() isn't available).
2988  *
2989  * This function could possibly be used to integrate the GLib event
2990  * loop with an external event loop.
2991  **/
2992 void
2993 g_main_context_set_poll_func (GMainContext *context,
2994                               GPollFunc     func)
2995 {
2996   if (!context)
2997     context = g_main_context_default ();
2998   
2999   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3000
3001   LOCK_CONTEXT (context);
3002   
3003   if (func)
3004     context->poll_func = func;
3005   else
3006     context->poll_func = g_poll;
3007
3008   UNLOCK_CONTEXT (context);
3009 }
3010
3011 /**
3012  * g_main_context_get_poll_func:
3013  * @context: a #GMainContext
3014  * 
3015  * Gets the poll function set by g_main_context_set_poll_func().
3016  * 
3017  * Return value: the poll function
3018  **/
3019 GPollFunc
3020 g_main_context_get_poll_func (GMainContext *context)
3021 {
3022   GPollFunc result;
3023   
3024   if (!context)
3025     context = g_main_context_default ();
3026   
3027   g_return_val_if_fail (g_atomic_int_get (&context->ref_count) > 0, NULL);
3028
3029   LOCK_CONTEXT (context);
3030   result = context->poll_func;
3031   UNLOCK_CONTEXT (context);
3032
3033   return result;
3034 }
3035
3036 /* HOLDS: context's lock */
3037 /* Wake the main loop up from a poll() */
3038 static void
3039 g_main_context_wakeup_unlocked (GMainContext *context)
3040 {
3041 #ifdef G_THREADS_ENABLED
3042   if (g_thread_supported() && context->poll_waiting)
3043     {
3044       context->poll_waiting = FALSE;
3045 #ifndef G_OS_WIN32
3046       write (context->wake_up_pipe[1], "A", 1);
3047 #else
3048       ReleaseSemaphore (context->wake_up_semaphore, 1, NULL);
3049 #endif
3050     }
3051 #endif
3052 }
3053
3054 /**
3055  * g_main_context_wakeup:
3056  * @context: a #GMainContext
3057  * 
3058  * If @context is currently waiting in a poll(), interrupt
3059  * the poll(), and continue the iteration process.
3060  **/
3061 void
3062 g_main_context_wakeup (GMainContext *context)
3063 {
3064   if (!context)
3065     context = g_main_context_default ();
3066   
3067   g_return_if_fail (g_atomic_int_get (&context->ref_count) > 0);
3068
3069   LOCK_CONTEXT (context);
3070   g_main_context_wakeup_unlocked (context);
3071   UNLOCK_CONTEXT (context);
3072 }
3073
3074 /**
3075  * g_main_context_is_owner:
3076  * @context: a #GMainContext
3077  * 
3078  * Determines whether this thread holds the (recursive)
3079  * ownership of this #GMaincontext. This is useful to
3080  * know before waiting on another thread that may be
3081  * blocking to get ownership of @context.
3082  *
3083  * Returns: %TRUE if current thread is owner of @context.
3084  *
3085  * Since: 2.10
3086  **/
3087 gboolean
3088 g_main_context_is_owner (GMainContext *context)
3089 {
3090   gboolean is_owner;
3091
3092   if (!context)
3093     context = g_main_context_default ();
3094
3095 #ifdef G_THREADS_ENABLED
3096   LOCK_CONTEXT (context);
3097   is_owner = context->owner == G_THREAD_SELF;
3098   UNLOCK_CONTEXT (context);
3099 #else
3100   is_owner = TRUE;
3101 #endif
3102
3103   return is_owner;
3104 }
3105
3106 /* Timeouts */
3107
3108 static void
3109 g_timeout_set_expiration (GTimeoutSource *timeout_source,
3110                           GTimeVal       *current_time)
3111 {
3112   guint seconds = timeout_source->interval / 1000;
3113   guint msecs = timeout_source->interval - seconds * 1000;
3114
3115   timeout_source->expiration.tv_sec = current_time->tv_sec + seconds;
3116   timeout_source->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
3117   if (timeout_source->expiration.tv_usec >= 1000000)
3118     {
3119       timeout_source->expiration.tv_usec -= 1000000;
3120       timeout_source->expiration.tv_sec++;
3121     }
3122   if (timer_perturb==-1)
3123     {
3124       /*
3125        * we want a per machine/session unique 'random' value; try the dbus
3126        * address first, that has a UUID in it. If there is no dbus, use the
3127        * hostname for hashing.
3128        */
3129       const char *session_bus_address = g_getenv ("DBUS_SESSION_BUS_ADDRESS");
3130       if (!session_bus_address)
3131         session_bus_address = g_getenv ("HOSTNAME");
3132       if (session_bus_address)
3133         timer_perturb = ABS ((gint) g_str_hash (session_bus_address));
3134       else
3135         timer_perturb = 0;
3136     }
3137   if (timeout_source->granularity)
3138     {
3139       gint remainder;
3140       gint gran; /* in usecs */
3141       gint perturb;
3142
3143       gran = timeout_source->granularity * 1000;
3144       perturb = timer_perturb % gran;
3145       /*
3146        * We want to give each machine a per machine pertubation;
3147        * shift time back first, and forward later after the rounding
3148        */
3149
3150       timeout_source->expiration.tv_usec -= perturb;
3151       if (timeout_source->expiration.tv_usec < 0)
3152         {
3153           timeout_source->expiration.tv_usec += 1000000;
3154           timeout_source->expiration.tv_sec--;
3155         }
3156
3157       remainder = timeout_source->expiration.tv_usec % gran;
3158       if (remainder >= gran/4) /* round up */
3159         timeout_source->expiration.tv_usec += gran;
3160       timeout_source->expiration.tv_usec -= remainder;
3161       /* shift back */
3162       timeout_source->expiration.tv_usec += perturb;
3163
3164       /* the rounding may have overflown tv_usec */
3165       while (timeout_source->expiration.tv_usec > 1000000)
3166         {
3167           timeout_source->expiration.tv_usec -= 1000000;
3168           timeout_source->expiration.tv_sec++;
3169         }
3170     }
3171 }
3172
3173 static gboolean
3174 g_timeout_prepare (GSource *source,
3175                    gint    *timeout)
3176 {
3177   glong sec;
3178   glong msec;
3179   GTimeVal current_time;
3180   
3181   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3182
3183   g_source_get_current_time (source, &current_time);
3184
3185   sec = timeout_source->expiration.tv_sec - current_time.tv_sec;
3186   msec = (timeout_source->expiration.tv_usec - current_time.tv_usec) / 1000;
3187
3188   /* We do the following in a rather convoluted fashion to deal with
3189    * the fact that we don't have an integral type big enough to hold
3190    * the difference of two timevals in millseconds.
3191    */
3192   if (sec < 0 || (sec == 0 && msec < 0))
3193     msec = 0;
3194   else
3195     {
3196       glong interval_sec = timeout_source->interval / 1000;
3197       glong interval_msec = timeout_source->interval % 1000;
3198
3199       if (msec < 0)
3200         {
3201           msec += 1000;
3202           sec -= 1;
3203         }
3204       
3205       if (sec > interval_sec ||
3206           (sec == interval_sec && msec > interval_msec))
3207         {
3208           /* The system time has been set backwards, so we
3209            * reset the expiration time to now + timeout_source->interval;
3210            * this at least avoids hanging for long periods of time.
3211            */
3212           g_timeout_set_expiration (timeout_source, &current_time);
3213           msec = MIN (G_MAXINT, timeout_source->interval);
3214         }
3215       else
3216         {
3217           msec = MIN (G_MAXINT, (guint)msec + 1000 * (guint)sec);
3218         }
3219     }
3220
3221   *timeout = (gint)msec;
3222   
3223   return msec == 0;
3224 }
3225
3226 static gboolean 
3227 g_timeout_check (GSource *source)
3228 {
3229   GTimeVal current_time;
3230   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3231
3232   g_source_get_current_time (source, &current_time);
3233   
3234   return ((timeout_source->expiration.tv_sec < current_time.tv_sec) ||
3235           ((timeout_source->expiration.tv_sec == current_time.tv_sec) &&
3236            (timeout_source->expiration.tv_usec <= current_time.tv_usec)));
3237 }
3238
3239 static gboolean
3240 g_timeout_dispatch (GSource     *source,
3241                     GSourceFunc  callback,
3242                     gpointer     user_data)
3243 {
3244   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3245
3246   if (!callback)
3247     {
3248       g_warning ("Timeout source dispatched without callback\n"
3249                  "You must call g_source_set_callback().");
3250       return FALSE;
3251     }
3252  
3253   if (callback (user_data))
3254     {
3255       GTimeVal current_time;
3256
3257       g_source_get_current_time (source, &current_time);
3258       g_timeout_set_expiration (timeout_source, &current_time);
3259
3260       return TRUE;
3261     }
3262   else
3263     return FALSE;
3264 }
3265
3266 /**
3267  * g_timeout_source_new:
3268  * @interval: the timeout interval in milliseconds.
3269  * 
3270  * Creates a new timeout source.
3271  *
3272  * The source will not initially be associated with any #GMainContext
3273  * and must be added to one with g_source_attach() before it will be
3274  * executed.
3275  * 
3276  * Return value: the newly-created timeout source
3277  **/
3278 GSource *
3279 g_timeout_source_new (guint interval)
3280 {
3281   GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3282   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3283   GTimeVal current_time;
3284
3285   timeout_source->interval = interval;
3286
3287   g_get_current_time (&current_time);
3288   g_timeout_set_expiration (timeout_source, &current_time);
3289   
3290   return source;
3291 }
3292
3293 /**
3294  * g_timeout_source_new_seconds:
3295  * @interval: the timeout interval in seconds
3296  *
3297  * Creates a new timeout source.
3298  *
3299  * The source will not initially be associated with any #GMainContext
3300  * and must be added to one with g_source_attach() before it will be
3301  * executed.
3302  *
3303  * The scheduling granularity/accuracy of this timeout source will be
3304  * in seconds.
3305  *
3306  * Return value: the newly-created timeout source
3307  *
3308  * Since: 2.14  
3309  **/
3310 GSource *
3311 g_timeout_source_new_seconds (guint interval)
3312 {
3313   GSource *source = g_source_new (&g_timeout_funcs, sizeof (GTimeoutSource));
3314   GTimeoutSource *timeout_source = (GTimeoutSource *)source;
3315   GTimeVal current_time;
3316
3317   timeout_source->interval = 1000*interval;
3318   timeout_source->granularity = 1000;
3319
3320   g_get_current_time (&current_time);
3321   g_timeout_set_expiration (timeout_source, &current_time);
3322
3323   return source;
3324 }
3325
3326
3327 /**
3328  * g_timeout_add_full:
3329  * @priority: the priority of the timeout source. Typically this will be in
3330  *            the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3331  * @interval: the time between calls to the function, in milliseconds
3332  *             (1/1000ths of a second)
3333  * @function: function to call
3334  * @data:     data to pass to @function
3335  * @notify:   function to call when the timeout is removed, or %NULL
3336  * 
3337  * Sets a function to be called at regular intervals, with the given
3338  * priority.  The function is called repeatedly until it returns
3339  * %FALSE, at which point the timeout is automatically destroyed and
3340  * the function will not be called again.  The @notify function is
3341  * called when the timeout is destroyed.  The first call to the
3342  * function will be at the end of the first @interval.
3343  *
3344  * Note that timeout functions may be delayed, due to the processing of other
3345  * event sources. Thus they should not be relied on for precise timing.
3346  * After each call to the timeout function, the time of the next
3347  * timeout is recalculated based on the current time and the given interval
3348  * (it does not try to 'catch up' time lost in delays).
3349  *
3350  * This internally creates a main loop source using g_timeout_source_new()
3351  * and attaches it to the main loop context using g_source_attach(). You can
3352  * do these steps manually if you need greater control.
3353  * 
3354  * Return value: the ID (greater than 0) of the event source.
3355  **/
3356 guint
3357 g_timeout_add_full (gint           priority,
3358                     guint          interval,
3359                     GSourceFunc    function,
3360                     gpointer       data,
3361                     GDestroyNotify notify)
3362 {
3363   GSource *source;
3364   guint id;
3365   
3366   g_return_val_if_fail (function != NULL, 0);
3367
3368   source = g_timeout_source_new (interval);
3369
3370   if (priority != G_PRIORITY_DEFAULT)
3371     g_source_set_priority (source, priority);
3372
3373   g_source_set_callback (source, function, data, notify);
3374   id = g_source_attach (source, NULL);
3375   g_source_unref (source);
3376
3377   return id;
3378 }
3379
3380 /**
3381  * g_timeout_add:
3382  * @interval: the time between calls to the function, in milliseconds
3383  *             (1/1000ths of a second)
3384  * @function: function to call
3385  * @data:     data to pass to @function
3386  * 
3387  * Sets a function to be called at regular intervals, with the default
3388  * priority, #G_PRIORITY_DEFAULT.  The function is called repeatedly
3389  * until it returns %FALSE, at which point the timeout is automatically
3390  * destroyed and the function will not be called again.  The first call
3391  * to the function will be at the end of the first @interval.
3392  *
3393  * Note that timeout functions may be delayed, due to the processing of other
3394  * event sources. Thus they should not be relied on for precise timing.
3395  * After each call to the timeout function, the time of the next
3396  * timeout is recalculated based on the current time and the given interval
3397  * (it does not try to 'catch up' time lost in delays).
3398  *
3399  * If you want to have a timer in the "seconds" range and do not care
3400  * about the exact time of the first call of the timer, use the
3401  * g_timeout_add_seconds() function; this function allows for more
3402  * optimizations and more efficient system power usage.
3403  *
3404  * This internally creates a main loop source using g_timeout_source_new()
3405  * and attaches it to the main loop context using g_source_attach(). You can
3406  * do these steps manually if you need greater control.
3407  * 
3408  * Return value: the ID (greater than 0) of the event source.
3409  **/
3410 guint
3411 g_timeout_add (guint32        interval,
3412                GSourceFunc    function,
3413                gpointer       data)
3414 {
3415   return g_timeout_add_full (G_PRIORITY_DEFAULT, 
3416                              interval, function, data, NULL);
3417 }
3418
3419 /**
3420  * g_timeout_add_seconds_full:
3421  * @priority: the priority of the timeout source. Typically this will be in
3422  *            the range between #G_PRIORITY_DEFAULT and #G_PRIORITY_HIGH.
3423  * @interval: the time between calls to the function, in seconds
3424  * @function: function to call
3425  * @data:     data to pass to @function
3426  * @notify:   function to call when the timeout is removed, or %NULL
3427  *
3428  * Sets a function to be called at regular intervals, with @priority.
3429  * The function is called repeatedly until it returns %FALSE, at which
3430  * point the timeout is automatically destroyed and the function will
3431  * not be called again.
3432  *
3433  * Unlike g_timeout_add(), this function operates at whole second granularity.
3434  * The initial starting point of the timer is determined by the implementation
3435  * and the implementation is expected to group multiple timers together so that
3436  * they fire all at the same time.
3437  * To allow this grouping, the @interval to the first timer is rounded
3438  * and can deviate up to one second from the specified interval.
3439  * Subsequent timer iterations will generally run at the specified interval.
3440  *
3441  * Note that timeout functions may be delayed, due to the processing of other
3442  * event sources. Thus they should not be relied on for precise timing.
3443  * After each call to the timeout function, the time of the next
3444  * timeout is recalculated based on the current time and the given @interval
3445  *
3446  * If you want timing more precise than whole seconds, use g_timeout_add()
3447  * instead.
3448  *
3449  * The grouping of timers to fire at the same time results in a more power
3450  * and CPU efficient behavior so if your timer is in multiples of seconds
3451  * and you don't require the first timer exactly one second from now, the
3452  * use of g_timeout_add_seconds() is preferred over g_timeout_add().
3453  *
3454  * This internally creates a main loop source using 
3455  * g_timeout_source_new_seconds() and attaches it to the main loop context 
3456  * using g_source_attach(). You can do these steps manually if you need 
3457  * greater control.
3458  * 
3459  * Return value: the ID (greater than 0) of the event source.
3460  *
3461  * Since: 2.14
3462  **/
3463 guint
3464 g_timeout_add_seconds_full (gint           priority,
3465                             guint32        interval,
3466                             GSourceFunc    function,
3467                             gpointer       data,
3468                             GDestroyNotify notify)
3469 {
3470   GSource *source;
3471   guint id;
3472
3473   g_return_val_if_fail (function != NULL, 0);
3474
3475   source = g_timeout_source_new_seconds (interval);
3476
3477   if (priority != G_PRIORITY_DEFAULT)
3478     g_source_set_priority (source, priority);
3479
3480   g_source_set_callback (source, function, data, notify);
3481   id = g_source_attach (source, NULL);
3482   g_source_unref (source);
3483
3484   return id;
3485 }
3486
3487 /**
3488  * g_timeout_add_seconds:
3489  * @interval: the time between calls to the function, in seconds
3490  * @function: function to call
3491  * @data: data to pass to @function
3492  *
3493  * Sets a function to be called at regular intervals with the default
3494  * priority, #G_PRIORITY_DEFAULT. The function is called repeatedly until
3495  * it returns %FALSE, at which point the timeout is automatically destroyed
3496  * and the function will not be called again.
3497  *
3498  * This internally creates a main loop source using 
3499  * g_timeout_source_new_seconds() and attaches it to the main loop context 
3500  * using g_source_attach(). You can do these steps manually if you need 
3501  * greater control. Also see g_timout_add_seconds_full().
3502  * 
3503  * Return value: the ID (greater than 0) of the event source.
3504  *
3505  * Since: 2.14
3506  **/
3507 guint
3508 g_timeout_add_seconds (guint       interval,
3509                        GSourceFunc function,
3510                        gpointer    data)
3511 {
3512   g_return_val_if_fail (function != NULL, 0);
3513
3514   return g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, interval, function, data, NULL);
3515 }
3516
3517 /* Child watch functions */
3518
3519 #ifdef G_OS_WIN32
3520
3521 static gboolean
3522 g_child_watch_prepare (GSource *source,
3523                        gint    *timeout)
3524 {
3525   *timeout = -1;
3526   return FALSE;
3527 }
3528
3529
3530 static gboolean 
3531 g_child_watch_check (GSource  *source)
3532 {
3533   GChildWatchSource *child_watch_source;
3534   gboolean child_exited;
3535
3536   child_watch_source = (GChildWatchSource *) source;
3537
3538   child_exited = child_watch_source->poll.revents & G_IO_IN;
3539
3540   if (child_exited)
3541     {
3542       DWORD child_status;
3543
3544       /*
3545        * Note: We do _not_ check for the special value of STILL_ACTIVE
3546        * since we know that the process has exited and doing so runs into
3547        * problems if the child process "happens to return STILL_ACTIVE(259)"
3548        * as Microsoft's Platform SDK puts it.
3549        */
3550       if (!GetExitCodeProcess (child_watch_source->pid, &child_status))
3551         {
3552           gchar *emsg = g_win32_error_message (GetLastError ());
3553           g_warning (G_STRLOC ": GetExitCodeProcess() failed: %s", emsg);
3554           g_free (emsg);
3555
3556           child_watch_source->child_status = -1;
3557         }
3558       else
3559         child_watch_source->child_status = child_status;
3560     }
3561
3562   return child_exited;
3563 }
3564
3565 #else /* G_OS_WIN32 */
3566
3567 static gboolean
3568 check_for_child_exited (GSource *source)
3569 {
3570   GChildWatchSource *child_watch_source;
3571   gint count;
3572
3573   /* protect against another SIGCHLD in the middle of this call */
3574   count = child_watch_count;
3575
3576   child_watch_source = (GChildWatchSource *) source;
3577
3578   if (child_watch_source->child_exited)
3579     return TRUE;
3580
3581   if (child_watch_source->count < count)
3582     {
3583       gint child_status;
3584
3585       if (waitpid (child_watch_source->pid, &child_status, WNOHANG) > 0)
3586         {
3587           child_watch_source->child_status = child_status;
3588           child_watch_source->child_exited = TRUE;
3589         }
3590       child_watch_source->count = count;
3591     }
3592
3593   return child_watch_source->child_exited;
3594 }
3595
3596 static gboolean
3597 g_child_watch_prepare (GSource *source,
3598                        gint    *timeout)
3599 {
3600   *timeout = -1;
3601
3602   return check_for_child_exited (source);
3603 }
3604
3605
3606 static gboolean 
3607 g_child_watch_check (GSource  *source)
3608 {
3609   return check_for_child_exited (source);
3610 }
3611
3612 #endif /* G_OS_WIN32 */
3613
3614 static gboolean
3615 g_child_watch_dispatch (GSource    *source, 
3616                         GSourceFunc callback,
3617                         gpointer    user_data)
3618 {
3619   GChildWatchSource *child_watch_source;
3620   GChildWatchFunc child_watch_callback = (GChildWatchFunc) callback;
3621
3622   child_watch_source = (GChildWatchSource *) source;
3623
3624   if (!callback)
3625     {
3626       g_warning ("Child watch source dispatched without callback\n"
3627                  "You must call g_source_set_callback().");
3628       return FALSE;
3629     }
3630
3631   (child_watch_callback) (child_watch_source->pid, child_watch_source->child_status, user_data);
3632
3633   /* We never keep a child watch source around as the child is gone */
3634   return FALSE;
3635 }
3636
3637 #ifndef G_OS_WIN32
3638
3639 static void
3640 g_child_watch_signal_handler (int signum)
3641 {
3642   child_watch_count ++;
3643
3644   if (child_watch_init_state == CHILD_WATCH_INITIALIZED_THREADED)
3645     {
3646       write (child_watch_wake_up_pipe[1], "B", 1);
3647     }
3648   else
3649     {
3650       /* We count on the signal interrupting the poll in the same thread.
3651        */
3652     }
3653 }
3654  
3655 static void
3656 g_child_watch_source_init_single (void)
3657 {
3658   struct sigaction action;
3659
3660   g_assert (! g_thread_supported());
3661   g_assert (child_watch_init_state == CHILD_WATCH_UNINITIALIZED);
3662
3663   child_watch_init_state = CHILD_WATCH_INITIALIZED_SINGLE;
3664
3665   action.sa_handler = g_child_watch_signal_handler;
3666   sigemptyset (&action.sa_mask);
3667   action.sa_flags = SA_NOCLDSTOP;
3668   sigaction (SIGCHLD, &action, NULL);
3669 }
3670
3671 static gpointer
3672 child_watch_helper_thread (gpointer data)
3673 {
3674   while (1)
3675     {
3676       gchar b[20];
3677       GSList *list;
3678
3679       read (child_watch_wake_up_pipe[0], b, 20);
3680
3681       /* We were woken up.  Wake up all other contexts in all other threads */
3682       G_LOCK (main_context_list);
3683       for (list = main_context_list; list; list = list->next)
3684         {
3685           GMainContext *context;
3686
3687           context = list->data;
3688           if (g_atomic_int_get (&context->ref_count) > 0)
3689             /* Due to racing conditions we can find ref_count == 0, in
3690              * that case, however, the context is still not destroyed
3691              * and no poll can be active, otherwise the ref_count
3692              * wouldn't be 0 */
3693             g_main_context_wakeup (context);
3694         }
3695       G_UNLOCK (main_context_list);
3696     }
3697
3698   return NULL;
3699 }
3700
3701 static void
3702 g_child_watch_source_init_multi_threaded (void)
3703 {
3704   GError *error = NULL;
3705   struct sigaction action;
3706
3707   g_assert (g_thread_supported());
3708
3709   if (pipe (child_watch_wake_up_pipe) < 0)
3710     g_error ("Cannot create wake up pipe: %s\n", g_strerror (errno));
3711   fcntl (child_watch_wake_up_pipe[1], F_SETFL, O_NONBLOCK | fcntl (child_watch_wake_up_pipe[1], F_GETFL));
3712
3713   /* We create a helper thread that polls on the wakeup pipe indefinitely */
3714   /* FIXME: Think this through for races */
3715   if (g_thread_create (child_watch_helper_thread, NULL, FALSE, &error) == NULL)
3716     g_error ("Cannot create a thread to monitor child exit status: %s\n", error->message);
3717   child_watch_init_state = CHILD_WATCH_INITIALIZED_THREADED;
3718  
3719   action.sa_handler = g_child_watch_signal_handler;
3720   sigemptyset (&action.sa_mask);
3721   action.sa_flags = SA_RESTART | SA_NOCLDSTOP;
3722   sigaction (SIGCHLD, &action, NULL);
3723 }
3724
3725 static void
3726 g_child_watch_source_init_promote_single_to_threaded (void)
3727 {
3728   g_child_watch_source_init_multi_threaded ();
3729 }
3730
3731 static void
3732 g_child_watch_source_init (void)
3733 {
3734   if (g_thread_supported())
3735     {
3736       if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3737         g_child_watch_source_init_multi_threaded ();
3738       else if (child_watch_init_state == CHILD_WATCH_INITIALIZED_SINGLE)
3739         g_child_watch_source_init_promote_single_to_threaded ();
3740     }
3741   else
3742     {
3743       if (child_watch_init_state == CHILD_WATCH_UNINITIALIZED)
3744         g_child_watch_source_init_single ();
3745     }
3746 }
3747
3748 #endif /* !G_OS_WIN32 */
3749
3750 /**
3751  * g_child_watch_source_new:
3752  * @pid: process to watch. On POSIX the pid of a child process. On
3753  * Windows a handle for a process (which doesn't have to be a child).
3754  * 
3755  * Creates a new child_watch source.
3756  *
3757  * The source will not initially be associated with any #GMainContext
3758  * and must be added to one with g_source_attach() before it will be
3759  * executed.
3760  * 
3761  * Note that child watch sources can only be used in conjunction with
3762  * <literal>g_spawn...</literal> when the %G_SPAWN_DO_NOT_REAP_CHILD
3763  * flag is used.
3764  *
3765  * Note that on platforms where #GPid must be explicitly closed
3766  * (see g_spawn_close_pid()) @pid must not be closed while the
3767  * source is still active. Typically, you will want to call
3768  * g_spawn_close_pid() in the callback function for the source.
3769  *
3770  * Note further that using g_child_watch_source_new() is not 
3771  * compatible with calling <literal>waitpid(-1)</literal> in 
3772  * the application. Calling waitpid() for individual pids will
3773  * still work fine. 
3774  * 
3775  * Return value: the newly-created child watch source
3776  *
3777  * Since: 2.4
3778  **/
3779 GSource *
3780 g_child_watch_source_new (GPid pid)
3781 {
3782   GSource *source = g_source_new (&g_child_watch_funcs, sizeof (GChildWatchSource));
3783   GChildWatchSource *child_watch_source = (GChildWatchSource *)source;
3784
3785 #ifdef G_OS_WIN32
3786   child_watch_source->poll.fd = (gintptr) pid;
3787   child_watch_source->poll.events = G_IO_IN;
3788
3789   g_source_add_poll (source, &child_watch_source->poll);
3790 #else /* G_OS_WIN32 */
3791   g_child_watch_source_init ();
3792 #endif /* G_OS_WIN32 */
3793
3794   child_watch_source->pid = pid;
3795
3796   return source;
3797 }
3798
3799 /**
3800  * g_child_watch_add_full:
3801  * @priority: the priority of the idle source. Typically this will be in the
3802  *            range between #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3803  * @pid:      process to watch. On POSIX the pid of a child process. On
3804  * Windows a handle for a process (which doesn't have to be a child).
3805  * @function: function to call
3806  * @data:     data to pass to @function
3807  * @notify:   function to call when the idle is removed, or %NULL
3808  * 
3809  * Sets a function to be called when the child indicated by @pid 
3810  * exits, at the priority @priority.
3811  *
3812  * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes() 
3813  * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to 
3814  * the spawn function for the child watching to work.
3815  * 
3816  * Note that on platforms where #GPid must be explicitly closed
3817  * (see g_spawn_close_pid()) @pid must not be closed while the
3818  * source is still active. Typically, you will want to call
3819  * g_spawn_close_pid() in the callback function for the source.
3820  * 
3821  * GLib supports only a single callback per process id.
3822  *
3823  * This internally creates a main loop source using 
3824  * g_child_watch_source_new() and attaches it to the main loop context 
3825  * using g_source_attach(). You can do these steps manually if you 
3826  * need greater control.
3827  *
3828  * Return value: the ID (greater than 0) of the event source.
3829  *
3830  * Since: 2.4
3831  **/
3832 guint
3833 g_child_watch_add_full (gint            priority,
3834                         GPid            pid,
3835                         GChildWatchFunc function,
3836                         gpointer        data,
3837                         GDestroyNotify  notify)
3838 {
3839   GSource *source;
3840   guint id;
3841   
3842   g_return_val_if_fail (function != NULL, 0);
3843
3844   source = g_child_watch_source_new (pid);
3845
3846   if (priority != G_PRIORITY_DEFAULT)
3847     g_source_set_priority (source, priority);
3848
3849   g_source_set_callback (source, (GSourceFunc) function, data, notify);
3850   id = g_source_attach (source, NULL);
3851   g_source_unref (source);
3852
3853   return id;
3854 }
3855
3856 /**
3857  * g_child_watch_add:
3858  * @pid:      process id to watch. On POSIX the pid of a child process. On
3859  * Windows a handle for a process (which doesn't have to be a child).
3860  * @function: function to call
3861  * @data:     data to pass to @function
3862  * 
3863  * Sets a function to be called when the child indicated by @pid 
3864  * exits, at a default priority, #G_PRIORITY_DEFAULT.
3865  * 
3866  * If you obtain @pid from g_spawn_async() or g_spawn_async_with_pipes() 
3867  * you will need to pass #G_SPAWN_DO_NOT_REAP_CHILD as flag to 
3868  * the spawn function for the child watching to work.
3869  * 
3870  * Note that on platforms where #GPid must be explicitly closed
3871  * (see g_spawn_close_pid()) @pid must not be closed while the
3872  * source is still active. Typically, you will want to call
3873  * g_spawn_close_pid() in the callback function for the source.
3874  *
3875  * GLib supports only a single callback per process id.
3876  *
3877  * This internally creates a main loop source using 
3878  * g_child_watch_source_new() and attaches it to the main loop context 
3879  * using g_source_attach(). You can do these steps manually if you 
3880  * need greater control.
3881  *
3882  * Return value: the ID (greater than 0) of the event source.
3883  *
3884  * Since: 2.4
3885  **/
3886 guint 
3887 g_child_watch_add (GPid            pid,
3888                    GChildWatchFunc function,
3889                    gpointer        data)
3890 {
3891   return g_child_watch_add_full (G_PRIORITY_DEFAULT, pid, function, data, NULL);
3892 }
3893
3894
3895 /* Idle functions */
3896
3897 static gboolean 
3898 g_idle_prepare  (GSource  *source,
3899                  gint     *timeout)
3900 {
3901   *timeout = 0;
3902
3903   return TRUE;
3904 }
3905
3906 static gboolean 
3907 g_idle_check    (GSource  *source)
3908 {
3909   return TRUE;
3910 }
3911
3912 static gboolean
3913 g_idle_dispatch (GSource    *source, 
3914                  GSourceFunc callback,
3915                  gpointer    user_data)
3916 {
3917   if (!callback)
3918     {
3919       g_warning ("Idle source dispatched without callback\n"
3920                  "You must call g_source_set_callback().");
3921       return FALSE;
3922     }
3923   
3924   return callback (user_data);
3925 }
3926
3927 /**
3928  * g_idle_source_new:
3929  * 
3930  * Creates a new idle source.
3931  *
3932  * The source will not initially be associated with any #GMainContext
3933  * and must be added to one with g_source_attach() before it will be
3934  * executed. Note that the default priority for idle sources is
3935  * %G_PRIORITY_DEFAULT_IDLE, as compared to other sources which
3936  * have a default priority of %G_PRIORITY_DEFAULT.
3937  * 
3938  * Return value: the newly-created idle source
3939  **/
3940 GSource *
3941 g_idle_source_new (void)
3942 {
3943   GSource *source;
3944
3945   source = g_source_new (&g_idle_funcs, sizeof (GSource));
3946   g_source_set_priority (source, G_PRIORITY_DEFAULT_IDLE);
3947
3948   return source;
3949 }
3950
3951 /**
3952  * g_idle_add_full:
3953  * @priority: the priority of the idle source. Typically this will be in the
3954  *            range btweeen #G_PRIORITY_DEFAULT_IDLE and #G_PRIORITY_HIGH_IDLE.
3955  * @function: function to call
3956  * @data:     data to pass to @function
3957  * @notify:   function to call when the idle is removed, or %NULL
3958  * 
3959  * Adds a function to be called whenever there are no higher priority
3960  * events pending.  If the function returns %FALSE it is automatically
3961  * removed from the list of event sources and will not be called again.
3962  * 
3963  * This internally creates a main loop source using g_idle_source_new()
3964  * and attaches it to the main loop context using g_source_attach(). 
3965  * You can do these steps manually if you need greater control.
3966  * 
3967  * Return value: the ID (greater than 0) of the event source.
3968  **/
3969 guint 
3970 g_idle_add_full (gint           priority,
3971                  GSourceFunc    function,
3972                  gpointer       data,
3973                  GDestroyNotify notify)
3974 {
3975   GSource *source;
3976   guint id;
3977   
3978   g_return_val_if_fail (function != NULL, 0);
3979
3980   source = g_idle_source_new ();
3981
3982   if (priority != G_PRIORITY_DEFAULT_IDLE)
3983     g_source_set_priority (source, priority);
3984
3985   g_source_set_callback (source, function, data, notify);
3986   id = g_source_attach (source, NULL);
3987   g_source_unref (source);
3988
3989   return id;
3990 }
3991
3992 /**
3993  * g_idle_add:
3994  * @function: function to call 
3995  * @data: data to pass to @function.
3996  * 
3997  * Adds a function to be called whenever there are no higher priority
3998  * events pending to the default main loop. The function is given the
3999  * default idle priority, #G_PRIORITY_DEFAULT_IDLE.  If the function
4000  * returns %FALSE it is automatically removed from the list of event
4001  * sources and will not be called again.
4002  * 
4003  * This internally creates a main loop source using g_idle_source_new()
4004  * and attaches it to the main loop context using g_source_attach(). 
4005  * You can do these steps manually if you need greater control.
4006  * 
4007  * Return value: the ID (greater than 0) of the event source.
4008  **/
4009 guint 
4010 g_idle_add (GSourceFunc    function,
4011             gpointer       data)
4012 {
4013   return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
4014 }
4015
4016 /**
4017  * g_idle_remove_by_data:
4018  * @data: the data for the idle source's callback.
4019  * 
4020  * Removes the idle function with the given data.
4021  * 
4022  * Return value: %TRUE if an idle source was found and removed.
4023  **/
4024 gboolean
4025 g_idle_remove_by_data (gpointer data)
4026 {
4027   return g_source_remove_by_funcs_user_data (&g_idle_funcs, data);
4028 }
4029
4030 #define __G_MAIN_C__
4031 #include "galiasdef.c"