tst-cond14.c 2.7 KB

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