104 lines
2.7 KiB
C++
104 lines
2.7 KiB
C++
#include <boostAsioLinkageFix.h>
|
|
#include <iostream>
|
|
#include <config.h>
|
|
#include <componentThread.h>
|
|
#include <boost/asio/io_service.hpp>
|
|
#include <boost/asio/deadline_timer.hpp>
|
|
#include <boost/system/error_code.hpp>
|
|
#include <spinLock.h>
|
|
#include <user/stimulusBuffer.h>
|
|
|
|
namespace smo {
|
|
namespace stim_buff {
|
|
|
|
void StimulusBuffer::stop()
|
|
{
|
|
{
|
|
SpinLock::Guard lock(shouldContinueLock);
|
|
shouldContinue = false;
|
|
}
|
|
|
|
// Cancel timer immediately
|
|
timer.cancel();
|
|
|
|
std::cout << __func__ << ": Stopped stimulus buffer for device "
|
|
<< deviceAttachmentSpec->deviceSelector << std::endl;
|
|
}
|
|
|
|
void StimulusBuffer::scheduleNextTimeout(int delayMs)
|
|
{
|
|
if (!shouldContinue)
|
|
{ return; }
|
|
|
|
// Schedule the next timeout using the provided delay
|
|
timer.expires_from_now(
|
|
boost::posix_time::milliseconds(delayMs));
|
|
|
|
timer.async_wait(
|
|
std::bind(
|
|
&StimulusBuffer::onTimeout, this, std::placeholders::_1));
|
|
}
|
|
|
|
void StimulusBuffer::onTimeout(const boost::system::error_code& error)
|
|
{
|
|
// Timer was cancelled, which is expected when stopping
|
|
if (error == boost::asio::error::operation_aborted) {
|
|
return;
|
|
}
|
|
|
|
if (error)
|
|
{
|
|
std::cerr << "StimulusBuffer: Timer error: " << error.message()
|
|
<< std::endl;
|
|
return;
|
|
}
|
|
|
|
SpinLock::Guard lock(shouldContinueLock);
|
|
if (!shouldContinue)
|
|
{ return; }
|
|
|
|
/** EXPLANATION:
|
|
* We need to ensure that there's only ever one stimframe being produced
|
|
* during any CONFIG_STIMBUFF_FRAME_PERIOD_MS period. To guarantee this, we
|
|
* use a spinlock.
|
|
*
|
|
* When a new frame is to be produced, the async producer will first acquire
|
|
* the frameAssemblyLimiter spinlock. This way, when the next timeout is
|
|
* fired it can check whether its predecessor stimframe has finished being
|
|
* produced. If the preceding stimframe is still being produced, then we'll
|
|
* sleep for CONFIG_STIMBUFF_FRAME_RETRY_DELAY_MS ms before trying again.
|
|
*/
|
|
int nextWakeupDelayMs;
|
|
bool deferred = false;
|
|
if (frameAssemblyRateLimiter.tryAcquire())
|
|
{
|
|
nextWakeupDelayMs = CONFIG_STIMBUFF_FRAME_PERIOD_MS;
|
|
|
|
/** EXPLANATION:
|
|
* Call the derived class's frame production handler
|
|
* Note: The derived class's frame production handler (aka
|
|
* its implementation of stimFrameProductionTimesliceInd()) must
|
|
* release the lock when frame production completes
|
|
*/
|
|
stimFrameProductionTimesliceInd();
|
|
}
|
|
else
|
|
{
|
|
nextWakeupDelayMs = CONFIG_STIMBUFF_FRAME_RETRY_DELAY_MS;
|
|
deferred = true;
|
|
}
|
|
|
|
// Schedule next timeout with the pre-determined duration
|
|
scheduleNextTimeout(nextWakeupDelayMs);
|
|
|
|
// We should be able to release the start/stop lock before printing here.
|
|
if (deferred)
|
|
{
|
|
std::cout << __func__ << ": Deferring frame by " << nextWakeupDelayMs
|
|
<< "ms due to rate limit." << std::endl;
|
|
}
|
|
}
|
|
|
|
} // namespace stim_buff
|
|
} // namespace smo
|