Please find below a patch as per suggestion of Martin Ling: call 
libusb_interrupt_event_handler in rtlsdr_cancel_async to trigger the event loop 
handler to look at the async_cancel variable.
(https://lists.osmocom.org/hyperkitty/list/[email protected]/thread/7TKOAOP5KNQBBCDJRV7EI67AFKEDTNCP/)

I have tested timings on a windows 11 machine with and without the patch when 
running a async transfer at two different sampling rates:

sample rate     with            without         theory
1536K           0 - 2 ms        0 - 84 ms       85 ms
288K            0 - 4 ms        0 - 457 ms      455 ms

The table shows the range of timings to cancel the async transfer as measured 
over a couple of runs.
The "theory" column shows the theoretical time to complete one transfer at the 
particular sampling rate (i.e. driven by the size of a transfer buffer). And 
indeed, on some machines it appears that cancelling the async transfer, if 
unlucky, can take as much time as waiting for a full transfer to complete. The 
call to libsub_intertrupt_event_handler fixes that. For large transfer buffers 
and/or low sample rates this can make a difference.

Thanks to Martin for sharing the insight.

Jasper

Author: jvde.github <[email protected]>
Date:   Wed Jan 26 19:00:55 2022 +0100

    call interrupt_event_handler in cancel_async

diff --git a/src/librtlsdr.c b/src/librtlsdr.c
index 67e30c4..b949692 100644
--- a/src/librtlsdr.c
+++ b/src/librtlsdr.c
@@ -1948,6 +1948,9 @@ int rtlsdr_cancel_async(rtlsdr_dev_t *dev)
        if (RTLSDR_RUNNING == dev->async_status) {
                dev->async_status = RTLSDR_CANCELING;
                dev->async_cancel = 1;
+
+               libusb_interrupt_event_handler(dev->ctx);
+
                return 0;
        }

Reply via email to