2009-11-05 22:44:33 +01:00
|
|
|
/****************************************************************************
|
2018-05-29 21:21:26 +02:00
|
|
|
* libs/libc/wqueue/work_cancel.c
|
2009-11-05 22:44:33 +01:00
|
|
|
*
|
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 22:44:33 +01:00
|
|
|
*
|
2021-03-02 15:54:21 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2009-11-05 22:44:33 +01: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 22:44:33 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <queue.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <nuttx/arch.h>
|
|
|
|
#include <nuttx/wqueue.h>
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
#include "wqueue/wqueue.h"
|
|
|
|
|
2021-08-01 09:27:08 +02:00
|
|
|
#if defined(CONFIG_LIBC_USRWORK) && !defined(__KERNEL__)
|
2009-11-05 22:44:33 +01:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-10-10 16:35:58 +02:00
|
|
|
* Name: work_qcancel
|
2009-11-05 22:44:33 +01:00
|
|
|
*
|
|
|
|
* Description:
|
2009-11-06 14:42:49 +01:00
|
|
|
* Cancel previously queued work. This removes work from the work queue.
|
2021-07-05 23:19:30 +02: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 22:44:33 +01:00
|
|
|
*
|
2018-02-01 17:00:02 +01:00
|
|
|
* Input Parameters:
|
2021-07-26 04:36:13 +02:00
|
|
|
* wqueue - The work queue
|
|
|
|
* work - The previously queue work structure to cancel
|
2009-11-05 22:44:33 +01:00
|
|
|
*
|
|
|
|
* Returned Value:
|
2014-10-05 16:22:39 +02:00
|
|
|
* Zero (OK) on success, a negated errno on failure. This error may be
|
|
|
|
* reported:
|
|
|
|
*
|
|
|
|
* -ENOENT - There is no such work queued.
|
2014-10-10 16:35:58 +02:00
|
|
|
* -EINVAL - An invalid work queue was specified
|
2009-11-05 22:44:33 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2021-03-02 14:33:27 +01:00
|
|
|
static int work_qcancel(FAR struct usr_wqueue_s *wqueue,
|
|
|
|
FAR struct work_s *work)
|
2009-11-05 22:44:33 +01:00
|
|
|
{
|
2021-07-26 04:36:13 +02:00
|
|
|
FAR sq_entry_t *prev = NULL;
|
|
|
|
FAR sq_entry_t *curr;
|
2014-10-05 16:22:39 +02:00
|
|
|
int ret = -ENOENT;
|
2021-07-26 04:36:13 +02:00
|
|
|
int semcount;
|
2009-11-05 22:44:33 +01:00
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
DEBUGASSERT(work != NULL);
|
2009-11-05 22:44:33 +01:00
|
|
|
|
2014-10-10 22:52:04 +02:00
|
|
|
/* Get exclusive access to the work queue */
|
|
|
|
|
2021-07-12 14:58:03 +02:00
|
|
|
while (_SEM_WAIT(&wqueue->lock) < 0);
|
2014-10-10 22:52:04 +02:00
|
|
|
|
2009-11-05 22:44:33 +01: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.
|
|
|
|
*/
|
|
|
|
|
2010-05-26 14:34:59 +02:00
|
|
|
if (work->worker != NULL)
|
|
|
|
{
|
2021-07-26 04:36:13 +02:00
|
|
|
/* Search the work activelist for the target work. We can't
|
|
|
|
* use sq_rem to do this because there are additional operations that
|
|
|
|
* need to be done.
|
|
|
|
*/
|
2012-09-02 15:37:28 +02:00
|
|
|
|
2021-07-26 04:36:13 +02:00
|
|
|
curr = wqueue->q.head;
|
|
|
|
while (curr && curr != &work->u.s.sq)
|
|
|
|
{
|
|
|
|
prev = curr;
|
|
|
|
curr = curr->flink;
|
|
|
|
}
|
2010-05-26 14:34:59 +02:00
|
|
|
|
2021-07-26 04:36:13 +02:00
|
|
|
/* Check if the work was found in the list. If not, then an OS
|
|
|
|
* error has occurred because the work is marked active!
|
2012-09-02 15:37:28 +02:00
|
|
|
*/
|
|
|
|
|
2021-07-26 04:36:13 +02:00
|
|
|
DEBUGASSERT(curr);
|
|
|
|
|
|
|
|
/* Now, remove the work from the work queue */
|
|
|
|
|
|
|
|
if (prev)
|
|
|
|
{
|
|
|
|
/* Remove the work from mid- or end-of-queue */
|
|
|
|
|
|
|
|
sq_remafter(prev, &wqueue->q);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Remove the work at the head of the queue */
|
|
|
|
|
|
|
|
sq_remfirst(&wqueue->q);
|
|
|
|
_SEM_GETVALUE(&wqueue->wake, &semcount);
|
|
|
|
if (semcount < 1)
|
|
|
|
{
|
|
|
|
_SEM_POST(&wqueue->wake);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-26 14:34:59 +02:00
|
|
|
work->worker = NULL;
|
2014-10-05 16:22:39 +02:00
|
|
|
ret = OK;
|
2010-05-26 14:34:59 +02:00
|
|
|
}
|
2012-07-14 21:30:31 +02:00
|
|
|
|
2021-07-12 14:58:03 +02:00
|
|
|
_SEM_POST(&wqueue->lock);
|
2014-10-05 16:22:39 +02:00
|
|
|
return ret;
|
2009-11-05 22:44:33 +01:00
|
|
|
}
|
2012-09-04 02:54:09 +02:00
|
|
|
|
2014-10-10 22:52:04 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-10-10 16:35:58 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* Name: work_cancel
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Cancel previously queued user-mode work. This removes work from the
|
2021-07-05 23:19:30 +02:00
|
|
|
* user mode work queue. After work has been cancelled, it may be
|
|
|
|
* requeued by calling work_queue() again.
|
2014-10-10 16:35:58 +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 (must be USRWORK)
|
2021-07-05 23:19:30 +02:00
|
|
|
* work - The previously queued work structure to cancel
|
2014-10-10 16:35:58 +02:00
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Zero (OK) on success, a negated errno on failure. This error may be
|
|
|
|
* reported:
|
|
|
|
*
|
|
|
|
* -ENOENT - There is no such work queued.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int work_cancel(int qid, FAR struct work_s *work)
|
|
|
|
{
|
|
|
|
if (qid == USRWORK)
|
|
|
|
{
|
|
|
|
return work_qcancel(&g_usrwork, work);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-01 09:27:08 +02:00
|
|
|
#endif /* CONFIG_LIBC_USRWORK && !__KERNEL__ */
|