tst-cancel1.c 3.6 KB

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