Files
salmanoff/smocore/componentThread.cpp
T

99 lines
2.8 KiB
C++
Raw Normal View History

2025-01-11 04:34:49 -04:00
#include <iostream>
#include <componentThread.h>
2025-07-22 06:48:04 -04:00
namespace smo {
2025-01-11 04:34:49 -04:00
namespace director {
2025-07-22 06:48:04 -04:00
/* The director is the seat of volition in Salmanoff. It receives sensor
* events from the body and world, and uses them to direct its implexors
* to implex new menties. It then loads the menties into canvas for simulation
* and correlation with intrins, in order to form new attrimotions and
* menties.
*/
2025-01-11 04:34:49 -04:00
ComponentThread director;
}
namespace simulator {
2025-07-22 06:48:04 -04:00
/* The canvas is the simulation engine in Salmanoff. It receives menties and
* simulates them in accordance with the instructions from director. It then
* re-renders them into perception for director to get feedback.
*/
2025-01-11 04:34:49 -04:00
ComponentThread canvas;
}
namespace subconscious {
2025-07-22 06:48:04 -04:00
/* The subconscious is the seat of memory in Salmanoff. It receives menties
* from director and stores them in memory for later recall.
*/
2025-01-11 04:34:49 -04:00
ComponentThread subconscious;
}
namespace body {
/* The body is a thread that polls, processes, and sends interoceptive sensor
* events to director. It enables these events to occur asynchronously,
* indepdendent any actions that the other threads are taking.
*/
ComponentThread body;
}
namespace world {
/* The world performs the same functions as the body, but for extrospective
* sensor events.
*/
ComponentThread world;
}
2025-01-11 04:34:49 -04:00
std::unordered_map<std::thread::id, ComponentThread&>
ComponentThread::componentThreads =
{
{director::director.thread.get_id(), director::director},
{simulator::canvas.thread.get_id(), simulator::canvas},
{subconscious::subconscious.thread.get_id(), subconscious::subconscious},
{body::body.thread.get_id(), body::body},
{world::world.thread.get_id(), world::world}
2025-01-11 04:34:49 -04:00
};
void ComponentThread::signalThread(std::thread::id id)
{
auto it = componentThreads.find(id);
if (it == componentThreads.end())
{
throw std::runtime_error(std::string(__func__)
+ ": Thread ID not found in componentThreads map");
}
ComponentThread& componentThread = it->second;
{
std::lock_guard<std::mutex> lock(componentThread.startupSync.mutex);
componentThread.startupSync.ready = true;
}
componentThread.startupSync.cv.notify_one();
}
void ComponentThread::main(ComponentThread& self)
{
// We sleep on spawn until the main thread tells us to continue.
{
std::unique_lock<std::mutex> lock(self.startupSync.mutex);
self.startupSync.cv.wait(lock, [&self]() {
return self.startupSync.ready;
});
}
std::cout << __func__ << ": Starting event loop." << std::endl;
self.getIoService().run();
std::cout << __func__ << ": Exiting." << std::endl;
}
void ComponentThread::validateThreadIds(void)
{
for (const auto& [id, componentThread] : componentThreads)
{
// std::thread::id() is usable as an invalid ID.
if (id == std::thread::id())
{
throw std::runtime_error(
std::string(__func__) + ": Invalid Thread ID.");
}
}
}
2025-07-22 06:48:04 -04:00
} // namespace smo