kwork_cancel.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /****************************************************************************
  2. * sched/wqueue/kwork_cancel.c
  3. *
  4. * Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <queue.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <nuttx/irq.h>
  43. #include <nuttx/arch.h>
  44. #include <nuttx/wqueue.h>
  45. #include "wqueue/wqueue.h"
  46. #ifdef CONFIG_SCHED_WORKQUEUE
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: work_qcancel
  52. *
  53. * Description:
  54. * Cancel previously queued work. This removes work from the work queue.
  55. * After work has been cancelled, it may be re-queue by calling work_queue()
  56. * again.
  57. *
  58. * Input Parameters:
  59. * qid - The work queue ID
  60. * work - The previously queue work structure to cancel
  61. *
  62. * Returned Value:
  63. * Zero (OK) on success, a negated errno on failure. This error may be
  64. * reported:
  65. *
  66. * -ENOENT - There is no such work queued.
  67. * -EINVAL - An invalid work queue was specified
  68. *
  69. ****************************************************************************/
  70. static int work_qcancel(FAR struct kwork_wqueue_s *wqueue,
  71. FAR struct work_s *work)
  72. {
  73. irqstate_t flags;
  74. int ret = -ENOENT;
  75. DEBUGASSERT(work != NULL);
  76. /* Cancelling the work is simply a matter of removing the work structure
  77. * from the work queue. This must be done with interrupts disabled because
  78. * new work is typically added to the work queue from interrupt handlers.
  79. */
  80. flags = enter_critical_section();
  81. if (work->worker != NULL)
  82. {
  83. /* A little test of the integrity of the work queue */
  84. DEBUGASSERT(work->dq.flink != NULL ||
  85. (FAR dq_entry_t *)work == wqueue->q.tail);
  86. DEBUGASSERT(work->dq.blink != NULL ||
  87. (FAR dq_entry_t *)work == wqueue->q.head);
  88. /* Remove the entry from the work queue and make sure that it is
  89. * marked as available (i.e., the worker field is nullified).
  90. */
  91. dq_rem((FAR dq_entry_t *)work, &wqueue->q);
  92. work->worker = NULL;
  93. ret = OK;
  94. }
  95. leave_critical_section(flags);
  96. return ret;
  97. }
  98. /****************************************************************************
  99. * Public Functions
  100. ****************************************************************************/
  101. /****************************************************************************
  102. * Name: work_cancel
  103. *
  104. * Description:
  105. * Cancel previously queued user-mode work. This removes work from the
  106. * user mode work queue. After work has been cancelled, it may be re-queue
  107. * by calling work_queue() again.
  108. *
  109. * Input Parameters:
  110. * qid - The work queue ID (must be HPWORK or LPWORK)
  111. * work - The previously queue work structure to cancel
  112. *
  113. * Returned Value:
  114. * Zero (OK) on success, a negated errno on failure. This error may be
  115. * reported:
  116. *
  117. * -ENOENT - There is no such work queued.
  118. * -EINVAL - An invalid work queue was specified
  119. *
  120. ****************************************************************************/
  121. int work_cancel(int qid, FAR struct work_s *work)
  122. {
  123. #ifdef CONFIG_SCHED_HPWORK
  124. if (qid == HPWORK)
  125. {
  126. /* Cancel high priority work */
  127. return work_qcancel((FAR struct kwork_wqueue_s *)&g_hpwork, work);
  128. }
  129. else
  130. #endif
  131. #ifdef CONFIG_SCHED_LPWORK
  132. if (qid == LPWORK)
  133. {
  134. /* Cancel low priority work */
  135. return work_qcancel((FAR struct kwork_wqueue_s *)&g_lpwork, work);
  136. }
  137. else
  138. #endif
  139. {
  140. return -EINVAL;
  141. }
  142. }
  143. #endif /* CONFIG_SCHED_WORKQUEUE */