tst-rwlock4.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, 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 <stdint.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/mman.h>
  24. #include <sys/wait.h>
  25. static int
  26. do_test (void)
  27. {
  28. size_t ps = sysconf (_SC_PAGESIZE);
  29. char tmpfname[] = "/tmp/tst-rwlock4.XXXXXX";
  30. char data[ps];
  31. void *mem;
  32. int fd;
  33. pthread_rwlock_t *r;
  34. pthread_rwlockattr_t a;
  35. pid_t pid;
  36. char *p;
  37. int err;
  38. int s;
  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. p = (char *) (r + 1);
  64. if (pthread_rwlockattr_init (&a) != 0)
  65. {
  66. puts ("rwlockattr_init failed");
  67. return 1;
  68. }
  69. if (pthread_rwlockattr_getpshared (&a, &s) != 0)
  70. {
  71. puts ("1st rwlockattr_getpshared failed");
  72. return 1;
  73. }
  74. if (s != PTHREAD_PROCESS_PRIVATE)
  75. {
  76. puts ("default pshared value wrong");
  77. return 1;
  78. }
  79. if (pthread_rwlockattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0)
  80. {
  81. puts ("rwlockattr_setpshared failed");
  82. return 1;
  83. }
  84. if (pthread_rwlockattr_getpshared (&a, &s) != 0)
  85. {
  86. puts ("2nd rwlockattr_getpshared failed");
  87. return 1;
  88. }
  89. if (s != PTHREAD_PROCESS_SHARED)
  90. {
  91. puts ("pshared value after setpshared call wrong");
  92. return 1;
  93. }
  94. if (pthread_rwlock_init (r, &a) != 0)
  95. {
  96. puts ("rwlock_init failed");
  97. return 1;
  98. }
  99. if (pthread_rwlock_rdlock (r) != 0)
  100. {
  101. puts ("rwlock_rdlock failed");
  102. return 1;
  103. }
  104. if (pthread_rwlockattr_destroy (&a) != 0)
  105. {
  106. puts ("rwlockattr_destroy failed");
  107. return 1;
  108. }
  109. err = pthread_rwlock_trywrlock (r);
  110. if (err == 0)
  111. {
  112. puts ("rwlock_trywrlock succeeded");
  113. return 1;
  114. }
  115. else if (err != EBUSY)
  116. {
  117. puts ("rwlock_trywrlock didn't return EBUSY");
  118. return 1;
  119. }
  120. *p = 0;
  121. puts ("going to fork now");
  122. pid = fork ();
  123. if (pid == -1)
  124. {
  125. puts ("fork failed");
  126. return 1;
  127. }
  128. else if (pid == 0)
  129. {
  130. /* Play some lock ping-pong. It's our turn to unlock first. */
  131. if ((*p)++ != 0)
  132. {
  133. puts ("child: *p != 0");
  134. return 1;
  135. }
  136. if (pthread_rwlock_unlock (r) != 0)
  137. {
  138. puts ("child: 1st rwlock_unlock failed");
  139. return 1;
  140. }
  141. puts ("child done");
  142. }
  143. else
  144. {
  145. if (pthread_rwlock_wrlock (r) != 0)
  146. {
  147. puts ("parent: rwlock_wrlock failed");
  148. return 1;
  149. }
  150. if (*p != 1)
  151. {
  152. puts ("*p != 1");
  153. return 1;
  154. }
  155. puts ("parent done");
  156. }
  157. return 0;
  158. }
  159. #define TEST_FUNCTION do_test ()
  160. #include "../test-skeleton.c"