tst-cancel22.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Jakub Jelinek <jakub@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 <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <unistd.h>
  20. pthread_barrier_t b;
  21. int seen;
  22. static void *
  23. tf (void *arg)
  24. {
  25. int i;
  26. int old;
  27. int r = pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &old);
  28. if (r != 0)
  29. {
  30. puts ("setcancelstate failed");
  31. exit (1);
  32. }
  33. r = pthread_barrier_wait (&b);
  34. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  35. {
  36. puts ("barrier_wait failed");
  37. exit (1);
  38. }
  39. for (i = 0; i < 10; ++i)
  40. {
  41. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  42. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  43. }
  44. seen = 1;
  45. pthread_setcancelstate (old, NULL);
  46. struct timespec ts = { .tv_sec = 0, .tv_nsec = 100000000 };
  47. TEMP_FAILURE_RETRY (nanosleep (&ts, &ts));
  48. exit (1);
  49. }
  50. static int
  51. do_test (void)
  52. {
  53. if (pthread_barrier_init (&b, NULL, 2) != 0)
  54. {
  55. puts ("barrier init failed");
  56. return 1;
  57. }
  58. pthread_t th;
  59. if (pthread_create (&th, NULL, tf, NULL) != 0)
  60. {
  61. puts ("thread creation failed");
  62. return 1;
  63. }
  64. int r = pthread_barrier_wait (&b);
  65. if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD)
  66. {
  67. puts ("barrier_wait failed");
  68. return 1;
  69. }
  70. if (pthread_cancel (th) != 0)
  71. {
  72. puts ("cancel failed");
  73. return 1;
  74. }
  75. void *status;
  76. if (pthread_join (th, &status) != 0)
  77. {
  78. puts ("join failed");
  79. return 1;
  80. }
  81. if (status != PTHREAD_CANCELED)
  82. {
  83. puts ("thread not canceled");
  84. return 1;
  85. }
  86. if (pthread_barrier_destroy (&b) != 0)
  87. {
  88. puts ("barrier_destroy failed");
  89. return 1;
  90. }
  91. if (seen != 1)
  92. {
  93. puts ("thread cancelled when PTHREAD_CANCEL_DISABLED");
  94. return 1;
  95. }
  96. return 0;
  97. }
  98. #define TIMEOUT 5
  99. #define TEST_FUNCTION do_test ()
  100. #include "../test-skeleton.c"