D-Bus 1.13.20
dbus-mainloop.c
1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2/* dbus-mainloop.c Main loop utility
3 *
4 * Copyright © 2003, 2004 Red Hat, Inc.
5 * Copyright © 2011 Nokia Corporation
6 *
7 * Licensed under the Academic Free License version 2.1
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 *
23 */
24
25#include <config.h>
26#include <malloc.h>
27#include "dbus-mainloop.h"
28
29#ifndef DOXYGEN_SHOULD_SKIP_THIS
30
31#include <dbus/dbus-hash.h>
32#include <dbus/dbus-list.h>
33#include <dbus/dbus-pollable-set.h>
34#include <dbus/dbus-timeout.h>
35#include <dbus/dbus-watch.h>
36
37#define MAINLOOP_SPEW 0
38
39struct DBusLoop
40{
41 int refcount;
43 DBusHashTable *watches;
44 DBusPollableSet *pollable_set;
45 DBusList *timeouts;
46 int callback_list_serial;
47 int watch_count;
48 int timeout_count;
49 int depth;
50 DBusList *need_dispatch;
53 unsigned oom_watch_pending : 1;
54};
55
56typedef struct
57{
58 DBusTimeout *timeout;
59 long last_tv_sec;
60 long last_tv_usec;
61} TimeoutCallback;
62
63#define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback)
64
65static TimeoutCallback*
66timeout_callback_new (DBusTimeout *timeout)
67{
68 TimeoutCallback *cb;
69
70 cb = dbus_new (TimeoutCallback, 1);
71 if (cb == NULL)
72 return NULL;
73
74 cb->timeout = timeout;
75 _dbus_get_monotonic_time (&cb->last_tv_sec,
76 &cb->last_tv_usec);
77 return cb;
78}
79
80static void
81timeout_callback_free (TimeoutCallback *cb)
82{
83 dbus_free (cb);
84}
85
86static void
87free_watch_table_entry (void *data)
88{
89 DBusList **watches = data;
90 DBusWatch *watch;
91
92 /* DBusHashTable sometimes calls free_function(NULL) even if you never
93 * have NULL as a value */
94 if (watches == NULL)
95 return;
96
97 for (watch = _dbus_list_pop_first (watches);
98 watch != NULL;
99 watch = _dbus_list_pop_first (watches))
100 {
101 _dbus_watch_unref (watch);
102 }
103
104 _dbus_assert (*watches == NULL);
105 dbus_free (watches);
106}
107
108DBusLoop*
109_dbus_loop_new (void)
110{
111 DBusLoop *loop;
112
113 loop = dbus_new0 (DBusLoop, 1);
114 if (loop == NULL)
115 return NULL;
116
117 loop->watches = _dbus_hash_table_new (DBUS_HASH_POLLABLE, NULL,
118 free_watch_table_entry);
119
120 loop->pollable_set = _dbus_pollable_set_new (0);
121
122 if (loop->watches == NULL || loop->pollable_set == NULL)
123 {
124 if (loop->watches != NULL)
125 _dbus_hash_table_unref (loop->watches);
126
127 if (loop->pollable_set != NULL)
128 _dbus_pollable_set_free (loop->pollable_set);
129
130 dbus_free (loop);
131 return NULL;
132 }
133
134 loop->refcount = 1;
135
136 return loop;
137}
138
139DBusLoop *
140_dbus_loop_ref (DBusLoop *loop)
141{
142 _dbus_assert (loop != NULL);
143 _dbus_assert (loop->refcount > 0);
144
145 loop->refcount += 1;
146
147 return loop;
148}
149
150void
151_dbus_loop_unref (DBusLoop *loop)
152{
153 _dbus_assert (loop != NULL);
154 _dbus_assert (loop->refcount > 0);
155
156 loop->refcount -= 1;
157 if (loop->refcount == 0)
158 {
159 while (loop->need_dispatch)
160 {
161 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
162
163 dbus_connection_unref (connection);
164 }
165
166 _dbus_hash_table_unref (loop->watches);
167 _dbus_pollable_set_free (loop->pollable_set);
168 dbus_free (loop);
169 }
170}
171
172static DBusList **
173ensure_watch_table_entry (DBusLoop *loop,
174 DBusPollable fd)
175{
176 DBusList **watches;
177
178 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
179
180 if (watches == NULL)
181 {
182 watches = dbus_new0 (DBusList *, 1);
183
184 if (watches == NULL)
185 return watches;
186
187 if (!_dbus_hash_table_insert_pollable (loop->watches, fd, watches))
188 {
189 dbus_free (watches);
190 watches = NULL;
191 }
192 }
193
194 return watches;
195}
196
197static void
198cull_watches_for_invalid_fd (DBusLoop *loop,
199 DBusPollable fd)
200{
201 DBusList *link;
202 DBusList **watches;
203
204 _dbus_warn ("invalid request, socket fd %" DBUS_POLLABLE_FORMAT " not open",
205 _dbus_pollable_printable (fd));
206 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
207
208 if (watches != NULL)
209 {
210 for (link = _dbus_list_get_first_link (watches);
211 link != NULL;
212 link = _dbus_list_get_next_link (watches, link))
214 }
215
216 _dbus_hash_table_remove_pollable (loop->watches, fd);
217}
218
219static dbus_bool_t
220gc_watch_table_entry (DBusLoop *loop,
221 DBusList **watches,
222 DBusPollable fd)
223{
224 /* If watches is already NULL we have nothing to do */
225 if (watches == NULL)
226 return FALSE;
227
228 /* We can't GC hash table entries if they're non-empty lists */
229 if (*watches != NULL)
230 return FALSE;
231
232 _dbus_hash_table_remove_pollable (loop->watches, fd);
233 return TRUE;
234}
235
236static void
237refresh_watches_for_fd (DBusLoop *loop,
238 DBusList **watches,
239 DBusPollable fd)
240{
241 DBusList *link;
242 unsigned int flags = 0;
243 dbus_bool_t interested = FALSE;
244
245 _dbus_assert (_dbus_pollable_is_valid (fd));
246
247 if (watches == NULL)
248 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
249
250 /* we allocated this in the first _dbus_loop_add_watch for the fd, and keep
251 * it until there are none left */
252 _dbus_assert (watches != NULL);
253
254 for (link = _dbus_list_get_first_link (watches);
255 link != NULL;
256 link = _dbus_list_get_next_link (watches, link))
257 {
258 if (dbus_watch_get_enabled (link->data) &&
259 !_dbus_watch_get_oom_last_time (link->data))
260 {
261 flags |= dbus_watch_get_flags (link->data);
262 interested = TRUE;
263 }
264 }
265
266 if (interested)
267 _dbus_pollable_set_enable (loop->pollable_set, fd, flags);
268 else
269 _dbus_pollable_set_disable (loop->pollable_set, fd);
270}
271
273_dbus_loop_add_watch (DBusLoop *loop,
274 DBusWatch *watch)
275{
276 DBusPollable fd;
277 DBusList **watches;
278
279 fd = _dbus_watch_get_pollable (watch);
280 _dbus_assert (_dbus_pollable_is_valid (fd));
281
282 watches = ensure_watch_table_entry (loop, fd);
283
284 if (watches == NULL)
285 return FALSE;
286
287 if (!_dbus_list_append (watches, _dbus_watch_ref (watch)))
288 {
289 _dbus_watch_unref (watch);
290 gc_watch_table_entry (loop, watches, fd);
291
292 return FALSE;
293 }
294
295 if (_dbus_list_length_is_one (watches))
296 {
297 if (!_dbus_pollable_set_add (loop->pollable_set, fd,
298 dbus_watch_get_flags (watch),
299 dbus_watch_get_enabled (watch)))
300 {
301 _dbus_hash_table_remove_pollable (loop->watches, fd);
302 return FALSE;
303 }
304 }
305 else
306 {
307 /* we're modifying, not adding, which can't fail with OOM */
308 refresh_watches_for_fd (loop, watches, fd);
309 }
310
311 loop->callback_list_serial += 1;
312 loop->watch_count += 1;
313 return TRUE;
314}
315
316void
317_dbus_loop_toggle_watch (DBusLoop *loop,
318 DBusWatch *watch)
319{
320 refresh_watches_for_fd (loop, NULL, _dbus_watch_get_pollable (watch));
321}
322
323void
324_dbus_loop_remove_watch (DBusLoop *loop,
325 DBusWatch *watch)
326{
327 DBusList **watches;
328 DBusList *link;
329 DBusPollable fd;
330
331 /* This relies on people removing watches before they invalidate them,
332 * which has been safe since fd.o #33336 was fixed. Assert about it
333 * so we don't regress. */
334 fd = _dbus_watch_get_pollable (watch);
335 _dbus_assert (_dbus_pollable_is_valid (fd));
336
337 watches = _dbus_hash_table_lookup_pollable (loop->watches, fd);
338
339 if (watches != NULL)
340 {
341 link = _dbus_list_get_first_link (watches);
342 while (link != NULL)
343 {
344 DBusList *next = _dbus_list_get_next_link (watches, link);
345 DBusWatch *this = link->data;
346
347 if (this == watch)
348 {
349 _dbus_list_remove_link (watches, link);
350 loop->callback_list_serial += 1;
351 loop->watch_count -= 1;
352 _dbus_watch_unref (this);
353
354 /* if that was the last watch for that fd, drop the hash table
355 * entry, and stop reserving space for it in the socket set */
356 if (gc_watch_table_entry (loop, watches, fd))
357 {
358 _dbus_pollable_set_remove (loop->pollable_set, fd);
359 }
360
361 return;
362 }
363
364 link = next;
365 }
366 }
367
368 _dbus_warn ("could not find watch %p to remove", watch);
369}
370
372_dbus_loop_add_timeout (DBusLoop *loop,
373 DBusTimeout *timeout)
374{
375 TimeoutCallback *tcb;
376
377 tcb = timeout_callback_new (timeout);
378 if (tcb == NULL)
379 return FALSE;
380
381 if (_dbus_list_append (&loop->timeouts, tcb))
382 {
383 loop->callback_list_serial += 1;
384 loop->timeout_count += 1;
385 }
386 else
387 {
388 timeout_callback_free (tcb);
389 return FALSE;
390 }
391
392 return TRUE;
393}
394
395void
396_dbus_loop_remove_timeout (DBusLoop *loop,
397 DBusTimeout *timeout)
398{
399 DBusList *link;
400
401 link = _dbus_list_get_first_link (&loop->timeouts);
402 while (link != NULL)
403 {
404 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link);
405 TimeoutCallback *this = link->data;
406
407 if (this->timeout == timeout)
408 {
409 _dbus_list_remove_link (&loop->timeouts, link);
410 loop->callback_list_serial += 1;
411 loop->timeout_count -= 1;
412 timeout_callback_free (this);
413
414 return;
415 }
416
417 link = next;
418 }
419
420 _dbus_warn ("could not find timeout %p to remove", timeout);
421}
422
423/* Convolutions from GLib, there really must be a better way
424 * to do this.
425 */
426static dbus_bool_t
427check_timeout (long tv_sec,
428 long tv_usec,
429 TimeoutCallback *tcb,
430 int *timeout)
431{
432 long sec_remaining;
433 long msec_remaining;
434 long expiration_tv_sec;
435 long expiration_tv_usec;
436 long interval_seconds;
437 long interval_milliseconds;
438 int interval;
439
440 /* I'm pretty sure this function could suck (a lot) less */
441
442 interval = dbus_timeout_get_interval (tcb->timeout);
443
444 interval_seconds = interval / 1000L;
445 interval_milliseconds = interval % 1000L;
446
447 expiration_tv_sec = tcb->last_tv_sec + interval_seconds;
448 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000;
449 if (expiration_tv_usec >= 1000000)
450 {
451 expiration_tv_usec -= 1000000;
452 expiration_tv_sec += 1;
453 }
454
455 sec_remaining = expiration_tv_sec - tv_sec;
456 msec_remaining = (expiration_tv_usec - tv_usec) / 1000L;
457
458#if MAINLOOP_SPEW
459 _dbus_verbose ("Interval is %ld seconds %ld msecs\n",
460 interval_seconds,
461 interval_milliseconds);
462 _dbus_verbose ("Now is %lu seconds %lu usecs\n",
463 tv_sec, tv_usec);
464 _dbus_verbose ("Last is %lu seconds %lu usecs\n",
465 tcb->last_tv_sec, tcb->last_tv_usec);
466 _dbus_verbose ("Exp is %lu seconds %lu usecs\n",
467 expiration_tv_sec, expiration_tv_usec);
468 _dbus_verbose ("Pre-correction, sec_remaining %ld msec_remaining %ld\n",
469 sec_remaining, msec_remaining);
470#endif
471
472 /* We do the following in a rather convoluted fashion to deal with
473 * the fact that we don't have an integral type big enough to hold
474 * the difference of two timevals in milliseconds.
475 */
476 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0))
477 {
478 *timeout = 0;
479 }
480 else
481 {
482 if (msec_remaining < 0)
483 {
484 msec_remaining += 1000;
485 sec_remaining -= 1;
486 }
487
488 if (sec_remaining > (_DBUS_INT_MAX / 1000) ||
489 msec_remaining > _DBUS_INT_MAX)
490 *timeout = _DBUS_INT_MAX;
491 else
492 *timeout = sec_remaining * 1000 + msec_remaining;
493 }
494
495 if (*timeout > interval)
496 {
497 /* This indicates that the system clock probably moved backward */
498 _dbus_verbose ("System clock set backward! Resetting timeout.\n");
499
500 tcb->last_tv_sec = tv_sec;
501 tcb->last_tv_usec = tv_usec;
502
503 *timeout = interval;
504 }
505
506#if MAINLOOP_SPEW
507 _dbus_verbose (" timeout expires in %d milliseconds\n", *timeout);
508#endif
509
510 return *timeout == 0;
511}
512
514_dbus_loop_dispatch (DBusLoop *loop)
515{
516
517#if MAINLOOP_SPEW
518 _dbus_verbose (" %d connections to dispatch\n", _dbus_list_get_length (&loop->need_dispatch));
519#endif
520
521 if (loop->need_dispatch == NULL)
522 return FALSE;
523
524 next:
525 while (loop->need_dispatch != NULL)
526 {
527 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch);
528
529 while (TRUE)
530 {
531 DBusDispatchStatus status;
532
533 status = dbus_connection_dispatch (connection);
534
535 if (status == DBUS_DISPATCH_COMPLETE)
536 {
537 dbus_connection_unref (connection);
538 goto next;
539 }
540 else
541 {
542 if (status == DBUS_DISPATCH_NEED_MEMORY)
543 _dbus_wait_for_memory ();
544 }
545 }
546 }
547
548 return TRUE;
549}
550
552_dbus_loop_queue_dispatch (DBusLoop *loop,
553 DBusConnection *connection)
554{
555 if (_dbus_list_append (&loop->need_dispatch, connection))
556 {
557 dbus_connection_ref (connection);
558 return TRUE;
559 }
560 else
561 return FALSE;
562}
563
564/* Returns TRUE if we invoked any timeouts or have ready file
565 * descriptors, which is just used in test code as a debug hack
566 */
567
569_dbus_loop_iterate (DBusLoop *loop,
570 dbus_bool_t block)
571{
572#define N_STACK_DESCRIPTORS 64
573 dbus_bool_t retval;
574 DBusPollableEvent ready_fds[N_STACK_DESCRIPTORS];
575 int i;
576 DBusList *link;
577 int n_ready;
578 int initial_serial;
579 long timeout;
580 int orig_depth;
581
582 retval = FALSE;
583
584 orig_depth = loop->depth;
585
586#if MAINLOOP_SPEW
587 _dbus_verbose ("Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
588 block, loop->depth, loop->timeout_count, loop->watch_count);
589#endif
590
591 if (_dbus_hash_table_get_n_entries (loop->watches) == 0 &&
592 loop->timeouts == NULL)
593 goto next_iteration;
594
595 timeout = -1;
596 if (loop->timeout_count > 0)
597 {
598 long tv_sec;
599 long tv_usec;
600
601 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
602
603 link = _dbus_list_get_first_link (&loop->timeouts);
604 while (link != NULL)
605 {
606 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link);
607 TimeoutCallback *tcb = link->data;
608
609 if (dbus_timeout_get_enabled (tcb->timeout))
610 {
611 int msecs_remaining;
612
613 if (_dbus_timeout_needs_restart (tcb->timeout))
614 {
615 tcb->last_tv_sec = tv_sec;
616 tcb->last_tv_usec = tv_usec;
617 _dbus_timeout_restarted (tcb->timeout);
618 }
619
620 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
621
622 if (timeout < 0)
623 timeout = msecs_remaining;
624 else
625 timeout = MIN (msecs_remaining, timeout);
626
627#if MAINLOOP_SPEW
628 _dbus_verbose (" timeout added, %d remaining, aggregate timeout %ld\n",
629 msecs_remaining, timeout);
630#endif
631
632 _dbus_assert (timeout >= 0);
633 }
634#if MAINLOOP_SPEW
635 else
636 {
637 _dbus_verbose (" skipping disabled timeout\n");
638 }
639#endif
640
641 link = next;
642 }
643 }
644
645 /* Never block if we have stuff to dispatch */
646 if (!block || loop->need_dispatch != NULL)
647 {
648 timeout = 0;
649#if MAINLOOP_SPEW
650 _dbus_verbose (" timeout is 0 as we aren't blocking\n");
651#endif
652 }
653
654 /* if a watch was OOM last time, don't wait longer than the OOM
655 * wait to re-enable it
656 */
657 if (loop->oom_watch_pending)
658 timeout = MIN (timeout, _dbus_get_oom_wait ());
659
660#if MAINLOOP_SPEW
661 _dbus_verbose (" polling on %d descriptors timeout %ld\n", _DBUS_N_ELEMENTS (ready_fds), timeout);
662#endif
663
664 n_ready = _dbus_pollable_set_poll (loop->pollable_set, ready_fds,
665 _DBUS_N_ELEMENTS (ready_fds), timeout);
666
667 /* re-enable any watches we skipped this time */
668 if (loop->oom_watch_pending)
669 {
670 DBusHashIter hash_iter;
671
672 loop->oom_watch_pending = FALSE;
673
674 _dbus_hash_iter_init (loop->watches, &hash_iter);
675
676 while (_dbus_hash_iter_next (&hash_iter))
677 {
678 DBusList **watches;
679 DBusPollable fd;
680 dbus_bool_t changed;
681
682 changed = FALSE;
683 fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
684 watches = _dbus_hash_iter_get_value (&hash_iter);
685
686 for (link = _dbus_list_get_first_link (watches);
687 link != NULL;
688 link = _dbus_list_get_next_link (watches, link))
689 {
690 DBusWatch *watch = link->data;
691
692 if (_dbus_watch_get_oom_last_time (watch))
693 {
694 _dbus_watch_set_oom_last_time (watch, FALSE);
695 changed = TRUE;
696 }
697 }
698
699 if (changed)
700 refresh_watches_for_fd (loop, watches, fd);
701 }
702
703 retval = TRUE; /* return TRUE here to keep the loop going,
704 * since we don't know the watch was inactive */
705 }
706
707 initial_serial = loop->callback_list_serial;
708
709 if (loop->timeout_count > 0)
710 {
711 long tv_sec;
712 long tv_usec;
713
714 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
715
716 /* It'd be nice to avoid this O(n) thingy here */
717 link = _dbus_list_get_first_link (&loop->timeouts);
718 while (link != NULL)
719 {
720 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link);
721 TimeoutCallback *tcb = link->data;
722
723 if (initial_serial != loop->callback_list_serial)
724 goto next_iteration;
725
726 if (loop->depth != orig_depth)
727 goto next_iteration;
728
729 if (dbus_timeout_get_enabled (tcb->timeout))
730 {
731 int msecs_remaining;
732
733 if (check_timeout (tv_sec, tv_usec,
734 tcb, &msecs_remaining))
735 {
736 /* Save last callback time and fire this timeout */
737 tcb->last_tv_sec = tv_sec;
738 tcb->last_tv_usec = tv_usec;
739
740#if MAINLOOP_SPEW
741 _dbus_verbose (" invoking timeout\n");
742#endif
743
744 /* can theoretically return FALSE on OOM, but we just
745 * let it fire again later - in practice that's what
746 * every wrapper callback in dbus-daemon used to do */
747 dbus_timeout_handle (tcb->timeout);
748
749 retval = TRUE;
750 }
751 else
752 {
753#if MAINLOOP_SPEW
754 _dbus_verbose (" timeout has not expired\n");
755#endif
756 }
757 }
758#if MAINLOOP_SPEW
759 else
760 {
761 _dbus_verbose (" skipping invocation of disabled timeout\n");
762 }
763#endif
764
765 link = next;
766 }
767 }
768
769 if (n_ready > 0)
770 {
771 for (i = 0; i < n_ready; i++)
772 {
773 DBusList **watches;
774 DBusList *next;
775 unsigned int condition;
776 dbus_bool_t any_oom;
777
778 /* FIXME I think this "restart if we change the watches"
779 * approach could result in starving watches
780 * toward the end of the list.
781 */
782 if (initial_serial != loop->callback_list_serial)
783 goto next_iteration;
784
785 if (loop->depth != orig_depth)
786 goto next_iteration;
787
788 _dbus_assert (ready_fds[i].flags != 0);
789
790 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
791 {
792 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
793 goto next_iteration;
794 }
795
796 condition = ready_fds[i].flags;
797 _dbus_assert ((condition & _DBUS_WATCH_NVAL) == 0);
798
799 /* condition may still be 0 if we got some
800 * weird POLLFOO thing like POLLWRBAND
801 */
802 if (condition == 0)
803 continue;
804
805 watches = _dbus_hash_table_lookup_pollable (loop->watches,
806 ready_fds[i].fd);
807
808 if (watches == NULL)
809 continue;
810
811 any_oom = FALSE;
812
813 for (link = _dbus_list_get_first_link (watches);
814 link != NULL;
815 link = next)
816 {
817 DBusWatch *watch = link->data;
818
819 next = _dbus_list_get_next_link (watches, link);
820
821 if (dbus_watch_get_enabled (watch))
822 {
823 dbus_bool_t oom;
824
825 oom = !dbus_watch_handle (watch, condition);
826
827 if (oom)
828 {
829 _dbus_watch_set_oom_last_time (watch, TRUE);
830 loop->oom_watch_pending = TRUE;
831 any_oom = TRUE;
832 }
833
834#if MAINLOOP_SPEW
835 _dbus_verbose (" Invoked watch, oom = %d\n", oom);
836#endif
837 retval = TRUE;
838
839 /* We re-check this every time, in case the callback
840 * added/removed watches, which might make our position in
841 * the linked list invalid. See the FIXME above. */
842 if (initial_serial != loop->callback_list_serial ||
843 loop->depth != orig_depth)
844 {
845 if (any_oom)
846 refresh_watches_for_fd (loop, NULL, ready_fds[i].fd);
847
848 goto next_iteration;
849 }
850 }
851 }
852
853 if (any_oom)
854 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
855 }
856 }
857
858 next_iteration:
859#if MAINLOOP_SPEW
860 _dbus_verbose (" moving to next iteration\n");
861#endif
862
863 if (_dbus_loop_dispatch (loop))
864 retval = TRUE;
865
866#if MAINLOOP_SPEW
867 _dbus_verbose ("Returning %d\n", retval);
868#endif
869
870 return retval;
871}
872
873void
874_dbus_loop_run (DBusLoop *loop)
875{
876 int our_exit_depth;
877
878 _dbus_assert (loop->depth >= 0);
879
880 _dbus_loop_ref (loop);
881
882 our_exit_depth = loop->depth;
883 loop->depth += 1;
884
885 _dbus_verbose ("Running main loop, depth %d -> %d\n",
886 loop->depth - 1, loop->depth);
887
888 while (loop->depth != our_exit_depth) {
889 malloc_trim(0);
890 _dbus_loop_iterate (loop, TRUE);
891 }
892
893 _dbus_loop_unref (loop);
894}
895
896void
897_dbus_loop_quit (DBusLoop *loop)
898{
899 _dbus_assert (loop->depth > 0);
900
901 loop->depth -= 1;
902
903 _dbus_verbose ("Quit main loop, depth %d -> %d\n",
904 loop->depth + 1, loop->depth);
905}
906
907int
908_dbus_get_oom_wait (void)
909{
910#ifdef DBUS_ENABLE_EMBEDDED_TESTS
911 /* make tests go fast */
912 return 0;
913#else
914 return 500;
915#endif
916}
917
918void
919_dbus_wait_for_memory (void)
920{
921 _dbus_verbose ("Waiting for more memory\n");
922 _dbus_sleep_milliseconds (_dbus_get_oom_wait ());
923}
924
925#endif /* !DOXYGEN_SHOULD_SKIP_THIS */
void dbus_connection_unref(DBusConnection *connection)
Decrements the reference count of a DBusConnection, and finalizes it if the count reaches zero.
DBusDispatchStatus dbus_connection_dispatch(DBusConnection *connection)
Processes any incoming data.
DBusDispatchStatus
Indicates the status of incoming data on a DBusConnection.
DBusConnection * dbus_connection_ref(DBusConnection *connection)
Increments the reference count of a DBusConnection.
@ DBUS_DISPATCH_NEED_MEMORY
More memory is needed to continue.
@ DBUS_DISPATCH_COMPLETE
All currently available data has been processed.
int _dbus_hash_table_get_n_entries(DBusHashTable *table)
Gets the number of hash entries in a hash table.
Definition: dbus-hash.c:1460
void * _dbus_hash_iter_get_value(DBusHashIter *iter)
Gets the value of the current entry.
Definition: dbus-hash.c:619
void _dbus_hash_table_unref(DBusHashTable *table)
Decrements the reference count for a hash table, freeing the hash table if the count reaches zero.
Definition: dbus-hash.c:367
dbus_bool_t _dbus_hash_iter_next(DBusHashIter *iter)
Move the hash iterator forward one step, to the next hash entry.
Definition: dbus-hash.c:549
void _dbus_hash_iter_init(DBusHashTable *table, DBusHashIter *iter)
Initializes a hash table iterator.
Definition: dbus-hash.c:523
DBusHashTable * _dbus_hash_table_new(DBusHashType type, DBusFreeFunction key_free_function, DBusFreeFunction value_free_function)
Constructs a new hash table.
Definition: dbus-hash.c:291
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
#define _DBUS_INT_MAX
Maximum value of type "int".
void _dbus_warn(const char *format,...)
Prints a warning message to stderr.
#define _DBUS_N_ELEMENTS(array)
Computes the number of elements in a fixed-size array using sizeof().
DBusList * _dbus_list_get_first_link(DBusList **list)
Gets the first link in the list.
Definition: dbus-list.c:595
dbus_bool_t _dbus_list_length_is_one(DBusList **list)
Check whether length is exactly one.
Definition: dbus-list.c:811
void _dbus_list_remove_link(DBusList **list, DBusList *link)
Removes a link from the list.
Definition: dbus-list.c:528
void * _dbus_list_pop_first(DBusList **list)
Removes the first value in the list and returns it.
Definition: dbus-list.c:677
int _dbus_list_get_length(DBusList **list)
Gets the length of a list.
Definition: dbus-list.c:758
dbus_bool_t _dbus_list_append(DBusList **list, void *data)
Appends a value to the list.
Definition: dbus-list.c:271
#define _dbus_list_get_next_link(list, link)
Gets the next link in the list, or NULL if there are no more links.
Definition: dbus-list.h:119
#define NULL
A null pointer, defined appropriately for C or C++.
#define TRUE
Expands to "1".
#define FALSE
Expands to "0".
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:692
#define dbus_new(type, count)
Safe macro for using dbus_malloc().
Definition: dbus-memory.h:57
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:58
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
void _dbus_get_monotonic_time(long *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void _dbus_timeout_restarted(DBusTimeout *timeout)
Mark timeout as restarted (setting timestamps is responsibility of the event loop).
Definition: dbus-timeout.c:399
dbus_bool_t _dbus_timeout_needs_restart(DBusTimeout *timeout)
Returns whether a timeout needs restart time counting in the event loop.
Definition: dbus-timeout.c:387
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
Definition: dbus-timeout.c:498
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
Definition: dbus-timeout.c:512
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
Definition: dbus-timeout.c:442
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
DBusWatch * _dbus_watch_ref(DBusWatch *watch)
Increments the reference count of a DBusWatch object.
Definition: dbus-watch.c:124
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition: dbus-watch.c:138
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it.
Definition: dbus-watch.c:169
DBUS_EXPORT dbus_bool_t dbus_watch_handle(DBusWatch *watch, unsigned int flags)
Called to notify the D-Bus library when a previously-added watch is ready for reading or writing,...
Definition: dbus-watch.c:733
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:702
DBUS_EXPORT unsigned int dbus_watch_get_flags(DBusWatch *watch)
Gets flags from DBusWatchFlags indicating what conditions should be monitored on the file descriptor.
Definition: dbus-watch.c:642
Implementation details of DBusConnection.
Hash iterator object.
Definition: dbus-hash.h:50
Internals of DBusHashTable.
Definition: dbus-hash.c:174
A node in a linked list.
Definition: dbus-list.h:35
void * data
Data stored at this element.
Definition: dbus-list.h:38
Internals of DBusTimeout.
Definition: dbus-timeout.c:41
Implementation of DBusWatch.
Definition: dbus-watch.c:41