diff --git a/libraries/AP_HAL_Linux/AP_HAL_Linux_Main.h b/libraries/AP_HAL_Linux/AP_HAL_Linux_Main.h index 5a5fd23c47..140fa12eb2 100644 --- a/libraries/AP_HAL_Linux/AP_HAL_Linux_Main.h +++ b/libraries/AP_HAL_Linux/AP_HAL_Linux_Main.h @@ -6,11 +6,9 @@ #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX #define AP_HAL_MAIN() extern "C" {\ int main (int argc, char * const argv[]) { \ - hal.init(argc, argv); \ - hal.scheduler->system_initialized(); \ - setup();\ - for(;;) loop();\ - return 0;\ + AP_HAL::HAL::FunCallbacks callbacks(setup, loop); \ + hal.run(argc, argv, &callbacks); \ + return 0; \ }\ } #endif // HAL_BOARD_LINUX diff --git a/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp b/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp index 1e1565ef46..df903b8cd6 100644 --- a/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp +++ b/libraries/AP_HAL_Linux/HAL_Linux_Class.cpp @@ -1,6 +1,8 @@ #include #if CONFIG_HAL_BOARD == HAL_BOARD_LINUX +#include + #include "HAL_Linux_Class.h" #include "AP_HAL_Linux_Private.h" @@ -236,6 +238,23 @@ void HAL_Linux::init(int argc,char* const argv[]) const utilInstance.init(argc+gopt.optind-1, &argv[gopt.optind-1]); } +void HAL_Linux::run(int argc, char* const argv[], Callbacks* callbacks) const +{ + assert(callbacks); + + init(argc, argv); + + // NOTE: See commit 9f5b4ffca ("AP_HAL_Linux_Class: Correct + // deadlock, and infinite loop in setup()") for details about the + // order of scheduler initialize and setup on Linux. + scheduler->system_initialized(); + callbacks->setup(); + + for (;;) { + callbacks->loop(); + } +} + const AP_HAL::HAL& AP_HAL::get_HAL() { static const HAL_Linux hal; return hal; diff --git a/libraries/AP_HAL_Linux/HAL_Linux_Class.h b/libraries/AP_HAL_Linux/HAL_Linux_Class.h index d783acfdeb..4050e30b7e 100644 --- a/libraries/AP_HAL_Linux/HAL_Linux_Class.h +++ b/libraries/AP_HAL_Linux/HAL_Linux_Class.h @@ -10,6 +10,7 @@ class HAL_Linux : public AP_HAL::HAL { public: HAL_Linux(); void init(int argc, char * const * argv) const; + void run(int argc, char* const* argv, Callbacks* callbacks) const override; }; extern const HAL_Linux AP_HAL_Linux;