tst-cond2.c 3.7 KB

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