tsd1.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * tsd1.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 = OS_MAX_SIMUL_THREADS
  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. assert(*j == 2);
  102. thread_destroyed[j - accesscount] = 1;
  103. }
  104. static void
  105. setkey(void * arg)
  106. {
  107. int * j = (int *) arg;
  108. thread_set[j - accesscount] = 1;
  109. assert(*j == 0);
  110. assert(pthread_setspecific(key, arg) == 0);
  111. assert(pthread_setspecific(key, arg) == 0);
  112. assert(pthread_setspecific(key, arg) == 0);
  113. assert(pthread_getspecific(key) == arg);
  114. (*j)++;
  115. assert(*j == 1);
  116. }
  117. static void *
  118. mythread(void * arg)
  119. {
  120. (void) pthread_barrier_wait(&startBarrier);
  121. // Note that this test must go here, rather than
  122. // in setkey() above, as setkey() is called by the main
  123. // (non-pthread) thread, which has already been running
  124. // and thus may have a non-zero value for the TLS for
  125. // this key.
  126. assert(pthread_getspecific(key) == NULL);
  127. setkey(arg);
  128. return 0;
  129. /* Exiting the thread will call the key destructor. */
  130. }
  131. int pthread_test_tsd1()
  132. {
  133. int i;
  134. int fail = 0;
  135. pthread_t thread[NUM_THREADS];
  136. /* Just to avoid compiler warnings */
  137. thread_set[0] = thread_set[0];
  138. thread_destroyed[0] = thread_destroyed[0];
  139. assert(pthread_barrier_init(&startBarrier, NULL, NUM_THREADS/2) == 0);
  140. for (i = 1; i < NUM_THREADS/2; i++)
  141. {
  142. accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
  143. assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
  144. }
  145. /*
  146. * Here we test that existing threads will get a key created
  147. * for them.
  148. */
  149. assert(pthread_key_create(&key, destroy_key) == 0);
  150. (void) pthread_barrier_wait(&startBarrier);
  151. /*
  152. * Test main thread key.
  153. */
  154. accesscount[0] = 0;
  155. setkey((void *) &accesscount[0]);
  156. /*
  157. * Here we test that new threads will get a key created
  158. * for them.
  159. */
  160. for (i = NUM_THREADS/2; i < NUM_THREADS; i++)
  161. {
  162. accesscount[i] = thread_set[i] = thread_destroyed[i] = 0;
  163. assert(pthread_create(&thread[i], NULL, mythread, (void *)&accesscount[i]) == 0);
  164. }
  165. /*
  166. * Wait for all threads to complete.
  167. */
  168. for (i = 1; i < NUM_THREADS; i++)
  169. {
  170. int result = 0;
  171. assert(pthread_join(thread[i], (void **) &result) == 0);
  172. }
  173. // assert(pthread_key_delete(key) == 0);
  174. assert(pthread_barrier_destroy(&startBarrier) == 0);
  175. for (i = 1; i < NUM_THREADS; i++)
  176. {
  177. /*
  178. * The counter is incremented once when the key is set to
  179. * a value, and again when the key is destroyed. If the key
  180. * doesn't get set for some reason then it will still be
  181. * NULL and the destroy function will not be called, and
  182. * hence accesscount will not equal 2.
  183. */
  184. if (accesscount[i] != 2)
  185. {
  186. fail++;
  187. }
  188. }
  189. assert(fail == 0);
  190. return (fail);
  191. }