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