tst-barrier4.c 2.6 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. /* This is a test for behavior not guaranteed by POSIX. */
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. static pthread_barrier_t b1;
  21. static pthread_barrier_t b2;
  22. #define N 20
  23. static void *
  24. tf (void *arg)
  25. {
  26. int round = 0;
  27. while (round++ < 30)
  28. {
  29. if (pthread_barrier_wait (&b1) == PTHREAD_BARRIER_SERIAL_THREAD)
  30. {
  31. pthread_barrier_destroy (&b1);
  32. if (pthread_barrier_init (&b1, NULL, N) != 0)
  33. {
  34. puts ("tf: 1st barrier_init failed");
  35. exit (1);
  36. }
  37. }
  38. if (pthread_barrier_wait (&b2) == PTHREAD_BARRIER_SERIAL_THREAD)
  39. {
  40. pthread_barrier_destroy (&b2);
  41. if (pthread_barrier_init (&b2, NULL, N) != 0)
  42. {
  43. puts ("tf: 2nd barrier_init failed");
  44. exit (1);
  45. }
  46. }
  47. }
  48. return NULL;
  49. }
  50. static int
  51. do_test (void)
  52. {
  53. pthread_attr_t at;
  54. int cnt;
  55. if (pthread_attr_init (&at) != 0)
  56. {
  57. puts ("attr_init failed");
  58. return 1;
  59. }
  60. if (pthread_attr_setstacksize (&at, 1 * 1024 * 1024) != 0)
  61. {
  62. puts ("attr_setstacksize failed");
  63. return 1;
  64. }
  65. if (pthread_barrier_init (&b1, NULL, N) != 0)
  66. {
  67. puts ("1st barrier_init failed");
  68. return 1;
  69. }
  70. if (pthread_barrier_init (&b2, NULL, N) != 0)
  71. {
  72. puts ("2nd barrier_init failed");
  73. return 1;
  74. }
  75. pthread_t th[N - 1];
  76. for (cnt = 0; cnt < N - 1; ++cnt)
  77. if (pthread_create (&th[cnt], &at, tf, NULL) != 0)
  78. {
  79. puts ("pthread_create failed");
  80. return 1;
  81. }
  82. if (pthread_attr_destroy (&at) != 0)
  83. {
  84. puts ("attr_destroy failed");
  85. return 1;
  86. }
  87. tf (NULL);
  88. for (cnt = 0; cnt < N - 1; ++cnt)
  89. if (pthread_join (th[cnt], NULL) != 0)
  90. {
  91. puts ("pthread_join failed");
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. #define TEST_FUNCTION do_test ()
  97. #include "../test-skeleton.c"