sequence1.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * File: sequence1.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. * - that unique thread sequence numbers are generated.
  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. * - analysis output on success.
  66. *
  67. * Assumptions:
  68. * -
  69. *
  70. * Pass Criteria:
  71. * - unique sequence numbers are generated for every new thread.
  72. *
  73. * Fail Criteria:
  74. * -
  75. */
  76. #include "test.h"
  77. /*
  78. */
  79. enum {
  80. NUMTHREADS = 10000
  81. };
  82. static long done = 0;
  83. /*
  84. * seqmap should have 1 in every element except [0]
  85. * Thread sequence numbers start at 1 and we will also
  86. * include this main thread so we need NUMTHREADS+2
  87. * elements.
  88. */
  89. static UINT64 seqmap[NUMTHREADS+2];
  90. void * func(void * arg)
  91. {
  92. sched_yield();
  93. seqmap[(int)pthread_getunique_np(pthread_self())] = 1;
  94. InterlockedIncrement(&done);
  95. return (void *) 0;
  96. }
  97. int
  98. main()
  99. {
  100. pthread_t t[NUMTHREADS];
  101. pthread_attr_t attr;
  102. int i;
  103. assert(pthread_attr_init(&attr) == 0);
  104. assert(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED) == 0);
  105. for (i = 0; i < NUMTHREADS+2; i++)
  106. {
  107. seqmap[i] = 0;
  108. }
  109. for (i = 0; i < NUMTHREADS; i++)
  110. {
  111. if (NUMTHREADS/2 == i)
  112. {
  113. /* Include this main thread, which will be an implicit pthread_t */
  114. seqmap[(int)pthread_getunique_np(pthread_self())] = 1;
  115. }
  116. assert(pthread_create(&t[i], &attr, func, NULL) == 0);
  117. }
  118. while (NUMTHREADS > InterlockedExchangeAdd((LPLONG)&done, 0L))
  119. Sleep(100);
  120. Sleep(100);
  121. assert(seqmap[0] == 0);
  122. for (i = 1; i < NUMTHREADS+2; i++)
  123. {
  124. assert(seqmap[i] == 1);
  125. }
  126. return 0;
  127. }