|
D-Bus
1.8.20
|
00001 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 00002 /* dbus-mainloop.c Main loop utility 00003 * 00004 * Copyright © 2003, 2004 Red Hat, Inc. 00005 * Copyright © 2011 Nokia Corporation 00006 * 00007 * Licensed under the Academic Free License version 2.1 00008 * 00009 * This program is free software; you can redistribute it and/or modify 00010 * it under the terms of the GNU General Public License as published by 00011 * the Free Software Foundation; either version 2 of the License, or 00012 * (at your option) any later version. 00013 * 00014 * This program is distributed in the hope that it will be useful, 00015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 * GNU General Public License for more details. 00018 * 00019 * You should have received a copy of the GNU General Public License 00020 * along with this program; if not, write to the Free Software 00021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00022 * 00023 */ 00024 00025 #include <config.h> 00026 #include "dbus-mainloop.h" 00027 00028 #ifndef DOXYGEN_SHOULD_SKIP_THIS 00029 00030 #include <dbus/dbus-hash.h> 00031 #include <dbus/dbus-list.h> 00032 #include <dbus/dbus-socket-set.h> 00033 #include <dbus/dbus-watch.h> 00034 00035 #define MAINLOOP_SPEW 0 00036 00037 struct DBusLoop 00038 { 00039 int refcount; 00041 DBusHashTable *watches; 00042 DBusSocketSet *socket_set; 00043 DBusList *timeouts; 00044 int callback_list_serial; 00045 int watch_count; 00046 int timeout_count; 00047 int depth; 00048 DBusList *need_dispatch; 00051 unsigned oom_watch_pending : 1; 00052 }; 00053 00054 typedef struct 00055 { 00056 DBusTimeout *timeout; 00057 unsigned long last_tv_sec; 00058 unsigned long last_tv_usec; 00059 } TimeoutCallback; 00060 00061 #define TIMEOUT_CALLBACK(callback) ((TimeoutCallback*)callback) 00062 00063 static TimeoutCallback* 00064 timeout_callback_new (DBusTimeout *timeout) 00065 { 00066 TimeoutCallback *cb; 00067 00068 cb = dbus_new (TimeoutCallback, 1); 00069 if (cb == NULL) 00070 return NULL; 00071 00072 cb->timeout = timeout; 00073 _dbus_get_monotonic_time (&cb->last_tv_sec, 00074 &cb->last_tv_usec); 00075 return cb; 00076 } 00077 00078 static void 00079 timeout_callback_free (TimeoutCallback *cb) 00080 { 00081 dbus_free (cb); 00082 } 00083 00084 static void 00085 free_watch_table_entry (void *data) 00086 { 00087 DBusList **watches = data; 00088 DBusWatch *watch; 00089 00090 /* DBusHashTable sometimes calls free_function(NULL) even if you never 00091 * have NULL as a value */ 00092 if (watches == NULL) 00093 return; 00094 00095 for (watch = _dbus_list_pop_first (watches); 00096 watch != NULL; 00097 watch = _dbus_list_pop_first (watches)) 00098 { 00099 _dbus_watch_unref (watch); 00100 } 00101 00102 _dbus_assert (*watches == NULL); 00103 dbus_free (watches); 00104 } 00105 00106 DBusLoop* 00107 _dbus_loop_new (void) 00108 { 00109 DBusLoop *loop; 00110 00111 loop = dbus_new0 (DBusLoop, 1); 00112 if (loop == NULL) 00113 return NULL; 00114 00115 loop->watches = _dbus_hash_table_new (DBUS_HASH_INT, NULL, 00116 free_watch_table_entry); 00117 00118 loop->socket_set = _dbus_socket_set_new (0); 00119 00120 if (loop->watches == NULL || loop->socket_set == NULL) 00121 { 00122 if (loop->watches != NULL) 00123 _dbus_hash_table_unref (loop->watches); 00124 00125 if (loop->socket_set != NULL) 00126 _dbus_socket_set_free (loop->socket_set); 00127 00128 dbus_free (loop); 00129 return NULL; 00130 } 00131 00132 loop->refcount = 1; 00133 00134 return loop; 00135 } 00136 00137 DBusLoop * 00138 _dbus_loop_ref (DBusLoop *loop) 00139 { 00140 _dbus_assert (loop != NULL); 00141 _dbus_assert (loop->refcount > 0); 00142 00143 loop->refcount += 1; 00144 00145 return loop; 00146 } 00147 00148 void 00149 _dbus_loop_unref (DBusLoop *loop) 00150 { 00151 _dbus_assert (loop != NULL); 00152 _dbus_assert (loop->refcount > 0); 00153 00154 loop->refcount -= 1; 00155 if (loop->refcount == 0) 00156 { 00157 while (loop->need_dispatch) 00158 { 00159 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch); 00160 00161 dbus_connection_unref (connection); 00162 } 00163 00164 _dbus_hash_table_unref (loop->watches); 00165 _dbus_socket_set_free (loop->socket_set); 00166 dbus_free (loop); 00167 } 00168 } 00169 00170 static DBusList ** 00171 ensure_watch_table_entry (DBusLoop *loop, 00172 int fd) 00173 { 00174 DBusList **watches; 00175 00176 watches = _dbus_hash_table_lookup_int (loop->watches, fd); 00177 00178 if (watches == NULL) 00179 { 00180 watches = dbus_new0 (DBusList *, 1); 00181 00182 if (watches == NULL) 00183 return watches; 00184 00185 if (!_dbus_hash_table_insert_int (loop->watches, fd, watches)) 00186 { 00187 dbus_free (watches); 00188 watches = NULL; 00189 } 00190 } 00191 00192 return watches; 00193 } 00194 00195 static void 00196 cull_watches_for_invalid_fd (DBusLoop *loop, 00197 int fd) 00198 { 00199 DBusList *link; 00200 DBusList **watches; 00201 00202 _dbus_warn ("invalid request, socket fd %d not open\n", fd); 00203 watches = _dbus_hash_table_lookup_int (loop->watches, fd); 00204 00205 if (watches != NULL) 00206 { 00207 for (link = _dbus_list_get_first_link (watches); 00208 link != NULL; 00209 link = _dbus_list_get_next_link (watches, link)) 00210 _dbus_watch_invalidate (link->data); 00211 } 00212 00213 _dbus_hash_table_remove_int (loop->watches, fd); 00214 } 00215 00216 static dbus_bool_t 00217 gc_watch_table_entry (DBusLoop *loop, 00218 DBusList **watches, 00219 int fd) 00220 { 00221 /* If watches is already NULL we have nothing to do */ 00222 if (watches == NULL) 00223 return FALSE; 00224 00225 /* We can't GC hash table entries if they're non-empty lists */ 00226 if (*watches != NULL) 00227 return FALSE; 00228 00229 _dbus_hash_table_remove_int (loop->watches, fd); 00230 return TRUE; 00231 } 00232 00233 static void 00234 refresh_watches_for_fd (DBusLoop *loop, 00235 DBusList **watches, 00236 int fd) 00237 { 00238 DBusList *link; 00239 unsigned int flags = 0; 00240 dbus_bool_t interested = FALSE; 00241 00242 _dbus_assert (fd != -1); 00243 00244 if (watches == NULL) 00245 watches = _dbus_hash_table_lookup_int (loop->watches, fd); 00246 00247 /* we allocated this in the first _dbus_loop_add_watch for the fd, and keep 00248 * it until there are none left */ 00249 _dbus_assert (watches != NULL); 00250 00251 for (link = _dbus_list_get_first_link (watches); 00252 link != NULL; 00253 link = _dbus_list_get_next_link (watches, link)) 00254 { 00255 if (dbus_watch_get_enabled (link->data) && 00256 !_dbus_watch_get_oom_last_time (link->data)) 00257 { 00258 flags |= dbus_watch_get_flags (link->data); 00259 interested = TRUE; 00260 } 00261 } 00262 00263 if (interested) 00264 _dbus_socket_set_enable (loop->socket_set, fd, flags); 00265 else 00266 _dbus_socket_set_disable (loop->socket_set, fd); 00267 } 00268 00269 dbus_bool_t 00270 _dbus_loop_add_watch (DBusLoop *loop, 00271 DBusWatch *watch) 00272 { 00273 int fd; 00274 DBusList **watches; 00275 00276 fd = dbus_watch_get_socket (watch); 00277 _dbus_assert (fd != -1); 00278 00279 watches = ensure_watch_table_entry (loop, fd); 00280 00281 if (watches == NULL) 00282 return FALSE; 00283 00284 if (!_dbus_list_append (watches, _dbus_watch_ref (watch))) 00285 { 00286 _dbus_watch_unref (watch); 00287 gc_watch_table_entry (loop, watches, fd); 00288 00289 return FALSE; 00290 } 00291 00292 if (_dbus_list_length_is_one (watches)) 00293 { 00294 if (!_dbus_socket_set_add (loop->socket_set, fd, 00295 dbus_watch_get_flags (watch), 00296 dbus_watch_get_enabled (watch))) 00297 { 00298 _dbus_hash_table_remove_int (loop->watches, fd); 00299 return FALSE; 00300 } 00301 } 00302 else 00303 { 00304 /* we're modifying, not adding, which can't fail with OOM */ 00305 refresh_watches_for_fd (loop, watches, fd); 00306 } 00307 00308 loop->callback_list_serial += 1; 00309 loop->watch_count += 1; 00310 return TRUE; 00311 } 00312 00313 void 00314 _dbus_loop_toggle_watch (DBusLoop *loop, 00315 DBusWatch *watch) 00316 { 00317 refresh_watches_for_fd (loop, NULL, dbus_watch_get_socket (watch)); 00318 } 00319 00320 void 00321 _dbus_loop_remove_watch (DBusLoop *loop, 00322 DBusWatch *watch) 00323 { 00324 DBusList **watches; 00325 DBusList *link; 00326 int fd; 00327 00328 /* This relies on people removing watches before they invalidate them, 00329 * which has been safe since fd.o #33336 was fixed. Assert about it 00330 * so we don't regress. */ 00331 fd = dbus_watch_get_socket (watch); 00332 _dbus_assert (fd != -1); 00333 00334 watches = _dbus_hash_table_lookup_int (loop->watches, fd); 00335 00336 if (watches != NULL) 00337 { 00338 link = _dbus_list_get_first_link (watches); 00339 while (link != NULL) 00340 { 00341 DBusList *next = _dbus_list_get_next_link (watches, link); 00342 DBusWatch *this = link->data; 00343 00344 if (this == watch) 00345 { 00346 _dbus_list_remove_link (watches, link); 00347 loop->callback_list_serial += 1; 00348 loop->watch_count -= 1; 00349 _dbus_watch_unref (this); 00350 00351 /* if that was the last watch for that fd, drop the hash table 00352 * entry, and stop reserving space for it in the socket set */ 00353 if (gc_watch_table_entry (loop, watches, fd)) 00354 { 00355 _dbus_socket_set_remove (loop->socket_set, fd); 00356 } 00357 00358 return; 00359 } 00360 00361 link = next; 00362 } 00363 } 00364 00365 _dbus_warn ("could not find watch %p to remove\n", watch); 00366 } 00367 00368 dbus_bool_t 00369 _dbus_loop_add_timeout (DBusLoop *loop, 00370 DBusTimeout *timeout) 00371 { 00372 TimeoutCallback *tcb; 00373 00374 tcb = timeout_callback_new (timeout); 00375 if (tcb == NULL) 00376 return FALSE; 00377 00378 if (_dbus_list_append (&loop->timeouts, tcb)) 00379 { 00380 loop->callback_list_serial += 1; 00381 loop->timeout_count += 1; 00382 } 00383 else 00384 { 00385 timeout_callback_free (tcb); 00386 return FALSE; 00387 } 00388 00389 return TRUE; 00390 } 00391 00392 void 00393 _dbus_loop_remove_timeout (DBusLoop *loop, 00394 DBusTimeout *timeout) 00395 { 00396 DBusList *link; 00397 00398 link = _dbus_list_get_first_link (&loop->timeouts); 00399 while (link != NULL) 00400 { 00401 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link); 00402 TimeoutCallback *this = link->data; 00403 00404 if (this->timeout == timeout) 00405 { 00406 _dbus_list_remove_link (&loop->timeouts, link); 00407 loop->callback_list_serial += 1; 00408 loop->timeout_count -= 1; 00409 timeout_callback_free (this); 00410 00411 return; 00412 } 00413 00414 link = next; 00415 } 00416 00417 _dbus_warn ("could not find timeout %p to remove\n", timeout); 00418 } 00419 00420 /* Convolutions from GLib, there really must be a better way 00421 * to do this. 00422 */ 00423 static dbus_bool_t 00424 check_timeout (unsigned long tv_sec, 00425 unsigned long tv_usec, 00426 TimeoutCallback *tcb, 00427 int *timeout) 00428 { 00429 long sec_remaining; 00430 long msec_remaining; 00431 unsigned long expiration_tv_sec; 00432 unsigned long expiration_tv_usec; 00433 long interval_seconds; 00434 long interval_milliseconds; 00435 int interval; 00436 00437 /* I'm pretty sure this function could suck (a lot) less */ 00438 00439 interval = dbus_timeout_get_interval (tcb->timeout); 00440 00441 interval_seconds = interval / 1000L; 00442 interval_milliseconds = interval % 1000L; 00443 00444 expiration_tv_sec = tcb->last_tv_sec + interval_seconds; 00445 expiration_tv_usec = tcb->last_tv_usec + interval_milliseconds * 1000; 00446 if (expiration_tv_usec >= 1000000) 00447 { 00448 expiration_tv_usec -= 1000000; 00449 expiration_tv_sec += 1; 00450 } 00451 00452 sec_remaining = expiration_tv_sec - tv_sec; 00453 /* need to force this to be signed, as it is intended to sometimes 00454 * produce a negative result 00455 */ 00456 msec_remaining = ((long) expiration_tv_usec - (long) tv_usec) / 1000L; 00457 00458 #if MAINLOOP_SPEW 00459 _dbus_verbose ("Interval is %ld seconds %ld msecs\n", 00460 interval_seconds, 00461 interval_milliseconds); 00462 _dbus_verbose ("Now is %lu seconds %lu usecs\n", 00463 tv_sec, tv_usec); 00464 _dbus_verbose ("Last is %lu seconds %lu usecs\n", 00465 tcb->last_tv_sec, tcb->last_tv_usec); 00466 _dbus_verbose ("Exp is %lu seconds %lu usecs\n", 00467 expiration_tv_sec, expiration_tv_usec); 00468 _dbus_verbose ("Pre-correction, sec_remaining %ld msec_remaining %ld\n", 00469 sec_remaining, msec_remaining); 00470 #endif 00471 00472 /* We do the following in a rather convoluted fashion to deal with 00473 * the fact that we don't have an integral type big enough to hold 00474 * the difference of two timevals in milliseconds. 00475 */ 00476 if (sec_remaining < 0 || (sec_remaining == 0 && msec_remaining < 0)) 00477 { 00478 *timeout = 0; 00479 } 00480 else 00481 { 00482 if (msec_remaining < 0) 00483 { 00484 msec_remaining += 1000; 00485 sec_remaining -= 1; 00486 } 00487 00488 if (sec_remaining > (_DBUS_INT_MAX / 1000) || 00489 msec_remaining > _DBUS_INT_MAX) 00490 *timeout = _DBUS_INT_MAX; 00491 else 00492 *timeout = sec_remaining * 1000 + msec_remaining; 00493 } 00494 00495 if (*timeout > interval) 00496 { 00497 /* This indicates that the system clock probably moved backward */ 00498 _dbus_verbose ("System clock set backward! Resetting timeout.\n"); 00499 00500 tcb->last_tv_sec = tv_sec; 00501 tcb->last_tv_usec = tv_usec; 00502 00503 *timeout = interval; 00504 } 00505 00506 #if MAINLOOP_SPEW 00507 _dbus_verbose (" timeout expires in %d milliseconds\n", *timeout); 00508 #endif 00509 00510 return *timeout == 0; 00511 } 00512 00513 dbus_bool_t 00514 _dbus_loop_dispatch (DBusLoop *loop) 00515 { 00516 00517 #if MAINLOOP_SPEW 00518 _dbus_verbose (" %d connections to dispatch\n", _dbus_list_get_length (&loop->need_dispatch)); 00519 #endif 00520 00521 if (loop->need_dispatch == NULL) 00522 return FALSE; 00523 00524 next: 00525 while (loop->need_dispatch != NULL) 00526 { 00527 DBusConnection *connection = _dbus_list_pop_first (&loop->need_dispatch); 00528 00529 while (TRUE) 00530 { 00531 DBusDispatchStatus status; 00532 00533 status = dbus_connection_dispatch (connection); 00534 00535 if (status == DBUS_DISPATCH_COMPLETE) 00536 { 00537 dbus_connection_unref (connection); 00538 goto next; 00539 } 00540 else 00541 { 00542 if (status == DBUS_DISPATCH_NEED_MEMORY) 00543 _dbus_wait_for_memory (); 00544 } 00545 } 00546 } 00547 00548 return TRUE; 00549 } 00550 00551 dbus_bool_t 00552 _dbus_loop_queue_dispatch (DBusLoop *loop, 00553 DBusConnection *connection) 00554 { 00555 if (_dbus_list_append (&loop->need_dispatch, connection)) 00556 { 00557 dbus_connection_ref (connection); 00558 return TRUE; 00559 } 00560 else 00561 return FALSE; 00562 } 00563 00564 /* Returns TRUE if we invoked any timeouts or have ready file 00565 * descriptors, which is just used in test code as a debug hack 00566 */ 00567 00568 dbus_bool_t 00569 _dbus_loop_iterate (DBusLoop *loop, 00570 dbus_bool_t block) 00571 { 00572 #define N_STACK_DESCRIPTORS 64 00573 dbus_bool_t retval; 00574 DBusSocketEvent ready_fds[N_STACK_DESCRIPTORS]; 00575 int i; 00576 DBusList *link; 00577 int n_ready; 00578 int initial_serial; 00579 long timeout; 00580 int orig_depth; 00581 00582 retval = FALSE; 00583 00584 orig_depth = loop->depth; 00585 00586 #if MAINLOOP_SPEW 00587 _dbus_verbose ("Iteration block=%d depth=%d timeout_count=%d watch_count=%d\n", 00588 block, loop->depth, loop->timeout_count, loop->watch_count); 00589 #endif 00590 00591 if (_dbus_hash_table_get_n_entries (loop->watches) == 0 && 00592 loop->timeouts == NULL) 00593 goto next_iteration; 00594 00595 timeout = -1; 00596 if (loop->timeout_count > 0) 00597 { 00598 unsigned long tv_sec; 00599 unsigned long tv_usec; 00600 00601 _dbus_get_monotonic_time (&tv_sec, &tv_usec); 00602 00603 link = _dbus_list_get_first_link (&loop->timeouts); 00604 while (link != NULL) 00605 { 00606 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link); 00607 TimeoutCallback *tcb = link->data; 00608 00609 if (dbus_timeout_get_enabled (tcb->timeout)) 00610 { 00611 int msecs_remaining; 00612 00613 check_timeout (tv_sec, tv_usec, tcb, &msecs_remaining); 00614 00615 if (timeout < 0) 00616 timeout = msecs_remaining; 00617 else 00618 timeout = MIN (msecs_remaining, timeout); 00619 00620 #if MAINLOOP_SPEW 00621 _dbus_verbose (" timeout added, %d remaining, aggregate timeout %ld\n", 00622 msecs_remaining, timeout); 00623 #endif 00624 00625 _dbus_assert (timeout >= 0); 00626 00627 if (timeout == 0) 00628 break; /* it's not going to get shorter... */ 00629 } 00630 #if MAINLOOP_SPEW 00631 else 00632 { 00633 _dbus_verbose (" skipping disabled timeout\n"); 00634 } 00635 #endif 00636 00637 link = next; 00638 } 00639 } 00640 00641 /* Never block if we have stuff to dispatch */ 00642 if (!block || loop->need_dispatch != NULL) 00643 { 00644 timeout = 0; 00645 #if MAINLOOP_SPEW 00646 _dbus_verbose (" timeout is 0 as we aren't blocking\n"); 00647 #endif 00648 } 00649 00650 /* if a watch was OOM last time, don't wait longer than the OOM 00651 * wait to re-enable it 00652 */ 00653 if (loop->oom_watch_pending) 00654 timeout = MIN (timeout, _dbus_get_oom_wait ()); 00655 00656 #if MAINLOOP_SPEW 00657 _dbus_verbose (" polling on %d descriptors timeout %ld\n", _DBUS_N_ELEMENTS (ready_fds), timeout); 00658 #endif 00659 00660 n_ready = _dbus_socket_set_poll (loop->socket_set, ready_fds, 00661 _DBUS_N_ELEMENTS (ready_fds), timeout); 00662 00663 /* re-enable any watches we skipped this time */ 00664 if (loop->oom_watch_pending) 00665 { 00666 DBusHashIter hash_iter; 00667 00668 loop->oom_watch_pending = FALSE; 00669 00670 _dbus_hash_iter_init (loop->watches, &hash_iter); 00671 00672 while (_dbus_hash_iter_next (&hash_iter)) 00673 { 00674 DBusList **watches; 00675 int fd; 00676 dbus_bool_t changed; 00677 00678 changed = FALSE; 00679 fd = _dbus_hash_iter_get_int_key (&hash_iter); 00680 watches = _dbus_hash_iter_get_value (&hash_iter); 00681 00682 for (link = _dbus_list_get_first_link (watches); 00683 link != NULL; 00684 link = _dbus_list_get_next_link (watches, link)) 00685 { 00686 DBusWatch *watch = link->data; 00687 00688 if (_dbus_watch_get_oom_last_time (watch)) 00689 { 00690 _dbus_watch_set_oom_last_time (watch, FALSE); 00691 changed = TRUE; 00692 } 00693 } 00694 00695 if (changed) 00696 refresh_watches_for_fd (loop, watches, fd); 00697 } 00698 00699 retval = TRUE; /* return TRUE here to keep the loop going, 00700 * since we don't know the watch was inactive */ 00701 } 00702 00703 initial_serial = loop->callback_list_serial; 00704 00705 if (loop->timeout_count > 0) 00706 { 00707 unsigned long tv_sec; 00708 unsigned long tv_usec; 00709 00710 _dbus_get_monotonic_time (&tv_sec, &tv_usec); 00711 00712 /* It'd be nice to avoid this O(n) thingy here */ 00713 link = _dbus_list_get_first_link (&loop->timeouts); 00714 while (link != NULL) 00715 { 00716 DBusList *next = _dbus_list_get_next_link (&loop->timeouts, link); 00717 TimeoutCallback *tcb = link->data; 00718 00719 if (initial_serial != loop->callback_list_serial) 00720 goto next_iteration; 00721 00722 if (loop->depth != orig_depth) 00723 goto next_iteration; 00724 00725 if (dbus_timeout_get_enabled (tcb->timeout)) 00726 { 00727 int msecs_remaining; 00728 00729 if (check_timeout (tv_sec, tv_usec, 00730 tcb, &msecs_remaining)) 00731 { 00732 /* Save last callback time and fire this timeout */ 00733 tcb->last_tv_sec = tv_sec; 00734 tcb->last_tv_usec = tv_usec; 00735 00736 #if MAINLOOP_SPEW 00737 _dbus_verbose (" invoking timeout\n"); 00738 #endif 00739 00740 /* can theoretically return FALSE on OOM, but we just 00741 * let it fire again later - in practice that's what 00742 * every wrapper callback in dbus-daemon used to do */ 00743 dbus_timeout_handle (tcb->timeout); 00744 00745 retval = TRUE; 00746 } 00747 else 00748 { 00749 #if MAINLOOP_SPEW 00750 _dbus_verbose (" timeout has not expired\n"); 00751 #endif 00752 } 00753 } 00754 #if MAINLOOP_SPEW 00755 else 00756 { 00757 _dbus_verbose (" skipping invocation of disabled timeout\n"); 00758 } 00759 #endif 00760 00761 link = next; 00762 } 00763 } 00764 00765 if (n_ready > 0) 00766 { 00767 for (i = 0; i < n_ready; i++) 00768 { 00769 DBusList **watches; 00770 DBusList *next; 00771 unsigned int condition; 00772 dbus_bool_t any_oom; 00773 00774 /* FIXME I think this "restart if we change the watches" 00775 * approach could result in starving watches 00776 * toward the end of the list. 00777 */ 00778 if (initial_serial != loop->callback_list_serial) 00779 goto next_iteration; 00780 00781 if (loop->depth != orig_depth) 00782 goto next_iteration; 00783 00784 _dbus_assert (ready_fds[i].flags != 0); 00785 00786 if (_DBUS_UNLIKELY (ready_fds[i].flags & _DBUS_WATCH_NVAL)) 00787 { 00788 cull_watches_for_invalid_fd (loop, ready_fds[i].fd); 00789 goto next_iteration; 00790 } 00791 00792 condition = ready_fds[i].flags; 00793 _dbus_assert ((condition & _DBUS_WATCH_NVAL) == 0); 00794 00795 /* condition may still be 0 if we got some 00796 * weird POLLFOO thing like POLLWRBAND 00797 */ 00798 if (condition == 0) 00799 continue; 00800 00801 watches = _dbus_hash_table_lookup_int (loop->watches, 00802 ready_fds[i].fd); 00803 00804 if (watches == NULL) 00805 continue; 00806 00807 any_oom = FALSE; 00808 00809 for (link = _dbus_list_get_first_link (watches); 00810 link != NULL; 00811 link = next) 00812 { 00813 DBusWatch *watch = link->data; 00814 00815 next = _dbus_list_get_next_link (watches, link); 00816 00817 if (dbus_watch_get_enabled (watch)) 00818 { 00819 dbus_bool_t oom; 00820 00821 oom = !dbus_watch_handle (watch, condition); 00822 00823 if (oom) 00824 { 00825 _dbus_watch_set_oom_last_time (watch, TRUE); 00826 loop->oom_watch_pending = TRUE; 00827 any_oom = TRUE; 00828 } 00829 00830 #if MAINLOOP_SPEW 00831 _dbus_verbose (" Invoked watch, oom = %d\n", oom); 00832 #endif 00833 retval = TRUE; 00834 00835 /* We re-check this every time, in case the callback 00836 * added/removed watches, which might make our position in 00837 * the linked list invalid. See the FIXME above. */ 00838 if (initial_serial != loop->callback_list_serial || 00839 loop->depth != orig_depth) 00840 { 00841 if (any_oom) 00842 refresh_watches_for_fd (loop, NULL, ready_fds[i].fd); 00843 00844 goto next_iteration; 00845 } 00846 } 00847 } 00848 00849 if (any_oom) 00850 refresh_watches_for_fd (loop, watches, ready_fds[i].fd); 00851 } 00852 } 00853 00854 next_iteration: 00855 #if MAINLOOP_SPEW 00856 _dbus_verbose (" moving to next iteration\n"); 00857 #endif 00858 00859 if (_dbus_loop_dispatch (loop)) 00860 retval = TRUE; 00861 00862 #if MAINLOOP_SPEW 00863 _dbus_verbose ("Returning %d\n", retval); 00864 #endif 00865 00866 return retval; 00867 } 00868 00869 void 00870 _dbus_loop_run (DBusLoop *loop) 00871 { 00872 int our_exit_depth; 00873 00874 _dbus_assert (loop->depth >= 0); 00875 00876 _dbus_loop_ref (loop); 00877 00878 our_exit_depth = loop->depth; 00879 loop->depth += 1; 00880 00881 _dbus_verbose ("Running main loop, depth %d -> %d\n", 00882 loop->depth - 1, loop->depth); 00883 00884 while (loop->depth != our_exit_depth) 00885 _dbus_loop_iterate (loop, TRUE); 00886 00887 _dbus_loop_unref (loop); 00888 } 00889 00890 void 00891 _dbus_loop_quit (DBusLoop *loop) 00892 { 00893 _dbus_assert (loop->depth > 0); 00894 00895 loop->depth -= 1; 00896 00897 _dbus_verbose ("Quit main loop, depth %d -> %d\n", 00898 loop->depth + 1, loop->depth); 00899 } 00900 00901 int 00902 _dbus_get_oom_wait (void) 00903 { 00904 #ifdef DBUS_ENABLE_EMBEDDED_TESTS 00905 /* make tests go fast */ 00906 return 0; 00907 #else 00908 return 500; 00909 #endif 00910 } 00911 00912 void 00913 _dbus_wait_for_memory (void) 00914 { 00915 _dbus_verbose ("Waiting for more memory\n"); 00916 _dbus_sleep_milliseconds (_dbus_get_oom_wait ()); 00917 } 00918 00919 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */
1.7.6.1