tsd2.c 5.4 KB

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