tst-cond15.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, 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 <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/time.h>
  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. static int
  79. do_test (void)
  80. {
  81. if (pthread_mutex_lock (&mut2) != 0)
  82. {
  83. puts ("1st mutex_lock failed");
  84. return 1;
  85. }
  86. puts ("parent: create 1st child");
  87. pthread_t th;
  88. int err = pthread_create (&th, NULL, tf, NULL);
  89. if (err != 0)
  90. {
  91. printf ("parent: cannot 1st create thread: %s\n", strerror (err));
  92. return 1;
  93. }
  94. /* We have to synchronize with the child. */
  95. if (pthread_mutex_lock (&mut2) != 0)
  96. {
  97. puts ("2nd mutex_lock failed");
  98. return 1;
  99. }
  100. /* Give the child to reach to pthread_cond_wait. */
  101. sleep (1);
  102. if (pthread_cond_signal (&cond) != 0)
  103. {
  104. puts ("cond_signal failed");
  105. return 1;
  106. }
  107. err = pthread_join (th, NULL);
  108. if (err != 0)
  109. {
  110. printf ("parent: failed to join: %s\n", strerror (err));
  111. return 1;
  112. }
  113. puts ("parent: create 2nd child");
  114. err = pthread_create (&th, NULL, tf, (void *) 1l);
  115. if (err != 0)
  116. {
  117. printf ("parent: cannot 2nd create thread: %s\n", strerror (err));
  118. return 1;
  119. }
  120. err = pthread_join (th, NULL);
  121. if (err != 0)
  122. {
  123. printf ("parent: failed to join: %s\n", strerror (err));
  124. return 1;
  125. }
  126. puts ("done");
  127. return 0;
  128. }
  129. #define TEST_FUNCTION do_test ()
  130. #define TIMEOUT 6
  131. #include "../test-skeleton.c"