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