Registered signal handlers for joysticks and control loops
This *should* prevent us from leeking messages. I'm still seeing
leaks, so I bet we aren't fully set up to handle that yet.
Change-Id: Id4a8c5c1741db3e70a3d0a836667b957aba8165a
diff --git a/aos/common/controls/control_loop-tmpl.h b/aos/common/controls/control_loop-tmpl.h
index 474c5ce..d6813fb 100644
--- a/aos/common/controls/control_loop-tmpl.h
+++ b/aos/common/controls/control_loop-tmpl.h
@@ -102,10 +102,23 @@
template <class T>
void ControlLoop<T>::Run() {
- while (true) {
+ struct sigaction action;
+ action.sa_handler = &ControlLoop<T>::Quit;
+ sigemptyset(&action.sa_mask);
+ action.sa_flags = SA_RESETHAND;
+
+ PCHECK(sigaction(SIGTERM, &action, nullptr));
+ PCHECK(sigaction(SIGQUIT, &action, nullptr));
+ PCHECK(sigaction(SIGINT, &action, nullptr));
+
+ while (run_) {
Iterate();
}
+ LOG(INFO, "Shutting down\n");
}
+template <class T>
+::std::atomic<bool> ControlLoop<T>::run_{true};
+
} // namespace controls
} // namespace aos
diff --git a/aos/common/controls/control_loop.h b/aos/common/controls/control_loop.h
index 02aa067..70e87f8 100644
--- a/aos/common/controls/control_loop.h
+++ b/aos/common/controls/control_loop.h
@@ -2,10 +2,11 @@
#define AOS_CONTROL_LOOP_CONTROL_LOOP_H_
#include <string.h>
+#include <atomic>
-#include "aos/common/type_traits.h"
#include "aos/common/queue.h"
#include "aos/common/time.h"
+#include "aos/common/type_traits.h"
#include "aos/common/util/log_interval.h"
namespace aos {
@@ -115,7 +116,12 @@
uint32_t UniqueID() override { return control_loop_->hash(); }
+
protected:
+ static void Quit(int /*signum*/) {
+ run_ = false;
+ }
+
// Runs an iteration of the control loop.
// goal is the last goal that was sent. It might be any number of cycles old
// or nullptr if we haven't ever received a goal.
@@ -159,6 +165,8 @@
SimpleLogInterval(kStaleLogInterval, WARNING, "motors disabled");
SimpleLogInterval no_goal_ =
SimpleLogInterval(kStaleLogInterval, ERROR, "no goal");
+
+ static ::std::atomic<bool> run_;
};
} // namespace controls
diff --git a/aos/input/joystick_input.cc b/aos/input/joystick_input.cc
index a6ada96..7508c3c 100644
--- a/aos/input/joystick_input.cc
+++ b/aos/input/joystick_input.cc
@@ -1,6 +1,7 @@
#include "aos/input/joystick_input.h"
#include <string.h>
+#include <atomic>
#include "aos/common/messages/robot_state.q.h"
#include "aos/common/logging/logging.h"
@@ -9,9 +10,23 @@
namespace aos {
namespace input {
+::std::atomic<bool> JoystickInput::run_;
+
+void JoystickInput::Quit(int /*signum*/) { run_ = false; }
+
void JoystickInput::Run() {
+ run_ = true;
+ struct sigaction action;
+ action.sa_handler = &JoystickInput::Quit;
+ sigemptyset(&action.sa_mask);
+ action.sa_flags = SA_RESETHAND;
+
+ PCHECK(sigaction(SIGTERM, &action, nullptr));
+ PCHECK(sigaction(SIGQUIT, &action, nullptr));
+ PCHECK(sigaction(SIGINT, &action, nullptr));
+
driver_station::Data data;
- while (true) {
+ while (run_) {
joystick_state.FetchAnother();
data.Update(*joystick_state);
@@ -60,6 +75,7 @@
RunIteration(data);
}
+ LOG(INFO, "Shutting down\n");
}
} // namespace input
diff --git a/aos/input/joystick_input.h b/aos/input/joystick_input.h
index ec8b52a..b10b1ba 100644
--- a/aos/input/joystick_input.h
+++ b/aos/input/joystick_input.h
@@ -1,6 +1,8 @@
#ifndef AOS_INPUT_JOYSTICK_INPUT_H_
#define AOS_INPUT_JOYSTICK_INPUT_H_
+#include <atomic>
+
#include "aos/common/input/driver_station_data.h"
namespace aos {
@@ -18,6 +20,10 @@
private:
// Subclasses should do whatever they want with data here.
virtual void RunIteration(const driver_station::Data &data) = 0;
+
+ static void Quit(int /*signum*/);
+
+ static ::std::atomic<bool> run_;
};
// Class which will proxy joystick information from UDP packets to the queues.