core: Miscellaneous transfer timeout improvements
authorChris Dickens <christopher.a.dickens@gmail.com>
Mon, 27 Apr 2015 09:16:28 +0000 (02:16 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 7 May 2015 19:34:35 +0000 (12:34 -0700)
  * When removing a transfer from the flying_transfers list, only
    rearm the timerfd if the transfer being removed was the first
    transfer *and* had a timeout. This is the only case where the
    timerfd should be altered by a transfer being removed.

  * When searching the flying_transfers list for the next timeout,
    searching can cease when the first transfer with an infinite
    timeout is encountered.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
libusb/io.c
libusb/version_nano.h

index 7c01fda..c71a2a2 100644 (file)
@@ -1431,16 +1431,14 @@ out:
 static int remove_from_flying_list(struct usbi_transfer *transfer)
 {
        struct libusb_context *ctx = ITRANSFER_CTX(transfer);
+       int rearm_timerfd;
        int r = 0;
 
-       /* FIXME: could be more intelligent with the timerfd here. we don't need
-        * to disarm the timerfd if there was no timer running, and we only need
-        * to rearm the timerfd if the transfer that expired was the one with
-        * the shortest timeout. */
-
        usbi_mutex_lock(&ctx->flying_transfers_lock);
+       rearm_timerfd = (timerisset(&transfer->timeout) &&
+               list_first_entry(&ctx->flying_transfers, struct usbi_transfer, list) == transfer);
        list_del(&transfer->list);
-       if (usbi_using_timerfd(ctx))
+       if (usbi_using_timerfd(ctx) && rearm_timerfd)
                r = arm_timerfd_for_next_timeout(ctx);
        usbi_mutex_unlock(&ctx->flying_transfers_lock);
 
@@ -2511,9 +2509,9 @@ int API_EXPORTED libusb_get_next_timeout(libusb_context *ctx,
                if (transfer->flags & (USBI_TRANSFER_TIMED_OUT | USBI_TRANSFER_OS_HANDLES_TIMEOUT))
                        continue;
 
-               /* no timeout for this transfer? */
+               /* if we've reached transfers of infinte timeout, we're done looking */
                if (!timerisset(&transfer->timeout))
-                       continue;
+                       break;
 
                found = 1;
                break;
index bfafef7..2db3257 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 10980
+#define LIBUSB_NANO 10981