2014-10-10 20:22:51 +08:00
|
|
|
/****************************************************************************
|
2014-10-10 22:35:58 +08:00
|
|
|
* sched/wqueue/kwork_cancel.c
|
2014-10-10 20:22:51 +08:00
|
|
|
*
|
2024-09-11 19:45:11 +08:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
2020-03-12 04:23:38 +08: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 20:22:51 +08:00
|
|
|
*
|
2020-03-12 04:23:38 +08:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2014-10-10 20:22:51 +08:00
|
|
|
*
|
2020-03-12 04:23:38 +08: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 20:22:51 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
2014-10-11 04:52:04 +08:00
|
|
|
#include <assert.h>
|
2014-10-10 22:35:58 +08:00
|
|
|
#include <errno.h>
|
|
|
|
|
2016-02-14 22:17:46 +08:00
|
|
|
#include <nuttx/irq.h>
|
2014-10-11 04:52:04 +08:00
|
|
|
#include <nuttx/arch.h>
|
2022-09-25 23:08:38 +08:00
|
|
|
#include <nuttx/queue.h>
|
2014-10-10 20:22:51 +08:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
|
|
|
#include "wqueue/wqueue.h"
|
|
|
|
|
2014-10-10 22:35:58 +08:00
|
|
|
#ifdef CONFIG_SCHED_WORKQUEUE
|
2014-10-10 20:22:51 +08:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-06-25 21:01:48 +08:00
|
|
|
static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, bool sync,
|
2014-10-11 06:24:50 +08:00
|
|
|
FAR struct work_s *work)
|
2014-10-11 04:52:04 +08:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
2024-06-25 21:01:48 +08:00
|
|
|
if (wqueue == NULL || work == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-10-11 04:52:04 +08:00
|
|
|
|
|
|
|
/* Cancelling the work is simply a matter of removing the work structure
|
|
|
|
* from the work queue. This must be done with interrupts disabled because
|
|
|
|
* new work is typically added to the work queue from interrupt handlers.
|
|
|
|
*/
|
|
|
|
|
2016-02-14 22:17:46 +08:00
|
|
|
flags = enter_critical_section();
|
2014-10-11 04:52:04 +08:00
|
|
|
if (work->worker != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the entry from the work queue and make sure that it is
|
2015-10-01 04:03:55 +08:00
|
|
|
* marked as available (i.e., the worker field is nullified).
|
2014-10-11 04:52:04 +08:00
|
|
|
*/
|
|
|
|
|
2021-06-19 17:29:30 +08:00
|
|
|
if (WDOG_ISACTIVE(&work->u.timer))
|
|
|
|
{
|
|
|
|
wd_cancel(&work->u.timer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-17 15:55:03 +08:00
|
|
|
dq_rem((FAR dq_entry_t *)work, &wqueue->q);
|
2021-06-19 17:29:30 +08:00
|
|
|
}
|
|
|
|
|
2014-10-11 04:52:04 +08:00
|
|
|
work->worker = NULL;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2024-06-25 21:01:48 +08:00
|
|
|
else if (!up_interrupt_context() && !sched_idletask() && sync)
|
2023-08-28 19:29:54 +08:00
|
|
|
{
|
|
|
|
int wndx;
|
|
|
|
|
2024-06-25 21:01:48 +08:00
|
|
|
for (wndx = 0; wndx < wqueue->nthreads; wndx++)
|
2023-08-28 19:29:54 +08:00
|
|
|
{
|
|
|
|
if (wqueue->worker[wndx].work == work &&
|
|
|
|
wqueue->worker[wndx].pid != nxsched_gettid())
|
|
|
|
{
|
|
|
|
nxsem_wait_uninterruptible(&wqueue->worker[wndx].wait);
|
2024-09-10 23:46:19 +08:00
|
|
|
ret = 1;
|
2023-08-28 19:29:54 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-11 04:52:04 +08:00
|
|
|
|
2016-02-14 22:17:46 +08:00
|
|
|
leave_critical_section(flags);
|
2014-10-11 04:52:04 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-10 20:22:51 +08:00
|
|
|
/****************************************************************************
|
2014-10-10 22:35:58 +08:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2024-06-25 21:01:48 +08:00
|
|
|
* Name: work_cancel/work_cancel_wq
|
2014-10-10 20:22:51 +08:00
|
|
|
*
|
|
|
|
* Description:
|
2024-06-25 21:01:48 +08:00
|
|
|
* Cancel previously queued work. This removes work from the work queue.
|
|
|
|
* After work has been cancelled, it may be requeued by calling
|
|
|
|
* work_queue() again.
|
2014-10-10 20:22:51 +08:00
|
|
|
*
|
2018-02-02 00:00:02 +08:00
|
|
|
* Input Parameters:
|
2014-10-10 22:47:41 +08:00
|
|
|
* qid - The work queue ID (must be HPWORK or LPWORK)
|
2024-06-25 21:01:48 +08:00
|
|
|
* wqueue - The work queue handle
|
2021-07-06 05:19:30 +08:00
|
|
|
* work - The previously queued work structure to cancel
|
2014-10-10 20:22:51 +08:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2024-06-25 21:01:48 +08:00
|
|
|
* Zero on success, a negated errno on failure
|
2014-10-10 22:35:58 +08:00
|
|
|
*
|
|
|
|
* -ENOENT - There is no such work queued.
|
|
|
|
* -EINVAL - An invalid work queue was specified
|
2014-10-10 20:22:51 +08:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-10 22:35:58 +08:00
|
|
|
int work_cancel(int qid, FAR struct work_s *work)
|
2014-10-10 20:22:51 +08:00
|
|
|
{
|
2024-06-25 21:01:48 +08:00
|
|
|
return work_qcancel(work_qid2wq(qid), false, work);
|
|
|
|
}
|
2023-08-28 19:29:54 +08:00
|
|
|
|
2024-06-25 21:01:48 +08:00
|
|
|
int work_cancel_wq(FAR struct kwork_wqueue_s *wqueue,
|
|
|
|
FAR struct work_s *work)
|
|
|
|
{
|
|
|
|
return work_qcancel(wqueue, false, work);
|
2023-08-28 19:29:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2024-06-25 21:01:48 +08:00
|
|
|
* Name: work_cancel_sync/work_cancel_sync_wq
|
2023-08-28 19:29:54 +08:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Blocked cancel previously queued user-mode work. This removes work
|
|
|
|
* from the user mode work queue. After work has been cancelled, it may
|
|
|
|
* be requeued by calling work_queue() again.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* qid - The work queue ID (must be HPWORK or LPWORK)
|
2024-06-25 21:01:48 +08:00
|
|
|
* wqueue - The work queue handle
|
2023-08-28 19:29:54 +08:00
|
|
|
* work - The previously queued work structure to cancel
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2024-09-10 23:46:19 +08:00
|
|
|
* Zero means the work was successfully cancelled.
|
|
|
|
* One means the work was not cancelled because it is currently being
|
|
|
|
* processed by work thread, but wait for it to finish.
|
|
|
|
* A negated errno value is returned on any failure:
|
2023-08-28 19:29:54 +08:00
|
|
|
*
|
|
|
|
* -ENOENT - There is no such work queued.
|
|
|
|
* -EINVAL - An invalid work queue was specified
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int work_cancel_sync(int qid, FAR struct work_s *work)
|
|
|
|
{
|
2024-06-25 21:01:48 +08:00
|
|
|
return work_qcancel(work_qid2wq(qid), true, work);
|
|
|
|
}
|
2014-10-10 20:22:51 +08:00
|
|
|
|
2024-06-25 21:01:48 +08:00
|
|
|
int work_cancel_sync_wq(FAR struct kwork_wqueue_s *wqueue,
|
|
|
|
FAR struct work_s *work)
|
|
|
|
{
|
|
|
|
return work_qcancel(wqueue, true, work);
|
2014-10-10 20:22:51 +08:00
|
|
|
}
|
|
|
|
|
2014-10-10 22:35:58 +08:00
|
|
|
#endif /* CONFIG_SCHED_WORKQUEUE */
|