Nursery: Initial integration
StimulusProducer: syncAwaitAllSettlements should pump caller io_context
This commit is contained in:
@@ -5,7 +5,6 @@
|
||||
#include <atomic>
|
||||
#include <coroutine>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
#include <boost/asio/deadline_timer.hpp>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
@@ -15,7 +14,10 @@
|
||||
|
||||
namespace adapters::boostAsio {
|
||||
|
||||
/** Coroutine awaiter: true if the delay elapsed, false if cancelled/aborted. */
|
||||
/** Coroutine awaiter: true if the delay elapsed, false if cancelled/aborted.
|
||||
*
|
||||
* Reuses the caller-supplied deadline_timer; does not allocate a new timer.
|
||||
*/
|
||||
class DeadlineTimerAReq
|
||||
{
|
||||
public:
|
||||
@@ -24,22 +26,17 @@ public:
|
||||
std::atomic<bool> settled{false};
|
||||
bool timerExpiredNormally = false;
|
||||
std::coroutine_handle<> callerSchedHandle;
|
||||
std::shared_ptr<boost::asio::deadline_timer> timer;
|
||||
};
|
||||
|
||||
DeadlineTimerAReq(
|
||||
boost::asio::io_context &resumeIoContext,
|
||||
const boost::posix_time::milliseconds delay,
|
||||
std::optional<std::shared_ptr<boost::asio::deadline_timer>> &timerOut)
|
||||
boost::asio::deadline_timer &timer,
|
||||
const boost::posix_time::milliseconds delay)
|
||||
: asyncState(std::make_shared<AsyncState>()),
|
||||
resumeIoContext(resumeIoContext)
|
||||
{
|
||||
asyncState->timer =
|
||||
std::make_shared<boost::asio::deadline_timer>(resumeIoContext);
|
||||
timerOut = asyncState->timer;
|
||||
|
||||
asyncState->timer->expires_from_now(delay);
|
||||
asyncState->timer->async_wait(
|
||||
timer.expires_from_now(delay);
|
||||
timer.async_wait(
|
||||
[this](const boost::system::error_code &error)
|
||||
{
|
||||
onTimer(error);
|
||||
@@ -92,19 +89,11 @@ private:
|
||||
};
|
||||
|
||||
inline auto getDeadlineTimerAReqAwaiter(
|
||||
boost::asio::io_context &ioContext,
|
||||
boost::asio::io_context &resumeIoContext,
|
||||
boost::asio::deadline_timer &timer,
|
||||
const boost::posix_time::milliseconds delay)
|
||||
{
|
||||
std::optional<std::shared_ptr<boost::asio::deadline_timer>> timerOut;
|
||||
return DeadlineTimerAReq(ioContext, delay, timerOut);
|
||||
}
|
||||
|
||||
inline auto getDeadlineTimerAReqAwaiter(
|
||||
boost::asio::io_context &ioContext,
|
||||
const boost::posix_time::milliseconds delay,
|
||||
std::optional<std::shared_ptr<boost::asio::deadline_timer>> &timerOut)
|
||||
{
|
||||
return DeadlineTimerAReq(ioContext, delay, timerOut);
|
||||
return DeadlineTimerAReq(resumeIoContext, timer, delay);
|
||||
}
|
||||
|
||||
} // namespace adapters::boostAsio
|
||||
|
||||
@@ -4,14 +4,14 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <cstdint>
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <functional>
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
#include <config.h>
|
||||
#include <boost/asio/io_context.hpp>
|
||||
#include <boost/asio/deadline_timer.hpp>
|
||||
#include <spinscale/co/invokers.h>
|
||||
#include <spinscale/co/nonViralTaskNursery.h>
|
||||
#include <spinscale/spinLock.h>
|
||||
#include <spinscale/syncCancelerForAsyncWork.h>
|
||||
#include "deviceAttachmentSpec.h"
|
||||
@@ -39,9 +39,8 @@ public:
|
||||
const std::shared_ptr<device::DeviceAttachmentSpec>
|
||||
&deviceAttachmentSpec,
|
||||
boost::asio::io_context& ioContext_)
|
||||
: deviceAttachmentSpec(deviceAttachmentSpec),
|
||||
ioContext(ioContext_),
|
||||
timer(ioContext), nDeferrals(0)
|
||||
: daemonTimer(ioContext_), nDeferrals(0),
|
||||
deviceAttachmentSpec(deviceAttachmentSpec), ioContext(ioContext_)
|
||||
{}
|
||||
|
||||
virtual ~StimulusProducer() = default;
|
||||
@@ -52,17 +51,7 @@ public:
|
||||
StimulusProducer(StimulusProducer&&) = default;
|
||||
StimulusProducer& operator=(StimulusProducer&&) = default;
|
||||
|
||||
// Control methods
|
||||
virtual void start()
|
||||
{
|
||||
std::cout << __func__ << ": Starting stimulus producer for device "
|
||||
<< deviceAttachmentSpec->deviceSelector << std::endl;
|
||||
|
||||
stimulusProducerCanceler.startAcceptingWork();
|
||||
nDeferrals = 0;
|
||||
scheduleNextTimeout();
|
||||
}
|
||||
|
||||
virtual void start();
|
||||
virtual void stop();
|
||||
|
||||
void allowNextStimulusFrame()
|
||||
@@ -96,10 +85,19 @@ protected:
|
||||
return CONFIG_STIMBUFF_FRAME_PERIOD_MS;
|
||||
}
|
||||
|
||||
virtual void stimFrameProductionTimesliceInd() = 0;
|
||||
virtual sscl::co::ViralNonPostingInvoker<void>
|
||||
stimFrameProductionTimesliceCInd(
|
||||
sscl::SyncCancelerForAsyncWork &canceler) = 0;
|
||||
|
||||
private:
|
||||
void onTimeout(const boost::system::error_code& error);
|
||||
sscl::co::NonViralNonPostingInvoker productionCDaemon(
|
||||
std::exception_ptr &exceptionPtr,
|
||||
std::function<void()> callback,
|
||||
sscl::SyncCancelerForAsyncWork &canceler);
|
||||
|
||||
sscl::co::NonViralTaskNursery taskNursery;
|
||||
boost::asio::deadline_timer daemonTimer;
|
||||
size_t nDeferrals;
|
||||
std::chrono::high_resolution_clock::time_point deferralStartTime;
|
||||
|
||||
public:
|
||||
std::shared_ptr<device::DeviceAttachmentSpec> deviceAttachmentSpec;
|
||||
@@ -107,14 +105,6 @@ public:
|
||||
|
||||
private:
|
||||
boost::asio::io_context& ioContext;
|
||||
protected:
|
||||
sscl::SyncCancelerForAsyncWork stimulusProducerCanceler;
|
||||
private:
|
||||
boost::asio::deadline_timer timer;
|
||||
size_t nDeferrals;
|
||||
std::chrono::high_resolution_clock::time_point deferralStartTime;
|
||||
|
||||
void scheduleNextTimeout(int delayMs = CONFIG_STIMBUFF_FRAME_PERIOD_MS);
|
||||
};
|
||||
|
||||
} // namespace stim_buff
|
||||
|
||||
Reference in New Issue
Block a user