mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-11-13 22:21:37 +08:00
Showcase clang-format google style
This commit is contained in:
@@ -33,46 +33,38 @@
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
|
||||
namespace px4
|
||||
{
|
||||
namespace px4 {
|
||||
|
||||
ScheduledWorkItem::~ScheduledWorkItem()
|
||||
{
|
||||
ScheduledWorkItem::~ScheduledWorkItem() {
|
||||
if (_call.arg != nullptr) {
|
||||
ScheduleClear();
|
||||
}
|
||||
}
|
||||
|
||||
void ScheduledWorkItem::schedule_trampoline(void *arg)
|
||||
{
|
||||
void ScheduledWorkItem::schedule_trampoline(void *arg) {
|
||||
ScheduledWorkItem *dev = static_cast<ScheduledWorkItem *>(arg);
|
||||
dev->ScheduleNow();
|
||||
}
|
||||
|
||||
void ScheduledWorkItem::ScheduleDelayed(uint32_t delay_us)
|
||||
{
|
||||
void ScheduledWorkItem::ScheduleDelayed(uint32_t delay_us) {
|
||||
hrt_call_after(&_call, delay_us, (hrt_callout)&ScheduledWorkItem::schedule_trampoline, this);
|
||||
}
|
||||
|
||||
void ScheduledWorkItem::ScheduleOnInterval(uint32_t interval_us, uint32_t delay_us)
|
||||
{
|
||||
void ScheduledWorkItem::ScheduleOnInterval(uint32_t interval_us, uint32_t delay_us) {
|
||||
hrt_call_every(&_call, delay_us, interval_us, (hrt_callout)&ScheduledWorkItem::schedule_trampoline, this);
|
||||
}
|
||||
|
||||
void ScheduledWorkItem::ScheduleAt(hrt_abstime time_us)
|
||||
{
|
||||
void ScheduledWorkItem::ScheduleAt(hrt_abstime time_us) {
|
||||
hrt_call_at(&_call, time_us, (hrt_callout)&ScheduledWorkItem::schedule_trampoline, this);
|
||||
}
|
||||
|
||||
void ScheduledWorkItem::ScheduleClear()
|
||||
{
|
||||
void ScheduledWorkItem::ScheduleClear() {
|
||||
// first clear any scheduled hrt call, then remove the item from the runnable queue
|
||||
hrt_cancel(&_call);
|
||||
WorkItem::ScheduleClear();
|
||||
}
|
||||
|
||||
void ScheduledWorkItem::print_run_status()
|
||||
{
|
||||
void ScheduledWorkItem::print_run_status() {
|
||||
if (_call.period > 0) {
|
||||
PX4_INFO_RAW("%-29s %8.1f Hz %12.0f us (%" PRId64 " us)\n", _item_name, (double)average_rate(),
|
||||
(double)average_interval(), _call.period);
|
||||
@@ -82,4 +74,4 @@ void ScheduledWorkItem::print_run_status()
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace px4
|
||||
} // namespace px4
|
||||
|
||||
@@ -31,28 +31,22 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueue.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueueManager.hpp>
|
||||
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
namespace px4 {
|
||||
|
||||
namespace px4
|
||||
{
|
||||
|
||||
WorkItem::WorkItem(const char *name, const wq_config_t &config) :
|
||||
_item_name(name)
|
||||
{
|
||||
WorkItem::WorkItem(const char *name, const wq_config_t &config) : _item_name(name) {
|
||||
if (!Init(config)) {
|
||||
PX4_ERR("init failed");
|
||||
}
|
||||
}
|
||||
|
||||
WorkItem::WorkItem(const char *name, const WorkItem &work_item) :
|
||||
_item_name(name)
|
||||
{
|
||||
WorkItem::WorkItem(const char *name, const WorkItem &work_item) : _item_name(name) {
|
||||
px4::WorkQueue *wq = work_item._wq;
|
||||
|
||||
if ((wq != nullptr) && wq->Attach(this)) {
|
||||
@@ -60,13 +54,9 @@ WorkItem::WorkItem(const char *name, const WorkItem &work_item) :
|
||||
}
|
||||
}
|
||||
|
||||
WorkItem::~WorkItem()
|
||||
{
|
||||
Deinit();
|
||||
}
|
||||
WorkItem::~WorkItem() { Deinit(); }
|
||||
|
||||
bool WorkItem::Init(const wq_config_t &config)
|
||||
{
|
||||
bool WorkItem::Init(const wq_config_t &config) {
|
||||
// clear any existing first
|
||||
Deinit();
|
||||
|
||||
@@ -82,8 +72,7 @@ bool WorkItem::Init(const wq_config_t &config)
|
||||
return false;
|
||||
}
|
||||
|
||||
void WorkItem::Deinit()
|
||||
{
|
||||
void WorkItem::Deinit() {
|
||||
// remove any currently queued work
|
||||
if (_wq != nullptr) {
|
||||
// prevent additional insertions
|
||||
@@ -97,20 +86,15 @@ void WorkItem::Deinit()
|
||||
}
|
||||
}
|
||||
|
||||
void WorkItem::ScheduleClear()
|
||||
{
|
||||
void WorkItem::ScheduleClear() {
|
||||
if (_wq != nullptr) {
|
||||
_wq->Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
float WorkItem::elapsed_time() const
|
||||
{
|
||||
return hrt_elapsed_time(&_time_first_run) / 1e6f;
|
||||
}
|
||||
float WorkItem::elapsed_time() const { return hrt_elapsed_time(&_time_first_run) / 1e6f; }
|
||||
|
||||
float WorkItem::average_rate() const
|
||||
{
|
||||
float WorkItem::average_rate() const {
|
||||
const float rate = _run_count / elapsed_time();
|
||||
|
||||
if ((_run_count > 1) && PX4_ISFINITE(rate)) {
|
||||
@@ -120,8 +104,7 @@ float WorkItem::average_rate() const
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
float WorkItem::average_interval() const
|
||||
{
|
||||
float WorkItem::average_interval() const {
|
||||
const float rate = average_rate();
|
||||
const float interval = 1e6f / rate;
|
||||
|
||||
@@ -132,12 +115,11 @@ float WorkItem::average_interval() const
|
||||
return 0.f;
|
||||
}
|
||||
|
||||
void WorkItem::print_run_status()
|
||||
{
|
||||
void WorkItem::print_run_status() {
|
||||
PX4_INFO_RAW("%-29s %8.1f Hz %12.0f us\n", _item_name, (double)average_rate(), (double)average_interval());
|
||||
|
||||
// reset statistics
|
||||
_run_count = 0;
|
||||
}
|
||||
|
||||
} // namespace px4
|
||||
} // namespace px4
|
||||
|
||||
@@ -33,37 +33,28 @@
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkItemSingleShot.hpp>
|
||||
|
||||
|
||||
namespace px4
|
||||
{
|
||||
namespace px4 {
|
||||
|
||||
WorkItemSingleShot::WorkItemSingleShot(const px4::wq_config_t &config, worker_method method, void *argument)
|
||||
: px4::WorkItem("<single_shot>", config), _argument(argument), _method(method)
|
||||
{
|
||||
: px4::WorkItem("<single_shot>", config), _argument(argument), _method(method) {
|
||||
px4_sem_init(&_sem, 0, 0);
|
||||
}
|
||||
|
||||
WorkItemSingleShot::WorkItemSingleShot(const px4::WorkItem &work_item, worker_method method, void *argument)
|
||||
: px4::WorkItem("<single_shot>", work_item), _argument(argument), _method(method)
|
||||
{
|
||||
: px4::WorkItem("<single_shot>", work_item), _argument(argument), _method(method) {
|
||||
px4_sem_init(&_sem, 0, 0);
|
||||
}
|
||||
|
||||
WorkItemSingleShot::~WorkItemSingleShot()
|
||||
{
|
||||
px4_sem_destroy(&_sem);
|
||||
WorkItemSingleShot::~WorkItemSingleShot() { px4_sem_destroy(&_sem); }
|
||||
|
||||
void WorkItemSingleShot::wait() {
|
||||
while (px4_sem_wait(&_sem) != 0) {
|
||||
}
|
||||
}
|
||||
|
||||
void WorkItemSingleShot::wait()
|
||||
{
|
||||
while (px4_sem_wait(&_sem) != 0) {}
|
||||
}
|
||||
|
||||
void WorkItemSingleShot::Run()
|
||||
{
|
||||
void WorkItemSingleShot::Run() {
|
||||
_method(_argument);
|
||||
px4_sem_post(&_sem);
|
||||
}
|
||||
|
||||
|
||||
} // namespace px4
|
||||
} // namespace px4
|
||||
|
||||
@@ -31,21 +31,17 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueue.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <px4_platform_common/tasks.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace px4
|
||||
{
|
||||
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueue.hpp>
|
||||
|
||||
WorkQueue::WorkQueue(const wq_config_t &config) :
|
||||
_config(config)
|
||||
{
|
||||
namespace px4 {
|
||||
|
||||
WorkQueue::WorkQueue(const wq_config_t &config) : _config(config) {
|
||||
// set the threads name
|
||||
#ifdef __PX4_DARWIN
|
||||
pthread_setname_np(_config.name);
|
||||
@@ -64,9 +60,7 @@ WorkQueue::WorkQueue(const wq_config_t &config) :
|
||||
px4_sem_setprotocol(&_exit_lock, SEM_PRIO_NONE);
|
||||
}
|
||||
|
||||
WorkQueue::~WorkQueue()
|
||||
{
|
||||
|
||||
WorkQueue::~WorkQueue() {
|
||||
work_lock();
|
||||
|
||||
// Synchronize with ::Detach
|
||||
@@ -81,8 +75,7 @@ WorkQueue::~WorkQueue()
|
||||
#endif /* __PX4_NUTTX */
|
||||
}
|
||||
|
||||
bool WorkQueue::Attach(WorkItem *item)
|
||||
{
|
||||
bool WorkQueue::Attach(WorkItem *item) {
|
||||
work_lock();
|
||||
|
||||
if (!should_exit()) {
|
||||
@@ -96,8 +89,7 @@ bool WorkQueue::Attach(WorkItem *item)
|
||||
return false;
|
||||
}
|
||||
|
||||
void WorkQueue::Detach(WorkItem *item)
|
||||
{
|
||||
void WorkQueue::Detach(WorkItem *item) {
|
||||
bool exiting = false;
|
||||
|
||||
work_lock();
|
||||
@@ -125,8 +117,7 @@ void WorkQueue::Detach(WorkItem *item)
|
||||
}
|
||||
}
|
||||
|
||||
void WorkQueue::Add(WorkItem *item)
|
||||
{
|
||||
void WorkQueue::Add(WorkItem *item) {
|
||||
work_lock();
|
||||
|
||||
#if defined(ENABLE_LOCKSTEP_SCHEDULER)
|
||||
@@ -135,7 +126,7 @@ void WorkQueue::Add(WorkItem *item)
|
||||
_lockstep_component = px4_lockstep_register_component();
|
||||
}
|
||||
|
||||
#endif // ENABLE_LOCKSTEP_SCHEDULER
|
||||
#endif // ENABLE_LOCKSTEP_SCHEDULER
|
||||
|
||||
_q.push(item);
|
||||
work_unlock();
|
||||
@@ -143,8 +134,7 @@ void WorkQueue::Add(WorkItem *item)
|
||||
SignalWorkerThread();
|
||||
}
|
||||
|
||||
void WorkQueue::SignalWorkerThread()
|
||||
{
|
||||
void WorkQueue::SignalWorkerThread() {
|
||||
int sem_val;
|
||||
|
||||
if (px4_sem_getvalue(&_process_lock, &sem_val) == 0 && sem_val <= 0) {
|
||||
@@ -152,15 +142,13 @@ void WorkQueue::SignalWorkerThread()
|
||||
}
|
||||
}
|
||||
|
||||
void WorkQueue::Remove(WorkItem *item)
|
||||
{
|
||||
void WorkQueue::Remove(WorkItem *item) {
|
||||
work_lock();
|
||||
_q.remove(item);
|
||||
work_unlock();
|
||||
}
|
||||
|
||||
void WorkQueue::Clear()
|
||||
{
|
||||
void WorkQueue::Clear() {
|
||||
work_lock();
|
||||
|
||||
while (!_q.empty()) {
|
||||
@@ -170,11 +158,11 @@ void WorkQueue::Clear()
|
||||
work_unlock();
|
||||
}
|
||||
|
||||
void WorkQueue::Run()
|
||||
{
|
||||
void WorkQueue::Run() {
|
||||
while (!should_exit()) {
|
||||
// loop as the wait may be interrupted by a signal
|
||||
do {} while (px4_sem_wait(&_process_lock) != 0);
|
||||
do {
|
||||
} while (px4_sem_wait(&_process_lock) != 0);
|
||||
|
||||
work_lock();
|
||||
|
||||
@@ -182,11 +170,11 @@ void WorkQueue::Run()
|
||||
while (!_q.empty()) {
|
||||
WorkItem *work = _q.pop();
|
||||
|
||||
work_unlock(); // unlock work queue to run (item may requeue itself)
|
||||
work_unlock(); // unlock work queue to run (item may requeue itself)
|
||||
work->RunPreamble();
|
||||
work->Run();
|
||||
// Note: after Run() we cannot access work anymore, as it might have been deleted
|
||||
work_lock(); // re-lock
|
||||
work_lock(); // re-lock
|
||||
}
|
||||
|
||||
#if defined(ENABLE_LOCKSTEP_SCHEDULER)
|
||||
@@ -196,7 +184,7 @@ void WorkQueue::Run()
|
||||
_lockstep_component = -1;
|
||||
}
|
||||
|
||||
#endif // ENABLE_LOCKSTEP_SCHEDULER
|
||||
#endif // ENABLE_LOCKSTEP_SCHEDULER
|
||||
|
||||
work_unlock();
|
||||
}
|
||||
@@ -204,8 +192,7 @@ void WorkQueue::Run()
|
||||
PX4_DEBUG("%s: exiting", _config.name);
|
||||
}
|
||||
|
||||
void WorkQueue::print_status(bool last)
|
||||
{
|
||||
void WorkQueue::print_status(bool last) {
|
||||
const size_t num_items = _work_items.size();
|
||||
PX4_INFO_RAW("%-16s\n", get_name());
|
||||
unsigned i = 0;
|
||||
@@ -231,4 +218,4 @@ void WorkQueue::print_status(bool last)
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace px4
|
||||
} // namespace px4
|
||||
|
||||
@@ -31,27 +31,24 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueueManager.hpp>
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueue.hpp>
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <lib/mathlib/mathlib.h>
|
||||
#include <limits.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <px4_platform_common/posix.h>
|
||||
#include <px4_platform_common/tasks.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
#include <px4_platform_common/atomic.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <containers/BlockingList.hpp>
|
||||
#include <containers/BlockingQueue.hpp>
|
||||
#include <lib/drivers/device/Device.hpp>
|
||||
#include <lib/mathlib/mathlib.h>
|
||||
|
||||
#include <limits.h>
|
||||
#include <string.h>
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueue.hpp>
|
||||
#include <px4_platform_common/px4_work_queue/WorkQueueManager.hpp>
|
||||
|
||||
using namespace time_literals;
|
||||
|
||||
namespace px4
|
||||
{
|
||||
namespace px4 {
|
||||
|
||||
// list of current work queues
|
||||
static BlockingList<WorkQueue *> *_wq_manager_wqs_list{nullptr};
|
||||
@@ -61,10 +58,7 @@ static BlockingQueue<const wq_config_t *, 1> *_wq_manager_create_queue{nullptr};
|
||||
|
||||
static px4::atomic_bool _wq_manager_should_exit{true};
|
||||
|
||||
|
||||
static WorkQueue *
|
||||
FindWorkQueueByName(const char *name)
|
||||
{
|
||||
static WorkQueue *FindWorkQueueByName(const char *name) {
|
||||
if (_wq_manager_wqs_list == nullptr) {
|
||||
PX4_ERR("not running");
|
||||
return nullptr;
|
||||
@@ -82,9 +76,7 @@ FindWorkQueueByName(const char *name)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
WorkQueue *
|
||||
WorkQueueFindOrCreate(const wq_config_t &new_wq)
|
||||
{
|
||||
WorkQueue *WorkQueueFindOrCreate(const wq_config_t &new_wq) {
|
||||
if (_wq_manager_create_queue == nullptr) {
|
||||
PX4_ERR("not running");
|
||||
return nullptr;
|
||||
@@ -118,9 +110,7 @@ WorkQueueFindOrCreate(const wq_config_t &new_wq)
|
||||
return wq;
|
||||
}
|
||||
|
||||
const wq_config_t &
|
||||
device_bus_to_wq(uint32_t device_id_int)
|
||||
{
|
||||
const wq_config_t &device_bus_to_wq(uint32_t device_id_int) {
|
||||
union device::Device::DeviceId device_id;
|
||||
device_id.devid = device_id_int;
|
||||
|
||||
@@ -129,32 +119,44 @@ device_bus_to_wq(uint32_t device_id_int)
|
||||
|
||||
if (bus_type == device::Device::DeviceBusType_I2C) {
|
||||
switch (bus) {
|
||||
case 0: return wq_configurations::I2C0;
|
||||
case 0:
|
||||
return wq_configurations::I2C0;
|
||||
|
||||
case 1: return wq_configurations::I2C1;
|
||||
case 1:
|
||||
return wq_configurations::I2C1;
|
||||
|
||||
case 2: return wq_configurations::I2C2;
|
||||
case 2:
|
||||
return wq_configurations::I2C2;
|
||||
|
||||
case 3: return wq_configurations::I2C3;
|
||||
case 3:
|
||||
return wq_configurations::I2C3;
|
||||
|
||||
case 4: return wq_configurations::I2C4;
|
||||
case 4:
|
||||
return wq_configurations::I2C4;
|
||||
}
|
||||
|
||||
} else if (bus_type == device::Device::DeviceBusType_SPI) {
|
||||
switch (bus) {
|
||||
case 0: return wq_configurations::SPI0;
|
||||
case 0:
|
||||
return wq_configurations::SPI0;
|
||||
|
||||
case 1: return wq_configurations::SPI1;
|
||||
case 1:
|
||||
return wq_configurations::SPI1;
|
||||
|
||||
case 2: return wq_configurations::SPI2;
|
||||
case 2:
|
||||
return wq_configurations::SPI2;
|
||||
|
||||
case 3: return wq_configurations::SPI3;
|
||||
case 3:
|
||||
return wq_configurations::SPI3;
|
||||
|
||||
case 4: return wq_configurations::SPI4;
|
||||
case 4:
|
||||
return wq_configurations::SPI4;
|
||||
|
||||
case 5: return wq_configurations::SPI5;
|
||||
case 5:
|
||||
return wq_configurations::SPI5;
|
||||
|
||||
case 6: return wq_configurations::SPI6;
|
||||
case 6:
|
||||
return wq_configurations::SPI6;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,9 +164,7 @@ device_bus_to_wq(uint32_t device_id_int)
|
||||
return wq_configurations::hp_default;
|
||||
};
|
||||
|
||||
const wq_config_t &
|
||||
serial_port_to_wq(const char *serial)
|
||||
{
|
||||
const wq_config_t &serial_port_to_wq(const char *serial) {
|
||||
if (serial == nullptr) {
|
||||
return wq_configurations::ttyUnknown;
|
||||
|
||||
@@ -207,16 +207,19 @@ serial_port_to_wq(const char *serial)
|
||||
return wq_configurations::ttyUnknown;
|
||||
}
|
||||
|
||||
const wq_config_t &ins_instance_to_wq(uint8_t instance)
|
||||
{
|
||||
const wq_config_t &ins_instance_to_wq(uint8_t instance) {
|
||||
switch (instance) {
|
||||
case 0: return wq_configurations::INS0;
|
||||
case 0:
|
||||
return wq_configurations::INS0;
|
||||
|
||||
case 1: return wq_configurations::INS1;
|
||||
case 1:
|
||||
return wq_configurations::INS1;
|
||||
|
||||
case 2: return wq_configurations::INS2;
|
||||
case 2:
|
||||
return wq_configurations::INS2;
|
||||
|
||||
case 3: return wq_configurations::INS3;
|
||||
case 3:
|
||||
return wq_configurations::INS3;
|
||||
}
|
||||
|
||||
PX4_WARN("no INS%d wq configuration, using INS0", instance);
|
||||
@@ -224,9 +227,7 @@ const wq_config_t &ins_instance_to_wq(uint8_t instance)
|
||||
return wq_configurations::INS0;
|
||||
}
|
||||
|
||||
static void *
|
||||
WorkQueueRunner(void *context)
|
||||
{
|
||||
static void *WorkQueueRunner(void *context) {
|
||||
wq_config_t *config = static_cast<wq_config_t *>(context);
|
||||
WorkQueue wq(*config);
|
||||
|
||||
@@ -243,18 +244,14 @@ WorkQueueRunner(void *context)
|
||||
|
||||
#if defined(__PX4_NUTTX) && !defined(CONFIG_BUILD_FLAT)
|
||||
// Wrapper for px4_task_spawn_cmd interface
|
||||
inline static int
|
||||
WorkQueueRunner(int argc, char *argv[])
|
||||
{
|
||||
inline static int WorkQueueRunner(int argc, char *argv[]) {
|
||||
wq_config_t *context = (wq_config_t *)strtoul(argv[argc - 1], nullptr, 16);
|
||||
WorkQueueRunner(context);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int
|
||||
WorkQueueManagerRun(int, char **)
|
||||
{
|
||||
static int WorkQueueManagerRun(int, char **) {
|
||||
_wq_manager_wqs_list = new BlockingList<WorkQueue *>();
|
||||
_wq_manager_create_queue = new BlockingQueue<const wq_config_t *, 1>();
|
||||
|
||||
@@ -271,17 +268,19 @@ WorkQueueManagerRun(int, char **)
|
||||
#elif defined(__PX4_NUTTX)
|
||||
const size_t stacksize = math::max(PTHREAD_STACK_MIN, PX4_STACK_ADJUSTED(wq->stacksize));
|
||||
#elif defined(__PX4_POSIX)
|
||||
// On posix system , the desired stacksize round to the nearest multiplier of the system pagesize
|
||||
// It is a requirement of the pthread_attr_setstacksize* function
|
||||
// On posix system , the desired stacksize round to the nearest multiplier of the system
|
||||
// pagesize It is a requirement of the pthread_attr_setstacksize* function
|
||||
const unsigned int page_size = sysconf(_SC_PAGESIZE);
|
||||
const size_t stacksize_adj = math::max((int)PTHREAD_STACK_MIN, PX4_STACK_ADJUSTED(wq->stacksize));
|
||||
const size_t stacksize_adj =
|
||||
math::max((int)PTHREAD_STACK_MIN, PX4_STACK_ADJUSTED(wq->stacksize));
|
||||
const size_t stacksize = (stacksize_adj + page_size - (stacksize_adj % page_size));
|
||||
#endif
|
||||
|
||||
// priority
|
||||
int sched_priority = sched_get_priority_max(SCHED_FIFO) + wq->relative_priority;
|
||||
|
||||
// use pthreads for NuttX flat and posix builds. For NuttX protected build, use tasks or kernel threads
|
||||
// use pthreads for NuttX flat and posix builds. For NuttX protected build, use tasks or kernel
|
||||
// threads
|
||||
#if !defined(__PX4_NUTTX) || defined(CONFIG_BUILD_FLAT)
|
||||
pthread_attr_t attr;
|
||||
int ret_attr_init = pthread_attr_init(&attr);
|
||||
@@ -312,7 +311,7 @@ WorkQueueManagerRun(int, char **)
|
||||
PX4_ERR("failed to set sched policy SCHED_FIFO (%i)", ret_setschedpolicy);
|
||||
}
|
||||
|
||||
#endif // ! QuRT
|
||||
#endif // ! QuRT
|
||||
|
||||
// priority
|
||||
param.sched_priority = sched_priority;
|
||||
@@ -327,10 +326,12 @@ WorkQueueManagerRun(int, char **)
|
||||
int ret_create = pthread_create(&thread, &attr, WorkQueueRunner, (void *)wq);
|
||||
|
||||
if (ret_create == 0) {
|
||||
PX4_DEBUG("starting: %s, priority: %d, stack: %zu bytes", wq->name, param.sched_priority, stacksize);
|
||||
PX4_DEBUG("starting: %s, priority: %d, stack: %zu bytes", wq->name,
|
||||
param.sched_priority, stacksize);
|
||||
|
||||
} else {
|
||||
PX4_ERR("failed to create thread for %s (%i): %s", wq->name, ret_create, strerror(ret_create));
|
||||
PX4_ERR("failed to create thread for %s (%i): %s", wq->name, ret_create,
|
||||
strerror(ret_create));
|
||||
}
|
||||
|
||||
// destroy thread attributes
|
||||
@@ -348,15 +349,12 @@ WorkQueueManagerRun(int, char **)
|
||||
sprintf(arg1, "%lx", (long unsigned)wq);
|
||||
const char *arg[2] = {arg1, nullptr};
|
||||
|
||||
int pid = px4_task_spawn_cmd(wq->name,
|
||||
SCHED_FIFO,
|
||||
sched_priority,
|
||||
stacksize,
|
||||
WorkQueueRunner,
|
||||
int pid = px4_task_spawn_cmd(wq->name, SCHED_FIFO, sched_priority, stacksize, WorkQueueRunner,
|
||||
(char *const *)arg);
|
||||
|
||||
if (pid > 0) {
|
||||
PX4_DEBUG("starting: %s, priority: %d, stack: %zu bytes", wq->name, sched_priority, stacksize);
|
||||
PX4_DEBUG("starting: %s, priority: %d, stack: %zu bytes", wq->name, sched_priority,
|
||||
stacksize);
|
||||
|
||||
} else {
|
||||
PX4_ERR("failed to create thread for %s (%i): %s", wq->name, pid, strerror(pid));
|
||||
@@ -369,19 +367,12 @@ WorkQueueManagerRun(int, char **)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
WorkQueueManagerStart()
|
||||
{
|
||||
int WorkQueueManagerStart() {
|
||||
if (_wq_manager_should_exit.load() && (_wq_manager_create_queue == nullptr)) {
|
||||
|
||||
_wq_manager_should_exit.store(false);
|
||||
|
||||
int task_id = px4_task_spawn_cmd("wq:manager",
|
||||
SCHED_DEFAULT,
|
||||
SCHED_PRIORITY_MAX,
|
||||
PX4_STACK_ADJUSTED(1280),
|
||||
(px4_main_t)&WorkQueueManagerRun,
|
||||
nullptr);
|
||||
int task_id = px4_task_spawn_cmd("wq:manager", SCHED_DEFAULT, SCHED_PRIORITY_MAX,
|
||||
PX4_STACK_ADJUSTED(1280), (px4_main_t)&WorkQueueManagerRun, nullptr);
|
||||
|
||||
if (task_id < 0) {
|
||||
_wq_manager_should_exit.store(true);
|
||||
@@ -397,11 +388,8 @@ WorkQueueManagerStart()
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int
|
||||
WorkQueueManagerStop()
|
||||
{
|
||||
int WorkQueueManagerStop() {
|
||||
if (!_wq_manager_should_exit.load()) {
|
||||
|
||||
// error can't shutdown until all WorkItems are removed/stopped
|
||||
if ((_wq_manager_wqs_list != nullptr) && (_wq_manager_wqs_list->size() > 0)) {
|
||||
PX4_ERR("can't shutdown with active WQs");
|
||||
@@ -448,11 +436,8 @@ WorkQueueManagerStop()
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
int
|
||||
WorkQueueManagerStatus()
|
||||
{
|
||||
int WorkQueueManagerStatus() {
|
||||
if (!_wq_manager_should_exit.load() && (_wq_manager_wqs_list != nullptr)) {
|
||||
|
||||
const size_t num_wqs = _wq_manager_wqs_list->size();
|
||||
PX4_INFO_RAW("\nWork Queue: %-2zu threads RATE INTERVAL\n", num_wqs);
|
||||
|
||||
@@ -481,4 +466,4 @@ WorkQueueManagerStatus()
|
||||
return PX4_OK;
|
||||
}
|
||||
|
||||
} // namespace px4
|
||||
} // namespace px4
|
||||
|
||||
@@ -31,16 +31,15 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include "wqueue_test.h"
|
||||
#include "wqueue_scheduled_test.h"
|
||||
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/app.h>
|
||||
#include <px4_platform_common/init.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int PX4_MAIN(int argc, char **argv)
|
||||
{
|
||||
#include "wqueue_scheduled_test.h"
|
||||
#include "wqueue_test.h"
|
||||
|
||||
int PX4_MAIN(int argc, char **argv) {
|
||||
px4::init(argc, argv, "wqueue_test");
|
||||
|
||||
PX4_INFO("wqueue test 1");
|
||||
|
||||
@@ -34,20 +34,18 @@
|
||||
#include "wqueue_scheduled_test.h"
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <inttypes.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace px4;
|
||||
|
||||
AppState WQueueScheduledTest::appState;
|
||||
|
||||
void WQueueScheduledTest::Run()
|
||||
{
|
||||
//PX4_INFO("iter: %d elapsed: %" PRId64 " us", _iter, hrt_elapsed_time(&_qtime));
|
||||
void WQueueScheduledTest::Run() {
|
||||
// PX4_INFO("iter: %d elapsed: %" PRId64 " us", _iter, hrt_elapsed_time(&_qtime));
|
||||
|
||||
if (_iter > 1000) {
|
||||
appState.requestExit();
|
||||
@@ -56,8 +54,7 @@ void WQueueScheduledTest::Run()
|
||||
_iter++;
|
||||
}
|
||||
|
||||
int WQueueScheduledTest::main()
|
||||
{
|
||||
int WQueueScheduledTest::main() {
|
||||
appState.setRunning(true);
|
||||
|
||||
_iter = 0;
|
||||
@@ -72,7 +69,7 @@ int WQueueScheduledTest::main()
|
||||
|
||||
PX4_INFO("WQueueScheduledTest finished");
|
||||
|
||||
//print_status();
|
||||
// print_status();
|
||||
|
||||
px4_sleep(2);
|
||||
|
||||
|
||||
@@ -34,13 +34,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <px4_platform_common/app.h>
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
#include <string.h>
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp>
|
||||
|
||||
using namespace px4;
|
||||
|
||||
class WQueueScheduledTest : public px4::ScheduledWorkItem
|
||||
{
|
||||
class WQueueScheduledTest : public px4::ScheduledWorkItem {
|
||||
public:
|
||||
WQueueScheduledTest() : px4::ScheduledWorkItem(MODULE_NAME, px4::wq_configurations::test2) {}
|
||||
~WQueueScheduledTest() = default;
|
||||
@@ -50,7 +50,6 @@ public:
|
||||
static px4::AppState appState; /* track requests to terminate app */
|
||||
|
||||
private:
|
||||
|
||||
void Run() override;
|
||||
|
||||
int _iter{0};
|
||||
|
||||
@@ -31,39 +31,32 @@
|
||||
*
|
||||
****************************************************************************/
|
||||
|
||||
#include "wqueue_test.h"
|
||||
|
||||
#include <px4_platform_common/app.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/tasks.h>
|
||||
#include <sched.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <sched.h>
|
||||
|
||||
static int daemon_task = -1; /* Handle of daemon task / thread */
|
||||
#include "wqueue_test.h"
|
||||
|
||||
static int daemon_task = -1; /* Handle of daemon task / thread */
|
||||
|
||||
extern "C" __EXPORT int wqueue_test_main(int argc, char *argv[]);
|
||||
int wqueue_test_main(int argc, char *argv[])
|
||||
{
|
||||
|
||||
int wqueue_test_main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
PX4_INFO("usage: wqueue_test {start|stop|status}\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (!strcmp(argv[1], "start")) {
|
||||
|
||||
if (WQueueTest::appState.isRunning()) {
|
||||
PX4_INFO("already running\n");
|
||||
/* this is not an error */
|
||||
return 0;
|
||||
}
|
||||
|
||||
daemon_task = px4_task_spawn_cmd("wqueue",
|
||||
SCHED_DEFAULT,
|
||||
SCHED_PRIORITY_MAX - 5,
|
||||
2000,
|
||||
PX4_MAIN,
|
||||
daemon_task = px4_task_spawn_cmd("wqueue", SCHED_DEFAULT, SCHED_PRIORITY_MAX - 5, 2000, PX4_MAIN,
|
||||
(argv[2]) ? (char *const *)&argv[2] : (char *const *)nullptr);
|
||||
|
||||
return 0;
|
||||
|
||||
@@ -34,20 +34,18 @@
|
||||
#include "wqueue_test.h"
|
||||
|
||||
#include <drivers/drv_hrt.h>
|
||||
#include <inttypes.h>
|
||||
#include <px4_platform_common/log.h>
|
||||
#include <px4_platform_common/time.h>
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace px4;
|
||||
|
||||
AppState WQueueTest::appState;
|
||||
|
||||
void WQueueTest::Run()
|
||||
{
|
||||
//PX4_INFO("iter: %d elapsed: %" PRId64 " us", _iter, hrt_elapsed_time(&_qtime));
|
||||
void WQueueTest::Run() {
|
||||
// PX4_INFO("iter: %d elapsed: %" PRId64 " us", _iter, hrt_elapsed_time(&_qtime));
|
||||
|
||||
if (_iter > 10000) {
|
||||
appState.requestExit();
|
||||
@@ -59,8 +57,7 @@ void WQueueTest::Run()
|
||||
_iter++;
|
||||
}
|
||||
|
||||
int WQueueTest::main()
|
||||
{
|
||||
int WQueueTest::main() {
|
||||
appState.setRunning(true);
|
||||
|
||||
_iter = 0;
|
||||
@@ -75,7 +72,7 @@ int WQueueTest::main()
|
||||
|
||||
PX4_INFO("WQueueTest finished");
|
||||
|
||||
//print_status();
|
||||
// print_status();
|
||||
|
||||
px4_sleep(2);
|
||||
|
||||
|
||||
@@ -34,13 +34,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <px4_platform_common/app.h>
|
||||
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
#include <string.h>
|
||||
|
||||
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
||||
|
||||
using namespace px4;
|
||||
|
||||
class WQueueTest : public px4::WorkItem
|
||||
{
|
||||
class WQueueTest : public px4::WorkItem {
|
||||
public:
|
||||
WQueueTest() : px4::WorkItem("WQueueTest", px4::wq_configurations::test1) {}
|
||||
~WQueueTest() = default;
|
||||
@@ -50,7 +50,6 @@ public:
|
||||
static px4::AppState appState; /* track requests to terminate app */
|
||||
|
||||
private:
|
||||
|
||||
void Run() override;
|
||||
|
||||
int _iter{0};
|
||||
|
||||
Reference in New Issue
Block a user