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