tst-barrier2.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <errno.h>
  16. #include <pthread.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. #include <sys/wait.h>
  24. static int
  25. do_test (void)
  26. {
  27. size_t ps = sysconf (_SC_PAGESIZE);
  28. char tmpfname[] = "/tmp/tst-barrier2.XXXXXX";
  29. char data[ps];
  30. void *mem;
  31. int fd;
  32. pthread_barrier_t *b;
  33. pthread_barrierattr_t a;
  34. pid_t pid;
  35. int serials = 0;
  36. int cnt;
  37. int status;
  38. int p;
  39. fd = mkstemp (tmpfname);
  40. if (fd == -1)
  41. {
  42. printf ("cannot open temporary file: %m\n");
  43. return 1;
  44. }
  45. /* Make sure it is always removed. */
  46. unlink (tmpfname);
  47. /* Create one page of data. */
  48. memset (data, '\0', ps);
  49. /* Write the data to the file. */
  50. if (write (fd, data, ps) != (ssize_t) ps)
  51. {
  52. puts ("short write");
  53. return 1;
  54. }
  55. mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  56. if (mem == MAP_FAILED)
  57. {
  58. printf ("mmap failed: %m\n");
  59. return 1;
  60. }
  61. b = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t))
  62. & ~(__alignof (pthread_barrier_t) - 1));
  63. if (pthread_barrierattr_init (&a) != 0)
  64. {
  65. puts ("barrierattr_init failed");
  66. return 1;
  67. }
  68. if (pthread_barrierattr_getpshared (&a, &p) != 0)
  69. {
  70. puts ("1st barrierattr_getpshared failed");
  71. return 1;
  72. }
  73. if (p != PTHREAD_PROCESS_PRIVATE)
  74. {
  75. puts ("default pshared value wrong");
  76. return 1;
  77. }
  78. if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
  79. {
  80. puts ("barrierattr_setpshared failed");
  81. return 1;
  82. }
  83. if (pthread_barrierattr_getpshared (&a, &p) != 0)
  84. {
  85. puts ("2nd barrierattr_getpshared failed");
  86. return 1;
  87. }
  88. if (p != PTHREAD_PROCESS_SHARED)
  89. {
  90. puts ("pshared value after setpshared call wrong");
  91. return 1;
  92. }
  93. if (pthread_barrier_init (b, &a, 2) != 0)
  94. {
  95. puts ("barrier_init failed");
  96. return 1;
  97. }
  98. if (pthread_barrierattr_destroy (&a) != 0)
  99. {
  100. puts ("barrierattr_destroy failed");
  101. return 1;
  102. }
  103. puts ("going to fork now");
  104. pid = fork ();
  105. if (pid == -1)
  106. {
  107. puts ("fork failed");
  108. return 1;
  109. }
  110. /* Just to be sure we don't hang forever. */
  111. alarm (4);
  112. #define N 30
  113. for (cnt = 0; cnt < N; ++cnt)
  114. {
  115. int e;
  116. e = pthread_barrier_wait (b);
  117. if (e == PTHREAD_BARRIER_SERIAL_THREAD)
  118. ++serials;
  119. else if (e != 0)
  120. {
  121. printf ("%s: barrier_wait returned value %d != 0 and PTHREAD_BARRIER_SERIAL_THREAD\n",
  122. pid == 0 ? "child" : "parent", e);
  123. return 1;
  124. }
  125. }
  126. alarm (0);
  127. printf ("%s: was %d times the serial thread\n",
  128. pid == 0 ? "child" : "parent", serials);
  129. if (pid == 0)
  130. /* The child. Pass the number of times we had the serializing
  131. thread back to the parent. */
  132. exit (serials);
  133. if (waitpid (pid, &status, 0) != pid)
  134. {
  135. puts ("waitpid failed");
  136. return 1;
  137. }
  138. if (!WIFEXITED (status))
  139. {
  140. puts ("child exited abnormally");
  141. return 1;
  142. }
  143. if (WEXITSTATUS (status) + serials != N)
  144. {
  145. printf ("total number of serials is %d, expected %d\n",
  146. WEXITSTATUS (status) + serials, N);
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. #define TEST_FUNCTION do_test ()
  152. #include "../test-skeleton.c"