tst-mutex9.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* Copyright (C) 2003, 2004 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 <stdio.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <sys/mman.h>
  21. #include <sys/wait.h>
  22. #include <stdint.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include "../test-skeleton.h"
  26. #ifdef __ARCH_USE_MMU__
  27. //int gettimeofday(struct timeval *tv, struct timezone *tz);
  28. static int
  29. do_test (void)
  30. {
  31. size_t ps = sysconf (_SC_PAGESIZE);
  32. char tmpfname[] = "/tmp/tst-mutex9.XXXXXX";
  33. char data[ps];
  34. void *mem;
  35. int fd;
  36. pthread_mutex_t *m;
  37. pthread_mutexattr_t a;
  38. pid_t pid;
  39. char *p;
  40. fd = mkstemp (tmpfname);
  41. if (fd == -1)
  42. {
  43. printf ("cannot open temporary file: %m\n");
  44. return 1;
  45. }
  46. /* Make sure it is always removed. */
  47. unlink (tmpfname);
  48. /* Create one page of data. */
  49. memset (data, '\0', ps);
  50. /* Write the data to the file. */
  51. if (write (fd, data, ps) != (ssize_t) ps)
  52. {
  53. puts ("short write");
  54. return 1;
  55. }
  56. mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  57. if (mem == MAP_FAILED)
  58. {
  59. printf ("mmap failed: %m\n");
  60. return 1;
  61. }
  62. m = (pthread_mutex_t *) (((uintptr_t) mem + __alignof (pthread_mutex_t))
  63. & ~(__alignof (pthread_mutex_t) - 1));
  64. p = (char *) (m + 1);
  65. if (pthread_mutexattr_init (&a) != 0)
  66. {
  67. puts ("mutexattr_init failed");
  68. return 1;
  69. }
  70. if (pthread_mutexattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
  71. {
  72. puts ("mutexattr_setpshared failed");
  73. return 1;
  74. }
  75. if (pthread_mutexattr_settype (&a, PTHREAD_MUTEX_RECURSIVE) != 0)
  76. {
  77. puts ("mutexattr_settype failed");
  78. return 1;
  79. }
  80. if (pthread_mutex_init (m, &a) != 0)
  81. {
  82. puts ("mutex_init failed");
  83. return 1;
  84. }
  85. if (pthread_mutex_lock (m) != 0)
  86. {
  87. puts ("mutex_lock failed");
  88. return 1;
  89. }
  90. if (pthread_mutexattr_destroy (&a) != 0)
  91. {
  92. puts ("mutexattr_destroy failed");
  93. return 1;
  94. }
  95. puts ("going to fork now");
  96. pid = fork ();
  97. if (pid == -1)
  98. {
  99. puts ("fork failed");
  100. return 1;
  101. }
  102. else if (pid == 0)
  103. {
  104. if (pthread_mutex_trylock (m) == 0)
  105. {
  106. puts ("child: mutex_trylock succeeded");
  107. exit (1);
  108. }
  109. if (pthread_mutex_unlock (m) == 0)
  110. {
  111. puts ("child: mutex_unlock succeeded");
  112. exit (1);
  113. }
  114. struct timeval tv;
  115. gettimeofday (&tv, NULL);
  116. struct timespec ts;
  117. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  118. ts.tv_nsec += 500000000;
  119. if (ts.tv_nsec >= 1000000000)
  120. {
  121. ++ts.tv_sec;
  122. ts.tv_nsec -= 1000000000;
  123. }
  124. int e = pthread_mutex_timedlock (m, &ts);
  125. if (e == 0)
  126. {
  127. puts ("child: mutex_timedlock succeeded");
  128. exit (1);
  129. }
  130. if (e != ETIMEDOUT)
  131. {
  132. puts ("child: mutex_timedlock didn't time out");
  133. exit (1);
  134. }
  135. alarm (1);
  136. pthread_mutex_lock (m);
  137. puts ("child: mutex_lock returned");
  138. exit (0);
  139. }
  140. sleep (2);
  141. int status;
  142. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  143. {
  144. puts ("waitpid failed");
  145. return 1;
  146. }
  147. if (! WIFSIGNALED (status))
  148. {
  149. puts ("child not killed by signal");
  150. return 1;
  151. }
  152. if (WTERMSIG (status) != SIGALRM)
  153. {
  154. puts ("child not killed by SIGALRM");
  155. return 1;
  156. }
  157. return 0;
  158. }
  159. #define TIMEOUT 3
  160. #define TEST_FUNCTION do_test ()
  161. #include "../test-skeleton.c"
  162. #else
  163. int main(void)
  164. {
  165. printf("Skipping test on non-mmu host!\n");
  166. return 23;
  167. }
  168. #endif