tst-tls2.c 3.7 KB

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