group_killchildren.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. * sched/group/group_killchildren.c
  3. *
  4. * Copyright (C) 2013, 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 <sched.h>
  42. #include <pthread.h>
  43. #include <nuttx/sched.h>
  44. #include "sched/sched.h"
  45. #include "group/group.h"
  46. #ifdef HAVE_GROUP_MEMBERS
  47. /****************************************************************************
  48. * Private Functions
  49. ****************************************************************************/
  50. /****************************************************************************
  51. * Name: group_killchildren_handler
  52. *
  53. * Description:
  54. * Callback from group_foreachchild that handles one member of the group.
  55. *
  56. * Input Parameters:
  57. * pid - The ID of the group member that may be signaled.
  58. * arg - The PID of the thread to be retained.
  59. *
  60. * Returned Value:
  61. * 0 (OK) always
  62. *
  63. ****************************************************************************/
  64. static int group_killchildren_handler(pid_t pid, FAR void *arg)
  65. {
  66. FAR struct tcb_s *rtcb;
  67. int ret;
  68. /* Cancel all threads except for the one specified by the argument */
  69. if (pid != (pid_t)((uintptr_t)arg))
  70. {
  71. /* Cancel this thread. This is a forced cancellation. Make sure that
  72. * cancellation is not disabled by the task/thread. That bit will
  73. * prevent pthread_cancel() or task_delete() from doing what they need
  74. * to do.
  75. */
  76. rtcb = sched_gettcb(pid);
  77. if (rtcb != NULL)
  78. {
  79. /* This is a forced cancellation. Make sure that cancellation is
  80. * not disabled by the task/thread. That bit would prevent
  81. * pthread_cancel() or task_delete() from doing what they need
  82. * to do.
  83. */
  84. rtcb->flags &= ~TCB_FLAG_NONCANCELABLE;
  85. /* 'pid' could refer to the main task of the thread. That pid
  86. * will appear in the group member list as well!
  87. */
  88. if ((rtcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_PTHREAD)
  89. {
  90. ret = pthread_cancel(pid);
  91. }
  92. else
  93. {
  94. ret = task_delete(pid);
  95. }
  96. if (ret < 0)
  97. {
  98. serr("ERROR: Failed to kill %d: %d\n", ret, pid);
  99. }
  100. }
  101. }
  102. /* Always return zero. We need to visit each member of the group*/
  103. return OK;
  104. }
  105. /****************************************************************************
  106. * Public Functions
  107. ****************************************************************************/
  108. /****************************************************************************
  109. * Name: group_killchildren
  110. *
  111. * Description:
  112. * Delete all children of a task except for the specified task. This is
  113. * used by the task restart logic and by the default signal handling
  114. * abnormal termination logic. When the main task is restarted or killed,
  115. * all of its child pthreads must be terminated.
  116. *
  117. * Input Parameters:
  118. * tcb - TCB of the task to be retained.
  119. *
  120. * Returned Value:
  121. * None
  122. *
  123. ****************************************************************************/
  124. int group_killchildren(FAR struct task_tcb_s *tcb)
  125. {
  126. int ret;
  127. /* Lock the scheduler so that there this thread will not lose priority
  128. * until all of its children are suspended.
  129. */
  130. sched_lock();
  131. ret = group_foreachchild(tcb->cmn.group, group_killchildren_handler,
  132. (FAR void *)((uintptr_t)tcb->cmn.pid));
  133. sched_unlock();
  134. return ret;
  135. }
  136. #endif /* HAVE_GROUP_MEMBERS */