tst-cond15.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Copyright (C) 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
  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 <string.h>
  20. #include <unistd.h>
  21. #include <sys/time.h>
  22. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  23. static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  24. static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
  25. static void *
  26. tf (void *p)
  27. {
  28. if (pthread_mutex_lock (&mut) != 0)
  29. {
  30. printf ("%s: 1st mutex_lock failed\n", __func__);
  31. exit (1);
  32. }
  33. if (pthread_mutex_lock (&mut) != 0)
  34. {
  35. printf ("%s: 2nd mutex_lock failed\n", __func__);
  36. exit (1);
  37. }
  38. if (pthread_mutex_lock (&mut) != 0)
  39. {
  40. printf ("%s: 3rd mutex_lock failed\n", __func__);
  41. exit (1);
  42. }
  43. if (pthread_mutex_unlock (&mut2) != 0)
  44. {
  45. printf ("%s: mutex_unlock failed\n", __func__);
  46. exit (1);
  47. }
  48. struct timeval tv;
  49. gettimeofday (&tv, NULL);
  50. struct timespec ts;
  51. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  52. ts.tv_sec += p == NULL ? 100 : 1;
  53. int err = pthread_cond_timedwait (&cond, &mut, &ts);
  54. if ((err != 0 && p == NULL) || (err != ETIMEDOUT && p != NULL))
  55. {
  56. printf ("%s: cond_wait failed\n", __func__);
  57. exit (1);
  58. }
  59. if (pthread_mutex_unlock (&mut) != 0)
  60. {
  61. printf ("%s: 1st mutex_unlock failed\n", __func__);
  62. exit (1);
  63. }
  64. if (pthread_mutex_unlock (&mut) != 0)
  65. {
  66. printf ("%s: 2nd mutex_unlock failed\n", __func__);
  67. exit (1);
  68. }
  69. if (pthread_mutex_unlock (&mut) != 0)
  70. {
  71. printf ("%s: 3rd mutex_unlock failed\n", __func__);
  72. exit (1);
  73. }
  74. puts ("child: done");
  75. return NULL;
  76. }
  77. static int
  78. do_test (void)
  79. {
  80. if (pthread_mutex_lock (&mut2) != 0)
  81. {
  82. puts ("1st mutex_lock failed");
  83. return 1;
  84. }
  85. puts ("parent: create 1st child");
  86. pthread_t th;
  87. int err = pthread_create (&th, NULL, tf, NULL);
  88. if (err != 0)
  89. {
  90. printf ("parent: cannot 1st create thread: %s\n", strerror (err));
  91. return 1;
  92. }
  93. /* We have to synchronize with the child. */
  94. if (pthread_mutex_lock (&mut2) != 0)
  95. {
  96. puts ("2nd mutex_lock failed");
  97. return 1;
  98. }
  99. /* Give the child to reach to pthread_cond_wait. */
  100. sleep (1);
  101. if (pthread_cond_signal (&cond) != 0)
  102. {
  103. puts ("cond_signal failed");
  104. return 1;
  105. }
  106. err = pthread_join (th, NULL);
  107. if (err != 0)
  108. {
  109. printf ("parent: failed to join: %s\n", strerror (err));
  110. return 1;
  111. }
  112. puts ("parent: create 2nd child");
  113. err = pthread_create (&th, NULL, tf, (void *) 1l);
  114. if (err != 0)
  115. {
  116. printf ("parent: cannot 2nd create thread: %s\n", strerror (err));
  117. return 1;
  118. }
  119. err = pthread_join (th, NULL);
  120. if (err != 0)
  121. {
  122. printf ("parent: failed to join: %s\n", strerror (err));
  123. return 1;
  124. }
  125. puts ("done");
  126. return 0;
  127. }
  128. #define TEST_FUNCTION do_test ()
  129. #define TIMEOUT 6
  130. #include "../test-skeleton.c"