group_foreachchild.c 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /****************************************************************************
  2. * sched/group/group_foreachchild.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 <nuttx/sched.h>
  40. #include "group/group.h"
  41. #ifdef HAVE_GROUP_MEMBERS
  42. /****************************************************************************
  43. * Public Functions
  44. ****************************************************************************/
  45. /****************************************************************************
  46. * Name: group_foreachchild
  47. *
  48. * Description:
  49. * Execute a function for each child of a group.
  50. *
  51. * Input Parameters:
  52. * group - The group containing the children
  53. * handler - The function to be called
  54. * arg - An additional argument to provide to the handler
  55. *
  56. * Returned Value:
  57. * Success (OK) is always returned unless the handler returns a non-zero
  58. * value (a negated errno on errors). In that case, the traversal
  59. * terminates and that non-zero value is returned.
  60. *
  61. * Assumptions:
  62. *
  63. ****************************************************************************/
  64. int group_foreachchild(FAR struct task_group_s *group,
  65. foreachchild_t handler, FAR void *arg)
  66. {
  67. int ret;
  68. int i;
  69. DEBUGASSERT(group);
  70. /* Visit the main thread last (if present) */
  71. for (i = group->tg_nmembers - 1; i >= 0; i--)
  72. {
  73. ret = handler(group->tg_members[i], arg);
  74. if (ret != 0)
  75. {
  76. return ret;
  77. }
  78. }
  79. return 0;
  80. }
  81. #endif /* HAVE_GROUP_MEMBERS */