tst-cond6.c 4.9 KB

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