tst-rwlock12.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /* Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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 <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/mman.h>
  23. #include <sys/wait.h>
  24. #include "../test-skeleton.h"
  25. #ifdef __ARCH_USE_MMU__
  26. static int
  27. do_test (void)
  28. {
  29. size_t ps = sysconf (_SC_PAGESIZE);
  30. char tmpfname[] = "/tmp/tst-rwlock12.XXXXXX";
  31. char data[ps];
  32. void *mem;
  33. int fd;
  34. pthread_mutex_t *m;
  35. pthread_mutexattr_t ma;
  36. pthread_rwlock_t *r;
  37. pthread_rwlockattr_t ra;
  38. pid_t pid;
  39. int status = 0;
  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. r = (pthread_rwlock_t *) (((uintptr_t) mem + __alignof (pthread_rwlock_t))
  63. & ~(__alignof (pthread_rwlock_t) - 1));
  64. /* The following assumes alignment for a mutex is at least as high
  65. as that for a rwlock. Which is true in our case. */
  66. m = (pthread_mutex_t *) (r + 1);
  67. if (pthread_rwlockattr_init (&ra) != 0)
  68. {
  69. puts ("rwlockattr_init failed");
  70. return 1;
  71. }
  72. if (pthread_rwlockattr_setpshared (&ra, PTHREAD_PROCESS_SHARED) != 0)
  73. {
  74. puts ("rwlockattr_setpshared failed");
  75. return 1;
  76. }
  77. if (pthread_rwlock_init (r, &ra) != 0)
  78. {
  79. puts ("rwlock_init failed");
  80. return 1;
  81. }
  82. if (pthread_mutexattr_init (&ma) != 0)
  83. {
  84. puts ("rwlockattr_init failed");
  85. return 1;
  86. }
  87. if (pthread_mutexattr_setpshared (&ma, PTHREAD_PROCESS_SHARED) != 0)
  88. {
  89. puts ("mutexattr_setpshared failed");
  90. return 1;
  91. }
  92. if (pthread_mutex_init (m, &ma) != 0)
  93. {
  94. puts ("mutex_init failed");
  95. return 1;
  96. }
  97. /* Lock the mutex. */
  98. if (pthread_mutex_lock (m) != 0)
  99. {
  100. puts ("mutex_lock failed");
  101. return 1;
  102. }
  103. puts ("going to fork now");
  104. pid = fork ();
  105. if (pid == -1)
  106. {
  107. puts ("fork failed");
  108. return 1;
  109. }
  110. else if (pid == 0)
  111. {
  112. /* Lock the mutex. */
  113. if (pthread_mutex_lock (m) != 0)
  114. {
  115. puts ("child: mutex_lock failed");
  116. return 1;
  117. }
  118. /* Try to get the rwlock. */
  119. if (pthread_rwlock_trywrlock (r) == 0)
  120. {
  121. puts ("rwlock_trywrlock succeeded");
  122. return 1;
  123. }
  124. /* Try again. */
  125. struct timespec ts = { .tv_sec = 0, .tv_nsec = 500000000 };
  126. int e = pthread_rwlock_timedwrlock (r, &ts);
  127. if (e == 0)
  128. {
  129. puts ("rwlock_timedwrlock succeeded");
  130. return 1;
  131. }
  132. if (e != ETIMEDOUT)
  133. {
  134. puts ("rwlock_timedwrlock didn't return ETIMEDOUT");
  135. status = 1;
  136. }
  137. if (pthread_rwlock_tryrdlock (r) == 0)
  138. {
  139. puts ("rwlock_tryrdlock succeeded");
  140. return 1;
  141. }
  142. e = pthread_rwlock_timedrdlock (r, &ts);
  143. if (e == 0)
  144. {
  145. puts ("rwlock_timedrdlock succeeded");
  146. return 1;
  147. }
  148. if (e != ETIMEDOUT)
  149. {
  150. puts ("rwlock_timedrdlock didn't return ETIMEDOUT");
  151. status = 1;
  152. }
  153. }
  154. else
  155. {
  156. /* Lock the rwlock for writing. */
  157. if (pthread_rwlock_wrlock (r) != 0)
  158. {
  159. puts ("rwlock_wrlock failed");
  160. kill (pid, SIGTERM);
  161. return 1;
  162. }
  163. /* Allow the child to run. */
  164. if (pthread_mutex_unlock (m) != 0)
  165. {
  166. puts ("mutex_unlock failed");
  167. kill (pid, SIGTERM);
  168. return 1;
  169. }
  170. /* Just wait for the child. */
  171. if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid)
  172. {
  173. puts ("waitpid failed");
  174. kill (pid, SIGTERM);
  175. return 1;
  176. }
  177. }
  178. return status;
  179. }
  180. #define TEST_FUNCTION do_test ()
  181. #include "../test-skeleton.c"
  182. #else
  183. int main(void)
  184. {
  185. printf("Skipping test on non-mmu host!\n");
  186. return 23;
  187. }
  188. #endif