Files
salmanoff/smocore/mind.cpp
T

263 lines
7.1 KiB
C++
Raw Normal View History

#include <config.h>
#include <iostream>
#include <opts.h>
#include <componentThread.h>
2025-12-26 01:18:39 -04:00
#include <spinscale/asynchronousContinuation.h>
#include <spinscale/asynchronousLoop.h>
#include <spinscale/callback.h>
#include <spinscale/callableTracer.h>
2026-02-22 17:46:27 -04:00
#include <spinscale/componentThread.h>
2024-09-08 01:04:41 +10:00
#include <mind.h>
2025-12-26 01:18:39 -04:00
#include <mindThread.h>
#include <director/director.h>
#include <simulator/simulator.h>
2025-10-01 18:47:42 -04:00
#include <stimBuffApis/stimBuffApiManager.h>
2026-02-22 17:46:27 -04:00
#include <marionette/marionette.h>
namespace smo {
2025-09-03 14:43:00 -04:00
Mind::Mind(void)
: sscl::PuppetApplication(
2025-12-27 16:21:22 -04:00
std::vector<std::shared_ptr<sscl::PuppetThread>>{
2026-02-22 17:46:27 -04:00
std::make_shared<MindThread>(
SmoThreadId::DIRECTOR, getThreadName(SmoThreadId::DIRECTOR),
2026-02-22 17:46:27 -04:00
sscl::PuppetComponent::defaultPuppetMain, director,
&MindComponent::preJoltHook),
std::make_shared<MindThread>(
SmoThreadId::SIMULATOR, getThreadName(SmoThreadId::SIMULATOR),
2026-02-22 17:46:27 -04:00
sscl::PuppetComponent::defaultPuppetMain, canvas,
&MindComponent::preJoltHook),
std::make_shared<MindThread>(
SmoThreadId::SUBCONSCIOUS,
getThreadName(SmoThreadId::SUBCONSCIOUS),
2026-02-22 17:46:27 -04:00
sscl::PuppetComponent::defaultPuppetMain, subconscious,
&MindComponent::preJoltHook),
std::make_shared<MindThread>(
SmoThreadId::BODY, getThreadName(SmoThreadId::BODY),
2026-02-22 17:46:27 -04:00
sscl::PuppetComponent::defaultPuppetMain, body,
&MindComponent::preJoltHook)
2025-10-01 11:02:28 -04:00
#ifndef CONFIG_WORLD_USE_BODY_THREAD
2026-02-22 17:46:27 -04:00
, std::make_shared<MindThread>(
SmoThreadId::WORLD, getThreadName(SmoThreadId::WORLD),
2026-02-22 17:46:27 -04:00
sscl::PuppetComponent::defaultPuppetMain, world,
&MindComponent::preJoltHook)
#endif
}
),
director(*this, componentThreads[SmoThreadId::DIRECTOR - 1]),
canvas(*this, componentThreads[SmoThreadId::SIMULATOR - 1]),
subconscious(*this, componentThreads[SmoThreadId::SUBCONSCIOUS - 1]),
body(*this, componentThreads[SmoThreadId::BODY - 1]),
world(*this, componentThreads[SmoThreadId::WORLD - 1])
2025-09-03 14:43:00 -04:00
{
}
std::shared_ptr<MindThread>
2025-12-27 16:21:22 -04:00
Mind::getComponentThread(sscl::ThreadId id) const
2025-09-03 14:43:00 -04:00
{
2025-12-26 01:18:39 -04:00
if (id == SmoThreadId::MRNTT)
{
throw std::runtime_error(
std::string(__func__) +
": MRNTT is not a MindThread and cannot be returned by "
"getComponentThread");
}
2025-09-03 14:43:00 -04:00
// Search through the vector for the thread with matching id
for (auto& thread : componentThreads)
{
if (thread->id == id) {
return std::static_pointer_cast<MindThread>(thread);
}
2025-09-03 14:43:00 -04:00
}
// Throw exception if no thread found
throw std::runtime_error(std::string(__func__) +
": No MindThread found with ID "
2025-09-03 14:43:00 -04:00
+ std::to_string(static_cast<int>(id)));
}
std::shared_ptr<MindThread>
2025-09-03 14:43:00 -04:00
Mind::getComponentThread(const std::string& name) const
{
if (name == "mrntt")
{
throw std::runtime_error(
std::string(__func__) +
": MRNTT is not a MindThread and cannot be returned by "
"getComponentThread");
}
2025-09-03 14:43:00 -04:00
for (auto& thread : componentThreads)
{
if (thread->name == name) {
return std::static_pointer_cast<MindThread>(thread);
}
2025-09-03 14:43:00 -04:00
}
// Throw exception if no thread found
throw std::runtime_error(std::string(__func__) +
": No MindThread found with name '" + name + "'");
2025-09-03 14:43:00 -04:00
}
std::vector<std::shared_ptr<MindThread>>
2025-09-03 14:43:00 -04:00
Mind::getMindThreads() const
{
std::vector<std::shared_ptr<MindThread>> mindThreads;
mindThreads.reserve(componentThreads.size());
for (auto& thread : componentThreads) {
mindThreads.push_back(std::static_pointer_cast<MindThread>(thread));
}
return mindThreads;
2025-09-03 14:43:00 -04:00
}
class Mind::MindLifetimeMgmtOp
2025-12-27 16:21:22 -04:00
: public sscl::PostedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>
{
public:
MindLifetimeMgmtOp(
2025-12-27 16:21:22 -04:00
Mind &parent, const std::shared_ptr<sscl::ComponentThread> &caller,
sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
: sscl::PostedAsynchronousContinuation<mindLifetimeMgmtOpCbFn>(
caller, callback),
parent(parent)
{}
public:
Mind &parent;
public:
void initializeReq1_posted(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
/* Jolt the threads, then start them */
parent.joltAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::initializeReq2,
context.get(), context)});
}
void initializeReq2(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads JOLTed." << "\n";
parent.startAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::initializeReq3,
context.get(), context)});
}
void initializeReq3(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads started." << "\n";
parent.body.initializeReq(
{context, std::bind(
&MindLifetimeMgmtOp::initializeReq4,
context.get(), context, std::placeholders::_1)});
}
void initializeReq4(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context,
bool success
)
{
std::cout << "Mrntt: Body component initialized." << "\n";
callOriginalCb(success);
}
void finalizeReq1_posted(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
parent.body.finalizeReq(
{context, std::bind(
&MindLifetimeMgmtOp::finalizeReq2,
context.get(), context, std::placeholders::_1)});
}
void finalizeReq2(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context,
bool success
)
{
if (!success) {
std::cerr << "Mrntt: Body component failed to finalize." << "\n";
} else {
std::cout << "Mrntt: Body component finalized." << "\n";
}
/* If the threads haven't been jolted, we need to do that first, because
* otherwise they'll just enter their main loops and wait for control
* messages from mrntt after processing the exit request.
*/
parent.joltAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::finalizeReq3,
context.get(), context)});
}
void finalizeReq3(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads JOLTed for finalization." << "\n";
parent.exitAllPuppetThreadsReq(
{context, std::bind(
&MindLifetimeMgmtOp::finalizeReq4,
context.get(), context)});
}
void finalizeReq4(
[[maybe_unused]] std::shared_ptr<MindLifetimeMgmtOp> context
)
{
std::cout << "Mrntt: All mind threads exited." << "\n";
callOriginalCb(true);
}
};
2025-12-27 16:21:22 -04:00
void Mind::initializeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
{
/* Distribute threads across available CPUs */
try
{
distributeAndPinThreadsAcrossCpus();
}
catch (const std::exception& e)
{
std::cerr << "Salmanoff couldn't distribute the mind threads across "
"the CPUs, so performance may be suboptimal.\n"
"Error: " << e.what() << "\n";
}
2025-12-27 16:21:22 -04:00
const auto& caller = sscl::ComponentThread::getSelf();
auto request = std::make_shared<MindLifetimeMgmtOp>(
*this, caller, callback);
2026-02-22 17:46:27 -04:00
mrntt::mrntt.thread->getIoService().post(
STC(std::bind(
&MindLifetimeMgmtOp::initializeReq1_posted,
request.get(), request)));
}
2025-12-27 16:21:22 -04:00
void Mind::finalizeReq(sscl::Callback<mindLifetimeMgmtOpCbFn> callback)
{
2025-12-27 16:21:22 -04:00
const auto& caller = sscl::ComponentThread::getSelf();
auto request = std::make_shared<MindLifetimeMgmtOp>(
*this, caller, callback);
2026-02-22 17:46:27 -04:00
mrntt::mrntt.thread->getIoService().post(
STC(std::bind(
&MindLifetimeMgmtOp::finalizeReq1_posted,
request.get(), request)));
}
} // namespace smo