StimProd,DevReattacher: use CDaemon nonviral nursery coro

We ported these two daemons over to the new nursery mechanism and
they work nicely.
This commit is contained in:
2026-06-09 19:47:44 -04:00
parent 165c846700
commit 87a8de9a2b
6 changed files with 136 additions and 124 deletions
+41 -5
View File
@@ -1,5 +1,6 @@
#include <config.h>
#include <iostream>
#include <chrono>
#include <functional>
#include <componentThread.h>
#include <adapters/boostAsio/deadlineTimerAReq.h>
@@ -10,6 +11,28 @@
namespace smo {
namespace device {
namespace {
long computeTimesliceResidueMs(
long workDurationMs, long periodMs)
{
if (workDurationMs >= periodMs) {
return 0;
}
return periodMs - workDurationMs;
}
long durationMsSince(
const std::chrono::high_resolution_clock::time_point &startStamp,
const std::chrono::high_resolution_clock::time_point &endStamp)
{
const auto duration = endStamp - startStamp;
return std::chrono::duration_cast<std::chrono::milliseconds>(
duration).count();
}
} // namespace
DeviceReattacher::DeviceReattacher(
DeviceManager& parent, std::shared_ptr<sscl::ComponentThread> ioThread)
: parent(parent),
@@ -19,6 +42,8 @@ ioThread(ioThread), daemonTimer(ioThread->getIoContext())
* deviceReattacherCDaemon is a dynamic posting non-viral coroutine: start()
* passes ExplicitPostTarget{ioThread->getIoContext()} so the daemon body
* always runs on ioThread. daemonTimer is reused each loop iteration.
* Each timeslice runs attach work first, then sleeps only the period
* residue so reattach polls stay on a wall-clock cadence.
*/
}
@@ -31,20 +56,31 @@ DeviceReattacher::deviceReattacherCDaemon(
{
boost::asio::io_context &timerIoContext =
sscl::ComponentThread::getSelf()->getIoContext();
const auto periodMs = boost::posix_time::milliseconds(
CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS);
const long reattacherPeriodMs = CONFIG_MRNTT_DEVMGR_REATTACHER_PERIOD_MS;
while (!canceler.isCancellationRequested())
{
const auto workStartStamp =
std::chrono::high_resolution_clock::now();
co_await parent.attachAllUnattachedDevicesFromKnownListCReq();
const auto workEndStamp =
std::chrono::high_resolution_clock::now();
const long workDurationMs = durationMsSince(
workStartStamp, workEndStamp);
const long residueMs = computeTimesliceResidueMs(
workDurationMs, reattacherPeriodMs);
const bool expiredNormally = co_await
adapters::boostAsio::getDeadlineTimerAReqAwaiter(
timerIoContext, daemonTimer, periodMs);
timerIoContext, daemonTimer,
boost::posix_time::milliseconds(residueMs));
if (!expiredNormally) {
break;
}
co_await parent.attachAllUnattachedDevicesFromKnownListCReq();
}
co_return;