tst-cancel2.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <pthread.h>
  16. #include <signal.h>
  17. #include <stdio.h>
  18. #include <unistd.h>
  19. static int fd[2];
  20. static void *
  21. tf (void *arg)
  22. {
  23. /* The buffer size must be larger than the pipe size so that the
  24. write blocks. */
  25. char buf[100000];
  26. if (write (fd[1], buf, sizeof (buf)) == sizeof (buf))
  27. {
  28. puts ("write succeeded");
  29. return (void *) 1l;
  30. }
  31. return (void *) 42l;
  32. }
  33. static int
  34. do_test (void)
  35. {
  36. pthread_t th;
  37. void *r;
  38. struct sigaction sa;
  39. sa.sa_handler = SIG_IGN;
  40. sigemptyset (&sa.sa_mask);
  41. sa.sa_flags = 0;
  42. if (sigaction (SIGPIPE, &sa, NULL) != 0)
  43. {
  44. puts ("sigaction failed");
  45. return 1;
  46. }
  47. if (pipe (fd) != 0)
  48. {
  49. puts ("pipe failed");
  50. return 1;
  51. }
  52. if (pthread_create (&th, NULL, tf, NULL) != 0)
  53. {
  54. puts ("create failed");
  55. return 1;
  56. }
  57. if (pthread_cancel (th) != 0)
  58. {
  59. puts ("cancel failed");
  60. return 1;
  61. }
  62. /* This will cause the write in the child to return. */
  63. close (fd[0]);
  64. if (pthread_join (th, &r) != 0)
  65. {
  66. puts ("join failed");
  67. return 1;
  68. }
  69. if (r != PTHREAD_CANCELED)
  70. {
  71. printf ("result is wrong: expected %p, got %p\n", PTHREAD_CANCELED, r);
  72. return 1;
  73. }
  74. return 0;
  75. }
  76. #define TEST_FUNCTION do_test ()
  77. #include "../test-skeleton.c"