ImageBuffer: a slot can be discarded - given back unsent and forgotten
DiscardSlot (ZeroCopyReturnValue::discard) returns a slot that is in preparation or already readable (Sending) to the free list and clears its image number, so that no request for that image finds it again; ReleaseSlot keeps the number, and an idle copy could shadow a newer slot carrying the same image. A held stills frame now goes back this way. For a frame still in preparation (the FPGA path) the outcome is what release() did with the number set to -1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01HVeDQsU5a8AJUdJPgWevAK
This commit is contained in:
1 parent
c7fb351bf5
commit
e2a845f6a0
6 files changed
+100
-8
No files matched your search
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
};
|
||||
@@ -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();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -336,4 +336,63 @@ TEST_CASE("ImageBuffer_SlotReuse_ReleaseFromInPreparation") {
|
||||
|
||||
REQUIRE(buf_ctrl.GetAvailSlots() == 1);
|
||||
REQUIRE(buf_ctrl.Finalize(std::chrono::microseconds(1)));
|
||||
}
|
||||
}
|
||||
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<uint8_t> 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<int64_t>{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)));
|
||||
}
|
||||
Reference in new issue
Block a user