2014-10-10 14:22:51 +02:00
|
|
|
/****************************************************************************
|
2014-10-10 16:35:58 +02:00
|
|
|
* sched/wqueue/kwork_cancel.c
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
2024-09-11 13:45:11 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*
|
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 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>
|
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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2024-06-25 15:01:48 +02:00
|
|
|
static int work_qcancel(FAR struct kwork_wqueue_s *wqueue, bool sync,
|
2014-10-11 00:24:50 +02:00
|
|
|
FAR struct work_s *work)
|
2014-10-10 22:52:04 +02:00
|
|
|
{
|
|
|
|
irqstate_t flags;
|
|
|
|
int ret = -ENOENT;
|
|
|
|
|
2024-06-25 15:01:48 +02:00
|
|
|
if (wqueue == NULL || work == NULL)
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
2014-10-10 22:52:04 +02: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 15:17:46 +01:00
|
|
|
flags = enter_critical_section();
|
2014-10-10 22:52:04 +02:00
|
|
|
if (work->worker != NULL)
|
|
|
|
{
|
|
|
|
/* Remove the entry from the work queue and make sure that it is
|
2015-09-30 22:03:55 +02:00
|
|
|
* marked as available (i.e., the worker field is nullified).
|
2014-10-10 22:52:04 +02:00
|
|
|
*/
|
|
|
|
|
2021-06-19 11:29:30 +02:00
|
|
|
if (WDOG_ISACTIVE(&work->u.timer))
|
|
|
|
{
|
|
|
|
wd_cancel(&work->u.timer);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-17 09:55:03 +02:00
|
|
|
dq_rem((FAR dq_entry_t *)work, &wqueue->q);
|
2021-06-19 11:29:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 22:52:04 +02:00
|
|
|
work->worker = NULL;
|
|
|
|
ret = OK;
|
|
|
|
}
|
2024-06-25 15:01:48 +02:00
|
|
|
else if (!up_interrupt_context() && !sched_idletask() && sync)
|
2023-08-28 13:29:54 +02:00
|
|
|
{
|
|
|
|
int wndx;
|
|
|
|
|
2024-06-25 15:01:48 +02:00
|
|
|
for (wndx = 0; wndx < wqueue->nthreads; wndx++)
|
2023-08-28 13:29:54 +02:00
|
|
|
{
|
|
|
|
if (wqueue->worker[wndx].work == work &&
|
|
|
|
wqueue->worker[wndx].pid != nxsched_gettid())
|
|
|
|
{
|
|
|
|
nxsem_wait_uninterruptible(&wqueue->worker[wndx].wait);
|
2024-09-10 17:46:19 +02:00
|
|
|
ret = 1;
|
2023-08-28 13:29:54 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-10 22:52:04 +02:00
|
|
|
|
2016-02-14 15:17:46 +01:00
|
|
|
leave_critical_section(flags);
|
2014-10-10 22:52:04 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-10-10 14:22:51 +02:00
|
|
|
/****************************************************************************
|
2014-10-10 16:35:58 +02:00
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2024-06-25 15:01:48 +02:00
|
|
|
* Name: work_cancel/work_cancel_wq
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2024-06-25 15:01:48 +02: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 14:22:51 +02:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2014-10-10 16:47:41 +02:00
|
|
|
* qid - The work queue ID (must be HPWORK or LPWORK)
|
2024-06-25 15:01:48 +02:00
|
|
|
* wqueue - The work queue handle
|
2021-07-05 23:19:30 +02:00
|
|
|
* work - The previously queued work structure to cancel
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2024-06-25 15:01:48 +02:00
|
|
|
* Zero on success, a negated errno on failure
|
2014-10-10 16:35:58 +02:00
|
|
|
*
|
|
|
|
* -ENOENT - There is no such work queued.
|
|
|
|
* -EINVAL - An invalid work queue was specified
|
2014-10-10 14:22:51 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
int work_cancel(int qid, FAR struct work_s *work)
|
2014-10-10 14:22:51 +02:00
|
|
|
{
|
2024-06-25 15:01:48 +02:00
|
|
|
return work_qcancel(work_qid2wq(qid), false, work);
|
|
|
|
}
|
2023-08-28 13:29:54 +02:00
|
|
|
|
2024-06-25 15:01:48 +02: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 13:29:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2024-06-25 15:01:48 +02:00
|
|
|
* Name: work_cancel_sync/work_cancel_sync_wq
|
2023-08-28 13:29:54 +02: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 15:01:48 +02:00
|
|
|
* wqueue - The work queue handle
|
2023-08-28 13:29:54 +02:00
|
|
|
* work - The previously queued work structure to cancel
|
|
|
|
*
|
|
|
|
* Returned Value:
|
2024-09-10 17:46:19 +02: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 13:29:54 +02: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 15:01:48 +02:00
|
|
|
return work_qcancel(work_qid2wq(qid), true, work);
|
|
|
|
}
|
2014-10-10 14:22:51 +02:00
|
|
|
|
2024-06-25 15:01:48 +02: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 14:22:51 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
#endif /* CONFIG_SCHED_WORKQUEUE */
|