From 3b64be44f4fbe0ce3a1ee640d57ce7129da940f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beat=20K=C3=BCng?= Date: Wed, 3 May 2017 11:41:21 +0200 Subject: [PATCH] ModuleBase: add wait_until_running() method --- src/modules/events/send_event.cpp | 12 +++--------- src/platforms/px4_module.h | 22 ++++++++++++++++++++++ 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/modules/events/send_event.cpp b/src/modules/events/send_event.cpp index ab80e5afff..6e087dc4a2 100644 --- a/src/modules/events/send_event.cpp +++ b/src/modules/events/send_event.cpp @@ -52,16 +52,10 @@ int SendEvent::task_spawn(int argc, char *argv[]) return ret; } - int i = 0; + ret = wait_until_running(); - do { - /* wait up to 1s */ - usleep(2500); - - } while (!_object && ++i < 400); - - if (i == 400) { - return -1; + if (ret < 0) { + return ret; } _task_id = task_id_is_work_queue; diff --git a/src/platforms/px4_module.h b/src/platforms/px4_module.h index faee961924..29c41bee97 100644 --- a/src/platforms/px4_module.h +++ b/src/platforms/px4_module.h @@ -320,6 +320,28 @@ protected: unlock_module(); } + /** + * Wait until _object got initialized (from the new thread). This can be called from task_spawn(). + * @return 0 on success, -1 on timeout. + */ + static int wait_until_running() + { + int i = 0; + + do { + /* wait up to 1s */ + usleep(2500); + + } while (!_object && ++i < 400); + + if (i == 400) { + PX4_ERR("Timed out while waiting for thread to start"); + return -1; + } + + return 0; + } + /** get the module's object instance (this is null if it's not running) */ static T *get_instance() {