kwork_lpthread.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /****************************************************************************
  2. * sched/wqueue/work_lpthread.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 <unistd.h>
  25. #include <sched.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <queue.h>
  29. #include <debug.h>
  30. #include <nuttx/wqueue.h>
  31. #include <nuttx/kthread.h>
  32. #include <nuttx/kmalloc.h>
  33. #include <nuttx/clock.h>
  34. #include "wqueue/wqueue.h"
  35. #ifdef CONFIG_SCHED_LPWORK
  36. /****************************************************************************
  37. * Public Data
  38. ****************************************************************************/
  39. /* The state of the kernel mode, low priority work queue(s). */
  40. struct lp_wqueue_s g_lpwork;
  41. /****************************************************************************
  42. * Private Functions
  43. ****************************************************************************/
  44. /****************************************************************************
  45. * Name: work_lpthread
  46. *
  47. * Description:
  48. * These are the worker thread(s) that performs the actions placed on the
  49. * low priority work queue.
  50. *
  51. * These, along with the higher priority worker thread are the kernel mode
  52. * work queues (also build in the flat build).
  53. *
  54. * All kernel mode worker threads are started by the OS during normal
  55. * bring up. This entry point is referenced by OS internally and should
  56. * not be accessed by application logic.
  57. *
  58. * Input Parameters:
  59. * argc, argv (not used)
  60. *
  61. * Returned Value:
  62. * Does not return
  63. *
  64. ****************************************************************************/
  65. static int work_lpthread(int argc, char *argv[])
  66. {
  67. int wndx = 0;
  68. #if CONFIG_SCHED_LPNTHREADS > 1
  69. pid_t me = getpid();
  70. int i;
  71. /* Find out thread index by search the workers in g_lpwork */
  72. for (wndx = 0, i = 0; i < CONFIG_SCHED_LPNTHREADS; i++)
  73. {
  74. if (g_lpwork.worker[i].pid == me)
  75. {
  76. wndx = i;
  77. break;
  78. }
  79. }
  80. DEBUGASSERT(i < CONFIG_SCHED_LPNTHREADS);
  81. #endif
  82. /* Loop forever */
  83. for (; ; )
  84. {
  85. /* Then process queued work. work_process will not return until:
  86. * (1) there is no further work in the work queue, and (2) signal is
  87. * triggered, or delayed work expires.
  88. */
  89. work_process((FAR struct kwork_wqueue_s *)&g_lpwork, wndx);
  90. }
  91. return OK; /* To keep some compilers happy */
  92. }
  93. /****************************************************************************
  94. * Public Functions
  95. ****************************************************************************/
  96. /****************************************************************************
  97. * Name: work_start_lowpri
  98. *
  99. * Description:
  100. * Start the low-priority, kernel-mode worker thread(s)
  101. *
  102. * Input Parameters:
  103. * None
  104. *
  105. * Returned Value:
  106. * The task ID of the worker thread is returned on success. A negated
  107. * errno value is returned on failure.
  108. *
  109. ****************************************************************************/
  110. int work_start_lowpri(void)
  111. {
  112. pid_t pid;
  113. int wndx;
  114. /* Don't permit any of the threads to run until we have fully initialized
  115. * g_lpwork.
  116. */
  117. sched_lock();
  118. /* Start the low-priority, kernel mode worker thread(s) */
  119. sinfo("Starting low-priority kernel worker thread(s)\n");
  120. for (wndx = 0; wndx < CONFIG_SCHED_LPNTHREADS; wndx++)
  121. {
  122. pid = kthread_create(LPWORKNAME, CONFIG_SCHED_LPWORKPRIORITY,
  123. CONFIG_SCHED_LPWORKSTACKSIZE,
  124. (main_t)work_lpthread,
  125. (FAR char * const *)NULL);
  126. DEBUGASSERT(pid > 0);
  127. if (pid < 0)
  128. {
  129. serr("ERROR: kthread_create %d failed: %d\n", wndx, (int)pid);
  130. sched_unlock();
  131. return (int)pid;
  132. }
  133. g_lpwork.worker[wndx].pid = pid;
  134. g_lpwork.worker[wndx].busy = true;
  135. }
  136. sched_unlock();
  137. return g_lpwork.worker[0].pid;
  138. }
  139. #endif /* CONFIG_SCHED_LPWORK */