tst-cond12.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Copyright (C) 2003 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
  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 <signal.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <unistd.h>
  21. #include <sys/mman.h>
  22. #include <sys/wait.h>
  23. #ifdef __ARCH_USE_MMU__
  24. static char fname[] = "/tmp/tst-cond12-XXXXXX";
  25. static int fd;
  26. static void prepare (void);
  27. #define PREPARE(argc, argv) prepare ()
  28. static int do_test (void);
  29. #define TEST_FUNCTION do_test ()
  30. #include "../test-skeleton.c"
  31. static void
  32. prepare (void)
  33. {
  34. fd = mkstemp (fname);
  35. if (fd == -1)
  36. {
  37. printf ("mkstemp failed: %m\n");
  38. exit (1);
  39. }
  40. add_temp_file (fname);
  41. if (ftruncate (fd, 1000) < 0)
  42. {
  43. printf ("ftruncate failed: %m\n");
  44. exit (1);
  45. }
  46. }
  47. static int
  48. do_test (void)
  49. {
  50. struct
  51. {
  52. pthread_mutex_t m;
  53. pthread_cond_t c;
  54. int var;
  55. } *p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  56. if (p == MAP_FAILED)
  57. {
  58. printf ("initial mmap failed: %m\n");
  59. return 1;
  60. }
  61. pthread_mutexattr_t ma;
  62. if (pthread_mutexattr_init (&ma) != 0)
  63. {
  64. puts ("mutexattr_init failed");
  65. return 1;
  66. }
  67. if (pthread_mutexattr_setpshared (&ma, 1) != 0)
  68. {
  69. puts ("mutexattr_setpshared failed");
  70. return 1;
  71. }
  72. if (pthread_mutex_init (&p->m, &ma) != 0)
  73. {
  74. puts ("mutex_init failed");
  75. return 1;
  76. }
  77. if (pthread_mutexattr_destroy (&ma) != 0)
  78. {
  79. puts ("mutexattr_destroy failed");
  80. return 1;
  81. }
  82. pthread_condattr_t ca;
  83. if (pthread_condattr_init (&ca) != 0)
  84. {
  85. puts ("condattr_init failed");
  86. return 1;
  87. }
  88. if (pthread_condattr_setpshared (&ca, 1) != 0)
  89. {
  90. puts ("condattr_setpshared failed");
  91. return 1;
  92. }
  93. if (pthread_cond_init (&p->c, &ca) != 0)
  94. {
  95. puts ("mutex_init failed");
  96. return 1;
  97. }
  98. if (pthread_condattr_destroy (&ca) != 0)
  99. {
  100. puts ("condattr_destroy failed");
  101. return 1;
  102. }
  103. if (pthread_mutex_lock (&p->m) != 0)
  104. {
  105. puts ("initial mutex_lock failed");
  106. return 1;
  107. }
  108. p->var = 42;
  109. pid_t pid = fork ();
  110. if (pid == -1)
  111. {
  112. printf ("fork failed: %m\n");
  113. return 1;
  114. }
  115. if (pid == 0)
  116. {
  117. void *oldp = p;
  118. p = mmap (NULL, sizeof (*p), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
  119. if (p == oldp)
  120. {
  121. puts ("child: mapped to same address");
  122. kill (getppid (), SIGKILL);
  123. exit (1);
  124. }
  125. munmap (oldp, sizeof (*p));
  126. if (pthread_mutex_lock (&p->m) != 0)
  127. {
  128. puts ("child: mutex_lock failed");
  129. kill (getppid (), SIGKILL);
  130. exit (1);
  131. }
  132. p->var = 0;
  133. #ifndef USE_COND_SIGNAL
  134. if (pthread_cond_broadcast (&p->c) != 0)
  135. {
  136. puts ("child: cond_broadcast failed");
  137. kill (getppid (), SIGKILL);
  138. exit (1);
  139. }
  140. #else
  141. if (pthread_cond_signal (&p->c) != 0)
  142. {
  143. puts ("child: cond_signal failed");
  144. kill (getppid (), SIGKILL);
  145. exit (1);
  146. }
  147. #endif
  148. if (pthread_mutex_unlock (&p->m) != 0)
  149. {
  150. puts ("child: mutex_unlock failed");
  151. kill (getppid (), SIGKILL);
  152. exit (1);
  153. }
  154. exit (0);
  155. }
  156. do
  157. pthread_cond_wait (&p->c, &p->m);
  158. while (p->var != 0);
  159. if (TEMP_FAILURE_RETRY (waitpid (pid, NULL, 0)) != pid)
  160. {
  161. printf ("waitpid failed: %m\n");
  162. kill (pid, SIGKILL);
  163. return 1;
  164. }
  165. return 0;
  166. }
  167. #else
  168. int main(void)
  169. {
  170. printf("Skipping test on non-mmu host!\n");
  171. return 23;
  172. }
  173. #endif