module_base: remove CRTP template pattern to reduce flash bloat (#26476)

* module_base: claude rewrite to remove CRTP bloat

* module_base: apply to all drivers/modules

* format

* fix build errors

* fix missing syntax

* remove reference to module.h in files that need module_base.h

* remove old ModuleBase<T>

* add module_base.cpp to px4_protected_layers.cmake

* fix IridiumSBD can_stop()

* fix IridiumSBD.cpp

* clang-tidy: downcast static cast

* get_instance() template accessor, revert clang-tidy global

* rename module_base.h to module.h

* revert changes in zenoh/Kconfig.topics
This commit is contained in:
Jacob Dahl
2026-02-19 15:17:17 +13:00
committed by GitHub
parent 657854ae1b
commit ce3e62841f
227 changed files with 1741 additions and 1496 deletions
@@ -43,220 +43,6 @@
#include "rclcpp/rclcpp.hpp"
/**
* @class ModuleBase
* Base class for modules, implementing common functionality,
* such as 'start', 'stop' and 'status' commands.
* Currently does not support modules which allow multiple instances,
* such as mavlink.
*
* The class is implemented as curiously recurring template pattern (CRTP).
* It allows to have a static object in the base class that is different for
* each module type, and call static methods from the base class.
*
* @note Required methods for a derived class:
* When running in its own thread:
* static int task_spawn(int argc, char *argv[])
* {
* // call px4_task_spawn_cmd() with &run_trampoline as startup method
* // optional: wait until _object is not null, which means the task got initialized (use a timeout)
* // set _task_id and return 0
* // on error return != 0 (and _task_id must be -1)
* }
*
* static T *instantiate(int argc, char *argv[])
* {
* // this is called from within the new thread, from run_trampoline()
* // parse the arguments
* // create a new object T & return it
* // or return nullptr on error
* }
*
* static int custom_command(int argc, char *argv[])
* {
* // support for custom commands
* // if none are supported, just do:
* return print_usage("unrecognized command");
* }
*
* static int print_usage(const char *reason = nullptr)
* {
* // use the PRINT_MODULE_* methods...
* }
*
* When running on the work queue:
* - custom_command & print_usage as above
* static int task_spawn(int argc, char *argv[]) {
* // parse the arguments
* // set _object (here or from the work_queue() callback)
* // call work_queue() (with a custom cycle trampoline)
* // optional: wait until _object is not null, which means the task got initialized (use a timeout)
* // set _task_id to task_id_is_work_queue and return 0
* // on error return != 0 (and _task_id must be -1)
* }
*/
template<class T>
class ModuleBase : public rclcpp::Node
{
public:
ModuleBase() : Node(MODULE_NAME)
{
RCLCPP_INFO(get_logger(), "constructing %lu", hrt_absolute_time());
}
virtual ~ModuleBase() {}
/**
* @brief main Main entry point to the module that should be
* called directly from the module's main method.
* @param argc The task argument count.
* @param argc Pointer to the task argument variable array.
* @return Returns 0 iff successful, -1 otherwise.
*/
static int main(int argc, char *argv[])
{
return ret;
}
/**
* @brief Print the status if the module is running. This can be overridden by the module to provide
* more specific information.
* @return Returns 0 iff successful, -1 otherwise.
*/
virtual int print_status()
{
PX4_INFO("running");
return 0;
}
/**
* @brief Main loop method for modules running in their own thread. Called from run_trampoline().
* This method must return when should_exit() returns true.
*/
virtual void run()
{
}
/**
* @brief Returns the status of the module.
* @return Returns true if the module is running, false otherwise.
*/
static bool is_running()
{
return _task_id != -1;
}
protected:
/**
* @brief Tells the module to stop (used from outside or inside the module thread).
*/
virtual void request_stop()
{
_task_should_exit.store(true);
}
/**
* @brief Checks if the module should stop (used within the module thread).
* @return Returns True iff successful, false otherwise.
*/
bool should_exit() const
{
return _task_should_exit.load();
}
/**
* @brief Exits the module and delete the object. Called from within the module's thread.
* For work queue modules, this needs to be called from the derived class in the
* cycle method, when should_exit() returns true.
*/
static void exit_and_cleanup()
{
// Take the lock here:
// - if startup fails and we're faster than the parent thread, it will set
// _task_id and subsequently it will look like the task is running.
// - deleting the object must take place inside the lock.
lock_module();
delete _object.load();
_object.store(nullptr);
_task_id = -1; // Signal a potentially waiting thread for the module to exit that it can continue.
unlock_module();
}
/**
* @brief Waits until _object is initialized, (from the new thread). This can be called from task_spawn().
* @return Returns 0 iff successful, -1 on timeout or otherwise.
*/
static int wait_until_running(int timeout_ms = 1000)
{
int i = 0;
do {
px4_usleep(2000);
} while (!_object.load() && ++i < timeout_ms / 2);
if (i >= timeout_ms / 2) {
PX4_ERR("Timed out while waiting for thread to start");
return -1;
}
return 0;
}
/**
* @brief Get the module's object instance, (this is null if it's not running).
*/
static T *get_instance()
{
return (T *)_object.load();
}
/**
* @var _object Instance if the module is running.
* @note There will be one instance for each template type.
*/
static px4::atomic<T *> _object;
/** @var _task_id The task handle: -1 = invalid, otherwise task is assumed to be running. */
static int _task_id;
/** @var task_id_is_work_queue Value to indicate if the task runs on the work queue. */
static constexpr const int task_id_is_work_queue = -2;
private:
/**
* @brief lock_module Mutex to lock the module thread.
*/
static void lock_module()
{
pthread_mutex_lock(&px4_modules_mutex);
}
/**
* @brief unlock_module Mutex to unlock the module thread.
*/
static void unlock_module()
{
pthread_mutex_unlock(&px4_modules_mutex);
}
/** @var _task_should_exit Boolean flag to indicate if the task should exit. */
px4::atomic_bool _task_should_exit{false};
};
template<class T>
px4::atomic<T *> ModuleBase<T>::_object{nullptr};
template<class T>
int ModuleBase<T>::_task_id = -1;
#endif /* __cplusplus */