tst-barrier2.c 4.2 KB

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