tst-tls3.c 4.1 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 <dlfcn.h>
  17. #include <errno.h>
  18. #include <pthread.h>
  19. #include <signal.h>
  20. #include <semaphore.h>
  21. #include <stdint.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <unistd.h>
  25. #include <pthreaddef.h>
  26. #include <tls.h>
  27. #define THE_SIG SIGUSR1
  28. #define N 10
  29. static pthread_t th[N];
  30. #define CB(n) \
  31. static void \
  32. cb##n (void) \
  33. { \
  34. if (th[n] != pthread_self ()) \
  35. { \
  36. write (STDOUT_FILENO, "wrong callback\n", 15); \
  37. _exit (1); \
  38. } \
  39. }
  40. CB (0)
  41. CB (1)
  42. CB (2)
  43. CB (3)
  44. CB (4)
  45. CB (5)
  46. CB (6)
  47. CB (7)
  48. CB (8)
  49. CB (9)
  50. static void (*cbs[]) (void) =
  51. {
  52. cb0, cb1, cb2, cb3, cb4, cb5, cb6, cb7, cb8, cb9
  53. };
  54. sem_t s;
  55. pthread_barrier_t b;
  56. #define TOTAL_SIGS 1000
  57. int nsigs;
  58. int
  59. do_test (void)
  60. {
  61. #if !HAVE___THREAD
  62. puts ("No __thread support in compiler, test skipped.");
  63. return 0;
  64. #else
  65. if ((uintptr_t) pthread_self () & (TCB_ALIGNMENT - 1))
  66. {
  67. puts ("initial thread's struct pthread not aligned enough");
  68. exit (1);
  69. }
  70. if (pthread_barrier_init (&b, NULL, N + 1) != 0)
  71. {
  72. puts ("barrier_init failed");
  73. exit (1);
  74. }
  75. if (sem_init (&s, 0, 0) != 0)
  76. {
  77. puts ("sem_init failed");
  78. exit (1);
  79. }
  80. void *h = dlopen ("tst-tls3mod.so", RTLD_LAZY);
  81. if (h == NULL)
  82. {
  83. puts ("dlopen failed");
  84. exit (1);
  85. }
  86. void *(*tf) (void *) = dlsym (h, "tf");
  87. if (tf == NULL)
  88. {
  89. puts ("dlsym for tf failed");
  90. exit (1);
  91. }
  92. struct sigaction sa;
  93. sa.sa_handler = dlsym (h, "handler");
  94. if (sa.sa_handler == NULL)
  95. {
  96. puts ("dlsym for handler failed");
  97. exit (1);
  98. }
  99. sigemptyset (&sa.sa_mask);
  100. sa.sa_flags = 0;
  101. if (sigaction (THE_SIG, &sa, NULL) != 0)
  102. {
  103. puts ("sigaction failed");
  104. exit (1);
  105. }
  106. pthread_attr_t a;
  107. if (pthread_attr_init (&a) != 0)
  108. {
  109. puts ("attr_init failed");
  110. exit (1);
  111. }
  112. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  113. {
  114. puts ("attr_setstacksize failed");
  115. return 1;
  116. }
  117. int r;
  118. for (r = 0; r < 10; ++r)
  119. {
  120. int i;
  121. for (i = 0; i < N; ++i)
  122. if (pthread_create (&th[i], &a, tf, cbs[i]) != 0)
  123. {
  124. puts ("pthread_create failed");
  125. exit (1);
  126. }
  127. nsigs = 0;
  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. if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
  154. {
  155. puts ("pthread_sigmask failed");
  156. exit (1);
  157. }
  158. for (i = 0; i < N; ++i)
  159. if (pthread_join (th[i], NULL) != 0)
  160. {
  161. puts ("join failed");
  162. exit (1);
  163. }
  164. }
  165. if (pthread_attr_destroy (&a) != 0)
  166. {
  167. puts ("attr_destroy failed");
  168. exit (1);
  169. }
  170. return 0;
  171. #endif
  172. }
  173. #define TIMEOUT 5
  174. #define TEST_FUNCTION do_test ()
  175. #include "../test-skeleton.c"