a998ccea78fd9636cfb211a041c9fc413ea8eaaf
[platform/upstream/dbus.git] / dbus / dbus-mainloop.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-mainloop.c  Main loop utility
3  *
4  * Copyright (C) 2003  Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.0
7  * 
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include "dbus-mainloop.h"
25
26 #ifndef DOXYGEN_SHOULD_SKIP_THIS
27
28 #include <dbus/dbus-list.h>
29 #include <dbus/dbus-sysdeps.h>
30
31 #define MAINLOOP_SPEW 0
32
33 struct DBusLoop
34 {
35   int refcount;
36   DBusList *callbacks;
37   int callback_list_serial;
38   int watch_count;
39   int timeout_count;
40   int depth; /**< number of recursive runs */
41   DBusList *need_dispatch;
42 };
43
44 typedef enum
45 {
46   CALLBACK_WATCH,
47   CALLBACK_TIMEOUT
48 } CallbackType;
49
50 typedef struct
51 {
52   int refcount;
53   CallbackType type;
54   void *data;
55   DBusFreeFunction free_data_func;
56 } Callback;
57
58 typedef struct
59 {
60   Callback callback;
61   DBusWatchFunction function;
62   DBusWatch *watch;
63   /* last watch handle failed due to OOM */
64   unsigned int last_iteration_oom : 1;
65 } WatchCallback;
66
67 typedef struct
68 {
69   Callback callback;
70   DBusTimeout *timeout;
71   DBusTimeoutFunction function;
72   unsigned long last_tv_sec;
73   unsigned long last_tv_usec;
74 } TimeoutCallback;
75
76 #define WATCH_CALLBACK(callback)   ((WatchCallback*)callback)
77 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
78
79 static WatchCallback*
80 watch_callback_new (DBusWatch         *watch,
81                     DBusWatchFunction  function,
82                     void              *data,
83                     DBusFreeFunction   free_data_func)
84 {
85   WatchCallback *cb;
86
87   cb = dbus_new (WatchCallback, 1);
88   if (cb == NULL)
89     return NULL;
90
91   cb->watch = watch;
92   cb->function = function;
93   cb->last_iteration_oom = FALSE;
94   cb->callback.refcount = 1;
95   cb->callback.type = CALLBACK_WATCH;
96   cb->callback.data = data;
97   cb->callback.free_data_func = free_data_func;
98   
99   return cb;
100 }
101
102 static TimeoutCallback*
103 timeout_callback_new (DBusTimeout         *timeout,
104                       DBusTimeoutFunction  function,
105                       void                *data,
106                       DBusFreeFunction     free_data_func)
107 {
108   TimeoutCallback *cb;
109
110   cb = dbus_new (TimeoutCallback, 1);
111   if (cb == NULL)
112     return NULL;
113
114   cb->timeout = timeout;
115   cb->function = function;
116   _dbus_get_current_time (&cb->last_tv_sec,
117                           &cb->last_tv_usec);
118   cb->callback.refcount = 1;    
119   cb->callback.type = CALLBACK_TIMEOUT;
120   cb->callback.data = data;
121   cb->callback.free_data_func = free_data_func;
122   
123   return cb;
124 }
125
126 static Callback * 
127 callback_ref (Callback *cb)
128 {
129   _dbus_assert (cb->refcount > 0);
130   
131   cb->refcount += 1;
132
133   return cb;
134 }
135
136 static void
137 callback_unref (Callback *cb)
138 {
139   _dbus_assert (cb->refcount > 0);
140
141   cb->refcount -= 1;
142
143   if (cb->refcount == 0)
144     {
145       if (cb->free_data_func)
146         (* cb->free_data_func) (cb->data);
147       
148       dbus_free (cb);
149     }
150 }
151
152 static dbus_bool_t
153 add_callback (DBusLoop  *loop,
154               Callback *cb)
155 {
156   if (!_dbus_list_append (&loop->callbacks, cb))
157     return FALSE;
158
159   loop->callback_list_serial += 1;
160
161   switch (cb->type)
162     {
163     case CALLBACK_WATCH:
164       loop->watch_count += 1;
165       break;
166     case CALLBACK_TIMEOUT:
167       loop->timeout_count += 1;
168       break;
169     }
170   
171   return TRUE;
172 }
173
174 static void
175 remove_callback (DBusLoop  *loop,
176                  DBusList *link)
177 {
178   Callback *cb = link->data;
179   
180   switch (cb->type)
181     {
182     case CALLBACK_WATCH:
183       loop->watch_count -= 1;
184       break;
185     case CALLBACK_TIMEOUT:
186       loop->timeout_count -= 1;
187       break;
188     }
189   
190   callback_unref (cb);
191   _dbus_list_remove_link (&loop->callbacks, link);
192   loop->callback_list_serial += 1;
193 }
194
195 DBusLoop*
196 _dbus_loop_new (void)
197 {
198   DBusLoop *loop;
199
200   loop = dbus_new0 (DBusLoop, 1);
201   if (loop == NULL)
202     return NULL;
203
204   loop->refcount = 1;
205   
206   return loop;
207 }
208
209 DBusLoop *
210 _dbus_loop_ref (DBusLoop *loop)
211 {
212   _dbus_assert (loop != NULL);
213   _dbus_assert (loop->refcount > 0);
214
215   loop->refcount += 1;
216
217   return loop;
218 }
219
220 void
221 _dbus_loop_unref (DBusLoop *loop)
222 {
223   _dbus_assert (loop != NULL);
224   _dbus_assert (loop->refcount > 0);
225
226   loop->refcount -= 1;
227   if (loop->refcount == 0)
228     {
229       while (loop->need_dispatch)
230         {
231           DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
232
233           dbus_connection_unref (connection);
234         }
235       
236       dbus_free (loop);
237     }
238 }
239
240 dbus_bool_t
241 _dbus_loop_add_watch (DBusLoop          *loop,
242                       DBusWatch        *watch,
243                       DBusWatchFunction  function,
244                       void             *data,
245                       DBusFreeFunction  free_data_func)
246 {
247   WatchCallback *wcb;
248
249   wcb = watch_callback_new (watch, function, data, free_data_func);
250   if (wcb == NULL)
251     return FALSE;
252
253   if (!add_callback (loop, (Callback*) wcb))
254     {
255       wcb->callback.free_data_func = NULL; /* don't want to have this side effect */
256       callback_unref ((Callback*) wcb);
257       return FALSE;
258     }
259   
260   return TRUE;
261 }
262
263 void
264 _dbus_loop_remove_watch (DBusLoop          *loop,
265                          DBusWatch        *watch,
266                          DBusWatchFunction  function,
267                          void             *data)
268 {
269   DBusList *link;
270   
271   link = _dbus_list_get_first_link (&loop->callbacks);
272   while (link != NULL)
273     {
274       DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
275       Callback *this = link->data;
276
277       if (this->type == CALLBACK_WATCH &&
278           WATCH_CALLBACK (this)->watch == watch &&
279           this->data == data &&
280           WATCH_CALLBACK (this)->function == function)
281         {
282           remove_callback (loop, link);
283           
284           return;
285         }
286       
287       link = next;
288     }
289
290   _dbus_warn ("could not find watch %p function %p data %p to remove\n",
291               watch, (void *)function, data);
292 }
293
294 dbus_bool_t
295 _dbus_loop_add_timeout (DBusLoop            *loop,
296                         DBusTimeout        *timeout,
297                         DBusTimeoutFunction  function,
298                         void               *data,
299                         DBusFreeFunction    free_data_func)
300 {
301   TimeoutCallback *tcb;
302
303   tcb = timeout_callback_new (timeout, function, data, free_data_func);
304   if (tcb == NULL)
305     return FALSE;
306
307   if (!add_callback (loop, (Callback*) tcb))
308     {
309       tcb->callback.free_data_func = NULL; /* don't want to have this side effect */
310       callback_unref ((Callback*) tcb);
311       return FALSE;
312     }
313   
314   return TRUE;
315 }
316
317 void
318 _dbus_loop_remove_timeout (DBusLoop            *loop,
319                            DBusTimeout        *timeout,
320                            DBusTimeoutFunction  function,
321                            void               *data)
322 {
323   DBusList *link;
324   
325   link = _dbus_list_get_first_link (&loop->callbacks);
326   while (link != NULL)
327     {
328       DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
329       Callback *this = link->data;
330
331       if (this->type == CALLBACK_TIMEOUT &&
332           TIMEOUT_CALLBACK (this)->timeout == timeout &&
333           this->data == data &&
334           TIMEOUT_CALLBACK (this)->function == function)
335         {
336           remove_callback (loop, link);
337           
338           return;
339         }
340       
341       link = next;
342     }
343
344   _dbus_warn ("could not find timeout %p function %p data %p to remove\n",
345               timeout, (void *)function, data);
346 }
347
348 /* Convolutions from GLib, there really must be a better way
349  * to do this.
350  */
351 static dbus_bool_t
352 check_timeout (unsigned long    tv_sec,
353                unsigned long    tv_usec,
354                TimeoutCallback *tcb,
355                int             *timeout)
356 {
357   long sec_remaining;
358   long msec_remaining;
359   unsigned long expiration_tv_sec;
360   unsigned long expiration_tv_usec;
361   long interval_seconds;
362   long interval_milliseconds;
363   int interval;
364
365   /* I'm pretty sure this function could suck (a lot) less */
366   
367   interval = dbus_timeout_get_interval (tcb->timeout);
368   
369   interval_seconds = interval / 1000L;
370   interval_milliseconds = interval % 1000L;
371   
372   expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
373   expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
374   if (expiration_tv_usec >= 1000000)
375     {
376       expiration_tv_usec -= 1000000;
377       expiration_tv_sec += 1;
378     }
379   
380   sec_remaining = expiration_tv_sec - tv_sec;
381   /* need to force this to be signed, as it is intended to sometimes
382    * produce a negative result
383    */
384   msec_remaining = ((long) expiration_tv_usec - (long) tv_usec) / 1000L;
385
386 #if MAINLOOP_SPEW
387   _dbus_verbose ("Interval is %ld seconds %ld msecs\n",
388                  interval_seconds,
389                  interval_milliseconds);
390   _dbus_verbose ("Now is  %lu seconds %lu usecs\n",
391                  tv_sec, tv_usec);
392   _dbus_verbose ("Last is %lu seconds %lu usecs\n",
393                  tcb->last_tv_sec, tcb->last_tv_usec);
394   _dbus_verbose ("Exp is  %lu seconds %lu usecs\n",
395                  expiration_tv_sec, expiration_tv_usec);
396   _dbus_verbose ("Pre-correction, sec_remaining %ld msec_remaining %ld\n",
397                  sec_remaining, msec_remaining);
398 #endif
399   
400   /* We do the following in a rather convoluted fashion to deal with
401    * the fact that we don't have an integral type big enough to hold
402    * the difference of two timevals in milliseconds.
403    */
404   if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
405     {
406       *timeout = 0;
407     }
408   else
409     {
410       if (msec_remaining < 0)
411         {
412           msec_remaining += 1000;
413           sec_remaining -= 1;
414         }
415
416       if (sec_remaining > (_DBUS_INT_MAX / 1000) ||
417           msec_remaining > _DBUS_INT_MAX)
418         *timeout = _DBUS_INT_MAX;
419       else
420         *timeout = sec_remaining * 1000 + msec_remaining;        
421     }
422
423   if (*timeout > interval)
424     {
425       /* This indicates that the system clock probably moved backward */
426       _dbus_verbose ("System clock set backward! Resetting timeout.\n");
427       
428       tcb->last_tv_sec = tv_sec;
429       tcb->last_tv_usec = tv_usec;
430
431       *timeout = interval;
432     }
433   
434 #if MAINLOOP_SPEW
435   _dbus_verbose ("  timeout expires in %d milliseconds\n", *timeout);
436 #endif
437   
438   return *timeout == 0;
439 }
440
441 dbus_bool_t
442 _dbus_loop_dispatch (DBusLoop *loop)
443 {
444
445 #if MAINLOOP_SPEW
446   _dbus_verbose ("  %d connections to dispatch\n", _dbus_list_get_length (&loop->need_dispatch));
447 #endif
448   
449   if (loop->need_dispatch == NULL)
450     return FALSE;
451   
452  next:
453   while (loop->need_dispatch != NULL)
454     {
455       DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
456       
457       while (TRUE)
458         {
459           DBusDispatchStatus status;
460           
461           status = dbus_connection_dispatch (connection);
462
463           if (status == DBUS_DISPATCH_COMPLETE)
464             {
465               dbus_connection_unref (connection);
466               goto next;
467             }
468           else
469             {
470               if (status == DBUS_DISPATCH_NEED_MEMORY)
471                 _dbus_wait_for_memory ();
472             }
473         }
474     }
475
476   return TRUE;
477 }
478
479 dbus_bool_t
480 _dbus_loop_queue_dispatch (DBusLoop       *loop,
481                            DBusConnection *connection)
482 {
483   if (_dbus_list_append (&loop->need_dispatch, connection))
484     {
485       dbus_connection_ref (connection);
486       return TRUE;
487     }
488   else
489     return FALSE;
490 }
491
492 /* Returns TRUE if we invoked any timeouts or have ready file
493  * descriptors, which is just used in test code as a debug hack
494  */
495
496 dbus_bool_t
497 _dbus_loop_iterate (DBusLoop     *loop,
498                     dbus_bool_t   block)
499 {  
500 #define N_STACK_DESCRIPTORS 64
501   dbus_bool_t retval;
502   DBusPollFD *fds;
503   DBusPollFD stack_fds[N_STACK_DESCRIPTORS];
504   int n_fds;
505   WatchCallback **watches_for_fds;
506   WatchCallback *stack_watches_for_fds[N_STACK_DESCRIPTORS];
507   int i;
508   DBusList *link;
509   int n_ready;
510   int initial_serial;
511   long timeout;
512   dbus_bool_t oom_watch_pending;
513   int orig_depth;
514   
515   retval = FALSE;      
516
517   fds = NULL;
518   watches_for_fds = NULL;
519   n_fds = 0;
520   oom_watch_pending = FALSE;
521   orig_depth = loop->depth;
522   
523 #if MAINLOOP_SPEW
524   _dbus_verbose ("Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
525                  block, loop->depth, loop->timeout_count, loop->watch_count);
526 #endif
527   
528   if (loop->callbacks == NULL)
529     goto next_iteration;
530
531   if (loop->watch_count > N_STACK_DESCRIPTORS)
532     {
533       fds = dbus_new0 (DBusPollFD, loop->watch_count);
534       
535       while (fds == NULL)
536         {
537           _dbus_wait_for_memory ();
538           fds = dbus_new0 (DBusPollFD, loop->watch_count);
539         }
540       
541       watches_for_fds = dbus_new (WatchCallback*, loop->watch_count);
542       while (watches_for_fds == NULL)
543         {
544           _dbus_wait_for_memory ();
545           watches_for_fds = dbus_new (WatchCallback*, loop->watch_count);
546         }
547     }
548   else
549     {      
550       fds = stack_fds;
551       watches_for_fds = stack_watches_for_fds;
552     }
553
554   /* fill our array of fds and watches */
555   n_fds = 0;
556   link = _dbus_list_get_first_link (&loop->callbacks);
557   while (link != NULL)
558     {
559       DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
560       Callback *cb = link->data;
561       if (cb->type == CALLBACK_WATCH)
562         {
563           unsigned int flags;
564           WatchCallback *wcb = WATCH_CALLBACK (cb);
565
566           if (wcb->last_iteration_oom)
567             {
568               /* we skip this one this time, but reenable it next time,
569                * and have a timeout on this iteration
570                */
571               wcb->last_iteration_oom = FALSE;
572               oom_watch_pending = TRUE;
573               
574               retval = TRUE; /* return TRUE here to keep the loop going,
575                               * since we don't know the watch is inactive
576                               */
577
578 #if MAINLOOP_SPEW
579               _dbus_verbose ("  skipping watch on fd %d as it was out of memory last time\n",
580                              dbus_watch_get_fd (wcb->watch));
581 #endif
582             }
583           else if (dbus_watch_get_enabled (wcb->watch))
584             {
585               watches_for_fds[n_fds] = wcb;
586
587               callback_ref (cb);
588                   
589               flags = dbus_watch_get_flags (wcb->watch);
590                   
591               fds[n_fds].fd = dbus_watch_get_fd (wcb->watch);
592               fds[n_fds].revents = 0;
593               fds[n_fds].events = 0;
594               if (flags & DBUS_WATCH_READABLE)
595                 fds[n_fds].events |= _DBUS_POLLIN;
596               if (flags & DBUS_WATCH_WRITABLE)
597                 fds[n_fds].events |= _DBUS_POLLOUT;
598
599 #if MAINLOOP_SPEW
600               _dbus_verbose ("  polling watch on fd %d\n", fds[n_fds].fd);
601 #endif
602
603               n_fds += 1;
604             }
605           else
606             {
607 #if MAINLOOP_SPEW
608               _dbus_verbose ("  skipping disabled watch on fd %d\n",
609                              dbus_watch_get_fd (wcb->watch));
610 #endif
611             }
612         }
613               
614       link = next;
615     }
616   
617   timeout = -1;
618   if (loop->timeout_count > 0)
619     {
620       unsigned long tv_sec;
621       unsigned long tv_usec;
622       
623       _dbus_get_current_time (&tv_sec, &tv_usec);
624           
625       link = _dbus_list_get_first_link (&loop->callbacks);
626       while (link != NULL)
627         {
628           DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
629           Callback *cb = link->data;
630
631           if (cb->type == CALLBACK_TIMEOUT &&
632               dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
633             {
634               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
635               int msecs_remaining;
636
637               check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
638
639               if (timeout < 0)
640                 timeout = msecs_remaining;
641               else
642                 timeout = MIN (msecs_remaining, timeout);
643
644 #if MAINLOOP_SPEW
645               _dbus_verbose ("  timeout added, %d remaining, aggregate timeout %ld\n",
646                              msecs_remaining, timeout);
647 #endif
648               
649               _dbus_assert (timeout >= 0);
650                   
651               if (timeout == 0)
652                 break; /* it's not going to get shorter... */
653             }
654 #if MAINLOOP_SPEW
655           else if (cb->type == CALLBACK_TIMEOUT)
656             {
657               _dbus_verbose ("  skipping disabled timeout\n");
658             }
659 #endif
660           
661           link = next;
662         }
663     }
664
665   /* Never block if we have stuff to dispatch */
666   if (!block || loop->need_dispatch != NULL)
667     {
668       timeout = 0;
669 #if MAINLOOP_SPEW
670       _dbus_verbose ("  timeout is 0 as we aren't blocking\n");
671 #endif
672     }
673
674   /* if a watch is OOM, don't wait longer than the OOM
675    * wait to re-enable it
676    */
677   if (oom_watch_pending)
678     timeout = MIN (timeout, _dbus_get_oom_wait ());
679
680 #if MAINLOOP_SPEW
681   _dbus_verbose ("  polling on %d descriptors timeout %ld\n", n_fds, timeout);
682 #endif
683   
684   n_ready = _dbus_poll (fds, n_fds, timeout);
685
686   initial_serial = loop->callback_list_serial;
687
688   if (loop->timeout_count > 0)
689     {
690       unsigned long tv_sec;
691       unsigned long tv_usec;
692
693       _dbus_get_current_time (&tv_sec, &tv_usec);
694
695       /* It'd be nice to avoid this O(n) thingy here */
696       link = _dbus_list_get_first_link (&loop->callbacks);
697       while (link != NULL)
698         {
699           DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
700           Callback *cb = link->data;
701
702           if (initial_serial != loop->callback_list_serial)
703             goto next_iteration;
704
705           if (loop->depth != orig_depth)
706             goto next_iteration;
707               
708           if (cb->type == CALLBACK_TIMEOUT &&
709               dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
710             {
711               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
712               int msecs_remaining;
713               
714               if (check_timeout (tv_sec, tv_usec,
715                                  tcb, &msecs_remaining))
716                 {
717                   /* Save last callback time and fire this timeout */
718                   tcb->last_tv_sec = tv_sec;
719                   tcb->last_tv_usec = tv_usec;
720
721 #if MAINLOOP_SPEW
722                   _dbus_verbose ("  invoking timeout\n");
723 #endif
724                   
725                   (* tcb->function) (tcb->timeout,
726                                      cb->data);
727
728                   retval = TRUE;
729                 }
730               else
731                 {
732 #if MAINLOOP_SPEW
733                   _dbus_verbose ("  timeout has not expired\n");
734 #endif
735                 }
736             }
737 #if MAINLOOP_SPEW
738           else if (cb->type == CALLBACK_TIMEOUT)
739             {
740               _dbus_verbose ("  skipping invocation of disabled timeout\n");
741             }
742 #endif
743
744           link = next;
745         }
746     }
747       
748   if (n_ready > 0)
749     {
750       i = 0;
751       while (i < n_fds)
752         {
753           /* FIXME I think this "restart if we change the watches"
754            * approach could result in starving watches
755            * toward the end of the list.
756            */
757           if (initial_serial != loop->callback_list_serial)
758             goto next_iteration;
759
760           if (loop->depth != orig_depth)
761             goto next_iteration;
762
763           if (fds[i].revents != 0)
764             {
765               WatchCallback *wcb;
766               unsigned int condition;
767                   
768               wcb = watches_for_fds[i];
769               
770               condition = 0;
771               if (fds[i].revents & _DBUS_POLLIN)
772                 condition |= DBUS_WATCH_READABLE;
773               if (fds[i].revents & _DBUS_POLLOUT)
774                 condition |= DBUS_WATCH_WRITABLE;
775               if (fds[i].revents & _DBUS_POLLHUP)
776                 condition |= DBUS_WATCH_HANGUP;
777               if (fds[i].revents & _DBUS_POLLERR)
778                 condition |= DBUS_WATCH_ERROR;
779
780               /* condition may still be 0 if we got some
781                * weird POLLFOO thing like POLLWRBAND
782                */
783                   
784               if (condition != 0 &&
785                   dbus_watch_get_enabled (wcb->watch))
786                 {
787                   if (!(* wcb->function) (wcb->watch,
788                                           condition,
789                                           ((Callback*)wcb)->data))
790                     wcb->last_iteration_oom = TRUE;
791
792 #if MAINLOOP_SPEW
793                   _dbus_verbose ("  Invoked watch, oom = %d\n",
794                                  wcb->last_iteration_oom);
795 #endif
796                   
797                   retval = TRUE;
798                 }
799             }
800               
801           ++i;
802         }
803     }
804       
805  next_iteration:
806 #if MAINLOOP_SPEW
807   _dbus_verbose ("  moving to next iteration\n");
808 #endif
809   
810   if (fds && fds != stack_fds)
811     dbus_free (fds);
812   if (watches_for_fds)
813     {
814       i = 0;
815       while (i < n_fds)
816         {
817           callback_unref (&watches_for_fds[i]->callback);
818           ++i;
819         }
820       
821       if (watches_for_fds != stack_watches_for_fds)
822         dbus_free (watches_for_fds);
823     }
824   
825   if (_dbus_loop_dispatch (loop))
826     retval = TRUE;
827   
828 #if MAINLOOP_SPEW
829   _dbus_verbose ("Returning %d\n", retval);
830 #endif
831   
832   return retval;
833 }
834
835 void
836 _dbus_loop_run (DBusLoop *loop)
837 {
838   int our_exit_depth;
839
840   _dbus_assert (loop->depth >= 0);
841   
842   _dbus_loop_ref (loop);
843   
844   our_exit_depth = loop->depth;
845   loop->depth += 1;
846
847   _dbus_verbose ("Running main loop, depth %d -> %d\n",
848                  loop->depth - 1, loop->depth);
849   
850   while (loop->depth != our_exit_depth)
851     _dbus_loop_iterate (loop, TRUE);
852
853   _dbus_loop_unref (loop);
854 }
855
856 void
857 _dbus_loop_quit (DBusLoop *loop)
858 {
859   _dbus_assert (loop->depth > 0);  
860   
861   loop->depth -= 1;
862
863   _dbus_verbose ("Quit main loop, depth %d -> %d\n",
864                  loop->depth + 1, loop->depth);
865 }
866
867 int
868 _dbus_get_oom_wait (void)
869 {
870 #ifdef DBUS_BUILD_TESTS
871   /* make tests go fast */
872   return 0;
873 #else
874   return 500;
875 #endif
876 }
877
878 void
879 _dbus_wait_for_memory (void)
880 {
881   _dbus_verbose ("Waiting for more memory\n");
882   _dbus_sleep_milliseconds (_dbus_get_oom_wait ());
883 }
884
885 #endif /* DOXYGEN_SHOULD_SKIP_THIS */