2003-04-24 Havoc Pennington <hp@redhat.com>
[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   interval = dbus_timeout_get_interval (tcb->timeout);
360   
361   interval_seconds = interval / 1000;
362   interval_milliseconds = interval - interval_seconds * 1000;
363   
364   expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
365   expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
366   if (expiration_tv_usec >= 1000000)
367     {
368       expiration_tv_usec -= 1000000;
369       expiration_tv_sec += 1;
370     }
371
372   if (expiration_tv_sec < tv_sec ||
373       (expiration_tv_sec == tv_sec && expiration_tv_usec < tv_usec))
374     {
375       _dbus_verbose ("System clock went backward interval_seconds %ld interval_msecs %ld last_tv_sec %lu last_tv_usec %lu tv_sec %lu tv_usec %lu\n",
376                      interval_seconds, interval_milliseconds,
377                      tcb->last_tv_sec, tcb->last_tv_usec, tv_sec, tv_usec);
378           
379       /* The system time has been set backwards, reset the timeout to "interval" in the future */  
380       
381       tcb->last_tv_sec = tv_sec;
382       tcb->last_tv_usec = tv_usec;
383
384       *timeout = interval;
385
386       return FALSE;
387     }
388   
389   sec_remaining = expiration_tv_sec - tv_sec;
390   msec_remaining = (expiration_tv_usec - tv_usec) / 1000;
391
392 #if 0
393   printf ("Interval is %ld seconds %ld msecs\n",
394           interval_seconds,
395           interval_milliseconds);
396   printf ("Now is %lu seconds %lu usecs\n",
397           tv_sec, tv_usec);
398   printf ("Exp is %lu seconds %lu usecs\n",
399           expiration_tv_sec, expiration_tv_usec);
400   printf ("Pre-correction, remaining sec_remaining %ld msec_remaining %ld\n", sec_remaining, msec_remaining);
401 #endif
402   
403   /* We do the following in a rather convoluted fashion to deal with
404    * the fact that we don't have an integral type big enough to hold
405    * the difference of two timevals in millseconds.
406    */
407   if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
408     msec_remaining = 0;
409   else
410     {
411       if (msec_remaining < 0)
412         {
413           msec_remaining += 1000;
414           sec_remaining -= 1;
415         }
416
417       if (msec_remaining > _DBUS_INT_MAX)
418         {
419           /* Not going to fit in a 32-bit integer */
420           msec_remaining = _DBUS_INT_MAX;
421         }
422     }
423
424   *timeout = msec_remaining;
425
426 #if MAINLOOP_SPEW
427   _dbus_verbose ("  timeout expires in %d milliseconds\n", *timeout);
428 #endif
429   
430   return msec_remaining == 0;
431 }
432
433 dbus_bool_t
434 _dbus_loop_dispatch (DBusLoop *loop)
435 {
436
437 #if MAINLOOP_SPEW
438   _dbus_verbose ("  %d connections to dispatch\n", _dbus_list_get_length (&loop->need_dispatch));
439 #endif
440   
441   if (loop->need_dispatch == NULL)
442     return FALSE;
443   
444  next:
445   while (loop->need_dispatch != NULL)
446     {
447       DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
448       
449       while (TRUE)
450         {
451           DBusDispatchStatus status;
452           
453           status = dbus_connection_dispatch (connection);
454
455           if (status == DBUS_DISPATCH_COMPLETE)
456             {
457               dbus_connection_unref (connection);
458               goto next;
459             }
460           else
461             {
462               if (status == DBUS_DISPATCH_NEED_MEMORY)
463                 _dbus_wait_for_memory ();
464             }
465         }
466     }
467
468   return TRUE;
469 }
470
471 dbus_bool_t
472 _dbus_loop_queue_dispatch (DBusLoop       *loop,
473                            DBusConnection *connection)
474 {
475   if (_dbus_list_append (&loop->need_dispatch, connection))
476     {
477       dbus_connection_ref (connection);
478       return TRUE;
479     }
480   else
481     return FALSE;
482 }
483
484 /* Returns TRUE if we have any timeouts or ready file descriptors,
485  * which is just used in test code as a debug hack
486  */
487
488 dbus_bool_t
489 _dbus_loop_iterate (DBusLoop     *loop,
490                     dbus_bool_t   block)
491 {  
492 #define N_STACK_DESCRIPTORS 64
493   dbus_bool_t retval;
494   DBusPollFD *fds;
495   DBusPollFD stack_fds[N_STACK_DESCRIPTORS];
496   int n_fds;
497   WatchCallback **watches_for_fds;
498   WatchCallback *stack_watches_for_fds[N_STACK_DESCRIPTORS];
499   int i;
500   DBusList *link;
501   int n_ready;
502   int initial_serial;
503   long timeout;
504   dbus_bool_t oom_watch_pending;
505   int orig_depth;
506   
507   retval = FALSE;      
508
509   fds = NULL;
510   watches_for_fds = NULL;
511   n_fds = 0;
512   oom_watch_pending = FALSE;
513   orig_depth = loop->depth;
514   
515 #if MAINLOOP_SPEW
516   _dbus_verbose ("Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
517                  block, loop->depth, loop->timeout_count, loop->watch_count);
518 #endif
519   
520   if (loop->callbacks == NULL)
521     goto next_iteration;
522
523   if (loop->watch_count > N_STACK_DESCRIPTORS)
524     {
525       fds = dbus_new0 (DBusPollFD, loop->watch_count);
526       
527       while (fds == NULL)
528         {
529           _dbus_wait_for_memory ();
530           fds = dbus_new0 (DBusPollFD, loop->watch_count);
531         }
532       
533       watches_for_fds = dbus_new (WatchCallback*, loop->watch_count);
534       while (watches_for_fds == NULL)
535         {
536           _dbus_wait_for_memory ();
537           watches_for_fds = dbus_new (WatchCallback*, loop->watch_count);
538         }
539     }
540   else
541     {      
542       fds = stack_fds;
543       watches_for_fds = stack_watches_for_fds;
544     }
545
546   /* fill our array of fds and watches */
547   n_fds = 0;
548   link = _dbus_list_get_first_link (&loop->callbacks);
549   while (link != NULL)
550     {
551       DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
552       Callback *cb = link->data;
553       if (cb->type == CALLBACK_WATCH)
554         {
555           unsigned int flags;
556           WatchCallback *wcb = WATCH_CALLBACK (cb);
557
558           if (wcb->last_iteration_oom)
559             {
560               /* we skip this one this time, but reenable it next time,
561                * and have a timeout on this iteration
562                */
563               wcb->last_iteration_oom = FALSE;
564               oom_watch_pending = TRUE;
565               
566               retval = TRUE; /* return TRUE here to keep the loop going,
567                               * since we don't know the watch is inactive
568                               */
569
570 #if MAINLOOP_SPEW
571               _dbus_verbose ("  skipping watch on fd %d as it was out of memory last time\n",
572                              dbus_watch_get_fd (wcb->watch));
573 #endif
574             }
575           else if (dbus_watch_get_enabled (wcb->watch))
576             {
577               watches_for_fds[n_fds] = wcb;
578
579               callback_ref (cb);
580                   
581               flags = dbus_watch_get_flags (wcb->watch);
582                   
583               fds[n_fds].fd = dbus_watch_get_fd (wcb->watch);
584               fds[n_fds].revents = 0;
585               fds[n_fds].events = 0;
586               if (flags & DBUS_WATCH_READABLE)
587                 fds[n_fds].events |= _DBUS_POLLIN;
588               if (flags & DBUS_WATCH_WRITABLE)
589                 fds[n_fds].events |= _DBUS_POLLOUT;
590
591 #if MAINLOOP_SPEW
592               _dbus_verbose ("  polling watch on fd %d\n", fds[n_fds].fd);
593 #endif
594
595               n_fds += 1;
596             }
597           else
598             {
599 #if MAINLOOP_SPEW
600               _dbus_verbose ("  skipping disabled watch on fd %d\n",
601                              dbus_watch_get_fd (wcb->watch));
602 #endif
603             }
604         }
605               
606       link = next;
607     }
608   
609   timeout = -1;
610   if (loop->timeout_count > 0)
611     {
612       unsigned long tv_sec;
613       unsigned long tv_usec;
614       
615       _dbus_get_current_time (&tv_sec, &tv_usec);
616           
617       link = _dbus_list_get_first_link (&loop->callbacks);
618       while (link != NULL)
619         {
620           DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
621           Callback *cb = link->data;
622
623           if (cb->type == CALLBACK_TIMEOUT &&
624               dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
625             {
626               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
627               int msecs_remaining;
628
629               check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
630
631               if (timeout < 0)
632                 timeout = msecs_remaining;
633               else
634                 timeout = MIN (msecs_remaining, timeout);
635               
636               _dbus_assert (timeout >= 0);
637                   
638               if (timeout == 0)
639                 break; /* it's not going to get shorter... */
640             }
641               
642           link = next;
643         }
644     }
645
646   /* Never block if we have stuff to dispatch */
647   if (!block || loop->need_dispatch != NULL)
648     {
649       timeout = 0;
650 #if MAINLOOP_SPEW
651       _dbus_verbose ("  timeout is 0 as we aren't blocking\n");
652 #endif
653     }
654
655   /* if a watch is OOM, don't wait longer than the OOM
656    * wait to re-enable it
657    */
658   if (oom_watch_pending)
659     timeout = MIN (timeout, _dbus_get_oom_wait ());
660
661 #if MAINLOOP_SPEW
662   _dbus_verbose ("  polling on %d descriptors timeout %ld\n", n_fds, timeout);
663 #endif
664   
665   n_ready = _dbus_poll (fds, n_fds, timeout);
666
667   initial_serial = loop->callback_list_serial;
668
669   if (loop->timeout_count > 0)
670     {
671       unsigned long tv_sec;
672       unsigned long tv_usec;
673
674       _dbus_get_current_time (&tv_sec, &tv_usec);
675
676       /* It'd be nice to avoid this O(n) thingy here */
677       link = _dbus_list_get_first_link (&loop->callbacks);
678       while (link != NULL)
679         {
680           DBusList *next = _dbus_list_get_next_link (&loop->callbacks, link);
681           Callback *cb = link->data;
682
683           if (initial_serial != loop->callback_list_serial)
684             goto next_iteration;
685
686           if (loop->depth != orig_depth)
687             goto next_iteration;
688               
689           if (cb->type == CALLBACK_TIMEOUT &&
690               dbus_timeout_get_enabled (TIMEOUT_CALLBACK (cb)->timeout))
691             {
692               TimeoutCallback *tcb = TIMEOUT_CALLBACK (cb);
693               int msecs_remaining;
694               
695               if (check_timeout (tv_sec, tv_usec,
696                                  tcb, &msecs_remaining))
697                 {
698                   /* Save last callback time and fire this timeout */
699                   tcb->last_tv_sec = tv_sec;
700                   tcb->last_tv_usec = tv_usec;
701
702 #if MAINLOOP_SPEW
703                   _dbus_verbose ("  invoking timeout\n");
704 #endif
705                   
706                   (* tcb->function) (tcb->timeout,
707                                      cb->data);
708
709                   retval = TRUE;
710                 }
711             }
712
713           link = next;
714         }
715     }
716       
717   if (n_ready > 0)
718     {
719       i = 0;
720       while (i < n_fds)
721         {
722           /* FIXME I think this "restart if we change the watches"
723            * approach could result in starving watches
724            * toward the end of the list.
725            */
726           if (initial_serial != loop->callback_list_serial)
727             goto next_iteration;
728
729           if (loop->depth != orig_depth)
730             goto next_iteration;
731
732           if (fds[i].revents != 0)
733             {
734               WatchCallback *wcb;
735               unsigned int condition;
736                   
737               wcb = watches_for_fds[i];
738               
739               condition = 0;
740               if (fds[i].revents & _DBUS_POLLIN)
741                 condition |= DBUS_WATCH_READABLE;
742               if (fds[i].revents & _DBUS_POLLOUT)
743                 condition |= DBUS_WATCH_WRITABLE;
744               if (fds[i].revents & _DBUS_POLLHUP)
745                 condition |= DBUS_WATCH_HANGUP;
746               if (fds[i].revents & _DBUS_POLLERR)
747                 condition |= DBUS_WATCH_ERROR;
748
749               /* condition may still be 0 if we got some
750                * weird POLLFOO thing like POLLWRBAND
751                */
752                   
753               if (condition != 0 &&
754                   dbus_watch_get_enabled (wcb->watch))
755                 {
756                   if (!(* wcb->function) (wcb->watch,
757                                           condition,
758                                           ((Callback*)wcb)->data))
759                     wcb->last_iteration_oom = TRUE;
760
761 #if MAINLOOP_SPEW
762                   _dbus_verbose ("  Invoked watch, oom = %d\n",
763                                  wcb->last_iteration_oom);
764 #endif
765                   
766                   retval = TRUE;
767                 }
768             }
769               
770           ++i;
771         }
772     }
773       
774  next_iteration:
775   if (fds && fds != stack_fds)
776     dbus_free (fds);
777   if (watches_for_fds)
778     {
779       i = 0;
780       while (i < n_fds)
781         {
782           callback_unref (&watches_for_fds[i]->callback);
783           ++i;
784         }
785       
786       if (watches_for_fds != stack_watches_for_fds)
787         dbus_free (watches_for_fds);
788     }
789   
790   if (_dbus_loop_dispatch (loop))
791     retval = TRUE;
792   
793 #if MAINLOOP_SPEW
794   _dbus_verbose ("Returning %d\n", retval);
795 #endif
796   
797   return retval;
798 }
799
800 void
801 _dbus_loop_run (DBusLoop *loop)
802 {
803   int our_exit_depth;
804
805   _dbus_assert (loop->depth >= 0);
806   
807   _dbus_loop_ref (loop);
808   
809   our_exit_depth = loop->depth;
810   loop->depth += 1;
811
812   _dbus_verbose ("Running main loop, depth %d -> %d\n",
813                  loop->depth - 1, loop->depth);
814   
815   while (loop->depth != our_exit_depth)
816     _dbus_loop_iterate (loop, TRUE);
817
818   _dbus_loop_unref (loop);
819 }
820
821 void
822 _dbus_loop_quit (DBusLoop *loop)
823 {
824   _dbus_assert (loop->depth > 0);  
825   
826   loop->depth -= 1;
827
828   _dbus_verbose ("Quit main loop, depth %d -> %d\n",
829                  loop->depth + 1, loop->depth);
830 }
831
832 int
833 _dbus_get_oom_wait (void)
834 {
835 #ifdef DBUS_BUILD_TESTS
836   /* make tests go fast */
837   return 0;
838 #else
839   return 500;
840 #endif
841 }
842
843 void
844 _dbus_wait_for_memory (void)
845 {
846   _dbus_verbose ("Waiting for more memory\n");
847   _dbus_sleep_milliseconds (_dbus_get_oom_wait ());
848 }
849