tst-cancel2.c 2.2 KB

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