tst-cancel10.c 2.5 KB

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