tst-barrier4.c 2.7 KB

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