D-Bus: dbus-mainloop.c Source File

D-Bus 1.10.22

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