From 481bfc630804ced37ceb6219680af69889bcac8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Tue, 29 Jan 2019 07:19:22 +0100 Subject: [PATCH] px4_module: use px4_atomic_t --- src/drivers/batt_smbus/batt_smbus.cpp | 4 +- src/drivers/heater/heater.cpp | 4 +- src/drivers/px4fmu/fmu.cpp | 2 +- src/drivers/rc_input/RCInput.cpp | 2 +- src/modules/events/send_event.cpp | 2 +- src/modules/land_detector/LandDetector.cpp | 4 +- src/modules/load_mon/load_mon.cpp | 2 +- .../wind_estimator/wind_estimator_main.cpp | 2 +- src/platforms/px4_module.h | 42 +++++++++---------- 9 files changed, 31 insertions(+), 33 deletions(-) diff --git a/src/drivers/batt_smbus/batt_smbus.cpp b/src/drivers/batt_smbus/batt_smbus.cpp index a9e856fc4f..944acc790c 100644 --- a/src/drivers/batt_smbus/batt_smbus.cpp +++ b/src/drivers/batt_smbus/batt_smbus.cpp @@ -132,13 +132,13 @@ int BATT_SMBUS::task_spawn(int argc, char *argv[]) for (unsigned i = 0; i < NUM_BUS_OPTIONS; i++) { - if (_object == nullptr && (busid == BATT_SMBUS_BUS_ALL || bus_options[i].busid == busid)) { + if (!is_running() && (busid == BATT_SMBUS_BUS_ALL || bus_options[i].busid == busid)) { SMBus *interface = new SMBus(bus_options[i].busnum, BATT_SMBUS_ADDR); BATT_SMBUS *dev = new BATT_SMBUS(interface, bus_options[i].devpath); // Successful read of device type, we've found our battery - _object = dev; + _object.store(dev); _task_id = task_id_is_work_queue; int result = dev->get_startup_info(); diff --git a/src/drivers/heater/heater.cpp b/src/drivers/heater/heater.cpp index c22c69a6ee..7ee833836a 100644 --- a/src/drivers/heater/heater.cpp +++ b/src/drivers/heater/heater.cpp @@ -92,7 +92,7 @@ int Heater::controller_period(char *argv[]) int Heater::custom_command(int argc, char *argv[]) { // Check if the driver is running. - if (!is_running() && !_object) { + if (!is_running()) { PX4_INFO("not running"); return PX4_ERROR; } @@ -240,7 +240,7 @@ void Heater::initialize_trampoline(void *argv) return; } - _object = heater; + _object.store(heater); heater->start(); } diff --git a/src/drivers/px4fmu/fmu.cpp b/src/drivers/px4fmu/fmu.cpp index 48684b5bb5..ee96be69e9 100644 --- a/src/drivers/px4fmu/fmu.cpp +++ b/src/drivers/px4fmu/fmu.cpp @@ -1008,7 +1008,7 @@ PX4FMU::cycle_trampoline(void *arg) return; } - _object = dev; + _object.store(dev); } dev->cycle(); diff --git a/src/drivers/rc_input/RCInput.cpp b/src/drivers/rc_input/RCInput.cpp index 84c4281cd9..7afea2fd77 100644 --- a/src/drivers/rc_input/RCInput.cpp +++ b/src/drivers/rc_input/RCInput.cpp @@ -189,7 +189,7 @@ RCInput::cycle_trampoline(void *arg) return; } - _object = dev; + _object.store(dev); } dev->cycle(); diff --git a/src/modules/events/send_event.cpp b/src/modules/events/send_event.cpp index 9630d80446..7b53ece223 100644 --- a/src/modules/events/send_event.cpp +++ b/src/modules/events/send_event.cpp @@ -113,7 +113,7 @@ void SendEvent::initialize_trampoline(void *arg) } send_event->start(); - _object = send_event; + _object.store(send_event); } void SendEvent::cycle_trampoline(void *arg) diff --git a/src/modules/land_detector/LandDetector.cpp b/src/modules/land_detector/LandDetector.cpp index 6908397b3a..e3c8646212 100644 --- a/src/modules/land_detector/LandDetector.cpp +++ b/src/modules/land_detector/LandDetector.cpp @@ -92,7 +92,7 @@ void LandDetector::_cycle() { perf_begin(_cycle_perf); - if (_object == nullptr) { // not initialized yet + if (_object.load() == nullptr) { // not initialized yet // Advertise the first land detected uORB. _landDetected.timestamp = hrt_absolute_time(); _landDetected.freefall = false; @@ -110,7 +110,7 @@ void LandDetector::_cycle() _check_params(true); - _object = this; + _object.store(this); } _check_params(false); diff --git a/src/modules/load_mon/load_mon.cpp b/src/modules/load_mon/load_mon.cpp index 50f695dbae..4f1657373c 100644 --- a/src/modules/load_mon/load_mon.cpp +++ b/src/modules/load_mon/load_mon.cpp @@ -151,7 +151,7 @@ int LoadMon::task_spawn(int argc, char *argv[]) return ret; } - _object = obj; + _object.store(obj); _task_id = task_id_is_work_queue; return 0; } diff --git a/src/modules/wind_estimator/wind_estimator_main.cpp b/src/modules/wind_estimator/wind_estimator_main.cpp index 9c59b7655d..dad8ea55ca 100644 --- a/src/modules/wind_estimator/wind_estimator_main.cpp +++ b/src/modules/wind_estimator/wind_estimator_main.cpp @@ -173,7 +173,7 @@ WindEstimatorModule::cycle_trampoline(void *arg) return; } - _object = dev; + _object.store(dev); } if (dev) { diff --git a/src/platforms/px4_module.h b/src/platforms/px4_module.h index 0961ffb4c9..84d4e643f2 100644 --- a/src/platforms/px4_module.h +++ b/src/platforms/px4_module.h @@ -41,6 +41,7 @@ #include #include +#include #include #include #include @@ -174,10 +175,10 @@ public: argv += 1; #endif - _object = T::instantiate(argc, argv); + T *object = T::instantiate(argc, argv); + _object.store(object); - if (_object) { - T *object = (T *)_object; + if (object) { object->run(); } else { @@ -229,8 +230,9 @@ public: lock_module(); if (is_running()) { - if (_object) { - T *object = (T *)_object; + T *object = _object.load(); + + if (object) { object->request_stop(); unsigned int i = 0; @@ -247,10 +249,8 @@ public: _task_id = -1; - if (_object) { - delete _object; - _object = nullptr; - } + delete _object.load(); + _object.store(nullptr); ret = -1; break; @@ -279,8 +279,8 @@ public: int ret = -1; lock_module(); - if (is_running() && _object) { - T *object = (T *)_object; + if (is_running() && _object.load()) { + T *object = _object.load(); ret = object->print_status(); } else { @@ -326,7 +326,7 @@ protected: */ virtual void request_stop() { - _task_should_exit = true; + _task_should_exit.store(true); } /** @@ -335,7 +335,7 @@ protected: */ bool should_exit() const { - return _task_should_exit; + return _task_should_exit.load(); } /** @@ -351,10 +351,8 @@ protected: // - deleting the object must take place inside the lock. lock_module(); - if (_object) { - delete _object; - _object = nullptr; - } + 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(); @@ -372,7 +370,7 @@ protected: /* Wait up to 1s. */ px4_usleep(2500); - } while (!_object && ++i < 400); + } while (!_object.load() && ++i < 400); if (i == 400) { PX4_ERR("Timed out while waiting for thread to start"); @@ -387,14 +385,14 @@ protected: */ static T *get_instance() { - return (T *)_object; + return (T *)_object.load(); } /** * @var _object Instance if the module is running. * @note There will be one instance for each template type. */ - static volatile T *_object; + static px4::atomic _object; /** @var _task_id The task handle: -1 = invalid, otherwise task is assumed to be running. */ static int _task_id; @@ -420,11 +418,11 @@ private: } /** @var _task_should_exit Boolean flag to indicate if the task should exit. */ - volatile bool _task_should_exit = false; + px4::atomic_bool _task_should_exit{false}; }; template -volatile T *ModuleBase::_object = nullptr; +px4::atomic ModuleBase::_object{nullptr}; template int ModuleBase::_task_id = -1;