Async: new hierachy; manages reply posting and unlocking

Async: Use new [Non]PostedAsyncCont and callOriginalCb

This new hierarchy of classes gives us a central mechanism for
managing both reply-posting and lockSpec unlocking.

* callOriginalCb: Now uses a modern C++ variadic template design
  enabling it to handle both direct calling and std::bind()
  re-binding of an arbitrary number of arguments from the caller.

This enables us to mostly eliminate the repeated, bespoke
definitions of callOriginalCb littered throughout the codebase.

We've also propagated these changes throughout the codebase in
this patch.
This commit is contained in:
2025-09-17 16:32:20 -04:00
parent 33c006b178
commit 816a047920
9 changed files with 145 additions and 169 deletions
+4 -15
View File
@@ -16,27 +16,16 @@ Body::Body(Mind &parent, const std::shared_ptr<ComponentThread> &thread)
}
class Body::InitializeReq
: public AsynchronousContinuation<bodyLifetimeMgmtOpCbFn>,
public ContinuationTarget
: public PostedAsynchronousContinuation<bodyLifetimeMgmtOpCbFn>
{
public:
InitializeReq(
Mind &parent, const std::shared_ptr<ComponentThread> &caller,
bodyLifetimeMgmtOpCbFn callback)
: AsynchronousContinuation<bodyLifetimeMgmtOpCbFn>(callback),
ContinuationTarget(caller),
: PostedAsynchronousContinuation<bodyLifetimeMgmtOpCbFn>(caller, callback),
parent(parent)
{}
void callOriginalCbFn(bool success)
{
if (originalCbFn)
{
caller->getIoService().post(
std::bind(originalCbFn, success));
}
}
private:
Mind &parent;
@@ -104,7 +93,7 @@ public:
<< results.nSucceeded << " of " << results.nTotal
<< " sense devices." << "\n";
callOriginalCbFn(results.nSucceeded > 0);
callOriginalCb(results.nSucceeded > 0);
}
};
@@ -148,7 +137,7 @@ public:
std::cout << "Mrntt: About to unload all sense api libs." << "\n";
sense_api::SenseApiManager::getInstance().unloadAllSenseApiLibs();
callOriginalCbFn(results.nSucceeded == results.nTotal);
callOriginalCb(results.nSucceeded == results.nTotal);
}
};
+8 -22
View File
@@ -105,32 +105,18 @@ void MindThread::main(MindThread& self)
}
class MindThread::ThreadLifetimeMgmtOp
: public TargetedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>
: public PostedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>
{
public:
ThreadLifetimeMgmtOp(
const std::shared_ptr<ComponentThread> &caller,
const std::shared_ptr<MindThread> &target,
threadLifetimeMgmtOpCbFn callback)
: TargetedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>(
: PostedAsynchronousContinuation<threadLifetimeMgmtOpCbFn>(
caller, callback),
target(target)
{}
void callOriginalCbFn(void)
{
if (originalCbFn)
{
/** EXPLANATION:
* This is only permissible because originalCbFn doesn't take any
* further arguments. If we had to bind argument to it before
* posting it, we'd have to std::bind them and then post the
* resulting function object.
*/
caller->getIoService().post(originalCbFn);
}
}
public:
const std::shared_ptr<MindThread> target;
@@ -144,7 +130,7 @@ public:
<< "\n";
target->io_service.stop();
callOriginalCbFn();
callOriginalCb();
}
void startThreadReq1_posted(
@@ -158,7 +144,7 @@ public:
// Execute private setup sequence here
// This is where each thread would implement its specific initialization
callOriginalCbFn();
callOriginalCb();
}
void exitThreadReq1_mainQueue_posted(
@@ -170,7 +156,7 @@ public:
target->cleanup();
target->io_service.stop();
callOriginalCbFn();
callOriginalCb();
}
void exitThreadReq1_pauseQueue_posted(
@@ -183,7 +169,7 @@ public:
target->cleanup();
target->pause_io_service.stop();
target->io_service.stop();
callOriginalCbFn();
callOriginalCb();
}
void pauseThreadReq1_posted(
@@ -197,7 +183,7 @@ public:
* our next operation is going to block the thread, so it won't
* have a chance to invoke the callback until it's unblocked.
*/
callOriginalCbFn();
callOriginalCb();
target->pause_io_service.reset();
target->pause_io_service.run();
}
@@ -210,7 +196,7 @@ public:
"resumeThread." << "\n";
target->pause_io_service.stop();
callOriginalCbFn();
callOriginalCb();
}
};
+2 -15
View File
@@ -39,31 +39,18 @@ const std::string DeviceManager::stringifyDeviceSpecs(void)
}
class DeviceManager::NewDeviceAttachmentSpecInd
: public TargetedAsynchronousContinuation<newDeviceAttachmentSpecIndCbFn>
: public PostedAsynchronousContinuation<newDeviceAttachmentSpecIndCbFn>
{
public:
NewDeviceAttachmentSpecInd(
std::shared_ptr<DeviceAttachmentSpec> s,
const std::shared_ptr<ComponentThread> &caller,
newDeviceAttachmentSpecIndCbFn cb)
: TargetedAsynchronousContinuation<newDeviceAttachmentSpecIndCbFn>(
: PostedAsynchronousContinuation<newDeviceAttachmentSpecIndCbFn>(
caller, cb),
spec(s)
{}
void callOriginalCb(
bool success,
std::shared_ptr<Device> device,
std::shared_ptr<DeviceAttachmentSpec> deviceSpec)
{
if (originalCbFn)
{
caller->getIoService().post(
std::bind(
originalCbFn, success, device, deviceSpec));
}
}
public:
std::shared_ptr<DeviceAttachmentSpec> spec;
+8 -18
View File
@@ -10,27 +10,17 @@ namespace smo {
namespace mrntt {
class MarionetteComponent::MrnttLifetimeMgmtOp
: public AsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>,
public ContinuationTarget
: public PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>
{
public:
MrnttLifetimeMgmtOp(
MarionetteComponent &parent, const std::shared_ptr<ComponentThread> &caller,
mrnttLifetimeMgmtOpCbFn callback)
: AsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(callback),
ContinuationTarget(caller),
parent(parent)
: PostedAsynchronousContinuation<mrnttLifetimeMgmtOpCbFn>(
caller, callback),
parent(parent)
{}
void callOriginalCbFn(bool success)
{
if (originalCbFn)
{
caller->getIoService().post(
std::bind(originalCbFn, success));
}
}
private:
MarionetteComponent &parent;
@@ -62,11 +52,11 @@ public:
{
std::cerr << __func__ << ": Failed to initialize globalMind"
<< std::endl;
context->callOriginalCbFn(false);
context->callOriginalCb(false);
return;
}
context->callOriginalCbFn(success);
context->callOriginalCb(success);
}
void finalizeReq1_posted(
@@ -95,11 +85,11 @@ public:
{
std::cerr << __func__ << ": globalMind finalization failed"
<< std::endl;
context->callOriginalCbFn(false);
context->callOriginalCb(false);
return;
}
context->callOriginalCbFn(success);
context->callOriginalCb(success);
}
};
+9 -23
View File
@@ -84,24 +84,17 @@ Mind::getMindThreads() const
}
class Mind::MindLifetimeMgmtOp
: public TargetedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>
: public PostedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>
{
public:
MindLifetimeMgmtOp(
Mind &parent, const std::shared_ptr<ComponentThread> &caller,
mindLifetimeMgmtOpCbFn callback)
: TargetedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>(
: PostedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>(
caller, callback),
parent(parent)
{}
void callOriginalCbFn(bool success)
{
if (originalCbFn) {
caller->getIoService().post(std::bind(originalCbFn, success));
}
}
public:
Mind &parent;
@@ -147,7 +140,7 @@ public:
)
{
std::cout << "Mrntt: Body component initialized." << "\n";
callOriginalCbFn(success);
callOriginalCb(success);
}
void finalizeReq1_posted(
@@ -198,7 +191,7 @@ public:
)
{
std::cout << "Mrntt: All mind threads exited." << "\n";
callOriginalCbFn(true);
callOriginalCb(true);
}
};
@@ -260,24 +253,17 @@ void Mind::distributeAndPinThreadsAcrossCpus()
}
class Mind::MindThreadLifetimeMgmtOp
: public AsynchronousContinuation<mindThreadLifetimeMgmtOpCbFn>
: public NonPostedAsynchronousContinuation<mindThreadLifetimeMgmtOpCbFn>
{
public:
MindThreadLifetimeMgmtOp(
Mind &parent,unsigned int nThreads,
mindThreadLifetimeMgmtOpCbFn callback)
: AsynchronousContinuation<mindThreadLifetimeMgmtOpCbFn>(callback),
: NonPostedAsynchronousContinuation<mindThreadLifetimeMgmtOpCbFn>(callback),
loop(nThreads),
parent(parent)
{}
void callOriginalCbFn(void)
{
if (originalCbFn) {
originalCbFn();
}
}
public:
AsynchronousLoop loop;
Mind &parent;
@@ -293,7 +279,7 @@ public:
}
parent.threadsHaveBeenJolted = true;
callOriginalCbFn();
callOriginalCb();
}
void executeGenericOpOnAllMindThreadsReq1(
@@ -305,7 +291,7 @@ public:
return;
}
callOriginalCbFn();
callOriginalCb();
}
void exitAllMindThreadsReq1(
@@ -321,7 +307,7 @@ public:
thread->thread.join();
}
callOriginalCbFn();
callOriginalCb();
}
};
+6 -28
View File
@@ -262,30 +262,18 @@ void SenseApiManager::finalizeAllSenseApiLibs(void)
}
class SenseApiManager::AttachSenseDeviceReq
: public TargetedAsynchronousContinuation<attachSenseDeviceReqCbFn>
: public PostedAsynchronousContinuation<attachSenseDeviceReqCbFn>
{
public:
AttachSenseDeviceReq(
const std::shared_ptr<device::DeviceAttachmentSpec>& spec,
const std::shared_ptr<ComponentThread> &caller,
attachSenseDeviceReqCbFn cb)
: TargetedAsynchronousContinuation<attachSenseDeviceReqCbFn>(
: PostedAsynchronousContinuation<attachSenseDeviceReqCbFn>(
caller, cb),
spec(spec)
{}
void callOriginalCb(
bool success, std::shared_ptr<device::DeviceAttachmentSpec> deviceSpec
)
{
if (originalCbFn)
{
caller->getIoService().post(
std::bind(
originalCbFn, success, deviceSpec));
}
}
public:
void attachSenseDeviceReq1_posted(
[[maybe_unused]] std::shared_ptr<AttachSenseDeviceReq> context
@@ -448,7 +436,7 @@ void SenseApiManager::detachSenseDeviceReq(
}
class SenseApiManager::AttachAllSenseDevicesFromSpecsReq
: public TargetedAsynchronousContinuation<
: public PostedAsynchronousContinuation<
attachAllSenseDevicesFromSpecsReqCbFn>
{
public:
@@ -456,21 +444,11 @@ public:
const unsigned int totalNSpecs,
const std::shared_ptr<ComponentThread>& caller,
attachAllSenseDevicesFromSpecsReqCbFn cb)
: TargetedAsynchronousContinuation<attachAllSenseDevicesFromSpecsReqCbFn>(
: PostedAsynchronousContinuation<attachAllSenseDevicesFromSpecsReqCbFn>(
caller, cb),
loop(totalNSpecs)
{}
void callOriginalCallback()
{
if (originalCbFn)
{
caller->getIoService().post(
std::bind(
originalCbFn, loop));
}
}
public:
void attachAllSenseDevicesFromSpecsReq1_posted(
[[maybe_unused]] std::shared_ptr<AttachAllSenseDevicesFromSpecsReq> context
@@ -513,7 +491,7 @@ public:
<< context->loop.nFailed.load() << " devices failed\n";
}
context->callOriginalCallback();
context->callOriginalCb(loop);
}
public:
@@ -588,7 +566,7 @@ public:
<< context->loop.nFailed.load() << " devices failed\n";
}
context->callOriginalCallback();
context->callOriginalCb(loop);
}
};