tst-cond15.c 3.8 KB

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