reuse2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * File: reuse2.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  8. * Copyright(C) 2008 Jason Schmidlapp
  9. *
  10. * Contact Email: jschmidlapp@users.sourceforge.net
  11. *
  12. *
  13. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  14. * Copyright(C) 1998 John E. Bossom
  15. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  16. *
  17. * Contact Email: rpj@callisto.canberra.edu.au
  18. *
  19. * The original list of contributors to the Pthreads-win32 project
  20. * is contained in the file CONTRIBUTORS.ptw32 included with the
  21. * source code distribution. The list can also be seen at the
  22. * following World Wide Web location:
  23. * http://sources.redhat.com/pthreads-win32/contributors.html
  24. *
  25. * This library is free software; you can redistribute it and/or
  26. * modify it under the terms of the GNU Lesser General Public
  27. * License as published by the Free Software Foundation; either
  28. * version 2 of the License, or (at your option) any later version.
  29. *
  30. * This library is distributed in the hope that it will be useful,
  31. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  32. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  33. * Lesser General Public License for more details.
  34. *
  35. * You should have received a copy of the GNU Lesser General Public
  36. * License along with this library in the file COPYING.LIB;
  37. * if not, write to the Free Software Foundation, Inc.,
  38. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  39. *
  40. * --------------------------------------------------------------------------
  41. *
  42. * Test Synopsis:
  43. * - Test that thread reuse works for detached threads.
  44. * - Analyse thread struct reuse.
  45. *
  46. * Test Method (Validation or Falsification):
  47. * -
  48. *
  49. * Requirements Tested:
  50. * -
  51. *
  52. * Features Tested:
  53. * -
  54. *
  55. * Cases Tested:
  56. * -
  57. *
  58. * Description:
  59. * -
  60. *
  61. * Environment:
  62. * - This test is implementation specific
  63. * because it uses knowledge of internals that should be
  64. * opaque to an application.
  65. *
  66. * Input:
  67. * - None.
  68. *
  69. * Output:
  70. * - File name, Line number, and failed expression on failure.
  71. * - No output on success.
  72. *
  73. * Assumptions:
  74. * -
  75. *
  76. * Pass Criteria:
  77. * - Process returns zero exit status.
  78. *
  79. * Fail Criteria:
  80. * - Process returns non-zero exit status.
  81. */
  82. #include "test.h"
  83. #include "implement.h"
  84. /*
  85. */
  86. enum
  87. {
  88. NUMTHREADS = 100
  89. };
  90. static int done = 0;
  91. static void * func(void * arg)
  92. {
  93. sched_yield();
  94. PTE_ATOMIC_INCREMENT(&done);
  95. return (void *) 0;
  96. }
  97. int pthread_test_reuse2()
  98. {
  99. pthread_t t[NUMTHREADS];
  100. pthread_attr_t attr;
  101. int i;
  102. unsigned int notUnique = 0,
  103. totalHandles = 0,
  104. reuseMax = 0,
  105. reuseMin = NUMTHREADS;
  106. assert(pthread_attr_init(&attr) == 0);
  107. assert(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
  108. for (i = 0; i < NUMTHREADS; i++)
  109. {
  110. assert(pthread_create(&t[i], &attr, func, NULL) == 0);
  111. }
  112. while (NUMTHREADS > PTE_ATOMIC_EXCHANGE_ADD(&done, 0L))
  113. pte_osThreadSleep(100);
  114. pte_osThreadSleep(100);
  115. /*
  116. * Analyse reuse by computing min and max number of times pthread_create()
  117. * returned the same pthread_t value.
  118. */
  119. for (i = 0; i < NUMTHREADS; i++)
  120. {
  121. if (t[i].p != NULL)
  122. {
  123. unsigned int j, thisMax;
  124. thisMax = t[i].x;
  125. for (j = i+1; j < NUMTHREADS; j++)
  126. if (t[i].p == t[j].p)
  127. {
  128. if (t[i].x == t[j].x)
  129. notUnique++;
  130. if (thisMax < t[j].x)
  131. thisMax = t[j].x;
  132. t[j].p = NULL;
  133. }
  134. if (reuseMin > thisMax)
  135. reuseMin = thisMax;
  136. if (reuseMax < thisMax)
  137. reuseMax = thisMax;
  138. }
  139. }
  140. for (i = 0; i < NUMTHREADS; i++)
  141. if (t[i].p != NULL)
  142. totalHandles++;
  143. /*
  144. * pthread_t reuse counts start at 0, so we need to add 1
  145. * to the max and min values derived above.
  146. */
  147. return 0;
  148. }