tst-cond7.c 3.3 KB

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