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
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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();
|
2022-08-17 09:55:03 +02:00
|
|
|
dq_addlast((FAR dq_entry_t *)arg, &g_hpwork.q);
|
2021-06-19 11:29:30 +02:00
|
|
|
nxsem_post(&g_hpwork.sem);
|
|
|
|
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();
|
2022-08-17 09:55:03 +02:00
|
|
|
dq_addlast((FAR dq_entry_t *)arg, &g_lpwork.q);
|
2021-06-19 11:29:30 +02:00
|
|
|
nxsem_post(&g_lpwork.sem);
|
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. */
|
|
|
|
|
|
|
|
work_cancel(qid, work);
|
|
|
|
|
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)
|
|
|
|
{
|
2022-08-17 09:55:03 +02:00
|
|
|
dq_addlast((FAR dq_entry_t *)work, &g_hpwork.q);
|
2021-06-19 11:29:30 +02:00
|
|
|
nxsem_post(&g_hpwork.sem);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2022-08-17 09:55:03 +02:00
|
|
|
dq_addlast((FAR dq_entry_t *)work, &g_lpwork.q);
|
2021-06-19 11:29:30 +02:00
|
|
|
nxsem_post(&g_lpwork.sem);
|
|
|
|
}
|
|
|
|
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 */
|