Files
salmanoff/commonLibs/lcameraDev/tools/probeRunner.cpp
T

89 lines
2.1 KiB
C++

#include <boostAsioLinkageFix.h>
#include <probeRunner.h>
#include <spinscale/component.h>
#include <future>
#include <iostream>
namespace lcamera_dev_probe {
namespace {
constexpr sscl::ThreadId PROBE_PUPPETEER_THREAD_ID = 1;
class DummyPuppeteerComponent
: public sscl::pptr::PuppeteerComponent
{
public:
explicit DummyPuppeteerComponent(
const std::shared_ptr<sscl::PuppeteerThread>& componentThread)
: sscl::pptr::PuppeteerComponent(componentThread)
{}
void handleLoopExceptionHook() override
{
std::cerr << "lcameraDev probe: puppeteer loop exception\n";
}
};
void probePuppeteerMain(
const sscl::PuppeteerThread::EntryFnArguments& args,
const std::function<void(
const std::shared_ptr<sscl::ComponentThread>&)>& work,
std::promise<std::exception_ptr>& donePromise)
{
sscl::PuppeteerThread& thr = args.usableBeforeJolt;
thr.initializeTls();
sscl::ComponentThread::setPuppeteerThreadId(PROBE_PUPPETEER_THREAD_ID);
std::shared_ptr<sscl::PuppeteerThread> thrPtr =
std::static_pointer_cast<sscl::PuppeteerThread>(thr.shared_from_this());
sscl::ComponentThread::setPuppeteerThread(thrPtr);
try {
work(thrPtr);
donePromise.set_value(nullptr);
}
catch (...) {
donePromise.set_value(std::current_exception());
}
thr.getIoContext().stop();
}
} // namespace
void runOnComponentThread(
const std::function<void(
const std::shared_ptr<sscl::ComponentThread>&)>& work)
{
std::promise<std::exception_ptr> donePromise;
std::future<std::exception_ptr> doneFuture = donePromise.get_future();
DummyPuppeteerComponent dummyComponent{
std::shared_ptr<sscl::PuppeteerThread>()};
std::shared_ptr<sscl::PuppeteerThread> probeThread =
std::make_shared<sscl::PuppeteerThread>(
PROBE_PUPPETEER_THREAD_ID,
"lcameraDev-probe",
[&work, &donePromise](
const sscl::PuppeteerThread::EntryFnArguments& args)
{
probePuppeteerMain(args, work, donePromise);
},
dummyComponent,
nullptr);
dummyComponent.thread = probeThread;
probeThread->thread.join();
std::exception_ptr probeException = doneFuture.get();
if (probeException) {
std::rethrow_exception(probeException);
}
}
} // namespace lcamera_dev_probe