2018-11-28 23:15:29 -04:00
|
|
|
/****************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
* 3. Neither the name PX4 nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-08-27 05:57:38 -03:00
|
|
|
#include <px4_platform_common/px4_work_queue/WorkQueue.hpp>
|
|
|
|
#include <px4_platform_common/px4_work_queue/WorkItem.hpp>
|
2018-11-28 23:15:29 -04:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-09-23 07:58:44 -03:00
|
|
|
#include <px4_platform_common/log.h>
|
2019-10-25 05:56:32 -03:00
|
|
|
#include <px4_platform_common/tasks.h>
|
|
|
|
#include <px4_platform_common/time.h>
|
2018-11-28 23:15:29 -04:00
|
|
|
#include <drivers/drv_hrt.h>
|
|
|
|
|
|
|
|
namespace px4
|
|
|
|
{
|
|
|
|
|
|
|
|
WorkQueue::WorkQueue(const wq_config_t &config) :
|
|
|
|
_config(config)
|
|
|
|
{
|
|
|
|
// set the threads name
|
|
|
|
#ifdef __PX4_DARWIN
|
|
|
|
pthread_setname_np(_config.name);
|
2021-07-15 14:17:15 -03:00
|
|
|
#else
|
2018-11-28 23:15:29 -04:00
|
|
|
pthread_setname_np(pthread_self(), _config.name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __PX4_NUTTX
|
|
|
|
px4_sem_init(&_qlock, 0, 1);
|
|
|
|
#endif /* __PX4_NUTTX */
|
|
|
|
|
|
|
|
px4_sem_init(&_process_lock, 0, 0);
|
|
|
|
px4_sem_setprotocol(&_process_lock, SEM_PRIO_NONE);
|
2021-08-24 06:54:49 -03:00
|
|
|
|
|
|
|
px4_sem_init(&_exit_lock, 0, 1);
|
|
|
|
px4_sem_setprotocol(&_exit_lock, SEM_PRIO_NONE);
|
2018-11-28 23:15:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
WorkQueue::~WorkQueue()
|
|
|
|
{
|
2021-08-24 06:54:49 -03:00
|
|
|
|
2018-11-28 23:15:29 -04:00
|
|
|
work_lock();
|
2021-08-24 06:54:49 -03:00
|
|
|
|
|
|
|
// Synchronize with ::Detach
|
|
|
|
px4_sem_wait(&_exit_lock);
|
|
|
|
px4_sem_destroy(&_exit_lock);
|
|
|
|
|
2018-11-28 23:15:29 -04:00
|
|
|
px4_sem_destroy(&_process_lock);
|
|
|
|
work_unlock();
|
|
|
|
|
|
|
|
#ifndef __PX4_NUTTX
|
|
|
|
px4_sem_destroy(&_qlock);
|
|
|
|
#endif /* __PX4_NUTTX */
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
bool WorkQueue::Attach(WorkItem *item)
|
2018-11-28 23:15:29 -04:00
|
|
|
{
|
2019-10-02 13:23:17 -03:00
|
|
|
work_lock();
|
|
|
|
|
|
|
|
if (!should_exit()) {
|
|
|
|
_work_items.add(item);
|
|
|
|
work_unlock();
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-17 11:26:06 -03:00
|
|
|
|
2019-10-02 13:23:17 -03:00
|
|
|
work_unlock();
|
2021-08-24 06:54:49 -03:00
|
|
|
|
2019-10-02 13:23:17 -03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::Detach(WorkItem *item)
|
2019-10-02 13:23:17 -03:00
|
|
|
{
|
2021-08-24 06:54:49 -03:00
|
|
|
bool exiting = false;
|
|
|
|
|
2019-10-02 13:23:17 -03:00
|
|
|
work_lock();
|
|
|
|
|
|
|
|
_work_items.remove(item);
|
|
|
|
|
|
|
|
if (_work_items.size() == 0) {
|
|
|
|
// shutdown, no active WorkItems
|
|
|
|
PX4_DEBUG("stopping: %s, last active WorkItem closing", _config.name);
|
|
|
|
|
2021-08-24 06:54:49 -03:00
|
|
|
// Deletion of this work queue might happen right after request_stop or
|
|
|
|
// SignalWorkerThread. Use a separate lock to prevent premature deletion
|
|
|
|
px4_sem_wait(&_exit_lock);
|
|
|
|
exiting = true;
|
2019-10-02 13:23:17 -03:00
|
|
|
request_stop();
|
2020-05-19 13:49:42 -03:00
|
|
|
SignalWorkerThread();
|
2019-10-02 13:23:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
work_unlock();
|
2021-08-24 06:54:49 -03:00
|
|
|
|
|
|
|
// In case someone is deleting this wq already, signal
|
|
|
|
// that it is now allowed
|
|
|
|
if (exiting) {
|
|
|
|
px4_sem_post(&_exit_lock);
|
|
|
|
}
|
2019-10-02 13:23:17 -03:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::Add(WorkItem *item)
|
2019-10-02 13:23:17 -03:00
|
|
|
{
|
2018-11-28 23:15:29 -04:00
|
|
|
work_lock();
|
2021-01-03 22:41:08 -04:00
|
|
|
|
|
|
|
#if defined(ENABLE_LOCKSTEP_SCHEDULER)
|
|
|
|
|
|
|
|
if (_lockstep_component == -1) {
|
|
|
|
_lockstep_component = px4_lockstep_register_component();
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_LOCKSTEP_SCHEDULER
|
|
|
|
|
2018-11-28 23:15:29 -04:00
|
|
|
_q.push(item);
|
|
|
|
work_unlock();
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
SignalWorkerThread();
|
2019-10-16 06:48:14 -03:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::SignalWorkerThread()
|
2019-10-16 06:48:14 -03:00
|
|
|
{
|
|
|
|
int sem_val;
|
|
|
|
|
|
|
|
if (px4_sem_getvalue(&_process_lock, &sem_val) == 0 && sem_val <= 0) {
|
|
|
|
px4_sem_post(&_process_lock);
|
|
|
|
}
|
2018-11-28 23:15:29 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::Remove(WorkItem *item)
|
2019-06-17 11:26:06 -03:00
|
|
|
{
|
|
|
|
work_lock();
|
|
|
|
_q.remove(item);
|
|
|
|
work_unlock();
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::Clear()
|
2019-06-17 11:26:06 -03:00
|
|
|
{
|
|
|
|
work_lock();
|
|
|
|
|
|
|
|
while (!_q.empty()) {
|
|
|
|
_q.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
work_unlock();
|
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::Run()
|
2018-11-28 23:15:29 -04:00
|
|
|
{
|
|
|
|
while (!should_exit()) {
|
2020-04-05 11:12:24 -03:00
|
|
|
// loop as the wait may be interrupted by a signal
|
|
|
|
do {} while (px4_sem_wait(&_process_lock) != 0);
|
2018-11-28 23:15:29 -04:00
|
|
|
|
|
|
|
work_lock();
|
|
|
|
|
|
|
|
// process queued work
|
|
|
|
while (!_q.empty()) {
|
|
|
|
WorkItem *work = _q.pop();
|
|
|
|
|
|
|
|
work_unlock(); // unlock work queue to run (item may requeue itself)
|
2019-10-02 13:23:17 -03:00
|
|
|
work->RunPreamble();
|
2018-11-28 23:15:29 -04:00
|
|
|
work->Run();
|
2020-03-09 08:06:15 -03:00
|
|
|
// Note: after Run() we cannot access work anymore, as it might have been deleted
|
2018-11-28 23:15:29 -04:00
|
|
|
work_lock(); // re-lock
|
|
|
|
}
|
|
|
|
|
2021-01-03 22:41:08 -04:00
|
|
|
#if defined(ENABLE_LOCKSTEP_SCHEDULER)
|
|
|
|
|
|
|
|
if (_q.empty()) {
|
|
|
|
px4_lockstep_unregister_component(_lockstep_component);
|
|
|
|
_lockstep_component = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ENABLE_LOCKSTEP_SCHEDULER
|
|
|
|
|
2018-11-28 23:15:29 -04:00
|
|
|
work_unlock();
|
|
|
|
}
|
2019-10-02 13:23:17 -03:00
|
|
|
|
|
|
|
PX4_DEBUG("%s: exiting", _config.name);
|
2018-11-28 23:15:29 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 13:49:42 -03:00
|
|
|
void WorkQueue::print_status(bool last)
|
2018-11-28 23:15:29 -04:00
|
|
|
{
|
2019-10-02 13:23:17 -03:00
|
|
|
const size_t num_items = _work_items.size();
|
|
|
|
PX4_INFO_RAW("%-16s\n", get_name());
|
2020-05-19 13:49:42 -03:00
|
|
|
unsigned i = 0;
|
2019-10-02 13:23:17 -03:00
|
|
|
|
|
|
|
for (WorkItem *item : _work_items) {
|
|
|
|
i++;
|
|
|
|
|
|
|
|
if (last) {
|
|
|
|
PX4_INFO_RAW(" ");
|
|
|
|
|
|
|
|
} else {
|
|
|
|
PX4_INFO_RAW("| ");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i < num_items) {
|
2020-05-19 13:49:42 -03:00
|
|
|
PX4_INFO_RAW("|__%2d) ", i);
|
2019-10-02 13:23:17 -03:00
|
|
|
|
|
|
|
} else {
|
2020-05-19 13:49:42 -03:00
|
|
|
PX4_INFO_RAW("\\__%2d) ", i);
|
2019-10-02 13:23:17 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
item->print_run_status();
|
|
|
|
}
|
2018-11-28 23:15:29 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace px4
|