pthread_completejoin.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /****************************************************************************
  2. * sched/pthread/pthread_completejoin.c
  3. *
  4. * Copyright (C) 2007, 2009, 2011, 2013 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 <stdbool.h>
  41. #include <pthread.h>
  42. #include <errno.h>
  43. #include <debug.h>
  44. #include "sched/sched.h"
  45. #include "group/group.h"
  46. #include "pthread/pthread.h"
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: pthread_notifywaiters
  52. *
  53. * Description:
  54. * Notify all other threads waiting in phread join for this thread's
  55. * exit data. This must be done by the child at child thread
  56. * destruction time.
  57. *
  58. ****************************************************************************/
  59. static bool pthread_notifywaiters(FAR struct join_s *pjoin)
  60. {
  61. int ntasks_waiting;
  62. int status;
  63. sinfo("pjoin=0x%p\n", pjoin);
  64. /* Are any tasks waiting for our exit value? */
  65. status = nxsem_getvalue(&pjoin->exit_sem, &ntasks_waiting);
  66. if (status == OK && ntasks_waiting < 0)
  67. {
  68. /* Set the data semaphore so that this thread will be
  69. * awakened when all waiting tasks receive the data
  70. */
  71. (void)nxsem_init(&pjoin->data_sem, 0, (ntasks_waiting + 1));
  72. /* Post the semaphore to restart each thread that is waiting
  73. * on the semaphore
  74. */
  75. do
  76. {
  77. status = pthread_sem_give(&pjoin->exit_sem);
  78. if (status == OK)
  79. {
  80. status = nxsem_getvalue(&pjoin->exit_sem, &ntasks_waiting);
  81. }
  82. }
  83. while (ntasks_waiting < 0 && status == OK);
  84. /* Now wait for all these restarted tasks to obtain the return
  85. * value.
  86. */
  87. (void)pthread_sem_take(&pjoin->data_sem, false);
  88. return true;
  89. }
  90. return false;
  91. }
  92. /****************************************************************************
  93. * Name: pthread_removejoininfo
  94. *
  95. * Description:
  96. * Remove a join structure from the local data set.
  97. *
  98. * Input Parameters:
  99. * pid
  100. *
  101. * Returned Value:
  102. * None.
  103. *
  104. * Assumptions:
  105. * The caller has provided protection from re-entrancy.
  106. *
  107. ****************************************************************************/
  108. static void pthread_removejoininfo(FAR struct task_group_s *group,
  109. pid_t pid)
  110. {
  111. FAR struct join_s *prev;
  112. FAR struct join_s *join;
  113. /* Find the entry with the matching pid */
  114. for (prev = NULL, join = group->tg_joinhead;
  115. (join && (pid_t)join->thread != pid);
  116. prev = join, join = join->next);
  117. /* Remove it from the data set. */
  118. /* First check if this is the entry at the head of the list. */
  119. if (join)
  120. {
  121. if (!prev)
  122. {
  123. /* Check if this is the only entry in the list */
  124. if (!join->next)
  125. {
  126. group->tg_joinhead = NULL;
  127. group->tg_jointail = NULL;
  128. }
  129. /* Otherwise, remove it from the head of the list */
  130. else
  131. {
  132. group->tg_joinhead = join->next;
  133. }
  134. }
  135. /* It is not at the head of the list, check if it is at the tail. */
  136. else if (!join->next)
  137. {
  138. group->tg_jointail = prev;
  139. prev->next = NULL;
  140. }
  141. /* No, remove it from the middle of the list. */
  142. else
  143. {
  144. prev->next = join->next;
  145. }
  146. }
  147. }
  148. /****************************************************************************
  149. * Public Functions
  150. ****************************************************************************/
  151. /****************************************************************************
  152. * Name: pthread_completejoin
  153. *
  154. * Description:
  155. * A thread has been terminated -- either by returning, calling
  156. * pthread_exit(), or through pthread_cancel(). In any event, we must
  157. * complete any pending join events.
  158. *
  159. * Input Parameters:
  160. * exit_value
  161. *
  162. * Returned Value:
  163. * OK unless there is no join information associated with the pid.
  164. * This could happen, for example, if a task started with task_create()
  165. * calls pthread_exit().
  166. *
  167. * Assumptions:
  168. *
  169. ****************************************************************************/
  170. int pthread_completejoin(pid_t pid, FAR void *exit_value)
  171. {
  172. FAR struct task_group_s *group = task_getgroup(pid);
  173. FAR struct join_s *pjoin;
  174. sinfo("pid=%d exit_value=%p group=%p\n", pid, exit_value, group);
  175. DEBUGASSERT(group);
  176. /* First, find thread's structure in the private data set. */
  177. (void)pthread_sem_take(&group->tg_joinsem, false);
  178. pjoin = pthread_findjoininfo(group, pid);
  179. if (!pjoin)
  180. {
  181. serr("ERROR: Could not find join info, pid=%d\n", pid);
  182. (void)pthread_sem_give(&group->tg_joinsem);
  183. return ERROR;
  184. }
  185. else
  186. {
  187. bool waiters;
  188. /* Save the return exit value in the thread structure. */
  189. pjoin->terminated = true;
  190. pjoin->exit_value = exit_value;
  191. /* Notify waiters of the availability of the exit value */
  192. waiters = pthread_notifywaiters(pjoin);
  193. /* If there are no waiters and if the thread is marked as detached.
  194. * then discard the join information now. Otherwise, the pthread
  195. * join logic will call pthread_destroyjoin() when all of the threads
  196. * have sampled the exit value.
  197. */
  198. if (!waiters && pjoin->detached)
  199. {
  200. pthread_destroyjoin(group, pjoin);
  201. }
  202. /* Giving the following semaphore will allow the waiters
  203. * to call pthread_destroyjoin.
  204. */
  205. (void)pthread_sem_give(&group->tg_joinsem);
  206. }
  207. return OK;
  208. }
  209. /****************************************************************************
  210. * Name: pthread_destroyjoin
  211. *
  212. * Description:
  213. * This is called from pthread_completejoin if the join info was
  214. * detached or from pthread_join when the last waiting thread has
  215. * received the thread exit info.
  216. *
  217. * Or it may never be called if the join info was never detached or if
  218. * no thread ever calls pthread_join. In case, there is a memory leak!
  219. *
  220. * Assumptions:
  221. * The caller holds tg_joinsem
  222. *
  223. ****************************************************************************/
  224. void pthread_destroyjoin(FAR struct task_group_s *group,
  225. FAR struct join_s *pjoin)
  226. {
  227. sinfo("pjoin=0x%p\n", pjoin);
  228. /* Remove the join info from the set of joins */
  229. pthread_removejoininfo(group, (pid_t)pjoin->thread);
  230. /* Destroy its semaphores */
  231. (void)nxsem_destroy(&pjoin->data_sem);
  232. (void)nxsem_destroy(&pjoin->exit_sem);
  233. /* And deallocate the pjoin structure */
  234. sched_kfree(pjoin);
  235. }