tst-tls2.c 3.7 KB

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