smo::Mind instance now global; track & manage JOLT state in Mind
We moved the instance of smo::Mind to global scope. I suppose we'll only support one instance of Mind per SMO process at least for now. We now track the state of Mind threads' JOLT-waiting. This allows us to centralize the Mind thread shutdown logic. Mind::finalizeReq() now takes care of all Mind thread shutdown state logic by tracking whether Mind threads need to be JOLTed first before being told to exit.
This commit is contained in:
@@ -1,9 +1,10 @@
|
|||||||
|
#include <unistd.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <componentThread.h>
|
|
||||||
#include <boost/asio.hpp>
|
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <unistd.h>
|
#include <boost/asio.hpp>
|
||||||
|
#include <mind.h>
|
||||||
|
#include <componentThread.h>
|
||||||
|
|
||||||
namespace smo {
|
namespace smo {
|
||||||
|
|
||||||
@@ -377,7 +378,7 @@ void ComponentThread::exceptionInd(ComponentThread& thread)
|
|||||||
std::cerr << "Mrntt: Exception occurred: in thread "
|
std::cerr << "Mrntt: Exception occurred: in thread "
|
||||||
<< thread.name << ". Killing Salmanoff." << "\n";
|
<< thread.name << ". Killing Salmanoff." << "\n";
|
||||||
|
|
||||||
ComponentThread::exitAllMindThreadsReq(
|
smo::mind.finalizeReq(
|
||||||
[]()
|
[]()
|
||||||
{
|
{
|
||||||
mrntt::mrntt->keepLooping = false;
|
mrntt::mrntt->keepLooping = false;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include <director/director.h>
|
#include <director/director.h>
|
||||||
#include <simulator/simulator.h>
|
#include <simulator/simulator.h>
|
||||||
@@ -12,9 +13,11 @@ namespace smo {
|
|||||||
class Mind
|
class Mind
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Mind(void) : threadsHaveBeenJolted(false) {}
|
||||||
|
|
||||||
void initialize(void);
|
void initialize(void);
|
||||||
void execute(void);
|
void execute(void);
|
||||||
void finalize(void);
|
void finalizeReq(std::function<void()> callback);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
std::thread directorThread;
|
std::thread directorThread;
|
||||||
@@ -23,8 +26,13 @@ public:
|
|||||||
|
|
||||||
director::Director director;
|
director::Director director;
|
||||||
simulator::Simulator canvas;
|
simulator::Simulator canvas;
|
||||||
|
|
||||||
|
private:
|
||||||
|
bool threadsHaveBeenJolted;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
extern Mind mind;
|
||||||
|
|
||||||
} // namespace smo
|
} // namespace smo
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -57,29 +57,15 @@ void ComponentThread::marionetteMain(ComponentThread& self)
|
|||||||
/* We could make RAII guard classes to always shutdown the mind
|
/* We could make RAII guard classes to always shutdown the mind
|
||||||
* threads properly in these pre-event loop situations.
|
* threads properly in these pre-event loop situations.
|
||||||
*/
|
*/
|
||||||
self.getIoService().post([&options]()
|
mind.finalizeReq([]{
|
||||||
{
|
mrntt::mrntt->getIoService().stop();
|
||||||
ComponentThread::joltAllMindThreadsReq(
|
|
||||||
[&options]()
|
|
||||||
{
|
|
||||||
ComponentThread::exitAllMindThreadsReq(
|
|
||||||
[&options]()
|
|
||||||
{
|
|
||||||
std::cout << __func__ << ": " << options.getUsage()
|
|
||||||
<< '\n';
|
|
||||||
mrntt::mrntt->getIoService().stop();
|
|
||||||
}
|
|
||||||
);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
self.getIoService().reset();
|
self.getIoService().reset();
|
||||||
self.getIoService().run();
|
self.getIoService().run();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
smo::Mind mind;
|
self.getIoService().post([]()
|
||||||
|
|
||||||
self.getIoService().post([&mind]()
|
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
initializeSalmanoff();
|
initializeSalmanoff();
|
||||||
@@ -128,7 +114,7 @@ void ComponentThread::marionetteMain(ComponentThread& self)
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::cout << __func__ << ": Exited event loop" << "\n";
|
std::cout << __func__ << ": Exited event loop" << "\n";
|
||||||
mind.finalize();
|
mind.finalizeReq(nullptr);
|
||||||
shutdownSalmanoff();
|
shutdownSalmanoff();
|
||||||
}
|
}
|
||||||
catch (const std::exception& e)
|
catch (const std::exception& e)
|
||||||
|
|||||||
+35
-2
@@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
namespace smo {
|
namespace smo {
|
||||||
|
|
||||||
|
Mind mind;
|
||||||
|
|
||||||
void Mind::initialize()
|
void Mind::initialize()
|
||||||
{
|
{
|
||||||
/* Distribute threads across available CPUs */
|
/* Distribute threads across available CPUs */
|
||||||
@@ -20,8 +22,9 @@ void Mind::initialize()
|
|||||||
|
|
||||||
/* Jolt the threads, then start them */
|
/* Jolt the threads, then start them */
|
||||||
ComponentThread::joltAllMindThreadsReq(
|
ComponentThread::joltAllMindThreadsReq(
|
||||||
[]()
|
[this]()
|
||||||
{
|
{
|
||||||
|
this->threadsHaveBeenJolted = true;
|
||||||
std::cout << "Mrntt: All mind threads JOLTed." << "\n";
|
std::cout << "Mrntt: All mind threads JOLTed." << "\n";
|
||||||
ComponentThread::startAllMindThreadsReq(
|
ComponentThread::startAllMindThreadsReq(
|
||||||
[]()
|
[]()
|
||||||
@@ -33,8 +36,38 @@ void Mind::initialize()
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Mind::finalize(void)
|
void Mind::finalizeReq(std::function<void()> callback)
|
||||||
{
|
{
|
||||||
|
/* 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.
|
||||||
|
*/
|
||||||
|
if (!threadsHaveBeenJolted)
|
||||||
|
{
|
||||||
|
ComponentThread::joltAllMindThreadsReq(
|
||||||
|
[this, callback]()
|
||||||
|
{
|
||||||
|
this->threadsHaveBeenJolted = true;
|
||||||
|
ComponentThread::exitAllMindThreadsReq(
|
||||||
|
[callback]()
|
||||||
|
{
|
||||||
|
std::cout << "Mrntt: All mind threads exited." << "\n";
|
||||||
|
if (callback) { callback(); }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ComponentThread::exitAllMindThreadsReq(
|
||||||
|
[callback]()
|
||||||
|
{
|
||||||
|
std::cout << "Mrntt: All mind threads exited." << "\n";
|
||||||
|
if (callback) { callback(); }
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace smo
|
} // namespace smo
|
||||||
|
|||||||
Reference in New Issue
Block a user