tst-cond6.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. #include <sys/time.h>
  24. #include <sys/wait.h>
  25. #include <stdint.h>
  26. int *condition;
  27. static int
  28. do_test (void)
  29. {
  30. size_t ps = sysconf (_SC_PAGESIZE);
  31. char tmpfname[] = "/tmp/tst-cond6.XXXXXX";
  32. char data[ps];
  33. void *mem;
  34. int fd;
  35. pthread_mutexattr_t ma;
  36. pthread_mutex_t *mut1;
  37. pthread_mutex_t *mut2;
  38. pthread_condattr_t ca;
  39. pthread_cond_t *cond;
  40. pid_t pid;
  41. int result = 0;
  42. fd = mkstemp (tmpfname);
  43. if (fd == -1)
  44. {
  45. printf ("cannot open temporary file: %m\n");
  46. exit (1);
  47. }
  48. /* Make sure it is always removed. */
  49. unlink (tmpfname);
  50. /* Create one page of data. */
  51. memset (data, '\0', ps);
  52. /* Write the data to the file. */
  53. if (write (fd, data, ps) != (ssize_t) ps)
  54. {
  55. puts ("short write");
  56. exit (1);
  57. }
  58. mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  59. if (mem == MAP_FAILED)
  60. {
  61. printf ("mmap failed: %m\n");
  62. exit (1);
  63. }
  64. mut1 = (pthread_mutex_t *) (((uintptr_t) mem
  65. + __alignof (pthread_mutex_t))
  66. & ~(__alignof (pthread_mutex_t) - 1));
  67. mut2 = mut1 + 1;
  68. cond = (pthread_cond_t *) (((uintptr_t) (mut2 + 1)
  69. + __alignof (pthread_cond_t))
  70. & ~(__alignof (pthread_cond_t) - 1));
  71. condition = (int *) (((uintptr_t) (cond + 1) + __alignof (int))
  72. & ~(__alignof (int) - 1));
  73. if (pthread_mutexattr_init (&ma) != 0)
  74. {
  75. puts ("mutexattr_init failed");
  76. exit (1);
  77. }
  78. if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
  79. {
  80. puts ("mutexattr_setpshared failed");
  81. exit (1);
  82. }
  83. if (pthread_mutex_init (mut1, &ma) != 0)
  84. {
  85. puts ("1st mutex_init failed");
  86. exit (1);
  87. }
  88. if (pthread_mutex_init (mut2, &ma) != 0)
  89. {
  90. puts ("2nd mutex_init failed");
  91. exit (1);
  92. }
  93. if (pthread_condattr_init (&ca) != 0)
  94. {
  95. puts ("condattr_init failed");
  96. exit (1);
  97. }
  98. if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
  99. {
  100. puts ("condattr_setpshared failed");
  101. exit (1);
  102. }
  103. if (pthread_cond_init (cond, &ca) != 0)
  104. {
  105. puts ("cond_init failed");
  106. exit (1);
  107. }
  108. if (pthread_mutex_lock (mut1) != 0)
  109. {
  110. puts ("parent: 1st mutex_lock failed");
  111. exit (1);
  112. }
  113. puts ("going to fork now");
  114. pid = fork ();
  115. if (pid == -1)
  116. {
  117. puts ("fork failed");
  118. exit (1);
  119. }
  120. else if (pid == 0)
  121. {
  122. struct timespec ts;
  123. struct timeval tv;
  124. if (pthread_mutex_lock (mut2) != 0)
  125. {
  126. puts ("child: mutex_lock failed");
  127. exit (1);
  128. }
  129. if (pthread_mutex_unlock (mut1) != 0)
  130. {
  131. puts ("child: 1st mutex_unlock failed");
  132. exit (1);
  133. }
  134. if (gettimeofday (&tv, NULL) != 0)
  135. {
  136. puts ("gettimeofday failed");
  137. exit (1);
  138. }
  139. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  140. ts.tv_nsec += 500000000;
  141. if (ts.tv_nsec >= 1000000000)
  142. {
  143. ts.tv_nsec -= 1000000000;
  144. ++ts.tv_sec;
  145. }
  146. do
  147. if (pthread_cond_timedwait (cond, mut2, &ts) != 0)
  148. {
  149. puts ("child: cond_wait failed");
  150. exit (1);
  151. }
  152. while (*condition == 0);
  153. if (pthread_mutex_unlock (mut2) != 0)
  154. {
  155. puts ("child: 2nd mutex_unlock failed");
  156. exit (1);
  157. }
  158. puts ("child done");
  159. }
  160. else
  161. {
  162. int status;
  163. if (pthread_mutex_lock (mut1) != 0)
  164. {
  165. puts ("parent: 2nd mutex_lock failed");
  166. exit (1);
  167. }
  168. if (pthread_mutex_lock (mut2) != 0)
  169. {
  170. puts ("parent: 3rd mutex_lock failed");
  171. exit (1);
  172. }
  173. if (pthread_cond_signal (cond) != 0)
  174. {
  175. puts ("parent: cond_signal failed");
  176. exit (1);
  177. }
  178. *condition = 1;
  179. if (pthread_mutex_unlock (mut2) != 0)
  180. {
  181. puts ("parent: mutex_unlock failed");
  182. exit (1);
  183. }
  184. puts ("waiting for child");
  185. waitpid (pid, &status, 0);
  186. result |= status;
  187. puts ("parent done");
  188. }
  189. return result;
  190. }
  191. #define TEST_FUNCTION do_test ()
  192. #include "../test-skeleton.c"