tst-tls3.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 <dlfcn.h>
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <signal.h>
  19. #include <semaphore.h>
  20. #include <stdint.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <unistd.h>
  24. #define THE_SIG SIGUSR1
  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. sem_t s;
  52. pthread_barrier_t b;
  53. #define TOTAL_SIGS 1000
  54. int nsigs;
  55. int
  56. do_test (void)
  57. {
  58. #if !HAVE___THREAD
  59. puts ("No __thread support in compiler, test skipped.");
  60. return 0;
  61. #else
  62. if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
  63. {
  64. puts ("initial thread's struct pthread not aligned enough");
  65. exit (1);
  66. }
  67. if (pthread_barrier_init (&b, NULL, N + 1) != 0)
  68. {
  69. puts ("barrier_init failed");
  70. exit (1);
  71. }
  72. if (sem_init (&s, 0, 0) != 0)
  73. {
  74. puts ("sem_init failed");
  75. exit (1);
  76. }
  77. void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
  78. if (h == NULL)
  79. {
  80. puts ("dlopen failed");
  81. exit (1);
  82. }
  83. void *(*tf) (void *) = dlsym (h, "tf");
  84. if (tf == NULL)
  85. {
  86. puts ("dlsym for tf failed");
  87. exit (1);
  88. }
  89. void (*setup_tf) (pthread_barrier_t*, int*, sem_t*) = dlsym(h, "setup_tf");
  90. if (setup_tf == NULL)
  91. {
  92. puts ("dlsym for setup_tf failed");
  93. exit(1);
  94. }
  95. setup_tf (&b, &nsigs, &s);
  96. struct sigaction sa;
  97. sa.sa_handler = dlsym (h, "handler");
  98. if (sa.sa_handler == NULL)
  99. {
  100. puts ("dlsym for handler failed");
  101. exit (1);
  102. }
  103. sigemptyset (&sa.sa_mask);
  104. sa.sa_flags = 0;
  105. if (sigaction (THE_SIG, &sa, NULL) != 0)
  106. {
  107. puts ("sigaction failed");
  108. exit (1);
  109. }
  110. pthread_attr_t a;
  111. if (pthread_attr_init (&a) != 0)
  112. {
  113. puts ("attr_init failed");
  114. exit (1);
  115. }
  116. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  117. {
  118. puts ("attr_setstacksize failed");
  119. return 1;
  120. }
  121. int r;
  122. for (r = 0; r < 10; ++r)
  123. {
  124. int i;
  125. for (i = 0; i < N; ++i)
  126. if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
  127. {
  128. puts ("pthread_create failed");
  129. exit (1);
  130. }
  131. nsigs = 0;
  132. pthread_barrier_wait (&b);
  133. sigset_t ss;
  134. sigemptyset (&ss);
  135. sigaddset (&ss, THE_SIG);
  136. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  137. {
  138. puts ("pthread_sigmask failed");
  139. exit (1);
  140. }
  141. /* Start sending signals. */
  142. for (i = 0; i < TOTAL_SIGS; ++i)
  143. {
  144. if (kill (getpid (), THE_SIG) != 0)
  145. {
  146. puts ("kill failed");
  147. exit (1);
  148. }
  149. if (TEMP_FAILURE_RETRY (sem_wait (&s)) != 0)
  150. {
  151. puts ("sem_wait failed");
  152. exit (1);
  153. }
  154. ++nsigs;
  155. }
  156. pthread_barrier_wait (&b);
  157. if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
  158. {
  159. puts ("pthread_sigmask failed");
  160. exit (1);
  161. }
  162. for (i = 0; i < N; ++i)
  163. if (pthread_join (th[i], NULL) != 0)
  164. {
  165. puts ("join failed");
  166. exit (1);
  167. }
  168. }
  169. if (pthread_attr_destroy (&a) != 0)
  170. {
  171. puts ("attr_destroy failed");
  172. exit (1);
  173. }
  174. return 0;
  175. #endif
  176. }
  177. #define TIMEOUT 5
  178. #define TEST_FUNCTION do_test ()
  179. #include "../test-skeleton.c"