tst-cancel15.c 2.9 KB

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