tst-cancel22.c 2.7 KB

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