kwork_notifier.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /****************************************************************************
  2. * sched/wqueue/kwork_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 <stdint.h>
  41. #include <stdbool.h>
  42. #include <unistd.h>
  43. #include <string.h>
  44. #include <sched.h>
  45. #include <assert.h>
  46. #include <nuttx/kmalloc.h>
  47. #include <nuttx/semaphore.h>
  48. #include <nuttx/wqueue.h>
  49. #include "wqueue/wqueue.h"
  50. #ifdef CONFIG_WQUEUE_NOTIFIER
  51. /****************************************************************************
  52. * Private Data
  53. ****************************************************************************/
  54. /* This is a doubly linked list of pending notifications. When an event
  55. * occurs available, *all* of the waiters for that event in this list will
  56. * be notified and the entry will be freed. If there are multiple waiters
  57. * for some resource, then only the first to execute thread will get the
  58. * resource. Lower priority threads will need to call work_notifier_setup()
  59. * once again.
  60. */
  61. static dq_queue_t g_notifier_pending;
  62. /* This semaphore is used as mutex to enforce mutually exclusive access to
  63. * the notification data structures.
  64. */
  65. static sem_t g_notifier_sem = SEM_INITIALIZER(1);
  66. /* Used for lookup key generation */
  67. static uint16_t g_notifier_key;
  68. /****************************************************************************
  69. * Private Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: work_notifier_find
  73. *
  74. * Description:
  75. * Given a unique key for notification, find the corresponding notification
  76. * structure in the pending notification list.
  77. *
  78. ****************************************************************************/
  79. static FAR struct work_notifier_entry_s *work_notifier_find(int16_t key)
  80. {
  81. FAR struct work_notifier_entry_s *notifier;
  82. FAR dq_entry_t *entry;
  83. /* Find the entry matching this key in the g_notifier_pending list. */
  84. for (entry = dq_peek(&g_notifier_pending);
  85. entry != NULL;
  86. entry = dq_next(entry))
  87. {
  88. notifier = (FAR struct work_notifier_entry_s *)entry;
  89. /* Is this the one we were looking for? */
  90. if (notifier->key == key)
  91. {
  92. /* Yes.. return a reference to it */
  93. return notifier;
  94. }
  95. }
  96. return NULL;
  97. }
  98. /****************************************************************************
  99. * Name: work_notifier_key
  100. *
  101. * Description:
  102. * Generate a unique key for this notification.
  103. *
  104. ****************************************************************************/
  105. static int16_t work_notifier_key(void)
  106. {
  107. int16_t key;
  108. /* Loop until a unique key is generated. Range 1-INT16_MAX. */
  109. do
  110. {
  111. if (g_notifier_key >= INT16_MAX)
  112. {
  113. g_notifier_key = 0;
  114. }
  115. key = (int16_t)++g_notifier_key;
  116. }
  117. while (work_notifier_find(key) != NULL);
  118. return key;
  119. }
  120. /****************************************************************************
  121. * Public Functions
  122. ****************************************************************************/
  123. /****************************************************************************
  124. * Name: work_notifier_setup
  125. *
  126. * Description:
  127. * Set up to provide a notification when an event occurs.
  128. *
  129. * Input Parameters:
  130. * info - Describes the work notification.
  131. *
  132. * Returned Value:
  133. * > 0 - The key which may be used later in a call to
  134. * work_notifier_teardown().
  135. * == 0 - Not used (reserved for wrapper functions).
  136. * < 0 - An unexpected error occurred and no notification will be sent.
  137. * The returned value is a negated errno value that indicates the
  138. * nature of the failure.
  139. *
  140. ****************************************************************************/
  141. int work_notifier_setup(FAR struct work_notifier_s *info)
  142. {
  143. FAR struct work_notifier_entry_s *notifier;
  144. int ret;
  145. DEBUGASSERT(info != NULL && info->worker != NULL);
  146. DEBUGASSERT(info->qid == HPWORK || info->qid == LPWORK);
  147. /* Get exclusive access to the notifier data structures */
  148. ret = nxsem_wait(&g_notifier_sem);
  149. if (ret < 0)
  150. {
  151. return ret;
  152. }
  153. /* Allocate a new notification entry */
  154. notifier = kmm_malloc(sizeof(struct work_notifier_entry_s));
  155. if (notifier == NULL)
  156. {
  157. ret = -ENOMEM;
  158. }
  159. else
  160. {
  161. /* Duplicate the notification info */
  162. memcpy(&notifier->info, info, sizeof(struct work_notifier_s));
  163. /* Generate a unique key for this notification */
  164. notifier->key = work_notifier_key();
  165. /* Add the notification to the tail of the pending list
  166. *
  167. * REVISIT: Work will be processed in FIFO order. Perhaps
  168. * we should should consider saving the notification is the
  169. * order of the caller's execution priority so that the
  170. * notifications executed in a saner order?
  171. */
  172. dq_addlast((FAR dq_entry_t *)notifier, &g_notifier_pending);
  173. ret = work_notifier_key();
  174. }
  175. (void)nxsem_post(&g_notifier_sem);
  176. return ret;
  177. }
  178. /****************************************************************************
  179. * Name: work_notifier_teardown
  180. *
  181. * Description:
  182. * Eliminate a notification previously setup by work_notifier_setup().
  183. * This function should only be called if the notification should be
  184. * aborted prior to the notification. The notification will automatically
  185. * be torn down after the notification executes.
  186. *
  187. * Input Parameters:
  188. * key - The key value returned from a previous call to
  189. * work_notifier_setup().
  190. *
  191. * Returned Value:
  192. * Zero (OK) is returned on success; a negated errno value is returned on
  193. * any failure.
  194. *
  195. ****************************************************************************/
  196. int work_notifier_teardown(int key)
  197. {
  198. FAR struct work_notifier_entry_s *notifier;
  199. int ret;
  200. DEBUGASSERT(key > 0 && key <= INT16_MAX);
  201. /* Get exclusive access to the notifier data structures */
  202. ret = nxsem_wait(&g_notifier_sem);
  203. if (ret < 0)
  204. {
  205. return ret;
  206. }
  207. /* Find the entry matching this PID in the g_notifier_pending list. We
  208. * assume that there is only one.
  209. */
  210. notifier = work_notifier_find(key);
  211. if (notifier == NULL)
  212. {
  213. /* There is no notification with this key in the pending list */
  214. ret = -ENOENT;
  215. }
  216. else
  217. {
  218. /* Found it! Remove the notification from the pending list */
  219. dq_rem((FAR dq_entry_t *)notifier, &g_notifier_pending);
  220. /* Free the notification */
  221. kmm_free(notifier);
  222. ret = OK;
  223. }
  224. (void)nxsem_post(&g_notifier_sem);
  225. return ret;
  226. }
  227. /****************************************************************************
  228. * Name: work_notifier_signal
  229. *
  230. * Description:
  231. * An event has just occurred. Notify all threads waiting for that event.
  232. *
  233. * When an event of interest occurs, *all* of the workers waiting for this
  234. * event will be executed. If there are multiple workers for a resource
  235. * then only the first to execute will get the resource. Others will
  236. * need to call work_notifier_setup() once again.
  237. *
  238. * Input Parameters:
  239. * evtype - The type of the event that just occurred.
  240. * qualifier - Event qualifier to distinguish different cases of the
  241. * generic event type.
  242. *
  243. * Returned Value:
  244. * None.
  245. *
  246. ****************************************************************************/
  247. void work_notifier_signal(enum work_evtype_e evtype,
  248. FAR void *qualifier)
  249. {
  250. FAR struct work_notifier_entry_s *notifier;
  251. FAR dq_entry_t *entry;
  252. FAR dq_entry_t *next;
  253. int ret;
  254. /* Get exclusive access to the notifier data structure */
  255. ret = nxsem_wait(&g_notifier_sem);
  256. while (ret < 0)
  257. {
  258. DEBUGASSERT(ret == -EINTR || ret == -ECANCELED);
  259. }
  260. /* Don't let any newly started threads block this thread until all of
  261. * the notifications and been sent.
  262. */
  263. sched_lock();
  264. /* Process the notification at the head of the pending list until the
  265. * pending list is empty */
  266. /* Find the entry matching this key in the g_notifier_pending list. */
  267. for (entry = dq_peek(&g_notifier_pending);
  268. entry != NULL;
  269. entry = next)
  270. {
  271. FAR struct work_notifier_s *info;
  272. /* Set up for the next time through the loop (in case the entry is
  273. * removed from the list).
  274. */
  275. next = entry->flink;
  276. /* Set up some convenience pointers */
  277. notifier = (FAR struct work_notifier_entry_s *)entry;
  278. info = &notifier->info;
  279. /* Check if this is the a notification request for the event that
  280. * just occurred.
  281. */
  282. if (info->evtype == evtype && info->qualifier == qualifier)
  283. {
  284. /* Yes.. Remove the notification from the pending list */
  285. dq_rem((FAR dq_entry_t *)notifier, &g_notifier_pending);
  286. /* Schedule the work. The entire notifier entry is passed as an
  287. * argument to the work function because that function is
  288. * responsible for freeing the allocated memory.
  289. */
  290. (void)work_queue(info->qid, &notifier->work, info->worker,
  291. entry, 0);
  292. }
  293. }
  294. sched_unlock();
  295. (void)nxsem_post(&g_notifier_sem);
  296. }
  297. #endif /* CONFIG_WQUEUE_NOTIFIER */