tst-cond14.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  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. static int
  56. do_test (void)
  57. {
  58. if (pthread_mutex_lock (&mut2) != 0)
  59. {
  60. puts ("1st mutex_lock failed");
  61. return 1;
  62. }
  63. puts ("parent: create child");
  64. pthread_t th;
  65. int err = pthread_create (&th, NULL, tf, NULL);
  66. if (err != 0)
  67. {
  68. printf ("parent: cannot create thread: %s\n", strerror (err));
  69. return 1;
  70. }
  71. /* We have to synchronize with the child. */
  72. if (pthread_mutex_lock (&mut2) != 0)
  73. {
  74. puts ("2nd mutex_lock failed");
  75. return 1;
  76. }
  77. /* Give the child to reach to pthread_cond_wait. */
  78. sleep (1);
  79. if (pthread_cond_signal (&cond) != 0)
  80. {
  81. puts ("cond_signal failed");
  82. return 1;
  83. }
  84. err = pthread_join (th, NULL);
  85. if (err != 0)
  86. {
  87. printf ("parent: failed to join: %s\n", strerror (err));
  88. return 1;
  89. }
  90. puts ("done");
  91. return 0;
  92. }
  93. #define TEST_FUNCTION do_test ()
  94. #define TIMEOUT 3
  95. #include "../test-skeleton.c"