kwork_cancel.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /****************************************************************************
  2. * sched/wqueue/kwork_cancel.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <queue.h>
  25. #include <assert.h>
  26. #include <errno.h>
  27. #include <nuttx/irq.h>
  28. #include <nuttx/arch.h>
  29. #include <nuttx/wqueue.h>
  30. #include "wqueue/wqueue.h"
  31. #ifdef CONFIG_SCHED_WORKQUEUE
  32. /****************************************************************************
  33. * Private Functions
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Name: work_qcancel
  37. *
  38. * Description:
  39. * Cancel previously queued work. This removes work from the work queue.
  40. * After work has been cancelled, it may be re-queue by calling
  41. * work_queue() again.
  42. *
  43. * Input Parameters:
  44. * qid - The work queue ID
  45. * work - The previously queue work structure to cancel
  46. *
  47. * Returned Value:
  48. * Zero (OK) on success, a negated errno on failure. This error may be
  49. * reported:
  50. *
  51. * -ENOENT - There is no such work queued.
  52. * -EINVAL - An invalid work queue was specified
  53. *
  54. ****************************************************************************/
  55. static int work_qcancel(FAR struct kwork_wqueue_s *wqueue,
  56. FAR struct work_s *work)
  57. {
  58. irqstate_t flags;
  59. int ret = -ENOENT;
  60. DEBUGASSERT(work != NULL);
  61. /* Cancelling the work is simply a matter of removing the work structure
  62. * from the work queue. This must be done with interrupts disabled because
  63. * new work is typically added to the work queue from interrupt handlers.
  64. */
  65. flags = enter_critical_section();
  66. if (work->worker != NULL)
  67. {
  68. /* A little test of the integrity of the work queue */
  69. DEBUGASSERT(work->dq.flink != NULL ||
  70. (FAR dq_entry_t *)work == wqueue->q.tail);
  71. DEBUGASSERT(work->dq.blink != NULL ||
  72. (FAR dq_entry_t *)work == wqueue->q.head);
  73. /* Remove the entry from the work queue and make sure that it is
  74. * marked as available (i.e., the worker field is nullified).
  75. */
  76. dq_rem((FAR dq_entry_t *)work, &wqueue->q);
  77. work->worker = NULL;
  78. ret = OK;
  79. }
  80. leave_critical_section(flags);
  81. return ret;
  82. }
  83. /****************************************************************************
  84. * Public Functions
  85. ****************************************************************************/
  86. /****************************************************************************
  87. * Name: work_cancel
  88. *
  89. * Description:
  90. * Cancel previously queued user-mode work. This removes work from the
  91. * user mode work queue. After work has been cancelled, it may be re-queue
  92. * by calling work_queue() again.
  93. *
  94. * Input Parameters:
  95. * qid - The work queue ID (must be HPWORK or LPWORK)
  96. * work - The previously queue work structure to cancel
  97. *
  98. * Returned Value:
  99. * Zero (OK) on success, a negated errno on failure. This error may be
  100. * reported:
  101. *
  102. * -ENOENT - There is no such work queued.
  103. * -EINVAL - An invalid work queue was specified
  104. *
  105. ****************************************************************************/
  106. int work_cancel(int qid, FAR struct work_s *work)
  107. {
  108. #ifdef CONFIG_SCHED_HPWORK
  109. if (qid == HPWORK)
  110. {
  111. /* Cancel high priority work */
  112. return work_qcancel((FAR struct kwork_wqueue_s *)&g_hpwork, work);
  113. }
  114. else
  115. #endif
  116. #ifdef CONFIG_SCHED_LPWORK
  117. if (qid == LPWORK)
  118. {
  119. /* Cancel low priority work */
  120. return work_qcancel((FAR struct kwork_wqueue_s *)&g_lpwork, work);
  121. }
  122. else
  123. #endif
  124. {
  125. return -EINVAL;
  126. }
  127. }
  128. #endif /* CONFIG_SCHED_WORKQUEUE */