From cdade1790575d33a14a4d5a18743797b7e7da57a Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Sat, 1 Nov 2025 00:06:42 -0400 Subject: [PATCH] Add SpMcRingBuffer to base class StimulusBuffer This will hopefully genericise the interface for Stimbuffs. --- include/user/sequenceLock.h | 8 ++++++++ include/user/spMcRingBuffer.h | 11 +++++++---- include/user/stimulusBuffer.h | 5 +++++ stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp | 10 +++++++++- 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/include/user/sequenceLock.h b/include/user/sequenceLock.h index b8808ef..118e541 100644 --- a/include/user/sequenceLock.h +++ b/include/user/sequenceLock.h @@ -20,6 +20,14 @@ public: : sequenceNo(0) {} + ~SequenceLock() = default; + + // Non-copyable, non-movable (std::atomic is neither copyable nor movable) + SequenceLock(const SequenceLock&) = delete; + SequenceLock& operator=(const SequenceLock&) = delete; + SequenceLock(SequenceLock&&) = delete; + SequenceLock& operator=(SequenceLock&&) = delete; + /* Atomically increments sequenceNo and issues a release barrier. * Makes the sequence number odd, indicating a write is in progress. */ diff --git a/include/user/spMcRingBuffer.h b/include/user/spMcRingBuffer.h index 34f6a2a..e37fcc2 100644 --- a/include/user/spMcRingBuffer.h +++ b/include/user/spMcRingBuffer.h @@ -2,6 +2,7 @@ #define _SP_MC_RING_BUFFER_H #include +#include #include #include #include @@ -44,8 +45,8 @@ public: * locks array. */ explicit SpMcRingBuffer( - const InputEngineConstraints& constraints_, - size_t nSlots_) + size_t nSlots_, + const InputEngineConstraints& constraints_) : nSlots(nSlots_), strideNBytes(0), bufferNBytes(0), constraints(constraints_) { @@ -59,7 +60,8 @@ public: // Allocate data buffer: bufferNBytes (aligned up to alignment) data.resize(bufferNBytes); // Initialize sequence locks array: one lock per slot - sequenceLocks.resize(nSlots); + // Use unique_ptr array since SequenceLock is not copyable or movable + sequenceLocks = std::make_unique(nSlots); } ~SpMcRingBuffer() = default; @@ -121,7 +123,8 @@ private: std::vector data; // Sequence locks array: one lock per slot - std::vector sequenceLocks; + // Use unique_ptr array since SequenceLock is not copyable or movable + std::unique_ptr sequenceLocks; public: // Layout/invariants diff --git a/include/user/stimulusBuffer.h b/include/user/stimulusBuffer.h index 286a381..f648b97 100644 --- a/include/user/stimulusBuffer.h +++ b/include/user/stimulusBuffer.h @@ -13,6 +13,7 @@ #include #include #include +#include #include "stimFrame.h" #include "deviceAttachmentSpec.h" @@ -48,8 +49,11 @@ public: public: explicit StimulusBuffer( const device::DeviceAttachmentSpec& deviceAttachmentSpec, + size_t nSlots, + const SpMcRingBuffer::InputEngineConstraints& ringBufferConstraints, boost::asio::io_service& ioService) : deviceAttachmentSpec(deviceAttachmentSpec), + ringBuffer(nSlots, ringBufferConstraints), ioService(ioService), shouldContinue(false), timer(ioService) {} @@ -89,6 +93,7 @@ public: protected: SpinLock frameAssemblyRateLimiter; + SpMcRingBuffer ringBuffer; private: boost::asio::io_service& ioService; diff --git a/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp b/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp index 2a16e17..9b9199d 100644 --- a/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp +++ b/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp @@ -1,5 +1,8 @@ +#include #include #include +#include +#include #include #include "pcloudStimulusBuffer.h" @@ -11,7 +14,12 @@ PcloudStimulusBuffer::PcloudStimulusBuffer( std::shared_ptr &device, const PcloudFormatDesc& formatDesc, size_t nDgramsPerStagingBufferFrame) -: StimulusBuffer(deviceAttachmentSpec, device->componentThread->getIoService()), +: StimulusBuffer( + deviceAttachmentSpec, + static_cast((1000 * 30) / CONFIG_STIMBUFF_FRAME_PERIOD_MS), + SpMcRingBuffer::InputEngineConstraints( + static_cast(sysconf(_SC_PAGE_SIZE)), 4), + device->componentThread->getIoService()), deviceAttachmentSpec(deviceAttachmentSpec), device(device), formatDesc(formatDesc), stagingBuffer( StagingBuffer::InputEngineConstraints::ioUringConstraints,