Merge main loop into head. This probably breaks Win32, until
[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 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 #include "glib.h"
24 #include <sys/time.h>
25 #include <unistd.h>
26 #include "config.h"
27
28 /* Types */
29
30 typedef struct _GIdleData GIdleData;
31 typedef struct _GTimeoutData GTimeoutData;
32 typedef struct _GSource GSource;
33 typedef struct _GPollRec GPollRec;
34
35 typedef enum {
36   G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
37   G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
38 } GSourceFlags;
39
40 struct _GSource {
41   GHook hook;
42   gint priority;
43   gpointer source_data;
44 };
45
46 struct _GMainLoop {
47   gboolean flag;
48 };
49
50 struct _GIdleData {
51   GSourceFunc callback;
52 };
53
54 struct _GTimeoutData {
55   GTimeVal    expiration;
56   gint        interval;
57   GSourceFunc callback;
58 };
59
60 struct _GPollRec {
61   gint priority;
62   GPollFD *fd;
63   GPollRec *next;
64 };
65
66 /* Forward declarations */
67
68 static void     g_main_poll            (gint      timeout,
69                                         gboolean  use_priority, 
70                                         gint      priority);
71 static gboolean g_timeout_prepare      (gpointer  source_data, 
72                                         GTimeVal *current_time,
73                                         gint     *timeout);
74 static gboolean g_timeout_check        (gpointer  source_data,
75                                         GTimeVal *current_time);
76 static gboolean g_timeout_dispatch     (gpointer  source_data,
77                                         GTimeVal *current_time,
78                                         gpointer  user_data);
79 static gboolean g_idle_prepare         (gpointer  source_data, 
80                                         GTimeVal *current_time,
81                                         gint     *timeout);
82 static gboolean g_idle_check           (gpointer  source_data,
83                                         GTimeVal *current_time);
84 static gboolean g_idle_dispatch        (gpointer  source_data,
85                                         GTimeVal *current_time,
86                                         gpointer  user_data);
87
88 /* Data */
89
90 static GSList *pending_dispatches = NULL;
91 static GHookList source_list = { 0 };
92
93 static GSourceFuncs timeout_funcs = {
94   g_timeout_prepare,
95   g_timeout_check,
96   g_timeout_dispatch,
97   (GDestroyNotify)g_free
98 };
99
100 static GSourceFuncs idle_funcs = {
101   g_idle_prepare,
102   g_idle_check,
103   g_idle_dispatch,
104   (GDestroyNotify)g_free
105 };
106
107 #ifdef HAVE_POLL
108 static GPollFunc poll_func = (GPollFunc)poll;
109 #else
110
111 /* The following implementation of poll() comes from the GNU C Library.
112  * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
113  */
114
115 #ifdef HAVE_SYS_SELECT_H
116 #include <sys/select.h>
117 #endif /* HAVE_SYS_SELECT_H_ */
118
119 #ifndef NO_FD_SET
120 #  define SELECT_MASK fd_set
121 #else
122 #  ifndef _AIX
123 typedef long fd_mask;
124 #  endif
125 #  if defined(_IBMR2)
126 #    define SELECT_MASK void
127 #  else
128 #    define SELECT_MASK int
129 #  endif
130 #endif
131
132 static gint 
133 g_poll (GPollFD *fds, guint nfds, gint timeout)
134 {
135   struct timeval tv;
136   SELECT_MASK rset, wset, xset;
137   GPollFD *f;
138   int ready;
139   int maxfd = 0;
140
141   FD_ZERO (&rset);
142   FD_ZERO (&wset);
143   FD_ZERO (&xset);
144
145   for (f = fds; f < &fds[nfds]; ++f)
146     if (f->fd >= 0)
147       {
148         if (f->events & G_IO_IN)
149           FD_SET (f->fd, &rset);
150         if (f->events & G_IO_OUT)
151           FD_SET (f->fd, &wset);
152         if (f->events & G_IO_PRI)
153           FD_SET (f->fd, &xset);
154         if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
155           maxfd = f->fd;
156       }
157
158   tv.tv_sec = timeout / 1000;
159   tv.tv_usec = (timeout % 1000) * 1000;
160
161   ready = select (maxfd + 1, &rset, &wset, &xset,
162                   timeout == -1 ? NULL : &tv);
163   if (ready > 0)
164     for (f = fds; f < &fds[nfds]; ++f)
165       {
166         f->revents = 0;
167         if (f->fd >= 0)
168           {
169             if (FD_ISSET (f->fd, &rset))
170               f->revents |= G_IO_IN;
171             if (FD_ISSET (f->fd, &wset))
172               f->revents |= G_IO_OUT;
173             if (FD_ISSET (f->fd, &xset))
174               f->revents |= G_IO_PRI;
175           }
176       }
177
178   return ready;
179 }
180
181 static GPollFunc poll_func = g_poll;
182 #endif
183
184 /* Hooks for adding to the main loop */
185
186 /* Use knowledge of insert_sorted algorithm here to make
187  * sure we insert at the end of equal priority items
188  */
189 static gint
190 g_source_compare (GHook *a, GHook *b)
191 {
192   GSource *source_a = (GSource *)a;
193   GSource *source_b = (GSource *)b;
194
195   return (source_a->priority < source_b->priority) ? -1 : 1;
196 }
197
198 guint 
199 g_source_add (gint           priority,
200               gboolean       can_recurse,
201               GSourceFuncs  *funcs,
202               gpointer       source_data, 
203               gpointer       user_data,
204               GDestroyNotify notify)
205 {
206   GSource *source;
207
208   if (!source_list.is_setup)
209     g_hook_list_init (&source_list, sizeof(GSource));
210
211   source = (GSource *)g_hook_alloc (&source_list);
212   source->priority = priority;
213   source->source_data = source_data;
214   source->hook.func = funcs;
215   source->hook.data = user_data;
216   source->hook.destroy = notify;
217   
218   g_hook_insert_sorted (&source_list, 
219                         (GHook *)source, 
220                         g_source_compare);
221
222   if (can_recurse)
223     source->hook.flags |= G_SOURCE_CAN_RECURSE;
224
225   return source->hook.hook_id;
226 }
227
228 void 
229 g_source_remove (guint tag)
230 {
231   GHook *hook = g_hook_get (&source_list, tag);
232   if (hook)
233     {
234       GSource *source = (GSource *)hook;
235       ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
236       g_hook_destroy_link (&source_list, hook);
237     }
238 }
239
240 void 
241 g_source_remove_by_user_data (gpointer user_data)
242 {
243   GHook *hook = g_hook_find_data (&source_list, TRUE, user_data);
244   if (hook)
245     {
246       GSource *source = (GSource *)hook;
247       ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
248       g_hook_destroy_link (&source_list, hook);
249     }
250 }
251
252 static gboolean
253 g_source_find_source_data (GHook        *hook,
254                            gpointer      data)
255 {
256   GSource *source = (GSource *)hook;
257   return (source->source_data == data);
258 }
259
260 void 
261 g_source_remove_by_source_data (gpointer source_data)
262 {
263   GHook *hook = g_hook_find (&source_list, TRUE, 
264                              g_source_find_source_data, source_data);
265   if (hook)
266     {
267       GSource *source = (GSource *)hook;
268       ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
269       g_hook_destroy_link (&source_list, hook);
270     }
271 }
272
273 void g_get_current_time (GTimeVal *result)
274 {
275   gettimeofday ((struct timeval *)result, NULL);
276 }
277
278 /* Running the main loop */
279
280 static void
281 g_main_dispatch (GTimeVal *current_time)
282 {
283   while (pending_dispatches != NULL)
284     {
285       gboolean need_destroy;
286       GSource *source = pending_dispatches->data;
287       GSList *tmp_list;
288
289       tmp_list = pending_dispatches;
290       pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
291       g_slist_free_1 (tmp_list);
292
293       if (G_HOOK_IS_VALID (source))
294         {
295           source->hook.flags |= G_HOOK_FLAG_IN_CALL;
296           need_destroy = !((GSourceFuncs *)source->hook.func)->dispatch (source->source_data, 
297                                                    current_time,
298                                                    source->hook.data);
299           source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
300           
301           if (need_destroy)
302             g_hook_destroy_link (&source_list, (GHook *)source);
303         }
304
305       g_hook_unref (&source_list, (GHook *)source);
306     }
307 }
308
309 /* Run a single iteration of the mainloop, or, if !dispatch
310  * check to see if any events need dispatching, but don't
311  * run the loop.
312  */
313 static gboolean
314 g_main_iterate (gboolean block, gboolean dispatch)
315 {
316   GHook *hook;
317   GTimeVal current_time;
318   gint nready = 0;
319   gint current_priority = 0;
320   gint timeout;
321
322   g_return_val_if_fail (!block || dispatch, FALSE);
323
324   g_get_current_time (&current_time);
325   
326   /* If recursing, finish up current dispatch, before starting over */
327   if (pending_dispatches)
328     {
329       if (dispatch)
330         g_main_dispatch (&current_time);
331       
332       return TRUE;
333     }
334
335   /* Prepare all sources */
336
337   timeout = block ? -1 : 0;
338   
339   hook = g_hook_first_valid (&source_list, TRUE);
340   while (hook)
341     {
342       GSource *source = (GSource *)hook;
343       GHook *tmp;
344       gint source_timeout;
345
346       if ((nready > 0) && (source->priority > current_priority))
347         break;
348       if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
349         {
350           hook = g_hook_next_valid (hook, TRUE);
351           continue;
352         }
353
354       g_hook_ref (&source_list, hook);
355
356       if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
357                                                  &current_time,
358                                                  &source_timeout))
359         {
360           if (!dispatch)
361             {
362               g_hook_unref (&source_list, hook);
363               return TRUE;
364             }
365           else
366             {
367               hook->flags |= G_SOURCE_READY;
368               nready++;
369               current_priority = source->priority;
370               timeout = 0;
371             }
372         }
373       
374       if (source_timeout >= 0)
375         {
376           if (timeout < 0)
377             timeout = source_timeout;
378           else
379             timeout = MIN (timeout, source_timeout);
380         }
381
382       tmp = g_hook_next_valid (hook, TRUE);
383       
384       g_hook_unref (&source_list, hook);
385       hook = tmp;
386     }
387
388   /* poll(), if necessary */
389
390   g_main_poll (timeout, nready > 0, current_priority);
391
392   /* Check to see what sources need to be dispatched */
393
394   nready = 0;
395   
396   hook = g_hook_first_valid (&source_list, TRUE);
397   while (hook)
398     {
399       GSource *source = (GSource *)hook;
400       GHook *tmp;
401
402       if ((nready > 0) && (source->priority > current_priority))
403         break;
404       if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
405         {
406           hook = g_hook_next_valid (hook, TRUE);
407           continue;
408         }
409
410       g_hook_ref (&source_list, hook);
411
412       if ((hook->flags & G_SOURCE_READY) ||
413           ((GSourceFuncs *)hook->func)->check (source->source_data,
414                                                &current_time))
415         {
416           if (dispatch)
417             {
418               hook->flags &= ~G_SOURCE_READY;
419               g_hook_ref (&source_list, hook);
420               pending_dispatches = g_slist_prepend (pending_dispatches, source);
421               current_priority = source->priority;
422               nready++;
423             }
424           else
425             {
426               g_hook_unref (&source_list, hook);
427               return TRUE;
428             }
429         }
430       
431       tmp = g_hook_next_valid (hook, TRUE);
432       
433       g_hook_unref (&source_list, hook);
434       hook = tmp;
435     }
436
437   /* Now invoke the callbacks */
438
439   if (pending_dispatches)
440     {
441       pending_dispatches = g_slist_reverse (pending_dispatches);
442       g_main_dispatch (&current_time);
443       return TRUE;
444     }
445   else
446     return FALSE;
447 }
448
449 /* See if any events are pending
450  */
451 gboolean 
452 g_main_pending ()
453 {
454   return g_main_iterate (FALSE, FALSE);
455 }
456
457 /* Run a single iteration of the mainloop. If block is FALSE,
458  * will never block
459  */
460 gboolean
461 g_main_iteration (gboolean block)
462 {
463   return g_main_iterate (block, TRUE);
464 }
465
466 GMainLoop *
467 g_main_new ()
468 {
469   GMainLoop *result = g_new (GMainLoop, 1);
470   result->flag = FALSE;
471
472   return result;
473 }
474
475 void 
476 g_main_run (GMainLoop *loop)
477 {
478   loop->flag = FALSE;
479   while (!loop->flag)
480     g_main_iterate (TRUE, TRUE);
481 }
482
483 void 
484 g_main_quit (GMainLoop *loop)
485 {
486   loop->flag = TRUE;
487 }
488
489 void 
490 g_main_destroy (GMainLoop *loop)
491 {
492   g_free (loop);
493 }
494
495 static GPollRec *poll_records = NULL;
496 static GPollRec *poll_free_list = NULL;
497 static GMemChunk *poll_chunk;
498 static guint n_poll_records = 0;
499
500 static void
501 g_main_poll (gint timeout, gboolean use_priority, gint priority)
502 {
503   GPollFD *fd_array = g_new (GPollFD, n_poll_records);
504   GPollRec *pollrec;
505
506   gint i;
507   gint npoll;
508
509   pollrec = poll_records;
510   i = 0;
511   while (pollrec && (!use_priority || priority >= pollrec->priority))
512     {
513       fd_array[i].fd = pollrec->fd->fd;
514       fd_array[i].events = pollrec->fd->events;
515       fd_array[i].revents = 0;
516         
517       pollrec = pollrec->next;
518       i++;
519     }
520
521   npoll = i;
522   (*poll_func) (fd_array, npoll, timeout);
523
524   pollrec = poll_records;
525   i = 0;
526   while (i < npoll)
527     {
528       pollrec->fd->revents = fd_array[i].revents;
529       pollrec = pollrec->next;
530       i++;
531     }
532
533   g_free (fd_array);
534 }
535
536 void 
537 g_main_poll_add (gint     priority,
538                  GPollFD *fd)
539 {
540   GPollRec *lastrec, *pollrec, *newrec;
541
542   if (!poll_chunk)
543     poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
544
545   newrec = g_chunk_new (GPollRec, poll_chunk);
546   newrec->fd = fd;
547   newrec->priority = priority;
548
549   lastrec = NULL;
550   pollrec = poll_records;
551   while (pollrec && priority >= pollrec->priority)
552     {
553       lastrec = pollrec;
554       pollrec = pollrec->next;
555     }
556   
557   if (lastrec)
558     lastrec->next = newrec;
559   else
560     poll_records = newrec;
561
562   newrec->next = pollrec;
563
564   n_poll_records++;
565 }
566
567 void 
568 g_main_poll_remove (GPollFD *fd)
569 {
570   GPollRec *pollrec, *lastrec;
571
572   lastrec = NULL;
573   pollrec = poll_records;
574
575   while (pollrec)
576     {
577       if (pollrec->fd == fd)
578         {
579           if (lastrec != NULL)
580             lastrec->next = pollrec->next;
581           else
582             poll_records = pollrec->next;
583
584           pollrec->next = poll_free_list;
585           poll_free_list = pollrec;
586         }
587       lastrec = pollrec;
588       pollrec = pollrec->next;
589     }
590
591   n_poll_records--;
592 }
593
594 void 
595 g_main_set_poll_func (GPollFunc func)
596 {
597   if (func)
598     poll_func = func;
599   else
600     poll_func = (GPollFunc)poll;
601 }
602
603 /* Timeouts */
604
605 static gboolean 
606 g_timeout_prepare  (gpointer source_data, 
607                     GTimeVal *current_time,
608                     gint    *timeout)
609 {
610   glong msec;
611   GTimeoutData *data = source_data;
612
613   msec = (data->expiration.tv_sec  - current_time->tv_sec) * 1000 +
614          (data->expiration.tv_usec - current_time->tv_usec) / 1000;
615
616   *timeout = (msec <= 0) ? 0 : msec;
617
618   return (msec <= 0);
619 }
620
621 static gboolean 
622 g_timeout_check    (gpointer source_data,
623                     GTimeVal *current_time)
624 {
625   GTimeoutData *data = source_data;
626
627   return (data->expiration.tv_sec < current_time->tv_sec) ||
628          ((data->expiration.tv_sec == current_time->tv_sec) &&
629           (data->expiration.tv_usec <= current_time->tv_usec));
630 }
631
632 static gboolean
633 g_timeout_dispatch (gpointer source_data, 
634                     GTimeVal *current_time,
635                     gpointer user_data)
636 {
637   GTimeoutData *data = source_data;
638
639   if (data->callback(user_data))
640     {
641       data->expiration.tv_sec = current_time->tv_sec;
642       data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
643       if (data->expiration.tv_usec >= 1000000)
644         {
645           data->expiration.tv_usec -= 1000000;
646           data->expiration.tv_sec++;
647         }
648       return TRUE;
649     }
650   else
651     return FALSE;
652 }
653
654 guint 
655 g_timeout_add_full (gint           priority,
656                     guint          interval, 
657                     GSourceFunc    function,
658                     gpointer       data,
659                     GDestroyNotify notify)
660 {
661   GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
662
663   timeout_data->interval = interval;
664   timeout_data->callback = function;
665   g_get_current_time (&timeout_data->expiration);
666
667   timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
668   if (timeout_data->expiration.tv_usec >= 1000000)
669     {
670       timeout_data->expiration.tv_usec -= 1000000;
671       timeout_data->expiration.tv_sec++;
672     }
673
674   return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
675 }
676
677 guint 
678 g_timeout_add (guint32        interval,
679                GSourceFunc    function,
680                gpointer       data)
681 {
682   return g_timeout_add_full (0, interval, function, data, NULL);
683 }
684
685 /* Idle functions */
686
687 static gboolean 
688 g_idle_prepare  (gpointer source_data, 
689                  GTimeVal *current_time,
690                  gint     *timeout)
691 {
692   timeout = 0;
693   return TRUE;
694 }
695
696 static gboolean 
697 g_idle_check    (gpointer  source_data,
698                  GTimeVal *current_time)
699 {
700   return TRUE;
701 }
702
703 static gboolean
704 g_idle_dispatch (gpointer source_data, 
705                  GTimeVal *current_time,
706                  gpointer user_data)
707 {
708   GIdleData *data = source_data;
709
710   return (*data->callback)(user_data);
711 }
712
713 guint 
714 g_idle_add_full (gint          priority,
715                  GSourceFunc    function,
716                  gpointer       data,
717                  GDestroyNotify notify)
718 {
719   GIdleData *idle_data = g_new (GIdleData, 1);
720
721   idle_data->callback = function;
722
723   return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
724 }
725
726 guint 
727 g_idle_add (GSourceFunc    function,
728             gpointer       data)
729 {
730   return g_idle_add_full (0, function, data, NULL);
731 }