tst-cancel15.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 <semaphore.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/time.h>
  23. static pthread_barrier_t bar;
  24. static sem_t sem;
  25. static void
  26. cleanup (void *arg)
  27. {
  28. static int ncall;
  29. if (++ncall != 1)
  30. {
  31. puts ("second call to cleanup");
  32. exit (1);
  33. }
  34. printf ("cleanup call #%d\n", ncall);
  35. }
  36. static void *
  37. tf (void *arg)
  38. {
  39. int e;
  40. pthread_cleanup_push (cleanup, NULL);
  41. e = pthread_barrier_wait (&bar);
  42. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  43. {
  44. puts ("tf: 1st barrier_wait failed");
  45. exit (1);
  46. }
  47. struct timeval tv;
  48. (void) gettimeofday (&tv, NULL);
  49. struct timespec ts;
  50. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  51. /* Timeout in 5 seconds. */
  52. ts.tv_sec += 5;
  53. /* This call should block and be cancelable. */
  54. errno = 0;
  55. e = sem_timedwait (&sem, &ts);
  56. pthread_cleanup_pop (0);
  57. printf ("sem_timedwait returned, e = %d, errno = %d\n", e, errno);
  58. return NULL;
  59. }
  60. static int
  61. do_test (void)
  62. {
  63. pthread_t th;
  64. if (pthread_barrier_init (&bar, NULL, 2) != 0)
  65. {
  66. puts ("barrier_init failed");
  67. exit (1);
  68. }
  69. if (sem_init (&sem, 0, 0) != 0)
  70. {
  71. puts ("sem_init failed");
  72. exit (1);
  73. }
  74. if (pthread_create (&th, NULL, tf, NULL) != 0)
  75. {
  76. puts ("create failed");
  77. exit (1);
  78. }
  79. int e = pthread_barrier_wait (&bar);
  80. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  81. {
  82. puts ("1st barrier_wait failed");
  83. exit (1);
  84. }
  85. /* Give the child a chance to go to sleep in sem_wait. */
  86. sleep (1);
  87. /* Check whether cancellation is honored when waiting in sem_timedwait. */
  88. if (pthread_cancel (th) != 0)
  89. {
  90. puts ("1st cancel failed");
  91. exit (1);
  92. }
  93. void *r;
  94. if (pthread_join (th, &r) != 0)
  95. {
  96. puts ("join failed");
  97. exit (1);
  98. }
  99. if (r != PTHREAD_CANCELED)
  100. {
  101. puts ("thread not canceled");
  102. exit (1);
  103. }
  104. return 0;
  105. }
  106. #define TEST_FUNCTION do_test ()
  107. #include "../test-skeleton.c"