tst-signal3.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <semaphore.h>
  18. #include <signal.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. /* Number of different signalss to use. Also is the number of
  23. threads. */
  24. #define N 10
  25. /* Maximum number of threads in flight at any one time. */
  26. #define INFLIGHT 5
  27. /* Number of signals sent in total. */
  28. #define ROUNDS 10000
  29. static int received[N][N];
  30. static int nsig[N];
  31. static pthread_t th[N];
  32. static sem_t sem;
  33. static pthread_mutex_t lock[N];
  34. static pthread_t th_main;
  35. static int sig0;
  36. static void
  37. handler (int sig)
  38. {
  39. int i;
  40. for (i = 0; i < N; ++i)
  41. if (pthread_equal (pthread_self (), th[i]))
  42. break;
  43. if (i == N)
  44. {
  45. if (pthread_equal (pthread_self (), th_main))
  46. puts ("signal received by main thread");
  47. else
  48. printf ("signal received by unknown thread (%lx)\n",
  49. (unsigned long int) pthread_self ());
  50. exit (1);
  51. }
  52. ++received[i][sig - sig0];
  53. sem_post (&sem);
  54. }
  55. static void *
  56. tf (void *arg)
  57. {
  58. int idx = (long int) arg;
  59. sigset_t ss;
  60. sigemptyset (&ss);
  61. int i;
  62. for (i = 0; i <= idx; ++i)
  63. sigaddset (&ss, sig0 + i);
  64. if (pthread_sigmask (SIG_UNBLOCK, &ss, NULL) != 0)
  65. {
  66. printf ("thread %d: pthread_sigmask failed\n", i);
  67. exit (1);
  68. }
  69. pthread_mutex_lock (&lock[idx]);
  70. return NULL;
  71. }
  72. static int
  73. do_test (void)
  74. {
  75. /* Block all signals. */
  76. sigset_t ss;
  77. sigfillset (&ss);
  78. th_main = pthread_self ();
  79. sig0 = SIGRTMIN;
  80. if (pthread_sigmask (SIG_SETMASK, &ss, NULL) != 0)
  81. {
  82. puts ("1st pthread_sigmask failed");
  83. exit (1);
  84. }
  85. /* Install the handler. */
  86. int i;
  87. for (i = 0; i < N; ++i)
  88. {
  89. struct sigaction sa =
  90. {
  91. .sa_handler = handler,
  92. .sa_flags = 0
  93. };
  94. sigfillset (&sa.sa_mask);
  95. if (sigaction (sig0 + i, &sa, NULL) != 0)
  96. {
  97. printf ("sigaction for signal %d failed\n", i);
  98. exit (1);
  99. }
  100. }
  101. if (sem_init (&sem, 0, INFLIGHT) != 0)
  102. {
  103. puts ("sem_init 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. for (i = 0; i < N; ++i)
  118. {
  119. if (pthread_mutex_init (&lock[i], NULL) != 0)
  120. {
  121. printf ("mutex_init[%d] failed\n", i);
  122. }
  123. if (pthread_mutex_lock (&lock[i]) != 0)
  124. {
  125. printf ("mutex_lock[%d] failed\n", i);
  126. }
  127. if (pthread_create (&th[i], &a, tf, (void *) (long int) i) != 0)
  128. {
  129. printf ("create of thread %d failed\n", i);
  130. exit (1);
  131. }
  132. }
  133. if (pthread_attr_destroy (&a) != 0)
  134. {
  135. puts ("attr_destroy failed");
  136. exit (1);
  137. }
  138. int result = 0;
  139. unsigned int r = 42;
  140. pid_t pid = getpid ();
  141. for (i = 0; i < ROUNDS; ++i)
  142. {
  143. if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
  144. {
  145. printf ("sem_wait round %d failed: %m\n", i);
  146. exit (1);
  147. }
  148. int s = rand_r (&r) % N;
  149. kill (pid, sig0 + s);
  150. }
  151. void *status;
  152. for (i = 0; i < N; ++i)
  153. {
  154. if (pthread_mutex_unlock (&lock[i]) != 0)
  155. {
  156. printf ("unlock %d failed\n", i);
  157. exit (1);
  158. }
  159. if (pthread_join (th[i], &status) != 0)
  160. {
  161. printf ("join %d failed\n", i);
  162. result = 1;
  163. }
  164. else if (status != NULL)
  165. {
  166. printf ("%d: result != NULL\n", i);
  167. result = 1;
  168. }
  169. }
  170. int total = 0;
  171. for (i = 0; i < N; ++i)
  172. {
  173. int j;
  174. for (j = 0; j <= i; ++j)
  175. total += received[i][j];
  176. for (j = i + 1; j < N; ++j)
  177. if (received[i][j] != 0)
  178. {
  179. printf ("thread %d received signal SIGRTMIN+%d\n", i, j);
  180. result = 1;
  181. }
  182. }
  183. if (total != ROUNDS)
  184. {
  185. printf ("total number of handled signals is %d, expected %d\n",
  186. total, ROUNDS);
  187. result = 1;
  188. }
  189. printf ("A total of %d signals sent and received\n", total);
  190. for (i = 0; i < N; ++i)
  191. {
  192. printf ("thread %2d:", i);
  193. int j;
  194. for (j = 0; j <= i; ++j)
  195. {
  196. printf (" %5d", received[i][j]);
  197. nsig[j] += received[i][j];
  198. }
  199. putchar ('\n');
  200. }
  201. printf ("\nTotal :");
  202. for (i = 0; i < N; ++i)
  203. printf (" %5d", nsig[i]);
  204. putchar ('\n');
  205. return result;
  206. }
  207. #define TIMEOUT 10
  208. #define TEST_FUNCTION do_test ()
  209. #include "../test-skeleton.c"