tst-cond4.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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 <unistd.h>
  22. #include <sys/mman.h>
  23. #include <sys/wait.h>
  24. #include <stdint.h>
  25. int *condition;
  26. static int
  27. do_test (void)
  28. {
  29. size_t ps = sysconf (_SC_PAGESIZE);
  30. char tmpfname[] = "/tmp/tst-cond4.XXXXXX";
  31. char data[ps];
  32. void *mem;
  33. int fd;
  34. pthread_mutexattr_t ma;
  35. pthread_mutex_t *mut1;
  36. pthread_mutex_t *mut2;
  37. pthread_condattr_t ca;
  38. pthread_cond_t *cond;
  39. pid_t pid;
  40. int result = 0;
  41. int p;
  42. fd = mkstemp (tmpfname);
  43. if (fd == -1)
  44. {
  45. printf ("cannot open temporary file: %m\n");
  46. return 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. return 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. return 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. return 1;
  77. }
  78. if (pthread_mutexattr_getpshared (&ma, &p) != 0)
  79. {
  80. puts ("1st mutexattr_getpshared failed");
  81. return 1;
  82. }
  83. if (p != PTHREAD_PROCESS_PRIVATE)
  84. {
  85. puts ("default pshared value wrong");
  86. return 1;
  87. }
  88. if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
  89. {
  90. puts ("mutexattr_setpshared failed");
  91. return 1;
  92. }
  93. if (pthread_mutexattr_getpshared (&ma, &p) != 0)
  94. {
  95. puts ("2nd mutexattr_getpshared failed");
  96. return 1;
  97. }
  98. if (p != PTHREAD_PROCESS_SHARED)
  99. {
  100. puts ("pshared value after setpshared call wrong");
  101. return 1;
  102. }
  103. if (pthread_mutex_init (mut1, &ma) != 0)
  104. {
  105. puts ("1st mutex_init failed");
  106. return 1;
  107. }
  108. if (pthread_mutex_init (mut2, &ma) != 0)
  109. {
  110. puts ("2nd mutex_init failed");
  111. return 1;
  112. }
  113. if (pthread_condattr_init (&ca) != 0)
  114. {
  115. puts ("condattr_init failed");
  116. return 1;
  117. }
  118. if (pthread_condattr_getpshared (&ca, &p) != 0)
  119. {
  120. puts ("1st condattr_getpshared failed");
  121. return 1;
  122. }
  123. if (p != PTHREAD_PROCESS_PRIVATE)
  124. {
  125. puts ("default value for pshared in condattr wrong");
  126. return 1;
  127. }
  128. if (pthread_condattr_setpshared (&ca, PTHREAD_PROCESS_SHARED) != 0)
  129. {
  130. puts ("condattr_setpshared failed");
  131. return 1;
  132. }
  133. if (pthread_condattr_getpshared (&ca, &p) != 0)
  134. {
  135. puts ("2nd condattr_getpshared failed");
  136. return 1;
  137. }
  138. if (p != PTHREAD_PROCESS_SHARED)
  139. {
  140. puts ("pshared condattr still not set");
  141. return 1;
  142. }
  143. if (pthread_cond_init (cond, &ca) != 0)
  144. {
  145. puts ("cond_init failed");
  146. return 1;
  147. }
  148. if (pthread_mutex_lock (mut1) != 0)
  149. {
  150. puts ("parent: 1st mutex_lock failed");
  151. return 1;
  152. }
  153. puts ("going to fork now");
  154. pid = fork ();
  155. if (pid == -1)
  156. {
  157. puts ("fork failed");
  158. return 1;
  159. }
  160. else if (pid == 0)
  161. {
  162. if (pthread_mutex_lock (mut2) != 0)
  163. {
  164. puts ("child: mutex_lock failed");
  165. return 1;
  166. }
  167. if (pthread_mutex_unlock (mut1) != 0)
  168. {
  169. puts ("child: 1st mutex_unlock failed");
  170. return 1;
  171. }
  172. do
  173. if (pthread_cond_wait (cond, mut2) != 0)
  174. {
  175. puts ("child: cond_wait failed");
  176. return 1;
  177. }
  178. while (*condition == 0);
  179. if (pthread_mutex_unlock (mut2) != 0)
  180. {
  181. puts ("child: 2nd mutex_unlock failed");
  182. return 1;
  183. }
  184. puts ("child done");
  185. }
  186. else
  187. {
  188. int status;
  189. if (pthread_mutex_lock (mut1) != 0)
  190. {
  191. puts ("parent: 2nd mutex_lock failed");
  192. return 1;
  193. }
  194. if (pthread_mutex_lock (mut2) != 0)
  195. {
  196. puts ("parent: 3rd mutex_lock failed");
  197. return 1;
  198. }
  199. if (pthread_cond_signal (cond) != 0)
  200. {
  201. puts ("parent: cond_signal failed");
  202. return 1;
  203. }
  204. *condition = 1;
  205. if (pthread_mutex_unlock (mut2) != 0)
  206. {
  207. puts ("parent: mutex_unlock failed");
  208. return 1;
  209. }
  210. puts ("waiting for child");
  211. waitpid (pid, &status, 0);
  212. result |= status;
  213. puts ("parent done");
  214. }
  215. return result;
  216. }
  217. #define TEST_FUNCTION do_test ()
  218. #include "../test-skeleton.c"