udp_notifier.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /****************************************************************************
  2. * net/udp/udp_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 "udp/udp.h"
  44. #ifdef CONFIG_UDP_READAHEAD_NOTIFIER
  45. /****************************************************************************
  46. * Public Functions
  47. ****************************************************************************/
  48. /****************************************************************************
  49. * Name: udp_notifier_setup
  50. *
  51. * Description:
  52. * Set up to perform a callback to the worker function when an UDP data
  53. * is added to the read-ahead buffer. The worker function will execute
  54. * on the low priority worker thread.
  55. *
  56. * Input Parameters:
  57. * worker - The worker function to execute on the low priority work
  58. * queue when data is available in the UDP read-ahead buffer.
  59. * conn - The UDP connection where read-ahead data is needed.
  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 udp_notifier_teardown().
  66. * == 0 - There is already buffered read-ahead data. No notification
  67. * will be provided.
  68. * < 0 - An unexpected error occurred and notification will occur. The
  69. * returned value is a negated errno value that indicates the
  70. * nature of the failure.
  71. *
  72. ****************************************************************************/
  73. int udp_notifier_setup(worker_t worker, FAR struct udp_conn_s *conn,
  74. FAR void *arg)
  75. {
  76. struct work_notifier_s info;
  77. DEBUGASSERT(worker != NULL);
  78. /* If there is already buffered read-ahead data, then return zero without
  79. * setting up the notification.
  80. */
  81. if (conn->readahead.qh_head != NULL)
  82. {
  83. return 0;
  84. }
  85. /* Otherwise, this is just a simple wrapper around work_notifer_setup(). */
  86. info.evtype = WORK_UDP_READAHEAD;
  87. info.qid = LPWORK;
  88. info.qualifier = conn;
  89. info.arg = arg;
  90. info.worker = worker;
  91. return work_notifier_setup(&info);
  92. }
  93. /****************************************************************************
  94. * Name: udp_notifier_teardown
  95. *
  96. * Description:
  97. * Eliminate a UDP read-ahead notification previously setup by
  98. * udp_notifier_setup(). This function should only be called if the
  99. * notification should be aborted prior to the notification. The
  100. * notification will automatically be torn down after the notification.
  101. *
  102. * Input Parameters:
  103. * key - The key value returned from a previous call to
  104. * udp_notifier_setup().
  105. *
  106. * Returned Value:
  107. * Zero (OK) is returned on success; a negated errno value is returned on
  108. * any failure.
  109. *
  110. ****************************************************************************/
  111. int udp_notifier_teardown(int key)
  112. {
  113. /* This is just a simple wrapper around work_notifier_teardown(). */
  114. return work_notifier_teardown(key);
  115. }
  116. /****************************************************************************
  117. * Name: udp_notifier_signal
  118. *
  119. * Description:
  120. * Read-ahead data has been buffered. Notify all threads waiting for
  121. * read-ahead data to become available.
  122. *
  123. * When read-ahead data becomes available, *all* of the workers waiting
  124. * for read-ahead data will be executed. If there are multiple workers
  125. * waiting for read-ahead data then only the first to execute will get the
  126. * data. Others will need to call udp_notifier_setup() once again.
  127. *
  128. * Input Parameters:
  129. * conn - The UDP connection where read-ahead data was just buffered.
  130. *
  131. * Returned Value:
  132. * None.
  133. *
  134. ****************************************************************************/
  135. void udp_notifier_signal(FAR struct udp_conn_s *conn)
  136. {
  137. /* This is just a simple wrapper around work_notifier_signal(). */
  138. return work_notifier_signal(WORK_UDP_READAHEAD, conn);
  139. }
  140. #endif /* CONFIG_UDP_READAHEAD_NOTIFIER */