tst-cond14.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <pthread.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #if defined(__GLIBC__) || defined(__UCLIBC__)
  21. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  22. static pthread_mutex_t mut = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
  23. static pthread_mutex_t mut2 = PTHREAD_MUTEX_INITIALIZER;
  24. static void *
  25. tf (void *p)
  26. {
  27. if (pthread_mutex_lock (&mut) != 0)
  28. {
  29. printf ("%s: 1st mutex_lock failed\n", __func__);
  30. exit (1);
  31. }
  32. if (pthread_mutex_lock (&mut) != 0)
  33. {
  34. printf ("%s: 2nd mutex_lock failed\n", __func__);
  35. exit (1);
  36. }
  37. if (pthread_mutex_lock (&mut) != 0)
  38. {
  39. printf ("%s: 3rd mutex_lock failed\n", __func__);
  40. exit (1);
  41. }
  42. if (pthread_mutex_unlock (&mut2) != 0)
  43. {
  44. printf ("%s: mutex_unlock failed\n", __func__);
  45. exit (1);
  46. }
  47. if (pthread_cond_wait (&cond, &mut) != 0)
  48. {
  49. printf ("%s: cond_wait failed\n", __func__);
  50. exit (1);
  51. }
  52. puts ("child: done");
  53. return NULL;
  54. }
  55. #endif
  56. static int
  57. do_test (void)
  58. {
  59. #if defined(__GLIBC__) || defined(__UCLIBC__)
  60. if (pthread_mutex_lock (&mut2) != 0)
  61. {
  62. puts ("1st mutex_lock failed");
  63. return 1;
  64. }
  65. puts ("parent: create child");
  66. pthread_t th;
  67. int err = pthread_create (&th, NULL, tf, NULL);
  68. if (err != 0)
  69. {
  70. printf ("parent: cannot create thread: %s\n", strerror (err));
  71. return 1;
  72. }
  73. /* We have to synchronize with the child. */
  74. if (pthread_mutex_lock (&mut2) != 0)
  75. {
  76. puts ("2nd mutex_lock failed");
  77. return 1;
  78. }
  79. /* Give the child to reach to pthread_cond_wait. */
  80. sleep (1);
  81. if (pthread_cond_signal (&cond) != 0)
  82. {
  83. puts ("cond_signal failed");
  84. return 1;
  85. }
  86. err = pthread_join (th, NULL);
  87. if (err != 0)
  88. {
  89. printf ("parent: failed to join: %s\n", strerror (err));
  90. return 1;
  91. }
  92. puts ("done");
  93. return 0;
  94. #else
  95. return 23;
  96. #endif
  97. }
  98. #define TEST_FUNCTION do_test ()
  99. #define TIMEOUT 3
  100. #include "../test-skeleton.c"