tst-kill3.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /* Copyright (C) 2003 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 <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. static pthread_cond_t c = PTHREAD_COND_INITIALIZER;
  23. static pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
  24. static pthread_barrier_t b;
  25. static void
  26. handler (int sig)
  27. {
  28. write (1, "handler called\n", 15);
  29. _exit (1);
  30. }
  31. static void *
  32. tf (void *a)
  33. {
  34. /* Block SIGUSR1. */
  35. sigset_t ss;
  36. sigemptyset (&ss);
  37. sigaddset (&ss, SIGUSR1);
  38. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  39. {
  40. puts ("child: sigmask failed");
  41. exit (1);
  42. }
  43. if (pthread_mutex_lock (&m) != 0)
  44. {
  45. puts ("child: mutex_lock failed");
  46. exit (1);
  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. /* Compute timeout. */
  55. struct timeval tv;
  56. (void) gettimeofday (&tv, NULL);
  57. struct timespec ts;
  58. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  59. /* Timeout: 1sec. */
  60. ts.tv_sec += 1;
  61. /* This call should never return. */
  62. if (pthread_cond_timedwait (&c, &m, &ts) != ETIMEDOUT)
  63. {
  64. puts ("cond_timedwait didn't time out");
  65. exit (1);
  66. }
  67. return NULL;
  68. }
  69. int
  70. do_test (void)
  71. {
  72. pthread_t th;
  73. struct sigaction sa;
  74. sigemptyset (&sa.sa_mask);
  75. sa.sa_flags = 0;
  76. sa.sa_handler = handler;
  77. if (sigaction (SIGUSR1, &sa, NULL) != 0)
  78. {
  79. puts ("sigaction failed");
  80. exit (1);
  81. }
  82. if (pthread_barrier_init (&b, NULL, 2) != 0)
  83. {
  84. puts ("barrier_init failed");
  85. exit (1);
  86. }
  87. if (pthread_create (&th, NULL, tf, NULL) != 0)
  88. {
  89. puts ("create failed");
  90. exit (1);
  91. }
  92. int e = pthread_barrier_wait (&b);
  93. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  94. {
  95. puts ("barrier_wait failed");
  96. exit (1);
  97. }
  98. if (pthread_mutex_lock (&m) != 0)
  99. {
  100. puts ("mutex_lock failed");
  101. exit (1);
  102. }
  103. /* Send the thread a signal which it has blocked. */
  104. if (pthread_kill (th, SIGUSR1) != 0)
  105. {
  106. puts ("kill failed");
  107. exit (1);
  108. }
  109. if (pthread_mutex_unlock (&m) != 0)
  110. {
  111. puts ("mutex_unlock failed");
  112. exit (1);
  113. }
  114. void *r;
  115. if (pthread_join (th, &r) != 0)
  116. {
  117. puts ("join failed");
  118. exit (1);
  119. }
  120. if (r != NULL)
  121. {
  122. puts ("return value wrong");
  123. exit (1);
  124. }
  125. return 0;
  126. }
  127. #define TIMEOUT 5
  128. #define TEST_FUNCTION do_test ()
  129. #include "../test-skeleton.c"