tst-cancel11.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. static pthread_barrier_t bar;
  22. static int fd[2];
  23. static void
  24. cleanup (void *arg)
  25. {
  26. static int ncall;
  27. if (++ncall != 1)
  28. {
  29. puts ("second call to cleanup");
  30. exit (1);
  31. }
  32. printf ("cleanup call #%d\n", ncall);
  33. }
  34. static void *
  35. tf (void *arg)
  36. {
  37. pthread_cleanup_push (cleanup, NULL);
  38. int e = pthread_barrier_wait (&bar);
  39. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  40. {
  41. puts ("tf: 1st barrier_wait failed");
  42. exit (1);
  43. }
  44. /* This call should block and be cancelable. */
  45. char buf[20];
  46. read (fd[0], buf, sizeof (buf));
  47. pthread_cleanup_pop (0);
  48. return NULL;
  49. }
  50. static int
  51. do_test (void)
  52. {
  53. pthread_t th;
  54. if (pthread_barrier_init (&bar, NULL, 2) != 0)
  55. {
  56. puts ("barrier_init failed");
  57. exit (1);
  58. }
  59. if (pipe (fd) != 0)
  60. {
  61. puts ("pipe failed");
  62. exit (1);
  63. }
  64. if (pthread_create (&th, NULL, tf, NULL) != 0)
  65. {
  66. puts ("create failed");
  67. exit (1);
  68. }
  69. int e = pthread_barrier_wait (&bar);
  70. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  71. {
  72. puts ("1st barrier_wait failed");
  73. exit (1);
  74. }
  75. if (pthread_cancel (th) != 0)
  76. {
  77. puts ("1st cancel failed");
  78. exit (1);
  79. }
  80. void *r;
  81. if (pthread_join (th, &r) != 0)
  82. {
  83. puts ("join failed");
  84. exit (1);
  85. }
  86. if (r != PTHREAD_CANCELED)
  87. {
  88. puts ("thread not canceled");
  89. exit (1);
  90. }
  91. return 0;
  92. }
  93. #define TEST_FUNCTION do_test ()
  94. #include "../test-skeleton.c"