tst-cancel9.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright (C) 2003, 2004 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 <fcntl.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 b;
  22. static void
  23. cleanup (void *arg)
  24. {
  25. fputs ("in cleanup\n", stdout);
  26. }
  27. static void *
  28. tf (void *arg)
  29. {
  30. int fd = open ("/dev/null", O_RDWR);
  31. if (fd == -1)
  32. {
  33. puts ("cannot open /dev/null");
  34. exit (1);
  35. }
  36. FILE *fp = fdopen (fd, "w");
  37. if (fp == NULL)
  38. {
  39. puts ("fdopen failed");
  40. exit (1);
  41. }
  42. pthread_cleanup_push (cleanup, NULL);
  43. int e = pthread_barrier_wait (&b);
  44. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  45. {
  46. puts ("barrier_wait failed");
  47. exit (1);
  48. }
  49. while (1)
  50. /* fprintf() uses write() which is a cancallation point. */
  51. fprintf (fp, "foo");
  52. pthread_cleanup_pop (0);
  53. return NULL;
  54. }
  55. static int
  56. do_test (void)
  57. {
  58. if (pthread_barrier_init (&b, NULL, 2) != 0)
  59. {
  60. puts ("barrier_init failed");
  61. exit (1);
  62. }
  63. pthread_t th;
  64. if (pthread_create (&th, NULL, tf, NULL) != 0)
  65. {
  66. puts ("create failed");
  67. return 1;
  68. }
  69. int e = pthread_barrier_wait (&b);
  70. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  71. {
  72. puts ("barrier_wait failed");
  73. exit (1);
  74. }
  75. sleep (1);
  76. puts ("cancel now");
  77. if (pthread_cancel (th) != 0)
  78. {
  79. puts ("cancel failed");
  80. exit (1);
  81. }
  82. puts ("waiting for the child");
  83. void *r;
  84. if (pthread_join (th, &r) != 0)
  85. {
  86. puts ("join failed");
  87. exit (1);
  88. }
  89. if (r != PTHREAD_CANCELED)
  90. {
  91. puts ("thread wasn't canceled");
  92. exit (1);
  93. }
  94. return 0;
  95. }
  96. #define TEST_FUNCTION do_test ()
  97. #include "../test-skeleton.c"