From 7ff92b3b05ccf2ffe6ca03e95f2e0a90e30a1e28 Mon Sep 17 00:00:00 2001 From: Yan Date: Tue, 15 Mar 2022 12:43:45 +0000 Subject: [PATCH 1/2] Ensure fast exit transfer_threadproc has a timeout of half a second, so when kill_transfer_thread tries to pthread_join, it often has to wait until the timeout kicks in. With this fix, we ensure that a final request is made after request_exit has been called, so that transfer_threadproc can exit its loop in a fast and clean manner. --- host/libhackrf/src/hackrf.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 73cbade5..528863d7 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -1786,6 +1786,13 @@ static int kill_transfer_thread(hackrf_device* device) * Now call request_exit() to halt the main loop. */ request_exit(device); + /* + * Make a final request, so that threadproc can exit immediately, + * without waiting for timeout. Don't care about result. + */ + uint8_t board_id; + result = hackrf_board_id_read(device, &board_id); + value = NULL; result = pthread_join(device->transfer_thread, &value); if( result != 0 ) From 790de7f47b7919f944dfb39956405c34b4bb486b Mon Sep 17 00:00:00 2001 From: Yan Date: Wed, 16 Mar 2022 10:44:04 +0000 Subject: [PATCH 2/2] Cleaner fast exit Interrupt the event handling thread instead of waiting for timeout. --- host/libhackrf/src/hackrf.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 528863d7..530b93c0 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -1787,11 +1787,10 @@ static int kill_transfer_thread(hackrf_device* device) */ request_exit(device); /* - * Make a final request, so that threadproc can exit immediately, - * without waiting for timeout. Don't care about result. + * Interrupt the event handling thread instead of + * waiting for timeout. */ - uint8_t board_id; - result = hackrf_board_id_read(device, &board_id); + libusb_interrupt_event_handler(g_libusb_context); value = NULL; result = pthread_join(device->transfer_thread, &value);