This is the main loop. */
| 371 | |
| 372 | /* This is the main loop. */ |
| 373 | void *io_loop(struct timers *timers, struct timer **expired) |
| 374 | { |
| 375 | void *ret; |
| 376 | /* This ensures we don't always service lower fds first */ |
| 377 | static int fairness_counter; |
| 378 | |
| 379 | /* if timers is NULL, expired must be. If not, not. */ |
| 380 | assert(!timers == !expired); |
| 381 | |
| 382 | /* Make sure this is NULL if we exit for some other reason. */ |
| 383 | if (expired) |
| 384 | *expired = NULL; |
| 385 | |
| 386 | while (!io_loop_return) { |
| 387 | int i, r, ms_timeout = -1; |
| 388 | |
| 389 | /* Everything closed? */ |
| 390 | if (num_fds == 0) |
| 391 | break; |
| 392 | |
| 393 | /* You can't tell them all to go to sleep! */ |
| 394 | assert(num_waiting || num_always); |
| 395 | |
| 396 | if (timers) { |
| 397 | struct timemono now, first; |
| 398 | |
| 399 | now = nowfn(); |
| 400 | |
| 401 | /* Call functions for expired timers. */ |
| 402 | *expired = timers_expire(timers, now); |
| 403 | if (*expired) |
| 404 | break; |
| 405 | |
| 406 | /* Now figure out how long to wait for the next one. */ |
| 407 | if (timer_earliest(timers, &first)) { |
| 408 | uint64_t next; |
| 409 | next = time_to_msec(timemono_between(first, now)); |
| 410 | if (next < INT_MAX) |
| 411 | ms_timeout = next; |
| 412 | else |
| 413 | ms_timeout = INT_MAX; |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | /* Don't wait if we have always requests pending! */ |
| 418 | if (num_always != 0) |
| 419 | ms_timeout = 0; |
| 420 | |
| 421 | /* We do this temporarily, assuming exclusive is unusual */ |
| 422 | exclude_pollfds(); |
| 423 | r = pollfn(pollfds, num_fds, ms_timeout); |
| 424 | restore_pollfds(); |
| 425 | |
| 426 | if (r < 0) { |
| 427 | /* Signals shouldn't break us, unless they set |
| 428 | * io_loop_return. */ |
| 429 | if (errno == EINTR) |
| 430 | continue; |