From 0724bd36ebbf2236ac51e5eb8dccc8e84e287553 Mon Sep 17 00:00:00 2001 From: Martin Ling Date: Mon, 4 Jul 2022 18:00:07 +0100 Subject: [PATCH] Lock the whole code block that touches active transfer count. I believe this was safe before, because this code is only called from the transfer thread, and the condition being protected is just whether the count is zero, not the actual value of the count. However, this isn't performance critical and it's a lot easier to reason about the code if we just hold the lock for this whole section. --- host/libhackrf/src/hackrf.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 9544354a..b418a1c6 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -1695,14 +1695,14 @@ static void transfer_finished(struct hackrf_device* device, struct libusb_transf device->streaming = false; // If this is the last transfer, signal that all are now finished. + pthread_mutex_lock(&device->all_finished_lock); if (device->active_transfers == 1) { - pthread_mutex_lock(&device->all_finished_lock); device->active_transfers = 0; pthread_cond_signal(&device->all_finished_cv); - pthread_mutex_unlock(&device->all_finished_lock); } else { device->active_transfers--; } + pthread_mutex_unlock(&device->all_finished_lock); } static void LIBUSB_CALL hackrf_libusb_transfer_callback(struct libusb_transfer* usb_transfer)