tst-cancel8.c 3.2 KB

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