tst-mutex9.c 4.0 KB

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