iob_notifier.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /****************************************************************************
  2. * mm/iob/iob_notifier.c
  3. *
  4. * Copyright (C) 2018 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 <sys/types.h>
  40. #include <assert.h>
  41. #include <nuttx/wqueue.h>
  42. #include <nuttx/mm/iob.h>
  43. #include "iob.h"
  44. #ifdef CONFIG_IOB_NOTIFIER
  45. /****************************************************************************
  46. * Public Functions
  47. ****************************************************************************/
  48. /****************************************************************************
  49. * Name: iob_notifier_setup
  50. *
  51. * Description:
  52. * Set up to perform a callback to the worker function when an IOB is
  53. * available. The worker function will execute on the selected priority
  54. * worker thread.
  55. *
  56. * Input Parameters:
  57. * qid - Selects work queue. Must be HPWORK or LPWORK.
  58. * worker - The worker function to execute on the high priority work queue
  59. * when the event occurs.
  60. * arg - A user-defined argument that will be available to the worker
  61. * function when it runs.
  62. *
  63. * Returned Value:
  64. * > 0 - The notification is in place. The returned value is a key that
  65. * may be used later in a call to iob_notifier_teardown().
  66. * == 0 - There are already free IOBs. No notification will be provided.
  67. * < 0 - An unexpected error occurred and notification will occur. The
  68. * returned value is a negated errno value that indicates the
  69. * nature of the failure.
  70. *
  71. ****************************************************************************/
  72. int iob_notifier_setup(int qid, worker_t worker, FAR void *arg)
  73. {
  74. struct work_notifier_s info;
  75. DEBUGASSERT(worker != NULL);
  76. info.evtype = WORK_IOB_AVAIL;
  77. info.qid = qid;
  78. info.qualifier = NULL;
  79. info.arg = arg;
  80. info.worker = worker;
  81. return work_notifier_setup(&info);
  82. }
  83. /****************************************************************************
  84. * Name: iob_notifier_teardown
  85. *
  86. * Description:
  87. * Eliminate an IOB notification previously setup by iob_notifier_setup().
  88. * This function should only be called if the notification should be
  89. * aborted prior to the notification. The notification will automatically
  90. * be torn down after the notification.
  91. *
  92. * Input Parameters:
  93. * key - The key value returned from a previous call to
  94. * iob_notifier_setup().
  95. *
  96. * Returned Value:
  97. * Zero (OK) is returned on success; a negated errno value is returned on
  98. * any failure.
  99. *
  100. ****************************************************************************/
  101. int iob_notifier_teardown(int key)
  102. {
  103. /* This is just a simple wrapper around work_notifier_teardown(). */
  104. return work_notifier_teardown(key);
  105. }
  106. /****************************************************************************
  107. * Name: iob_notifier_signal
  108. *
  109. * Description:
  110. * An IOB has become available. Signal all threads waiting for an IOB
  111. * that an IOB is available.
  112. *
  113. * When an IOB becomes available, *all* of the workers waiting for an
  114. * IOB will be executed. If there are multiple workers waiting for an IOB
  115. * then only the first to execute will get the IOB. Others will
  116. * need to call iob_notify_setup() once again.
  117. *
  118. * Input Parameters:
  119. * None.
  120. *
  121. * Returned Value:
  122. * None.
  123. *
  124. ****************************************************************************/
  125. void iob_notifier_signal(void)
  126. {
  127. /* This is just a simple wrapper around work_notifier_signal(). */
  128. return work_notifier_signal(WORK_IOB_AVAIL, NULL);
  129. }
  130. #endif /* CONFIG_IOB_NOTIFIER */