tst-cond12.c 4.0 KB

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