tsd2.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * tsd2.c
  3. *
  4. * Test Thread Specific Data (TSD) key creation and destruction.
  5. *
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  35. *
  36. *
  37. * --------------------------------------------------------------------------
  38. *
  39. * Description:
  40. * -
  41. *
  42. * Test Method (validation or falsification):
  43. * - validation
  44. *
  45. * Requirements Tested:
  46. * - keys are created for each existing thread including the main thread
  47. * - keys are created for newly created threads
  48. * - keys are thread specific
  49. * - destroy routine is called on each thread exit including the main thread
  50. *
  51. * Features Tested:
  52. * -
  53. *
  54. * Cases Tested:
  55. * -
  56. *
  57. * Environment:
  58. * -
  59. *
  60. * Input:
  61. * - none
  62. *
  63. * Output:
  64. * - text to stdout
  65. *
  66. * Assumptions:
  67. * - already validated: pthread_create()
  68. * pthread_once()
  69. * - main thread also has a POSIX thread identity
  70. *
  71. * Pass Criteria:
  72. * - stdout matches file reference/tsd1.out
  73. *
  74. * Fail Criteria:
  75. * - fails to match file reference/tsd1.out
  76. * - output identifies failed component
  77. */
  78. #include <sched.h>
  79. #include "test.h"
  80. enum {
  81. NUM_THREADS = 100
  82. };
  83. static pthread_key_t key = NULL;
  84. static int accesscount[NUM_THREADS];
  85. static int thread_set[NUM_THREADS];
  86. static int thread_destroyed[NUM_THREADS];
  87. static pthread_barrier_t startBarrier;
  88. static void
  89. destroy_key(void * arg)
  90. {
  91. int * j = (int *) arg;
  92. (*j)++;
  93. /* Set TSD key from the destructor to test destructor iteration */
  94. if (*j == 2)
  95. assert(pthread_setspecific(key, arg) == 0);
  96. else
  97. assert(*j == 3);
  98. thread_destroyed[j - accesscount] = 1;
  99. }
  100. static void
  101. setkey(void * arg)
  102. {
  103. int * j = (int *) arg;
  104. thread_set[j - accesscount] = 1;
  105. assert(*j == 0);
  106. assert(pthread_getspecific(key) == NULL);
  107. assert(pthread_setspecific(key, arg) == 0);
  108. assert(pthread_setspecific(key, arg) == 0);
  109. assert(pthread_setspecific(key, arg) == 0);
  110. assert(pthread_getspecific(key) == arg);
  111. (*j)++;
  112. assert(*j == 1);
  113. }
  114. static void *
  115. mythread(void * arg)
  116. {
  117. (void) pthread_barrier_wait(&startBarrier);
  118. setkey(arg);
  119. return 0;
  120. /* Exiting the thread will call the key destructor. */
  121. }
  122. int
  123. main()
  124. {
  125. int i;
  126. int fail = 0;
  127. pthread_t thread[NUM_THREADS];
  128. assert(pthread_barrier_init(&startBarrier, NULL, NUM_THREADS/2) == 0);
  129. for (i = 1; i < NUM_THREADS/2; i++)
  130. {
  131. accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
  132. assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
  133. }
  134. /*
  135. * Here we test that existing threads will get a key created
  136. * for them.
  137. */
  138. assert(pthread_key_create(&key, destroy_key) == 0);
  139. (void) pthread_barrier_wait(&startBarrier);
  140. /*
  141. * Test main thread key.
  142. */
  143. accesscount[0] = 0;
  144. setkey((void *) &accesscount[0]);
  145. /*
  146. * Here we test that new threads will get a key created
  147. * for them.
  148. */
  149. for (i = NUM_THREADS/2; i < NUM_THREADS; i++)
  150. {
  151. accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
  152. assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
  153. }
  154. /*
  155. * Wait for all threads to complete.
  156. */
  157. for (i = 1; i < NUM_THREADS; i++)
  158. {
  159. assert(pthread_join(thread[i], NULL) == 0);
  160. }
  161. assert(pthread_key_delete(key) == 0);
  162. assert(pthread_barrier_destroy(&startBarrier) == 0);
  163. for (i = 1; i < NUM_THREADS; i++)
  164. {
  165. /*
  166. * The counter is incremented once when the key is set to
  167. * a value, and again when the key is destroyed. If the key
  168. * doesn't get set for some reason then it will still be
  169. * NULL and the destroy function will not be called, and
  170. * hence accesscount will not equal 2.
  171. */
  172. if (accesscount[i] != 3)
  173. {
  174. fail++;
  175. fprintf(stderr, "Thread %d key, set = %d, destroyed = %d\n",
  176. i, thread_set[i], thread_destroyed[i]);
  177. }
  178. }
  179. fflush(stderr);
  180. return (fail);
  181. }