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