D-Bus 1.16.4
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 dbus_int64_t 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 if (watches == NULL)
252 return;
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 (dbus_int64_t tv_sec,
428 long tv_usec,
429 TimeoutCallback *tcb,
430 int *timeout)
431{
432 dbus_int64_t sec_remaining;
433 long msec_remaining;
434 dbus_int64_t expiration_tv_sec;
435 long expiration_tv_usec;
436 dbus_int64_t 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 the smaller non-negative number of the two, or the larger negative
565 * number if both numbers are negative. Poll interprets negative timeout as
566 * infinity, which makes it longer than any actual timeout.
567 */
568static int
569min_poll_timeout (int a,
570 int b)
571{
572 if (a < b)
573 return a < 0 ? b : a;
574 else
575 return b < 0 ? a : b;
576}
577
578/* Returns TRUE if we invoked any timeouts or have ready file
579 * descriptors, which is just used in test code as a debug hack
580 */
581
583_dbus_loop_iterate (DBusLoop *loop,
584 dbus_bool_t block)
585{
586#define N_STACK_DESCRIPTORS 64
587 dbus_bool_t retval;
588 DBusPollableEvent ready_fds[N_STACK_DESCRIPTORS];
589 int i;
590 DBusList *link;
591 int n_ready;
592 int initial_serial;
593 int timeout;
594 int orig_depth;
595
596 retval = FALSE;
597
598 orig_depth = loop->depth;
599
600#if MAINLOOP_SPEW
601 _dbus_verbose ("Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n",
602 block, loop->depth, loop->timeout_count, loop->watch_count);
603#endif
604
605 if (_dbus_hash_table_get_n_entries (loop->watches) == 0 &&
606 loop->timeouts == NULL)
607 goto next_iteration;
608
609 timeout = -1;
610 if (loop->timeout_count > 0)
611 {
612 dbus_int64_t tv_sec;
613 long tv_usec;
614
615 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
616
617 link = _dbus_list_get_first_link (&loop->timeouts);
618 while (link != NULL)
619 {
620 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link);
621 TimeoutCallback *tcb = link->data;
622
623 if (dbus_timeout_get_enabled (tcb->timeout))
624 {
625 int msecs_remaining;
626
627 if (_dbus_timeout_needs_restart (tcb->timeout))
628 {
629 tcb->last_tv_sec = tv_sec;
630 tcb->last_tv_usec = tv_usec;
631 _dbus_timeout_restarted (tcb->timeout);
632 }
633
634 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining);
635
636 timeout = min_poll_timeout (msecs_remaining, timeout);
637
638#if MAINLOOP_SPEW
639 _dbus_verbose (" timeout added, %d remaining, aggregate timeout %ld\n",
640 msecs_remaining, timeout);
641#endif
642
643 _dbus_assert (timeout >= 0);
644 }
645#if MAINLOOP_SPEW
646 else
647 {
648 _dbus_verbose (" skipping disabled timeout\n");
649 }
650#endif
651
652 link = next;
653 }
654 }
655
656 /* Never block if we have stuff to dispatch */
657 if (!block || loop->need_dispatch != NULL)
658 {
659 timeout = 0;
660#if MAINLOOP_SPEW
661 _dbus_verbose (" timeout is 0 as we aren't blocking\n");
662#endif
663 }
664
665 /* if a watch was OOM last time, don't wait longer than the OOM
666 * wait to re-enable it
667 */
668 if (loop->oom_watch_pending)
669 timeout = min_poll_timeout (timeout, _dbus_get_oom_wait ());
670
671#if MAINLOOP_SPEW
672 _dbus_verbose (" polling on %d descriptors timeout %ld\n", _DBUS_N_ELEMENTS (ready_fds), timeout);
673#endif
674
675 n_ready = _dbus_pollable_set_poll (loop->pollable_set, ready_fds,
676 _DBUS_N_ELEMENTS (ready_fds), timeout);
677
678 /* re-enable any watches we skipped this time */
679 if (loop->oom_watch_pending)
680 {
681 DBusHashIter hash_iter;
682
683 loop->oom_watch_pending = FALSE;
684
685 _dbus_hash_iter_init (loop->watches, &hash_iter);
686
687 while (_dbus_hash_iter_next (&hash_iter))
688 {
689 DBusList **watches;
690 DBusPollable fd;
691 dbus_bool_t changed;
692
693 changed = FALSE;
694 fd = _dbus_hash_iter_get_pollable_key (&hash_iter);
695 watches = _dbus_hash_iter_get_value (&hash_iter);
696
697 for (link = _dbus_list_get_first_link (watches);
698 link != NULL;
699 link = _dbus_list_get_next_link (watches, link))
700 {
701 DBusWatch *watch = link->data;
702
703 if (_dbus_watch_get_oom_last_time (watch))
704 {
705 _dbus_watch_set_oom_last_time (watch, FALSE);
706 changed = TRUE;
707 }
708 }
709
710 if (changed)
711 refresh_watches_for_fd (loop, watches, fd);
712 }
713
714 retval = TRUE; /* return TRUE here to keep the loop going,
715 * since we don't know the watch was inactive */
716 }
717
718 initial_serial = loop->callback_list_serial;
719
720 if (loop->timeout_count > 0)
721 {
722 dbus_int64_t tv_sec;
723 long tv_usec;
724
725 _dbus_get_monotonic_time (&tv_sec, &tv_usec);
726
727 /* It'd be nice to avoid this O(n) thingy here */
728 link = _dbus_list_get_first_link (&loop->timeouts);
729 while (link != NULL)
730 {
731 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link);
732 TimeoutCallback *tcb = link->data;
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 (dbus_timeout_get_enabled (tcb->timeout))
741 {
742 int msecs_remaining;
743
744 if (check_timeout (tv_sec, tv_usec,
745 tcb, &msecs_remaining))
746 {
747 /* Save last callback time and fire this timeout */
748 tcb->last_tv_sec = tv_sec;
749 tcb->last_tv_usec = tv_usec;
750
751#if MAINLOOP_SPEW
752 _dbus_verbose (" invoking timeout\n");
753#endif
754
755 /* can theoretically return FALSE on OOM, but we just
756 * let it fire again later - in practice that's what
757 * every wrapper callback in dbus-daemon used to do */
758 dbus_timeout_handle (tcb->timeout);
759
760 retval = TRUE;
761 }
762 else
763 {
764#if MAINLOOP_SPEW
765 _dbus_verbose (" timeout has not expired\n");
766#endif
767 }
768 }
769#if MAINLOOP_SPEW
770 else
771 {
772 _dbus_verbose (" skipping invocation of disabled timeout\n");
773 }
774#endif
775
776 link = next;
777 }
778 }
779
780 if (n_ready > 0)
781 {
782 for (i = 0; i < n_ready; i++)
783 {
784 DBusList **watches;
785 DBusList *next;
786 unsigned int condition;
787 dbus_bool_t any_oom;
788
789 /* FIXME I think this "restart if we change the watches"
790 * approach could result in starving watches
791 * toward the end of the list.
792 */
793 if (initial_serial != loop->callback_list_serial)
794 goto next_iteration;
795
796 if (loop->depth != orig_depth)
797 goto next_iteration;
798
799 _dbus_assert (ready_fds[i].flags != 0);
800
801 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL))
802 {
803 cull_watches_for_invalid_fd (loop, ready_fds[i].fd);
804 goto next_iteration;
805 }
806
807 condition = ready_fds[i].flags;
808 _dbus_assert ((condition & _DBUS_WATCH_NVAL) == 0);
809
810 /* condition may still be 0 if we got some
811 * weird POLLFOO thing like POLLWRBAND
812 */
813 if (condition == 0)
814 continue;
815
816 watches = _dbus_hash_table_lookup_pollable (loop->watches,
817 ready_fds[i].fd);
818
819 if (watches == NULL)
820 continue;
821
822 any_oom = FALSE;
823
824 for (link = _dbus_list_get_first_link (watches);
825 link != NULL;
826 link = next)
827 {
828 DBusWatch *watch = link->data;
829
830 next = _dbus_list_get_next_link (watches, link);
831
832 if (dbus_watch_get_enabled (watch))
833 {
834 dbus_bool_t oom;
835
836 oom = !dbus_watch_handle (watch, condition);
837
838 if (oom)
839 {
840 _dbus_watch_set_oom_last_time (watch, TRUE);
841 loop->oom_watch_pending = TRUE;
842 any_oom = TRUE;
843 }
844
845#if MAINLOOP_SPEW
846 _dbus_verbose (" Invoked watch, oom = %d\n", oom);
847#endif
848 retval = TRUE;
849
850 /* We re-check this every time, in case the callback
851 * added/removed watches, which might make our position in
852 * the linked list invalid. See the FIXME above. */
853 if (initial_serial != loop->callback_list_serial ||
854 loop->depth != orig_depth)
855 {
856 if (any_oom)
857 refresh_watches_for_fd (loop, NULL, ready_fds[i].fd);
858
859 goto next_iteration;
860 }
861 }
862 }
863
864 if (any_oom)
865 refresh_watches_for_fd (loop, watches, ready_fds[i].fd);
866 }
867 }
868
869 next_iteration:
870#if MAINLOOP_SPEW
871 _dbus_verbose (" moving to next iteration\n");
872#endif
873
874 if (_dbus_loop_dispatch (loop))
875 retval = TRUE;
876
877#if MAINLOOP_SPEW
878 _dbus_verbose ("Returning %d\n", retval);
879#endif
880
881 return retval;
882}
883
884void
885_dbus_loop_run (DBusLoop *loop)
886{
887 int our_exit_depth;
888
889 _dbus_assert (loop->depth >= 0);
890
891 _dbus_loop_ref (loop);
892
893 our_exit_depth = loop->depth;
894 loop->depth += 1;
895
896 _dbus_verbose ("Running main loop, depth %d -> %d\n",
897 loop->depth - 1, loop->depth);
898
899 while (loop->depth != our_exit_depth)
900 _dbus_loop_iterate (loop, TRUE);
901
902 _dbus_loop_unref (loop);
903}
904
905void
906_dbus_loop_quit (DBusLoop *loop)
907{
908 _dbus_assert (loop->depth > 0);
909
910 loop->depth -= 1;
911
912 _dbus_verbose ("Quit main loop, depth %d -> %d\n",
913 loop->depth + 1, loop->depth);
914}
915
916int
917_dbus_get_oom_wait (void)
918{
919#ifdef DBUS_ENABLE_EMBEDDED_TESTS
920 /* make tests go fast */
921 return 0;
922#else
923 return 500;
924#endif
925}
926
927void
928_dbus_wait_for_memory (void)
929{
930 _dbus_verbose ("Waiting for more memory\n");
931 _dbus_sleep_milliseconds (_dbus_get_oom_wait ());
932}
933
934#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
struct DBusHashTable DBusHashTable
Public opaque hash table object.
Definition dbus-hash.h:59
#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++.
Definition dbus-macros.h:51
#define TRUE
Expands to "1".
Definition dbus-macros.h:41
#define FALSE
Expands to "0".
Definition dbus-macros.h:44
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
#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_get_monotonic_time(dbus_int64_t *tv_sec, long *tv_usec)
Get current time, as in gettimeofday().
void _dbus_sleep_milliseconds(int milliseconds)
Sleeps the given number of milliseconds.
void _dbus_timeout_restarted(DBusTimeout *timeout)
Mark timeout as restarted (setting timestamps is responsibility of the event loop).
dbus_bool_t _dbus_timeout_needs_restart(DBusTimeout *timeout)
Returns whether a timeout needs restart time counting in the event loop.
DBUS_EXPORT dbus_bool_t dbus_timeout_handle(DBusTimeout *timeout)
Calls the timeout handler for this timeout.
DBUS_EXPORT dbus_bool_t dbus_timeout_get_enabled(DBusTimeout *timeout)
Returns whether a timeout is enabled or not.
DBUS_EXPORT int dbus_timeout_get_interval(DBusTimeout *timeout)
Gets the timeout interval.
_DBUS_GNUC_EXTENSION typedef long dbus_int64_t
A 64-bit signed integer.
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
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.
Implementation of DBusWatch.
Definition dbus-watch.c:43