tst-rwlock12.c 4.6 KB

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