tst-tls2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* Copyright (C) 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <signal.h>
  18. #include <semaphore.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <tls.h>
  23. #if HAVE___THREAD
  24. #define N 10
  25. static pthread_t th[N];
  26. #define CB(n) \
  27. static void \
  28. cb##n (void) \
  29. { \
  30. if (th[n] != pthread_self ()) \
  31. { \
  32. write (STDOUT_FILENO, "wrong callback\n", 15); \
  33. _exit (1); \
  34. } \
  35. }
  36. CB (0)
  37. CB (1)
  38. CB (2)
  39. CB (3)
  40. CB (4)
  41. CB (5)
  42. CB (6)
  43. CB (7)
  44. CB (8)
  45. CB (9)
  46. static void (*cbs[]) (void) =
  47. {
  48. cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
  49. };
  50. static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
  51. static sem_t s;
  52. #define THE_SIG SIGUSR1
  53. static void
  54. handler (int sig)
  55. {
  56. if (sig != THE_SIG)
  57. {
  58. write (STDOUT_FILENO, "wrong signal\n", 13);
  59. _exit (1);
  60. }
  61. fp ();
  62. if (sem_post (&s) != 0)
  63. {
  64. write (STDOUT_FILENO, "sem_post failed\n", 16);
  65. _exit (1);
  66. }
  67. }
  68. static pthread_barrier_t b;
  69. #define TOTAL_SIGS 1000
  70. static int nsigs;
  71. static void *
  72. tf (void *arg)
  73. {
  74. fp = arg;
  75. pthread_barrier_wait (&b);
  76. pthread_barrier_wait (&b);
  77. if (nsigs != TOTAL_SIGS)
  78. {
  79. puts ("barrier_wait prematurely returns");
  80. exit (1);
  81. }
  82. return NULL;
  83. }
  84. #endif
  85. int
  86. do_test (void)
  87. {
  88. #if !HAVE___THREAD
  89. puts ("No __thread support in compiler, test skipped.");
  90. return 0;
  91. #else
  92. if (pthread_barrier_init (&b, NULL, N + 1) != 0)
  93. {
  94. puts ("barrier_init failed");
  95. exit (1);
  96. }
  97. if (sem_init (&s, 0, 0) != 0)
  98. {
  99. puts ("sem_init failed");
  100. exit (1);
  101. }
  102. struct sigaction sa;
  103. sa.sa_handler = handler;
  104. sigemptyset (&sa.sa_mask);
  105. sa.sa_flags = 0;
  106. if (sigaction (THE_SIG, &sa, NULL) != 0)
  107. {
  108. puts ("sigaction failed");
  109. exit (1);
  110. }
  111. pthread_attr_t a;
  112. if (pthread_attr_init (&a) != 0)
  113. {
  114. puts ("attr_init failed");
  115. exit (1);
  116. }
  117. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  118. {
  119. puts ("attr_setstacksize failed");
  120. return 1;
  121. }
  122. int i;
  123. for (i = 0; i < N; ++i)
  124. if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
  125. {
  126. puts ("pthread_create failed");
  127. exit (1);
  128. }
  129. if (pthread_attr_destroy (&a) != 0)
  130. {
  131. puts ("attr_destroy failed");
  132. exit (1);
  133. }
  134. pthread_barrier_wait (&b);
  135. sigset_t ss;
  136. sigemptyset (&ss);
  137. sigaddset (&ss, THE_SIG);
  138. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  139. {
  140. puts ("pthread_sigmask failed");
  141. exit (1);
  142. }
  143. /* Start sending signals. */
  144. for (i = 0; i < TOTAL_SIGS; ++i)
  145. {
  146. if (kill (getpid (), THE_SIG) != 0)
  147. {
  148. puts ("kill failed");
  149. exit (1);
  150. }
  151. if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
  152. {
  153. puts ("sem_wait failed");
  154. exit (1);
  155. }
  156. ++nsigs;
  157. }
  158. pthread_barrier_wait (&b);
  159. for (i = 0; i < N; ++i)
  160. if (pthread_join (th[i], NULL) != 0)
  161. {
  162. puts ("join failed");
  163. exit (1);
  164. }
  165. return 0;
  166. #endif
  167. }
  168. #define TEST_FUNCTION do_test ()
  169. #include "../test-skeleton.c"