Merge in current Win32 version. Almost no Unix code touched.
[platform/upstream/glib.git] / gmain.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * gmain.c: Main loop abstraction, timeouts, and idle functions
5  * Copyright 1998 Owen Taylor
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #include "config.h"
28
29 #include "glib.h"
30 #include <sys/types.h>
31 #include <time.h>
32 #ifdef HAVE_SYS_TIME_H
33 #include <sys/time.h>
34 #endif
35 #ifdef  GLIB_HAVE_SYS_POLL_H
36 #  include <sys/poll.h>
37 #  undef events  /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
38 #  undef revents /* AIX 4.1.5 & 4.3.2 define this for SVR3,4 compatibility */
39 #endif  /* GLIB_HAVE_SYS_POLL_H */
40 #ifdef HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif
43 #include <errno.h>
44
45 #ifdef NATIVE_WIN32
46 #define STRICT
47 #include <windows.h>
48 #endif
49
50 #ifdef _MSC_VER
51 #include <fcntl.h>
52 #include <io.h>
53 #endif
54
55 /* Types */
56
57 typedef struct _GIdleData GIdleData;
58 typedef struct _GTimeoutData GTimeoutData;
59 typedef struct _GSource GSource;
60 typedef struct _GPollRec GPollRec;
61
62 typedef enum
63 {
64   G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
65   G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
66 } GSourceFlags;
67
68 struct _GSource
69 {
70   GHook hook;
71   gint priority;
72   gpointer source_data;
73 };
74
75 struct _GMainLoop
76 {
77   gboolean is_running;
78 };
79
80 struct _GIdleData
81 {
82   GSourceFunc callback;
83 };
84
85 struct _GTimeoutData
86 {
87   GTimeVal    expiration;
88   gint        interval;
89   GSourceFunc callback;
90 };
91
92 struct _GPollRec
93 {
94   gint priority;
95   GPollFD *fd;
96   GPollRec *next;
97 };
98
99 /* Forward declarations */
100
101 static gint     g_source_compare          (GHook      *a,
102                                            GHook      *b);
103 static void     g_source_free_func        (GHookList  *hook_list,
104                                            GHook      *hook);
105 static void     g_main_poll               (gint      timeout,
106                                            gboolean  use_priority, 
107                                            gint      priority);
108 static void     g_main_add_poll_unlocked  (gint      priority,
109                                            GPollFD  *fd);
110
111 static gboolean g_timeout_prepare      (gpointer  source_data, 
112                                         GTimeVal *current_time,
113                                         gint     *timeout);
114 static gboolean g_timeout_check        (gpointer  source_data,
115                                         GTimeVal *current_time);
116 static gboolean g_timeout_dispatch     (gpointer  source_data,
117                                         GTimeVal *current_time,
118                                         gpointer  user_data);
119 static gboolean g_idle_prepare         (gpointer  source_data, 
120                                         GTimeVal *current_time,
121                                         gint     *timeout);
122 static gboolean g_idle_check           (gpointer  source_data,
123                                         GTimeVal *current_time);
124 static gboolean g_idle_dispatch        (gpointer  source_data,
125                                         GTimeVal *current_time,
126                                         gpointer  user_data);
127
128 /* Data */
129
130 static GSList *pending_dispatches = NULL;
131 static GHookList source_list = { 0 };
132
133 /* The following lock is used for both the list of sources
134  * and the list of poll records
135  */
136 G_LOCK_DECLARE_STATIC (main_loop);
137
138 static GSourceFuncs timeout_funcs = {
139   g_timeout_prepare,
140   g_timeout_check,
141   g_timeout_dispatch,
142   (GDestroyNotify)g_free
143 };
144
145 static GSourceFuncs idle_funcs = {
146   g_idle_prepare,
147   g_idle_check,
148   g_idle_dispatch,
149   (GDestroyNotify)g_free
150 };
151
152 static GPollRec *poll_records = NULL;
153 static GPollRec *poll_free_list = NULL;
154 static GMemChunk *poll_chunk;
155 static guint n_poll_records = 0;
156
157 #ifdef G_THREADS_ENABLED
158 #ifndef NATIVE_WIN32
159 /* this pipe is used to wake up the main loop when a source is added.
160  */
161 static gint wake_up_pipe[2] = { -1, -1 };
162 #else
163 static HANDLE wake_up_semaphore = NULL;
164 #endif
165 static GPollFD wake_up_rec;
166 static gboolean poll_waiting = FALSE;
167 #endif
168
169 #ifdef HAVE_POLL
170 static GPollFunc poll_func = (GPollFunc) poll;
171 #else   /* !HAVE_POLL */
172 #ifdef NATIVE_WIN32
173
174 static gint
175 g_poll (GPollFD *fds, guint nfds, gint timeout)
176 {
177   HANDLE handles[MAXIMUM_WAIT_OBJECTS];
178   GPollFD *f;
179   DWORD ready;
180   MSG msg;
181   UINT timer;
182   LONG prevcnt;
183   gint poll_msgs = -1;
184   gint nhandles = 0;
185
186   for (f = fds; f < &fds[nfds]; ++f)
187     if (f->fd >= 0)
188       {
189         if (f->events & G_IO_IN)
190           if (f->fd == G_WIN32_MSG_HANDLE)
191             poll_msgs = f - fds;
192           else
193             {
194               /* g_print ("g_poll: waiting for handle %#x\n", f->fd); */
195               handles[nhandles++] = (HANDLE) f->fd;
196             }
197       }
198
199   if (timeout == -1)
200     timeout = INFINITE;
201
202   if (poll_msgs >= 0)
203     {
204       /* Waiting for messages, and maybe events */
205       if (nhandles == 0)
206         {
207           if (timeout == INFINITE)
208             {
209               /* Waiting just for messages, infinite timeout
210                * -> Use PeekMessage, then WaitMessage
211                */
212               /* g_print ("WaitMessage, PeekMessage\n"); */
213               if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
214                 ready = WAIT_OBJECT_0;
215               else if (!WaitMessage ())
216                 g_warning ("g_poll: WaitMessage failed");
217               ready = WAIT_OBJECT_0;
218             }
219           else if (timeout == 0)
220             {
221               /* Waiting just for messages, zero timeout
222                * -> Use PeekMessage
223                */
224               /* g_print ("PeekMessage\n"); */
225               if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
226                 ready = WAIT_OBJECT_0;
227               else
228                 ready = WAIT_TIMEOUT;
229             }
230           else
231             {
232               /* Waiting just for messages, some timeout
233                * -> First try PeekMessage, then set a timer, wait for message,
234                * kill timer, use PeekMessage
235                */
236               /* g_print ("PeekMessage\n"); */
237               if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
238                 ready = WAIT_OBJECT_0;
239               else if ((timer = SetTimer (NULL, 0, timeout, NULL)) == 0)
240                 g_warning ("g_poll: SetTimer failed");
241               else
242                 {
243                   /* g_print ("WaitMessage\n"); */
244                   WaitMessage ();
245                   KillTimer (NULL, timer);
246                   if (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
247                     ready = WAIT_OBJECT_0;
248                   else
249                     ready = WAIT_TIMEOUT;
250                 }
251             }
252         }
253       else
254         {
255           /* Wait for either message or event
256            * -> Use MsgWaitForMultipleObjects
257            */
258           /* g_print ("MsgWaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
259           ready = MsgWaitForMultipleObjects (nhandles, handles, FALSE,
260                                              timeout, QS_ALLINPUT);
261           /* g_print("=%d\n", ready); */
262           if (ready == WAIT_FAILED)
263             g_warning ("g_poll: MsgWaitForMultipleObjects failed");
264         }
265     }
266   else if (nhandles == 0)
267     {
268       /* Wait for nothing (huh?) */
269       return 0;
270     }
271   else
272     {
273       /* Wait for just events
274        * -> Use WaitForMultipleObjects
275        */
276       /* g_print ("WaitForMultipleObjects(%d, %d)\n", nhandles, timeout); */
277       ready = WaitForMultipleObjects (nhandles, handles, FALSE, timeout);
278       /* g_print("=%d\n", ready); */
279       if (ready == WAIT_FAILED)
280         g_warning ("g_poll: WaitForMultipleObjects failed");
281     }
282
283   for (f = fds; f < &fds[nfds]; ++f)
284     f->revents = 0;
285
286   if (ready == WAIT_FAILED)
287     return -1;
288   else if (poll_msgs >= 0 && ready == WAIT_OBJECT_0 + nhandles)
289     {
290       fds[poll_msgs].revents |= G_IO_IN;
291     }
292   else if (ready >= WAIT_OBJECT_0 && ready < WAIT_OBJECT_0 + nhandles)
293     for (f = fds; f < &fds[nfds]; ++f)
294       {
295         if ((f->events & G_IO_IN)
296             && f->fd == (gint) handles[ready - WAIT_OBJECT_0])
297           {
298             f->revents |= G_IO_IN;
299             /* g_print ("event %#x\n", f->fd); */
300             ResetEvent ((HANDLE) f->fd);
301           }
302       }
303     
304   if (ready == WAIT_TIMEOUT)
305     return 0;
306   else
307     return 1;
308 }
309
310 #else  /* !NATIVE_WIN32 */
311
312 /* The following implementation of poll() comes from the GNU C Library.
313  * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
314  */
315
316 #include <string.h> /* for bzero on BSD systems */
317
318 #ifdef HAVE_SYS_SELECT_H
319 #include <sys/select.h>
320 #endif /* HAVE_SYS_SELECT_H_ */
321
322 #ifndef NO_FD_SET
323 #  define SELECT_MASK fd_set
324 #else   /* !NO_FD_SET */
325 #  ifndef _AIX
326 typedef long fd_mask;
327 #  endif
328 #  if defined(_IBMR2)
329 #    define SELECT_MASK void
330 #  else
331 #    define SELECT_MASK int
332 #  endif
333 #endif  /* !NO_FD_SET */
334
335 static gint 
336 g_poll (GPollFD *fds, guint nfds, gint timeout)
337 {
338   struct timeval tv;
339   SELECT_MASK rset, wset, xset;
340   GPollFD *f;
341   int ready;
342   int maxfd = 0;
343
344   FD_ZERO (&rset);
345   FD_ZERO (&wset);
346   FD_ZERO (&xset);
347
348   for (f = fds; f < &fds[nfds]; ++f)
349     if (f->fd >= 0)
350       {
351         if (f->events & G_IO_IN)
352           FD_SET (f->fd, &rset);
353         if (f->events & G_IO_OUT)
354           FD_SET (f->fd, &wset);
355         if (f->events & G_IO_PRI)
356           FD_SET (f->fd, &xset);
357         if (f->fd > maxfd && (f->events & (G_IO_IN|G_IO_OUT|G_IO_PRI)))
358           maxfd = f->fd;
359       }
360
361   tv.tv_sec = timeout / 1000;
362   tv.tv_usec = (timeout % 1000) * 1000;
363
364   ready = select (maxfd + 1, &rset, &wset, &xset,
365                   timeout == -1 ? NULL : &tv);
366   if (ready > 0)
367     for (f = fds; f < &fds[nfds]; ++f)
368       {
369         f->revents = 0;
370         if (f->fd >= 0)
371           {
372             if (FD_ISSET (f->fd, &rset))
373               f->revents |= G_IO_IN;
374             if (FD_ISSET (f->fd, &wset))
375               f->revents |= G_IO_OUT;
376             if (FD_ISSET (f->fd, &xset))
377               f->revents |= G_IO_PRI;
378           }
379       }
380
381   return ready;
382 }
383 #endif /* !NATIVE_WIN32 */
384
385 static GPollFunc poll_func = g_poll;
386 #endif  /* !HAVE_POLL */
387
388 /* Hooks for adding to the main loop */
389
390 /* Use knowledge of insert_sorted algorithm here to make
391  * sure we insert at the end of equal priority items
392  */
393 static gint
394 g_source_compare (GHook *a,
395                   GHook *b)
396 {
397   GSource *source_a = (GSource *)a;
398   GSource *source_b = (GSource *)b;
399
400   return (source_a->priority < source_b->priority) ? -1 : 1;
401 }
402
403 static void
404 g_source_free_func (GHookList      *hook_list,
405                     GHook          *hook)
406 {
407   GSource *source = (GSource *)hook;
408
409   ((GSourceFuncs *) hook->func)->destroy (source->source_data);
410 }
411
412 guint 
413 g_source_add (gint           priority,
414               gboolean       can_recurse,
415               GSourceFuncs  *funcs,
416               gpointer       source_data, 
417               gpointer       user_data,
418               GDestroyNotify notify)
419 {
420   guint return_val;
421   GSource *source;
422
423   G_LOCK (main_loop);
424
425   if (!source_list.is_setup)
426     g_hook_list_init (&source_list, sizeof(GSource));
427
428   source_list.hook_free = g_source_free_func;
429
430   source = (GSource *)g_hook_alloc (&source_list);
431   source->priority = priority;
432   source->source_data = source_data;
433   source->hook.func = funcs;
434   source->hook.data = user_data;
435   source->hook.destroy = notify;
436   
437   g_hook_insert_sorted (&source_list, 
438                         (GHook *)source, 
439                         g_source_compare);
440
441   if (can_recurse)
442     source->hook.flags |= G_SOURCE_CAN_RECURSE;
443
444   return_val = source->hook.hook_id;
445
446 #ifdef G_THREADS_ENABLED
447   /* Now wake up the main loop if it is waiting in the poll() */
448
449   if (poll_waiting)
450     {
451       poll_waiting = FALSE;
452 #ifndef NATIVE_WIN32
453       write (wake_up_pipe[1], "A", 1);
454 #else
455       ReleaseSemaphore (wake_up_semaphore, 1, NULL);
456 #endif
457     }
458 #endif
459   G_UNLOCK (main_loop);
460
461   return return_val;
462 }
463
464 void 
465 g_source_remove (guint tag)
466 {
467   GHook *hook;
468
469   G_LOCK (main_loop);
470
471   hook = g_hook_get (&source_list, tag);
472   if (hook)
473     g_hook_destroy_link (&source_list, hook);
474
475   G_UNLOCK (main_loop);
476 }
477
478 void 
479 g_source_remove_by_user_data (gpointer user_data)
480 {
481   GHook *hook;
482   
483   G_LOCK (main_loop);
484   
485   hook = g_hook_find_data (&source_list, TRUE, user_data);
486   if (hook)
487     g_hook_destroy_link (&source_list, hook);
488
489   G_UNLOCK (main_loop);
490 }
491
492 static gboolean
493 g_source_find_source_data (GHook        *hook,
494                            gpointer      data)
495 {
496   GSource *source = (GSource *)hook;
497
498   return (source->source_data == data);
499 }
500
501 void 
502 g_source_remove_by_source_data (gpointer source_data)
503 {
504   GHook *hook;
505
506   G_LOCK (main_loop);
507
508   hook = g_hook_find (&source_list, TRUE, 
509                       g_source_find_source_data, source_data);
510   if (hook)
511     g_hook_destroy_link (&source_list, hook);
512
513   G_UNLOCK (main_loop);
514 }
515
516 void
517 g_get_current_time (GTimeVal *result)
518 {
519 #ifndef _MSC_VER
520   struct timeval r;
521   g_return_if_fail (result != NULL);
522
523   /*this is required on alpha, there the timeval structs are int's
524     not longs and a cast only would fail horribly*/
525   gettimeofday (&r, NULL);
526   result->tv_sec = r.tv_sec;
527   result->tv_usec = r.tv_usec;
528 #else
529   /* Avoid calling time() except for the first time.
530    * GetTickCount() should be pretty fast and low-level?
531    * I could also use ftime() but it seems unnecessarily overheady.
532    */
533   static DWORD start_tick = 0;
534   static time_t start_time;
535   DWORD tick;
536   time_t t;
537
538   g_return_if_fail (result != NULL);
539  
540   if (start_tick == 0)
541     {
542       start_tick = GetTickCount ();
543       time (&start_time);
544     }
545
546   tick = GetTickCount ();
547
548   result->tv_sec = (tick - start_tick) / 1000 + start_time;
549   result->tv_usec = ((tick - start_tick) % 1000) * 1000;
550 #endif
551 }
552
553 /* Running the main loop */
554
555 /* HOLDS: main_loop_lock */
556 static void
557 g_main_dispatch (GTimeVal *current_time)
558 {
559   while (pending_dispatches != NULL)
560     {
561       gboolean need_destroy;
562       GSource *source = pending_dispatches->data;
563       GSList *tmp_list;
564
565       tmp_list = pending_dispatches;
566       pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
567       g_slist_free_1 (tmp_list);
568
569       if (G_HOOK_IS_VALID (source))
570         {
571           gboolean was_in_call;
572           gpointer hook_data = source->hook.data;
573           gpointer source_data = source->source_data;
574           gboolean (*dispatch) (gpointer,
575                                 GTimeVal *,
576                                 gpointer);
577
578           dispatch = ((GSourceFuncs *) source->hook.func)->dispatch;
579           
580           was_in_call = G_HOOK_IN_CALL (source);
581           source->hook.flags |= G_HOOK_FLAG_IN_CALL;
582
583           G_UNLOCK (main_loop);
584           need_destroy = ! dispatch (source_data,
585                                      current_time,
586                                      hook_data);
587           G_LOCK (main_loop);
588
589           if (!was_in_call)
590             source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
591           
592           if (need_destroy && G_HOOK_IS_VALID (source))
593             g_hook_destroy_link (&source_list, (GHook *) source);
594         }
595
596       g_hook_unref (&source_list, (GHook*) source);
597     }
598 }
599
600 /* g_main_iterate () runs a single iteration of the mainloop, or,
601  * if !dispatch checks to see if any sources need dispatching.
602  * basic algorithm for dispatch=TRUE:
603  *
604  * 1) while the list of currently pending sources is non-empty,
605  *    we call (*dispatch) on those that are !IN_CALL or can_recurse,
606  *    removing sources from the list after each returns.
607  *    the return value of (*dispatch) determines whether the source
608  *    itself is kept alive.
609  *
610  * 2) call (*prepare) for sources that are not yet SOURCE_READY and
611  *    are !IN_CALL or can_recurse. a return value of TRUE determines
612  *    that the source would like to be dispatched immediatedly, it
613  *    is then flagged as SOURCE_READY.
614  *
615  * 3) poll with the pollfds from all sources at the priority of the
616  *    first source flagged as SOURCE_READY. if there are any sources
617  *    flagged as SOURCE_READY, we use a timeout of 0 or the minimum
618  *    of all timouts otherwise.
619  *
620  * 4) for each source !IN_CALL or can_recurse, if SOURCE_READY or
621  *    (*check) returns true, add the source to the pending list.
622  *    once one source returns true, stop after checking all sources
623  *    at that priority.
624  *
625  * 5) while the list of currently pending sources is non-empty,
626  *    call (*dispatch) on each source, removing the source
627  *    after the call.
628  *
629  */
630 static gboolean
631 g_main_iterate (gboolean block,
632                 gboolean dispatch)
633 {
634   GHook *hook;
635   GTimeVal current_time  ={ 0, 0 };
636   gint n_ready = 0;
637   gint current_priority = 0;
638   gint timeout;
639   gboolean retval = FALSE;
640
641   g_return_val_if_fail (!block || dispatch, FALSE);
642
643   g_get_current_time (&current_time);
644
645   G_LOCK (main_loop);
646   
647   /* If recursing, finish up current dispatch, before starting over */
648   if (pending_dispatches)
649     {
650       if (dispatch)
651         g_main_dispatch (&current_time);
652       
653       G_UNLOCK (main_loop);
654
655       return TRUE;
656     }
657
658   /* Prepare all sources */
659
660   timeout = block ? -1 : 0;
661   
662   hook = g_hook_first_valid (&source_list, TRUE);
663   while (hook)
664     {
665       GSource *source = (GSource *)hook;
666       gint source_timeout = -1;
667
668       if ((n_ready > 0) && (source->priority > current_priority))
669         {
670           g_hook_unref (&source_list, hook);
671           break;
672         }
673       if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
674         {
675           hook = g_hook_next_valid (&source_list, hook, TRUE);
676           continue;
677         }
678
679       if (hook->flags & G_SOURCE_READY ||
680           ((GSourceFuncs *) hook->func)->prepare (source->source_data,
681                                                   &current_time,
682                                                   &source_timeout))
683         {
684           if (!dispatch)
685             {
686               hook->flags |= G_SOURCE_READY;
687               g_hook_unref (&source_list, hook);
688               G_UNLOCK (main_loop);
689
690               return TRUE;
691             }
692           else
693             {
694               hook->flags |= G_SOURCE_READY;
695               n_ready++;
696               current_priority = source->priority;
697               timeout = 0;
698             }
699         }
700       
701       if (source_timeout >= 0)
702         {
703           if (timeout < 0)
704             timeout = source_timeout;
705           else
706             timeout = MIN (timeout, source_timeout);
707         }
708
709       hook = g_hook_next_valid (&source_list, hook, TRUE);
710     }
711
712   /* poll(), if necessary */
713
714   g_main_poll (timeout, n_ready > 0, current_priority);
715
716   /* Check to see what sources need to be dispatched */
717
718   n_ready = 0;
719   
720   hook = g_hook_first_valid (&source_list, TRUE);
721   while (hook)
722     {
723       GSource *source = (GSource *)hook;
724
725       if ((n_ready > 0) && (source->priority > current_priority))
726         {
727           g_hook_unref (&source_list, hook);
728           break;
729         }
730       if (G_HOOK_IN_CALL (hook) && !(hook->flags & G_SOURCE_CAN_RECURSE))
731         {
732           hook = g_hook_next_valid (&source_list, hook, TRUE);
733           continue;
734         }
735
736       if (hook->flags & G_SOURCE_READY ||
737           ((GSourceFuncs *) hook->func)->check (source->source_data,
738                                                 &current_time))
739         {
740           if (dispatch)
741             {
742               hook->flags &= ~G_SOURCE_READY;
743               g_hook_ref (&source_list, hook);
744               pending_dispatches = g_slist_prepend (pending_dispatches, source);
745               current_priority = source->priority;
746               n_ready++;
747             }
748           else
749             {
750               g_hook_unref (&source_list, hook);
751               G_UNLOCK (main_loop);
752
753               return TRUE;
754             }
755         }
756       
757       hook = g_hook_next_valid (&source_list, hook, TRUE);
758     }
759
760   /* Now invoke the callbacks */
761
762   if (pending_dispatches)
763     {
764       pending_dispatches = g_slist_reverse (pending_dispatches);
765       g_main_dispatch (&current_time);
766       retval = TRUE;
767     }
768
769   G_UNLOCK (main_loop);
770
771   return retval;
772 }
773
774 /* See if any events are pending
775  */
776 gboolean 
777 g_main_pending ()
778 {
779   return g_main_iterate (FALSE, FALSE);
780 }
781
782 /* Run a single iteration of the mainloop. If block is FALSE,
783  * will never block
784  */
785 gboolean
786 g_main_iteration (gboolean block)
787 {
788   return g_main_iterate (block, TRUE);
789 }
790
791 GMainLoop*
792 g_main_new (gboolean is_running)
793 {
794   GMainLoop *loop;
795
796   loop = g_new0 (GMainLoop, 1);
797   loop->is_running = is_running != FALSE;
798
799   return loop;
800 }
801
802 void 
803 g_main_run (GMainLoop *loop)
804 {
805   g_return_if_fail (loop != NULL);
806
807   loop->is_running = TRUE;
808   while (loop->is_running)
809     g_main_iterate (TRUE, TRUE);
810 }
811
812 void 
813 g_main_quit (GMainLoop *loop)
814 {
815   g_return_if_fail (loop != NULL);
816
817   loop->is_running = FALSE;
818 }
819
820 void 
821 g_main_destroy (GMainLoop *loop)
822 {
823   g_return_if_fail (loop != NULL);
824
825   g_free (loop);
826 }
827
828 gboolean
829 g_main_is_running (GMainLoop *loop)
830 {
831   g_return_val_if_fail (loop != NULL, FALSE);
832
833   return loop->is_running;
834 }
835
836 /* HOLDS: main_loop_lock */
837 static void
838 g_main_poll (gint     timeout,
839              gboolean use_priority,
840              gint     priority)
841 {
842   GPollFD *fd_array;
843   GPollRec *pollrec;
844
845   gint i;
846   gint npoll;
847 #ifdef G_THREADS_ENABLED
848 #ifndef NATIVE_WIN32
849   if (wake_up_pipe[0] < 0)
850     {
851       if (pipe (wake_up_pipe) < 0)
852         g_error ("Cannot create pipe main loop wake-up: %s\n",
853                  g_strerror (errno));
854
855       wake_up_rec.fd = wake_up_pipe[0];
856       wake_up_rec.events = G_IO_IN;
857       g_main_add_poll_unlocked (0, &wake_up_rec);
858     }
859 #else
860   if (wake_up_semaphore == NULL)
861     {
862       if ((wake_up_semaphore = CreateSemaphore (NULL, 0, 100, NULL)) == NULL)
863         g_error ("Cannot create wake-up semaphore: %d", GetLastError ());
864       wake_up_rec.fd = (gint) wake_up_semaphore;
865       wake_up_rec.events = G_IO_IN;
866       g_main_add_poll_unlocked (0, &wake_up_rec);
867     }
868 #endif
869 #endif
870   fd_array = g_new (GPollFD, n_poll_records);
871  
872   pollrec = poll_records;
873   i = 0;
874   while (pollrec && (!use_priority || priority >= pollrec->priority))
875     {
876       fd_array[i].fd = pollrec->fd->fd;
877       fd_array[i].events = pollrec->fd->events;
878       fd_array[i].revents = 0;
879         
880       pollrec = pollrec->next;
881       i++;
882     }
883 #ifdef G_THREADS_ENABLED
884   poll_waiting = TRUE;
885 #endif
886   G_UNLOCK (main_loop);
887   npoll = i;
888   (*poll_func) (fd_array, npoll, timeout);
889   G_LOCK (main_loop);
890
891 #ifdef G_THREADS_ENABLED
892   if (!poll_waiting)
893     {
894 #ifndef NATIVE_WIN32
895       gchar c;
896       read (wake_up_pipe[0], &c, 1);
897 #endif
898     }
899   else
900     poll_waiting = FALSE;
901 #endif
902
903   pollrec = poll_records;
904   i = 0;
905   while (i < npoll)
906     {
907       pollrec->fd->revents = fd_array[i].revents;
908       pollrec = pollrec->next;
909       i++;
910     }
911
912   g_free (fd_array);
913 }
914
915 void 
916 g_main_add_poll (GPollFD *fd,
917                  gint     priority)
918 {
919   G_LOCK (main_loop);
920   g_main_add_poll_unlocked (priority, fd);
921   G_UNLOCK (main_loop);
922 }
923
924 /* HOLDS: main_loop_lock */
925 static void 
926 g_main_add_poll_unlocked (gint     priority,
927                           GPollFD *fd)
928 {
929   GPollRec *lastrec, *pollrec, *newrec;
930
931   if (!poll_chunk)
932     poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
933
934   if (poll_free_list)
935     {
936       newrec = poll_free_list;
937       poll_free_list = newrec->next;
938     }
939   else
940     newrec = g_chunk_new (GPollRec, poll_chunk);
941
942   newrec->fd = fd;
943   newrec->priority = priority;
944
945   lastrec = NULL;
946   pollrec = poll_records;
947   while (pollrec && priority >= pollrec->priority)
948     {
949       lastrec = pollrec;
950       pollrec = pollrec->next;
951     }
952   
953   if (lastrec)
954     lastrec->next = newrec;
955   else
956     poll_records = newrec;
957
958   newrec->next = pollrec;
959
960   n_poll_records++;
961 }
962
963 void 
964 g_main_remove_poll (GPollFD *fd)
965 {
966   GPollRec *pollrec, *lastrec;
967
968   G_LOCK (main_loop);
969   
970   lastrec = NULL;
971   pollrec = poll_records;
972
973   while (pollrec)
974     {
975       if (pollrec->fd == fd)
976         {
977           if (lastrec != NULL)
978             lastrec->next = pollrec->next;
979           else
980             poll_records = pollrec->next;
981
982           pollrec->next = poll_free_list;
983           poll_free_list = pollrec;
984
985           n_poll_records--;
986           break;
987         }
988       lastrec = pollrec;
989       pollrec = pollrec->next;
990     }
991
992   G_UNLOCK (main_loop);
993 }
994
995 void 
996 g_main_set_poll_func (GPollFunc func)
997 {
998   if (func)
999     poll_func = func;
1000   else
1001 #ifdef HAVE_POLL
1002     poll_func = (GPollFunc)poll;
1003 #else
1004     poll_func = (GPollFunc)g_poll;
1005 #endif
1006 }
1007
1008 /* Timeouts */
1009
1010 static gboolean 
1011 g_timeout_prepare  (gpointer source_data, 
1012                     GTimeVal *current_time,
1013                     gint    *timeout)
1014 {
1015   glong msec;
1016   GTimeoutData *data = source_data;
1017
1018   msec = (data->expiration.tv_sec  - current_time->tv_sec) * 1000 +
1019          (data->expiration.tv_usec - current_time->tv_usec) / 1000;
1020
1021   *timeout = (msec <= 0) ? 0 : msec;
1022
1023   return (msec <= 0);
1024 }
1025
1026 static gboolean 
1027 g_timeout_check    (gpointer source_data,
1028                     GTimeVal *current_time)
1029 {
1030   GTimeoutData *data = source_data;
1031
1032   return (data->expiration.tv_sec < current_time->tv_sec) ||
1033          ((data->expiration.tv_sec == current_time->tv_sec) &&
1034           (data->expiration.tv_usec <= current_time->tv_usec));
1035 }
1036
1037 static gboolean
1038 g_timeout_dispatch (gpointer source_data, 
1039                     GTimeVal *current_time,
1040                     gpointer user_data)
1041 {
1042   GTimeoutData *data = source_data;
1043
1044   if (data->callback(user_data))
1045     {
1046       guint seconds = data->interval / 1000;
1047       guint msecs = data->interval - seconds * 1000;
1048
1049       data->expiration.tv_sec = current_time->tv_sec + seconds;
1050       data->expiration.tv_usec = current_time->tv_usec + msecs * 1000;
1051       if (data->expiration.tv_usec >= 1000000)
1052         {
1053           data->expiration.tv_usec -= 1000000;
1054           data->expiration.tv_sec++;
1055         }
1056       return TRUE;
1057     }
1058   else
1059     return FALSE;
1060 }
1061
1062 guint 
1063 g_timeout_add_full (gint           priority,
1064                     guint          interval, 
1065                     GSourceFunc    function,
1066                     gpointer       data,
1067                     GDestroyNotify notify)
1068 {
1069   guint seconds;
1070   guint msecs;
1071   GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
1072
1073   timeout_data->interval = interval;
1074   timeout_data->callback = function;
1075   g_get_current_time (&timeout_data->expiration);
1076
1077   seconds = timeout_data->interval / 1000;
1078   msecs = timeout_data->interval - seconds * 1000;
1079
1080   timeout_data->expiration.tv_sec += seconds;
1081   timeout_data->expiration.tv_usec += msecs * 1000;
1082   if (timeout_data->expiration.tv_usec >= 1000000)
1083     {
1084       timeout_data->expiration.tv_usec -= 1000000;
1085       timeout_data->expiration.tv_sec++;
1086     }
1087
1088   return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
1089 }
1090
1091 guint 
1092 g_timeout_add (guint32        interval,
1093                GSourceFunc    function,
1094                gpointer       data)
1095 {
1096   return g_timeout_add_full (G_PRIORITY_DEFAULT, 
1097                              interval, function, data, NULL);
1098 }
1099
1100 /* Idle functions */
1101
1102 static gboolean 
1103 g_idle_prepare  (gpointer source_data, 
1104                  GTimeVal *current_time,
1105                  gint     *timeout)
1106 {
1107   timeout = 0;
1108   return TRUE;
1109 }
1110
1111 static gboolean 
1112 g_idle_check    (gpointer  source_data,
1113                  GTimeVal *current_time)
1114 {
1115   return TRUE;
1116 }
1117
1118 static gboolean
1119 g_idle_dispatch (gpointer source_data, 
1120                  GTimeVal *current_time,
1121                  gpointer user_data)
1122 {
1123   GIdleData *data = source_data;
1124
1125   return (*data->callback)(user_data);
1126 }
1127
1128 guint 
1129 g_idle_add_full (gint          priority,
1130                  GSourceFunc    function,
1131                  gpointer       data,
1132                  GDestroyNotify notify)
1133 {
1134   GIdleData *idle_data = g_new (GIdleData, 1);
1135
1136   idle_data->callback = function;
1137
1138   return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
1139 }
1140
1141 guint 
1142 g_idle_add (GSourceFunc    function,
1143             gpointer       data)
1144 {
1145   return g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, function, data, NULL);
1146 }