tst-cond2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <error.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
  20. static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
  21. static pthread_barrier_t bar;
  22. static void *
  23. tf (void *a)
  24. {
  25. int i = (long int) a;
  26. int err;
  27. printf ("child %d: lock\n", i);
  28. err = pthread_mutex_lock (&mut);
  29. if (err != 0)
  30. error (EXIT_FAILURE, err, "locking in child failed");
  31. printf ("child %d: sync\n", i);
  32. int e = pthread_barrier_wait (&bar);
  33. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  34. {
  35. puts ("child: barrier_wait failed");
  36. exit (1);
  37. }
  38. printf ("child %d: wait\n", i);
  39. err = pthread_cond_wait (&cond, &mut);
  40. if (err != 0)
  41. error (EXIT_FAILURE, err, "child %d: failed to wait", i);
  42. printf ("child %d: woken up\n", i);
  43. err = pthread_mutex_unlock (&mut);
  44. if (err != 0)
  45. error (EXIT_FAILURE, err, "child %d: unlock[2] failed", i);
  46. printf ("child %d: done\n", i);
  47. return NULL;
  48. }
  49. #define N 10
  50. static int
  51. do_test (void)
  52. {
  53. pthread_t th[N];
  54. int i;
  55. int err;
  56. printf ("&cond = %p\n&mut = %p\n", &cond, &mut);
  57. if (pthread_barrier_init (&bar, NULL, 2) != 0)
  58. {
  59. puts ("barrier_init failed");
  60. exit (1);
  61. }
  62. pthread_attr_t at;
  63. if (pthread_attr_init (&at) != 0)
  64. {
  65. puts ("attr_init failed");
  66. return 1;
  67. }
  68. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  69. {
  70. puts ("attr_setstacksize failed");
  71. return 1;
  72. }
  73. for (i = 0; i < N; ++i)
  74. {
  75. printf ("create thread %d\n", i);
  76. err = pthread_create (&th[i], &at, tf, (void *) (long int) i);
  77. if (err != 0)
  78. error (EXIT_FAILURE, err, "cannot create thread %d", i);
  79. printf ("wait for child %d\n", i);
  80. /* Wait for the child to start up and get the mutex for the
  81. conditional variable. */
  82. int e = pthread_barrier_wait (&bar);
  83. if (e != 0 && e != PTHREAD_BARRIER_SERIAL_THREAD)
  84. {
  85. puts ("barrier_wait failed");
  86. exit (1);
  87. }
  88. }
  89. if (pthread_attr_destroy (&at) != 0)
  90. {
  91. puts ("attr_destroy failed");
  92. return 1;
  93. }
  94. puts ("get lock outselves");
  95. err = pthread_mutex_lock (&mut);
  96. if (err != 0)
  97. error (EXIT_FAILURE, err, "mut locking failed");
  98. puts ("broadcast");
  99. /* Wake up all threads. */
  100. err = pthread_cond_broadcast (&cond);
  101. if (err != 0)
  102. error (EXIT_FAILURE, err, "parent: broadcast failed");
  103. err = pthread_mutex_unlock (&mut);
  104. if (err != 0)
  105. error (EXIT_FAILURE, err, "mut unlocking failed");
  106. /* Join all threads. */
  107. for (i = 0; i < N; ++i)
  108. {
  109. printf ("join thread %d\n", i);
  110. err = pthread_join (th[i], NULL);
  111. if (err != 0)
  112. error (EXIT_FAILURE, err, "join of child %d failed", i);
  113. }
  114. puts ("done");
  115. return 0;
  116. }
  117. #define TEST_FUNCTION do_test ()
  118. #include "../test-skeleton.c"