tst-cond4.c 5.4 KB

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