tst-kill6.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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 <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. static pthread_t receiver;
  23. static sem_t sem;
  24. static pthread_barrier_t b;
  25. static void
  26. handler (int sig)
  27. {
  28. if (sig != SIGUSR1)
  29. {
  30. write (STDOUT_FILENO, "wrong signal\n", 13);
  31. _exit (1);
  32. }
  33. if (pthread_self () != receiver)
  34. {
  35. write (STDOUT_FILENO, "not the intended receiver\n", 26);
  36. _exit (1);
  37. }
  38. if (sem_post (&sem) != 0)
  39. {
  40. write (STDOUT_FILENO, "sem_post failed\n", 16);
  41. _exit (1);
  42. }
  43. }
  44. static void *
  45. tf (void *a)
  46. {
  47. int e = pthread_barrier_wait (&b);
  48. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  49. {
  50. puts ("child: barrier_wait failed");
  51. exit (1);
  52. }
  53. return NULL;
  54. }
  55. int
  56. do_test (void)
  57. {
  58. struct sigaction sa;
  59. sigemptyset (&sa.sa_mask);
  60. sa.sa_flags = 0;
  61. sa.sa_handler = handler;
  62. if (sigaction (SIGUSR1, &sa, NULL) != 0)
  63. {
  64. puts ("sigaction failed");
  65. exit (1);
  66. }
  67. #define N 20
  68. if (pthread_barrier_init (&b, NULL, N + 1) != 0)
  69. {
  70. puts ("barrier_init failed");
  71. exit (1);
  72. }
  73. pthread_attr_t a;
  74. if (pthread_attr_init (&a) != 0)
  75. {
  76. puts ("attr_init failed");
  77. exit (1);
  78. }
  79. if (pthread_attr_setstacksize (&a, 1 * 1024 * 1024) != 0)
  80. {
  81. puts ("attr_setstacksize failed");
  82. return 1;
  83. }
  84. pthread_t th[N];
  85. int i;
  86. for (i = 0; i < N; ++i)
  87. if (pthread_create (&th[i], &a, tf, NULL) != 0)
  88. {
  89. puts ("create failed");
  90. exit (1);
  91. }
  92. if (pthread_attr_destroy (&a) != 0)
  93. {
  94. puts ("attr_destroy failed");
  95. exit (1);
  96. }
  97. if (sem_init (&sem, 0, 0) != 0)
  98. {
  99. puts ("sem_init failed");
  100. exit (1);
  101. }
  102. for (i = 0; i < N * 10; ++i)
  103. {
  104. receiver = th[i % N];
  105. if (pthread_kill (receiver, SIGUSR1) != 0)
  106. {
  107. puts ("kill failed");
  108. exit (1);
  109. }
  110. if (TEMP_FAILURE_RETRY (sem_wait (&sem)) != 0)
  111. {
  112. puts ("sem_wait failed");
  113. exit (1);
  114. }
  115. }
  116. int e = pthread_barrier_wait (&b);
  117. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  118. {
  119. puts ("barrier_wait failed");
  120. exit (1);
  121. }
  122. for (i = 0; i < N; ++i)
  123. if (pthread_join (th[i], NULL) != 0)
  124. {
  125. puts ("join failed");
  126. exit (1);
  127. }
  128. return 0;
  129. }
  130. #define TEST_FUNCTION do_test ()
  131. #include "../test-skeleton.c"