diff --git a/common/ImageBuffer.cpp b/common/ImageBuffer.cpp index faa52b817..6711e0e90 100644 --- a/common/ImageBuffer.cpp +++ b/common/ImageBuffer.cpp @@ -184,6 +184,29 @@ void ImageBuffer::ReleaseSlot(uint32_t location) { cv_send_done.notify_all(); } +void ImageBuffer::DiscardSlot(uint32_t location) { + std::unique_lock ul(m); + + if (location >= v.size()) + return; + + if (v[location].status == ImageBufferEntryStatus::InPreparation) { + --slots_preparation; + cv_preparation_done.notify_all(); + } else if (v[location].status == ImageBufferEntryStatus::Sending) { + --slots_sending; + cv_send_done.notify_all(); + } else + return; + + ++counter; + v[location].status = ImageBufferEntryStatus::Idle; + v[location].image_number = -1; + v[location].image_size = 0; + v[location].indexed = false; + free_slots.push(location); +} + bool ImageBuffer::Finalize(std::chrono::microseconds timeout) { std::unique_lock ul(m); bool ret = FinalizeInternal(ul, timeout); diff --git a/common/ImageBuffer.h b/common/ImageBuffer.h index 1f87eca9d..bd1625172 100644 --- a/common/ImageBuffer.h +++ b/common/ImageBuffer.h @@ -79,6 +79,8 @@ public: ZeroCopyReturnValue *GetImageSlot(int64_t keep_free = 0); void ReadyToSend(uint32_t location, int64_t image_number, size_t image_size, bool indexed); void ReleaseSlot(uint32_t location); + // Gives the slot back unsent, in preparation or sending, and forgets its image: no request finds it again + void DiscardSlot(uint32_t location); int64_t GetAvailSlots() const; size_t GetBufferSize() const; diff --git a/common/ZeroCopyReturnValue.cpp b/common/ZeroCopyReturnValue.cpp index b5d88011d..81194c322 100644 --- a/common/ZeroCopyReturnValue.cpp +++ b/common/ZeroCopyReturnValue.cpp @@ -42,6 +42,15 @@ void ZeroCopyReturnValue::release() { } } +void ZeroCopyReturnValue::discard() { + // As in release: reset before the handle goes back on the free list + status = ImageBufferEntryStatus::InPreparation; + image_number = -1; + payload_size = 0; + indexed = false; + buf_ctrl.DiscardSlot(handle); +} + int64_t ZeroCopyReturnValue::GetImageNumber() const { return image_number; } diff --git a/common/ZeroCopyReturnValue.h b/common/ZeroCopyReturnValue.h index 2d46fb95c..038ef3fc2 100644 --- a/common/ZeroCopyReturnValue.h +++ b/common/ZeroCopyReturnValue.h @@ -30,4 +30,6 @@ public: [[nodiscard]] bool IsIndexed() const; void ReadyToSend(); void release(); + // Back to the buffer without being sent, and forgotten there (ImageBuffer::DiscardSlot) + void discard(); }; diff --git a/receiver/StillsPrePass.h b/receiver/StillsPrePass.h index afd12816a..dfd25de33 100644 --- a/receiver/StillsPrePass.h +++ b/receiver/StillsPrePass.h @@ -11,7 +11,8 @@ #include "../common/PixelMask.h" #include "../common/ZeroCopyReturnValue.h" -// An image held until analysed, pointing into `slot`, which goes back unsent with the last copy +// An image held until analysed, pointing into `slot`, which goes back unsent with the last copy. The slot is +// forgotten then, so that the copy sent with the analysis is the only one the image buffer finds. struct HeldImage { DataMessage message; ZeroCopyReturnValue *slot = nullptr; @@ -20,12 +21,8 @@ struct HeldImage { HeldImage(const HeldImage &) = delete; HeldImage &operator=(const HeldImage &) = delete; ~HeldImage() { - if (slot) { - slot->SetImageNumber(-1); - slot->SetImageSize(0); - slot->SetIndexed(false); - slot->release(); - } + if (slot) + slot->discard(); } }; diff --git a/tests/ImageBufferTest.cpp b/tests/ImageBufferTest.cpp index 9cd262217..7f6e7bbff 100644 --- a/tests/ImageBufferTest.cpp +++ b/tests/ImageBufferTest.cpp @@ -336,4 +336,63 @@ TEST_CASE("ImageBuffer_SlotReuse_ReleaseFromInPreparation") { REQUIRE(buf_ctrl.GetAvailSlots() == 1); REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1))); -} \ No newline at end of file +} +TEST_CASE("ImageBuffer_DiscardSlot") { + // A slot given back with DiscardSlot is found no more; the copy of the same image in another slot is. + DiffractionExperiment experiment(DetJF(18,3,8,36)); + + ImageBuffer buf_ctrl(4 * experiment.GetImageBufferLocationSize()); + buf_ctrl.StartMeasurement(experiment); + + ZeroCopyReturnValue *held = buf_ctrl.GetImageSlot(); + REQUIRE(held != nullptr); + memset(held->GetImage(), 1, 100); + held->SetImageNumber(5); + held->SetImageSize(100); + held->ReadyToSend(); + + std::vector v; + REQUIRE(buf_ctrl.GetImage(v, 5)); + CHECK(v[0] == 1); + + ZeroCopyReturnValue *analysed = buf_ctrl.GetImageSlot(); + REQUIRE(analysed != nullptr); + memset(analysed->GetImage(), 2, 200); + analysed->SetImageNumber(5); + analysed->SetImageSize(200); + analysed->ReadyToSend(); + + auto counter = buf_ctrl.GetStatus().current_counter; + held->discard(); + CHECK(buf_ctrl.GetStatus().current_counter != counter); + CHECK(buf_ctrl.GetStatus().images_in_the_buffer == std::vector{5}); + CHECK(buf_ctrl.GetStatus().sending_slots == 1); + + REQUIRE(buf_ctrl.GetImage(v, 5)); + CHECK(v.size() == 200); + CHECK(v[0] == 2); + REQUIRE(buf_ctrl.GetImage(v, ImageBuffer::MaxImage)); + CHECK(v.size() == 200); + + analysed->release(); + + // From preparation, as a frame dropped before it was shown + ZeroCopyReturnValue *dropped = buf_ctrl.GetImageSlot(); + REQUIRE(dropped != nullptr); + dropped->SetImageNumber(6); + dropped->discard(); + CHECK(buf_ctrl.GetStatus().preparation_slots == 0); + CHECK(!buf_ctrl.GetImage(v, 6)); + + CHECK(buf_ctrl.GetAvailSlots() == 4); + REQUIRE(buf_ctrl.CheckIfBufferReturned(std::chrono::microseconds(1))); + + // Every slot can be taken again + for (int i = 0; i < 4; i++) { + ZeroCopyReturnValue *ret = buf_ctrl.GetImageSlot(); + REQUIRE(ret != nullptr); + ret->SetImageNumber(10 + i); + ret->release(); + } + REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1))); +}