tst-cancel10.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 <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. static void
  20. cleanup (void *arg)
  21. {
  22. /* Just for fun. */
  23. if (pthread_cancel (pthread_self ()) != 0)
  24. {
  25. puts ("cleanup: cancel failed");
  26. exit (1);
  27. }
  28. printf ("cleanup for %ld\n", (long int) arg);
  29. }
  30. static void *
  31. tf (void *arg)
  32. {
  33. long int n = (long int) arg;
  34. pthread_cleanup_push (cleanup, arg);
  35. if (pthread_setcanceltype ((n & 1) == 0
  36. ? PTHREAD_CANCEL_DEFERRED
  37. : PTHREAD_CANCEL_ASYNCHRONOUS, NULL) != 0)
  38. {
  39. puts ("setcanceltype failed");
  40. exit (1);
  41. }
  42. if (pthread_cancel (pthread_self ()) != 0)
  43. {
  44. puts ("cancel failed");
  45. exit (1);
  46. }
  47. pthread_testcancel ();
  48. /* We should never come here. */
  49. pthread_cleanup_pop (0);
  50. return NULL;
  51. }
  52. static int
  53. do_test (void)
  54. {
  55. pthread_attr_t at;
  56. if (pthread_attr_init (&at) != 0)
  57. {
  58. puts ("attr_init failed");
  59. return 1;
  60. }
  61. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  62. {
  63. puts ("attr_setstacksize failed");
  64. return 1;
  65. }
  66. #define N 20
  67. int i;
  68. pthread_t th[N];
  69. for (i = 0; i < N; ++i)
  70. if (pthread_create (&th[i], &at, tf, (void *) (long int) i) != 0)
  71. {
  72. puts ("create failed");
  73. exit (1);
  74. }
  75. if (pthread_attr_destroy (&at) != 0)
  76. {
  77. puts ("attr_destroy failed");
  78. return 1;
  79. }
  80. for (i = 0; i < N; ++i)
  81. {
  82. void *r;
  83. if (pthread_join (th[i], &r) != 0)
  84. {
  85. puts ("join failed");
  86. exit (1);
  87. }
  88. if (r != PTHREAD_CANCELED)
  89. {
  90. puts ("thread not canceled");
  91. exit (1);
  92. }
  93. }
  94. return 0;
  95. }
  96. #define TEST_FUNCTION do_test ()
  97. #include "../test-skeleton.c"