tst-signal3.c 5.0 KB

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