From 0720ed9c765d30f6b32993c8bb6072d248636de3 Mon Sep 17 00:00:00 2001 From: Hayodea Hekol Date: Fri, 14 Nov 2025 02:23:04 -0400 Subject: [PATCH] StimBuff: Make produceFrameReq responsive to stop() --- include/user/stimulusBuffer.h | 2 ++ .../livoxGen1/pcloudStimulusBuffer.cpp | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/user/stimulusBuffer.h b/include/user/stimulusBuffer.h index cf21101..c9a1fc0 100644 --- a/include/user/stimulusBuffer.h +++ b/include/user/stimulusBuffer.h @@ -107,8 +107,10 @@ protected: private: boost::asio::io_service& ioService; +protected: SpinLock shouldContinueLock; bool shouldContinue; +private: boost::asio::deadline_timer timer; size_t nDeferrals; std::chrono::high_resolution_clock::time_point deferralStartTime; diff --git a/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp b/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp index 0f2ffdc..823b36e 100644 --- a/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp +++ b/stimBuffApis/livoxGen1/pcloudStimulusBuffer.cpp @@ -126,6 +126,13 @@ public: void produceFrameReq1_doAssemble_posted( std::shared_ptr context) { + SpinLock::Guard lock(stimBuff.shouldContinueLock); + if (!stimBuff.shouldContinue) + { + callOriginalCallback(); + return; + } + stimBuff.ioUringAssemblyEngine.assembleFrameReq( {context, std::bind( &ProduceFrameReq::produceFrameReq2_assembleDone, @@ -137,6 +144,13 @@ public: std::shared_ptr context, bool success, AsynchronousLoop loop) { + SpinLock::Guard lock(stimBuff.shouldContinueLock); + if (!stimBuff.shouldContinue) + { + callOriginalCallback(); + return; + } + if (!success) { std::cerr << __func__ << ": Failed to assemble frame" << std::endl; @@ -158,6 +172,13 @@ public: [[maybe_unused]] std::shared_ptr context, bool success, StimulusFrame& /*stimulusFrame*/) { + SpinLock::Guard lock(stimBuff.shouldContinueLock); + if (!stimBuff.shouldContinue) + { + callOriginalCallback(); + return; + } + if (!success) { std::cerr << __func__ << ": Failed to compact and collate frame" << std::endl; } else { @@ -177,6 +198,12 @@ public: void PcloudStimulusBuffer::produceFrameReq( smo::Callback callback) { + /** EXPLANATION: + * We shouldn't acquire the StimulusBuffer::shouldContinueLock here because + * this function is called from + * StimulusBuffer::stimFrameProductionTimesliceInd(), which is already + * holding the lock. + */ auto caller = smoHooksPtr->ComponentThread_getSelf(); auto request = std::make_shared( *this, caller, std::move(callback));