tst-cancel11.c 2.5 KB

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