From 73d28838c08486e68651a208141ede0ef87e7ab5 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Thu, 26 Nov 2020 13:08:40 -0500 Subject: [PATCH] Slightly rebalances template. More clearly to ensure the lock_guard stays in the correct place. --- Outputs/ScanTargets/BufferingScanTarget.cpp | 42 +++++++++++---------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/Outputs/ScanTargets/BufferingScanTarget.cpp b/Outputs/ScanTargets/BufferingScanTarget.cpp index f78fc4de4..c5bc95002 100644 --- a/Outputs/ScanTargets/BufferingScanTarget.cpp +++ b/Outputs/ScanTargets/BufferingScanTarget.cpp @@ -90,6 +90,16 @@ uint8_t *BufferingScanTarget::begin_data(size_t required_length, size_t required } template void BufferingScanTarget::end_data(size_t actual_length) { + // Bookend the start and end of the new data, to safeguard for precision errors in sampling. + DataUnit *const sized_write_area = &reinterpret_cast(write_area_)[write_pointers_.write_area]; + sized_write_area[-1] = sized_write_area[0]; + sized_write_area[actual_length] = sized_write_area[actual_length - 1]; +} + +void BufferingScanTarget::end_data(size_t actual_length) { + // Acquire the producer lock. + std::lock_guard lock_guard(producer_mutex_); + // Do nothing if no data write is actually ongoing. if(!data_is_allocated_) return; data_is_allocated_ = false; @@ -97,10 +107,18 @@ template void BufferingScanTarget::end_data(size_t actual_le // Check for other allocation failures. if(allocation_has_failed_) return; - // Bookend the start and end of the new data, to safeguard for precision errors in sampling. - DataUnit *const sized_write_area = &reinterpret_cast(write_area_)[write_pointers_.write_area]; - sized_write_area[-1] = sized_write_area[0]; - sized_write_area[actual_length] = sized_write_area[actual_length - 1]; + // Apply necessary bookends. + switch(data_type_size_) { + default: assert(false); + case 0: + // This just means that modals haven't been grabbed yet. So it's not + // a valid data type size, but it is a value that might legitimately + // be seen here. + break; + case 1: end_data(actual_length); break; + case 2: end_data(actual_length); break; + case 4: end_data(actual_length); break; + } // Advance to the end of the current run. write_pointers_.write_area += actual_length + 1; @@ -111,22 +129,6 @@ template void BufferingScanTarget::end_data(size_t actual_le write_pointers_.write_area %= WriteAreaWidth*WriteAreaHeight; } -void BufferingScanTarget::end_data(size_t actual_length) { - // Acquire the producer lock. - std::lock_guard lock_guard(producer_mutex_); - - // Just dispatch appropriately. - switch(data_type_size_) { - default: assert(false); - case 0: - // This just means that modals haven't been grabbed yet. - break; - case 1: end_data(actual_length); break; - case 2: end_data(actual_length); break; - case 4: end_data(actual_length); break; - } -} - // MARK: - Producer; scans. Outputs::Display::ScanTarget::Scan *BufferingScanTarget::begin_scan() {