tst-tls2.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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, write to the Free
  14. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 USA. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <signal.h>
  19. #include <semaphore.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <tls.h>
  24. #if HAVE___THREAD
  25. #define N 10
  26. static pthread_t th[N];
  27. #define CB(n) \
  28. static void \
  29. cb##n (void) \
  30. { \
  31. if (th[n] != pthread_self ()) \
  32. { \
  33. write (STDOUT_FILENO, "wrong callback\n", 15); \
  34. _exit (1); \
  35. } \
  36. }
  37. CB (0)
  38. CB (1)
  39. CB (2)
  40. CB (3)
  41. CB (4)
  42. CB (5)
  43. CB (6)
  44. CB (7)
  45. CB (8)
  46. CB (9)
  47. static void (*cbs[]) (void) =
  48. {
  49. cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
  50. };
  51. static __thread void (*fp) (void) __attribute__ ((tls_model ("local-exec")));
  52. static sem_t s;
  53. #define THE_SIG SIGUSR1
  54. static void
  55. handler (int sig)
  56. {
  57. if (sig != THE_SIG)
  58. {
  59. write (STDOUT_FILENO, "wrong signal\n", 13);
  60. _exit (1);
  61. }
  62. fp ();
  63. if (sem_post (&s) != 0)
  64. {
  65. write (STDOUT_FILENO, "sem_post failed\n", 16);
  66. _exit (1);
  67. }
  68. }
  69. static pthread_barrier_t b;
  70. #define TOTAL_SIGS 1000
  71. static int nsigs;
  72. static void *
  73. tf (void *arg)
  74. {
  75. fp = arg;
  76. pthread_barrier_wait (&b);
  77. pthread_barrier_wait (&b);
  78. if (nsigs != TOTAL_SIGS)
  79. {
  80. puts ("barrier_wait prematurely returns");
  81. exit (1);
  82. }
  83. return NULL;
  84. }
  85. #endif
  86. int
  87. do_test (void)
  88. {
  89. #if !HAVE___THREAD
  90. puts ("No __thread support in compiler, test skipped.");
  91. return 0;
  92. #else
  93. if (pthread_barrier_init (&b, NULL, N + 1) != 0)
  94. {
  95. puts ("barrier_init failed");
  96. exit (1);
  97. }
  98. if (sem_init (&s, 0, 0) != 0)
  99. {
  100. puts ("sem_init failed");
  101. exit (1);
  102. }
  103. struct sigaction sa;
  104. sa.sa_handler = handler;
  105. sigemptyset (&sa.sa_mask);
  106. sa.sa_flags = 0;
  107. if (sigaction (THE_SIG, &sa, NULL) != 0)
  108. {
  109. puts ("sigaction failed");
  110. exit (1);
  111. }
  112. pthread_attr_t a;
  113. if (pthread_attr_init (&a) != 0)
  114. {
  115. puts ("attr_init failed");
  116. exit (1);
  117. }
  118. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  119. {
  120. puts ("attr_setstacksize failed");
  121. return 1;
  122. }
  123. int i;
  124. for (i = 0; i < N; ++i)
  125. if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
  126. {
  127. puts ("pthread_create failed");
  128. exit (1);
  129. }
  130. if (pthread_attr_destroy (&a) != 0)
  131. {
  132. puts ("attr_destroy failed");
  133. exit (1);
  134. }
  135. pthread_barrier_wait (&b);
  136. sigset_t ss;
  137. sigemptyset (&ss);
  138. sigaddset (&ss, THE_SIG);
  139. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  140. {
  141. puts ("pthread_sigmask failed");
  142. exit (1);
  143. }
  144. /* Start sending signals. */
  145. for (i = 0; i < TOTAL_SIGS; ++i)
  146. {
  147. if (kill (getpid (), THE_SIG) != 0)
  148. {
  149. puts ("kill failed");
  150. exit (1);
  151. }
  152. if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
  153. {
  154. puts ("sem_wait failed");
  155. exit (1);
  156. }
  157. ++nsigs;
  158. }
  159. pthread_barrier_wait (&b);
  160. for (i = 0; i < N; ++i)
  161. if (pthread_join (th[i], NULL) != 0)
  162. {
  163. puts ("join failed");
  164. exit (1);
  165. }
  166. return 0;
  167. #endif
  168. }
  169. #define TEST_FUNCTION do_test ()
  170. #include "../test-skeleton.c"