tst-mutex9.c 4.1 KB

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