tst-cond7.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 <stdbool.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #include <sys/time.h>
  23. typedef struct
  24. {
  25. pthread_cond_t cond;
  26. pthread_mutex_t lock;
  27. pthread_t h;
  28. } T;
  29. static volatile bool done;
  30. static void *
  31. tf (void *arg)
  32. {
  33. puts ("child created");
  34. if (pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, NULL) != 0
  35. || pthread_setcanceltype (PTHREAD_CANCEL_DEFERRED, NULL) != 0)
  36. {
  37. puts ("cannot set cancellation options");
  38. exit (1);
  39. }
  40. T *t = (T *) arg;
  41. if (pthread_mutex_lock (&t->lock) != 0)
  42. {
  43. puts ("child: lock failed");
  44. exit (1);
  45. }
  46. done = true;
  47. if (pthread_cond_signal (&t->cond) != 0)
  48. {
  49. puts ("child: cond_signal failed");
  50. exit (1);
  51. }
  52. if (pthread_cond_wait (&t->cond, &t->lock) != 0)
  53. {
  54. puts ("child: cond_wait failed");
  55. exit (1);
  56. }
  57. if (pthread_mutex_unlock (&t->lock) != 0)
  58. {
  59. puts ("child: unlock failed");
  60. exit (1);
  61. }
  62. return NULL;
  63. }
  64. static int
  65. do_test (void)
  66. {
  67. int i;
  68. #define N 100
  69. T *t[N];
  70. for (i = 0; i < N; ++i)
  71. {
  72. printf ("round %d\n", i);
  73. t[i] = (T *) malloc (sizeof (T));
  74. if (t[i] == NULL)
  75. {
  76. puts ("out of memory");
  77. exit (1);
  78. }
  79. if (pthread_mutex_init (&t[i]->lock, NULL) != 0
  80. || pthread_cond_init (&t[i]->cond, NULL) != 0)
  81. {
  82. puts ("an _init function failed");
  83. exit (1);
  84. }
  85. if (pthread_mutex_lock (&t[i]->lock) != 0)
  86. {
  87. puts ("initial mutex_lock failed");
  88. exit (1);
  89. }
  90. done = false;
  91. if (pthread_create (&t[i]->h, NULL, tf, t[i]) != 0)
  92. {
  93. puts ("pthread_create failed");
  94. exit (1);
  95. }
  96. do
  97. if (pthread_cond_wait (&t[i]->cond, &t[i]->lock) != 0)
  98. {
  99. puts ("cond_wait failed");
  100. exit (1);
  101. }
  102. while (! done);
  103. /* Release the lock since the cancel handler will get it. */
  104. if (pthread_mutex_unlock (&t[i]->lock) != 0)
  105. {
  106. puts ("mutex_unlock failed");
  107. exit (1);
  108. }
  109. if (pthread_cancel (t[i]->h) != 0)
  110. {
  111. puts ("cancel failed");
  112. exit (1);
  113. }
  114. puts ("parent: joining now");
  115. void *result;
  116. if (pthread_join (t[i]->h, &result) != 0)
  117. {
  118. puts ("join failed");
  119. exit (1);
  120. }
  121. if (result != PTHREAD_CANCELED)
  122. {
  123. puts ("result != PTHREAD_CANCELED");
  124. exit (1);
  125. }
  126. }
  127. for (i = 0; i < N; ++i)
  128. free (t[i]);
  129. return 0;
  130. }
  131. #define TEST_FUNCTION do_test ()
  132. #include "../test-skeleton.c"