2014-10-10 14:22:51 +02:00
|
|
|
/****************************************************************************
|
2014-10-10 16:35:58 +02:00
|
|
|
* sched/wqueue/kwork_queue.c
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
2020-03-11 21:23:38 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
2020-03-11 21:23:38 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
2020-03-11 21:23:38 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
#include <stdint.h>
|
2014-10-10 22:52:04 +02:00
|
|
|
#include <assert.h>
|
2014-10-10 16:35:58 +02:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
#include <nuttx/irq.h>
|
2014-10-10 22:52:04 +02:00
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/clock.h>
|
2022-09-25 17:08:38 +02:00
|
|
|
#include <nuttx/queue.h>
|
2014-10-10 14:22:51 +02:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
|
|
|
#include "wqueue/wqueue.h"
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
#ifdef CONFIG_SCHED_WORKQUEUE
|
2014-10-10 14:22:51 +02:00
|
|
|
|
sched/wqueue: Do as much work as possible in work_thread
Decouple the semcount and the work queue length.
Previous Problem:
If a work is queued and cancelled in high priority threads (or queued
by timer and cancelled by another high priority thread) before
work_thread runs, the queue operation will mark work_thread as ready to
run, but the cancel operation minus the semcount back to -1 and makes
wqueue->q empty. Then the work_thread still runs, found empty queue,
and wait sem again, then semcount becomes -2 (being minused by 1)
This can be done multiple times, then semcount can become very small
value. Test case to produce incorrect semcount:
high_priority_task()
{
for (int i = 0; i < 10000; i++)
{
work_queue(LPWORK, &work, worker, NULL, 0);
work_cancel(LPWORK, &work);
usleep(1);
}
/* Now the g_lpwork.sem.semcount is a value near -10000 */
}
With incorrect semcount, any queue operation when the work_thread is
busy, will only increase semcount and push work into queue, but cannot
trigger work_thread (semcount is negative but work_thread is not
waiting), then there will be more and more works left in queue while
the work_thread is waiting sem and cannot call them.
Signed-off-by: Zhe Weng <wengzhe@xiaomi.com>
2023-03-15 14:55:28 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#define queue_work(wqueue, work) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
int sem_count; \
|
|
|
|
dq_addlast((FAR dq_entry_t *)(work), &(wqueue).q); \
|
|
|
|
nxsem_get_value(&(wqueue).sem, &sem_count); \
|
|
|
|
if (sem_count < 0) /* There are threads waiting for sem. */ \
|
|
|
|
{ \
|
|
|
|
nxsem_post(&(wqueue).sem); \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
while (0)
|
|
|
|
|
2014-10-10 14:22:51 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-10 22:52:04 +02:00
|
|
|
/****************************************************************************
|
2021-06-19 11:29:30 +02:00
|
|
|
* Name: hp_work_timer_expiry
|
2014-10-10 22:52:04 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
#ifdef CONFIG_SCHED_HPWORK
|
|
|
|
static void hp_work_timer_expiry(wdparm_t arg)
|
2014-10-10 22:52:04 +02:00
|
|
|
{
|
2021-06-19 11:29:30 +02:00
|
|
|
irqstate_t flags = enter_critical_section();
|
2024-06-06 10:49:50 +02:00
|
|
|
queue_work(g_hpwork, arg);
|
2021-06-19 11:29:30 +02:00
|
|
|
leave_critical_section(flags);
|
|
|
|
}
|
|
|
|
#endif
|
2014-10-10 22:52:04 +02:00
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: lp_work_timer_expiry
|
|
|
|
****************************************************************************/
|
2014-10-10 22:52:04 +02:00
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
#ifdef CONFIG_SCHED_LPWORK
|
|
|
|
static void lp_work_timer_expiry(wdparm_t arg)
|
|
|
|
{
|
|
|
|
irqstate_t flags = enter_critical_section();
|
2024-06-06 10:49:50 +02:00
|
|
|
queue_work(g_lpwork, arg);
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2014-10-10 22:52:04 +02:00
|
|
|
}
|
2021-06-19 11:29:30 +02:00
|
|
|
#endif
|
2014-10-10 22:52:04 +02:00
|
|
|
|
2014-10-10 14:22:51 +02:00
|
|
|
/****************************************************************************
|
2014-10-10 16:35:58 +02:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: work_queue
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2020-03-11 21:23:38 +01:00
|
|
|
* Queue kernel-mode work to be performed at a later time. All queued
|
2020-03-16 20:42:34 +01:00
|
|
|
* work will be performed on the worker thread of execution (not the
|
2020-03-11 21:23:38 +01:00
|
|
|
* caller's).
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
2017-08-28 15:46:48 +02:00
|
|
|
* The work structure is allocated and must be initialized to all zero by
|
|
|
|
* the caller. Otherwise, the work structure is completely managed by the
|
|
|
|
* work queue logic. The caller should never modify the contents of the
|
|
|
|
* work queue structure directly. If work_queue() is called before the
|
2021-07-05 23:19:30 +02:00
|
|
|
* previous work has been performed and removed from the queue, then any
|
2017-08-28 15:46:48 +02:00
|
|
|
* pending work will be canceled and lost.
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-10-10 16:35:58 +02:00
|
|
|
* qid - The work queue ID (index)
|
|
|
|
* work - The work structure to queue
|
2021-07-05 23:19:30 +02:00
|
|
|
* worker - The worker callback to be invoked. The callback will be
|
|
|
|
* invoked on the worker thread of execution.
|
|
|
|
* arg - The argument that will be passed to the worker callback when
|
2014-10-10 16:35:58 +02:00
|
|
|
* int is invoked.
|
|
|
|
* delay - Delay (in clock ticks) from the time queue until the worker
|
|
|
|
* is invoked. Zero means to perform the work immediately.
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2014-10-10 16:35:58 +02:00
|
|
|
* Zero on success, a negated errno on failure
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
int work_queue(int qid, FAR struct work_s *work, worker_t worker,
|
2018-06-16 20:16:13 +02:00
|
|
|
FAR void *arg, clock_t delay)
|
2014-10-10 16:35:58 +02:00
|
|
|
{
|
2021-06-19 11:29:30 +02:00
|
|
|
irqstate_t flags;
|
2021-12-22 06:26:10 +01:00
|
|
|
int ret = OK;
|
2021-06-19 11:29:30 +02:00
|
|
|
|
|
|
|
/* Interrupts are disabled so that this logic can be called from with
|
|
|
|
* task logic or from interrupt handling logic.
|
|
|
|
*/
|
|
|
|
|
|
|
|
flags = enter_critical_section();
|
|
|
|
|
2022-08-17 09:42:00 +02:00
|
|
|
/* Remove the entry from the timer and work queue. */
|
|
|
|
|
2023-01-11 07:12:20 +01:00
|
|
|
if (work->worker != NULL)
|
|
|
|
{
|
|
|
|
work_cancel(qid, work);
|
|
|
|
}
|
2022-08-17 09:42:00 +02:00
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
/* Initialize the work structure. */
|
|
|
|
|
|
|
|
work->worker = worker; /* Work callback. non-NULL means queued */
|
|
|
|
work->arg = arg; /* Callback argument */
|
|
|
|
|
2017-08-31 15:43:47 +02:00
|
|
|
/* Queue the new work */
|
2017-08-28 15:46:48 +02:00
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
#ifdef CONFIG_SCHED_HPWORK
|
|
|
|
if (qid == HPWORK)
|
2014-10-10 14:22:51 +02:00
|
|
|
{
|
2016-11-06 14:56:38 +01:00
|
|
|
/* Queue high priority work */
|
2014-10-10 14:22:51 +02:00
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
if (!delay)
|
|
|
|
{
|
2024-06-06 10:49:50 +02:00
|
|
|
queue_work(g_hpwork, work);
|
2021-06-19 11:29:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wd_start(&work->u.timer, delay, hp_work_timer_expiry,
|
|
|
|
(wdparm_t)work);
|
|
|
|
}
|
2014-10-10 14:22:51 +02:00
|
|
|
}
|
2014-10-10 16:35:58 +02:00
|
|
|
else
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_SCHED_LPWORK
|
|
|
|
if (qid == LPWORK)
|
|
|
|
{
|
2016-11-06 14:56:38 +01:00
|
|
|
/* Queue low priority work */
|
2014-10-10 14:22:51 +02:00
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
if (!delay)
|
|
|
|
{
|
2024-06-06 10:49:50 +02:00
|
|
|
queue_work(g_lpwork, work);
|
2021-06-19 11:29:30 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
wd_start(&work->u.timer, delay, lp_work_timer_expiry,
|
|
|
|
(wdparm_t)work);
|
|
|
|
}
|
2014-10-10 16:35:58 +02:00
|
|
|
}
|
2021-12-22 06:26:10 +01:00
|
|
|
else
|
2014-10-10 16:35:58 +02:00
|
|
|
#endif
|
2021-12-22 06:26:10 +01:00
|
|
|
{
|
|
|
|
ret = -EINVAL;
|
|
|
|
}
|
2021-06-19 11:29:30 +02:00
|
|
|
|
|
|
|
leave_critical_section(flags);
|
|
|
|
|
2021-12-22 06:26:10 +01:00
|
|
|
return ret;
|
2014-10-10 14:22:51 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
#endif /* CONFIG_SCHED_WORKQUEUE */
|