tst-kill3.c 3.3 KB

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