tst-cancel8.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 2003 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 <unistd.h>
  19. static pthread_barrier_t bar;
  20. static int global;
  21. static void
  22. cleanup (void *arg)
  23. {
  24. global = 1;
  25. }
  26. static void *
  27. tf (void *arg)
  28. {
  29. /* Enable cancellation, but defer it. */
  30. if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0)
  31. {
  32. puts ("setcancelstate failed");
  33. exit (1);
  34. }
  35. if (pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  36. {
  37. puts ("setcanceltype failed");
  38. exit (1);
  39. }
  40. /* Add cleanup handler. */
  41. pthread_cleanup_push (cleanup, NULL);
  42. /* Synchronize with the main thread. */
  43. int r = pthread_barrier_wait (&bar);
  44. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  45. {
  46. puts ("tf: first barrier_wait failed");
  47. exit (1);
  48. }
  49. /* And again. Once this is done the main thread should have canceled
  50. this thread. */
  51. r = pthread_barrier_wait (&bar);
  52. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  53. {
  54. puts ("tf: second barrier_wait failed");
  55. exit (1);
  56. }
  57. /* Remove the cleanup handler without executing it. */
  58. pthread_cleanup_pop (0);
  59. /* Now react on the cancellation. */
  60. pthread_testcancel ();
  61. /* This call should never return. */
  62. return NULL;
  63. }
  64. static int
  65. do_test (void)
  66. {
  67. if (pthread_barrier_init (&bar, NULL, 2) != 0)
  68. {
  69. puts ("barrier_init failed");
  70. exit (1);
  71. }
  72. pthread_t th;
  73. if (pthread_create (&th, NULL, tf, NULL) != 0)
  74. {
  75. puts ("pthread_create failed");
  76. return 1;
  77. }
  78. int r = pthread_barrier_wait (&bar);
  79. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  80. {
  81. puts ("first barrier_wait failed");
  82. exit (1);
  83. }
  84. if (pthread_cancel (th) != 0)
  85. {
  86. puts ("pthread_cancel failed");
  87. return 1;
  88. }
  89. r = pthread_barrier_wait (&bar);
  90. if (r != 0 && r!= PTHREAD_BARRIER_SERIAL_THREAD)
  91. {
  92. puts ("second barrier_wait failed");
  93. exit (1);
  94. }
  95. void *result;
  96. if (pthread_join (th, &result) != 0)
  97. {
  98. puts ("pthread_join failed");
  99. return 1;
  100. }
  101. if (result != PTHREAD_CANCELED)
  102. {
  103. puts ("thread was not canceled");
  104. exit (1);
  105. }
  106. if (global != 0)
  107. {
  108. puts ("cancellation handler has been called");
  109. exit (1);
  110. }
  111. return 0;
  112. }
  113. #define TEST_FUNCTION do_test ()
  114. #include "../test-skeleton.c"