tst-cancel1.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (C) 2002, 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <signal.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. static pthread_mutex_t m1 = PTHREAD_MUTEX_INITIALIZER;
  21. static pthread_mutex_t m2 = PTHREAD_MUTEX_INITIALIZER;
  22. static int cntr;
  23. static void
  24. cleanup (void *arg)
  25. {
  26. if (arg != (void *) 42l)
  27. cntr = 42;
  28. else
  29. cntr = 1;
  30. }
  31. static void *
  32. tf (void *arg)
  33. {
  34. /* Ignore all signals. This must not have any effect on delivering
  35. the cancellation signal. */
  36. sigset_t ss;
  37. sigfillset (&ss);
  38. if (pthread_sigmask (SIG_BLOCK, &ss, NULL) != 0)
  39. {
  40. puts ("pthread_sigmask failed");
  41. exit (1);
  42. }
  43. pthread_cleanup_push (cleanup, (void *) 42l);
  44. int err = pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  45. if (err != 0)
  46. {
  47. printf ("setcanceltype failed: %s\n", strerror (err));
  48. exit (1);
  49. }
  50. /* The following code is not standard compliant: the mutex functions
  51. must not be called with asynchronous cancellation enabled. */
  52. err = pthread_mutex_unlock (&m2);
  53. if (err != 0)
  54. {
  55. printf ("child: mutex_unlock failed: %s\n", strerror (err));
  56. exit (1);
  57. }
  58. err = pthread_mutex_lock (&m1);
  59. if (err != 0)
  60. {
  61. printf ("child: 1st mutex_lock failed: %s\n", strerror (err));
  62. exit (1);
  63. }
  64. /* We should never come here. */
  65. pthread_cleanup_pop (0);
  66. return NULL;
  67. }
  68. static int
  69. do_test (void)
  70. {
  71. int err;
  72. pthread_t th;
  73. int result = 0;
  74. void *retval;
  75. /* Get the mutexes. */
  76. err = pthread_mutex_lock (&m1);
  77. if (err != 0)
  78. {
  79. printf ("parent: 1st mutex_lock failed: %s\n", strerror (err));
  80. return 1;
  81. }
  82. err = pthread_mutex_lock (&m2);
  83. if (err != 0)
  84. {
  85. printf ("parent: 2nd mutex_lock failed: %s\n", strerror (err));
  86. return 1;
  87. }
  88. err = pthread_create (&th, NULL, tf, NULL);
  89. if (err != 0)
  90. {
  91. printf ("create failed: %s\n", strerror (err));
  92. return 1;
  93. }
  94. err = pthread_mutex_lock (&m2);
  95. if (err != 0)
  96. {
  97. printf ("parent: 3rd mutex_lock failed: %s\n", strerror (err));
  98. return 1;
  99. }
  100. err = pthread_cancel (th);
  101. if (err != 0)
  102. {
  103. printf ("cancel failed: %s\n", strerror (err));
  104. return 1;
  105. }
  106. err = pthread_join (th, &retval);
  107. if (err != 0)
  108. {
  109. printf ("join failed: %s\n", strerror (err));
  110. return 1;
  111. }
  112. if (retval != PTHREAD_CANCELED)
  113. {
  114. printf ("wrong return value: %p\n", retval);
  115. result = 1;
  116. }
  117. if (cntr == 42)
  118. {
  119. puts ("cleanup handler called with wrong argument");
  120. result = 1;
  121. }
  122. else if (cntr != 1)
  123. {
  124. puts ("cleanup handling not called");
  125. result = 1;
  126. }
  127. return result;
  128. }
  129. #define TEST_FUNCTION do_test ()
  130. #include "../test-skeleton.c"