reuse2.c 4.0 KB

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