2009-11-05 21:44:33 +00:00
|
|
|
/****************************************************************************
|
2018-05-29 13:21:26 -06:00
|
|
|
* libs/libc/wqueue/work_cancel.c
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
2024-09-25 14:05:00 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
2021-03-02 15:54:21 +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
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
2021-03-02 15:54:21 +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.
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Included Files
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
#include <unistd.h>
|
2009-11-05 21:44:33 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
2022-09-25 23:08:38 +08:00
|
|
|
#include <nuttx/queue.h>
|
2009-11-05 21:44:33 +00:00
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
|
2014-10-10 08:35:58 -06:00
|
|
|
#include "wqueue/wqueue.h"
|
|
|
|
|
|
2021-08-01 15:27:08 +08:00
|
|
|
#if defined(CONFIG_LIBC_USRWORK) && !defined(__KERNEL__)
|
2009-11-05 21:44:33 +00:00
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
|
* Private Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-10 08:35:58 -06:00
|
|
|
* Name: work_qcancel
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
|
|
|
|
* Description:
|
2009-11-06 13:42:49 +00:00
|
|
|
* Cancel previously queued work. This removes work from the work queue.
|
2021-07-05 17:19:30 -04:00
|
|
|
* After work has been cancelled, it may be requeued by calling
|
2021-03-02 14:33:27 +01:00
|
|
|
* work_queue() again.
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Input Parameters:
|
2021-07-26 10:36:13 +08:00
|
|
|
* wqueue - The work queue
|
2026-08-27 20:52:26 +08:00
|
|
|
* work - The previously queued work structure to cancel
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
|
|
|
|
* Returned Value:
|
2014-10-05 08:22:39 -06:00
|
|
|
* Zero (OK) on success, a negated errno on failure. This error may be
|
|
|
|
|
* reported:
|
|
|
|
|
*
|
2014-10-10 08:35:58 -06:00
|
|
|
* -EINVAL - An invalid work queue was specified
|
2009-11-05 21:44:33 +00:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
static int work_qcancel(FAR struct usr_wqueue_s *wqueue, bool sync,
|
2021-03-02 14:33:27 +01:00
|
|
|
FAR struct work_s *work)
|
2009-11-05 21:44:33 +00:00
|
|
|
{
|
2026-08-27 20:52:26 +08:00
|
|
|
pid_t self = sync ? gettid() : 0;
|
|
|
|
|
int ret;
|
2014-10-10 14:52:04 -06:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
if (wqueue == NULL || work == NULL)
|
|
|
|
|
{
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
2014-10-10 14:52:04 -06:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
/* A work structure becomes available for requeue after it is dequeued,
|
|
|
|
|
* before its callback returns. Multiple workers can therefore execute
|
|
|
|
|
* callbacks using the same work structure. Find one such worker and
|
|
|
|
|
* repeat after it finishes until no callback remains. Exclude the
|
|
|
|
|
* calling worker to avoid self-deadlock.
|
2009-11-05 21:44:33 +00:00
|
|
|
*/
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
for (; ; )
|
2010-05-26 12:34:59 +00:00
|
|
|
{
|
2026-08-27 20:52:26 +08:00
|
|
|
FAR sem_t *sync_wait = NULL;
|
|
|
|
|
int wndx;
|
|
|
|
|
|
|
|
|
|
/* Get exclusive access to the work queue */
|
2021-07-26 10:36:13 +08:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
do
|
|
|
|
|
{
|
|
|
|
|
ret = nxmutex_lock(&wqueue->lock);
|
|
|
|
|
}
|
|
|
|
|
while (ret < 0);
|
2021-07-26 10:36:13 +08:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
/* Remove a pending instance from the queue. */
|
2021-07-26 10:36:13 +08:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
if (work->worker != NULL)
|
2021-07-26 10:36:13 +08:00
|
|
|
{
|
2026-08-27 20:52:26 +08:00
|
|
|
if (work_remove(wqueue, work))
|
|
|
|
|
{
|
|
|
|
|
work_wake(wqueue);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-07-26 10:36:13 +08:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
if (sync)
|
|
|
|
|
{
|
|
|
|
|
for (wndx = 0; wndx < wqueue->nthreads; wndx++)
|
2021-07-26 10:36:13 +08:00
|
|
|
{
|
2026-08-27 20:52:26 +08:00
|
|
|
FAR struct usr_worker_s *worker = &wqueue->worker[wndx];
|
|
|
|
|
|
|
|
|
|
if (worker->work == work && self != worker->tid)
|
|
|
|
|
{
|
|
|
|
|
worker->wait_count++;
|
|
|
|
|
sync_wait = &worker->wait;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2021-07-26 10:36:13 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
nxmutex_unlock(&wqueue->lock);
|
2012-07-14 19:30:31 +00:00
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
if (sync_wait == NULL)
|
|
|
|
|
{
|
|
|
|
|
return OK;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 20:53:03 +08:00
|
|
|
nxsem_wait_uninterruptible(sync_wait);
|
2026-08-27 20:52:26 +08:00
|
|
|
}
|
2009-11-05 21:44:33 +00:00
|
|
|
}
|
2012-09-04 00:54:09 +00:00
|
|
|
|
2014-10-10 14:52:04 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Public Functions
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2014-10-10 08:35:58 -06:00
|
|
|
/****************************************************************************
|
|
|
|
|
* Name: work_cancel
|
|
|
|
|
*
|
|
|
|
|
* Description:
|
|
|
|
|
* Cancel previously queued user-mode work. This removes work from the
|
2021-07-05 17:19:30 -04:00
|
|
|
* user mode work queue. After work has been cancelled, it may be
|
|
|
|
|
* requeued by calling work_queue() again.
|
2014-10-10 08:35:58 -06:00
|
|
|
*
|
2018-02-01 10:00:02 -06:00
|
|
|
* Input Parameters:
|
2014-10-10 08:35:58 -06:00
|
|
|
* qid - The work queue ID (must be USRWORK)
|
2021-07-05 17:19:30 -04:00
|
|
|
* work - The previously queued work structure to cancel
|
2014-10-10 08:35:58 -06:00
|
|
|
*
|
|
|
|
|
* Returned Value:
|
|
|
|
|
* Zero (OK) on success, a negated errno on failure. This error may be
|
|
|
|
|
* reported:
|
|
|
|
|
*
|
2026-08-27 20:52:26 +08:00
|
|
|
* -EINVAL - An invalid work queue was specified
|
2014-10-10 08:35:58 -06:00
|
|
|
*
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
|
|
int work_cancel(int qid, FAR struct work_s *work)
|
|
|
|
|
{
|
|
|
|
|
if (qid == USRWORK)
|
|
|
|
|
{
|
2026-08-27 20:52:26 +08:00
|
|
|
return work_qcancel(&g_usrwork, false, work);
|
2014-10-10 08:35:58 -06:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-27 20:52:26 +08:00
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
|
int work_cancel_wq(FAR struct kwork_wqueue_s *handle,
|
|
|
|
|
FAR struct work_s *work)
|
|
|
|
|
{
|
|
|
|
|
return work_qcancel((FAR struct usr_wqueue_s *)handle, false, work);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
int work_cancel_sync(int qid, FAR struct work_s *work)
|
|
|
|
|
{
|
|
|
|
|
if (qid == USRWORK)
|
|
|
|
|
{
|
|
|
|
|
return work_qcancel(&g_usrwork, true, work);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return -EINVAL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifndef CONFIG_DISABLE_PTHREAD
|
|
|
|
|
int work_cancel_sync_wq(FAR struct kwork_wqueue_s *handle,
|
|
|
|
|
FAR struct work_s *work)
|
|
|
|
|
{
|
|
|
|
|
return work_qcancel((FAR struct usr_wqueue_s *)handle, true, work);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2021-08-01 15:27:08 +08:00
|
|
|
#endif /* CONFIG_LIBC_USRWORK && !__KERNEL__ */
|