tst-rwlock6.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* Copyright (C) 2002, 2003 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 <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <sys/time.h>
  21. static int kind[] =
  22. {
  23. PTHREAD_RWLOCK_PREFER_READER_NP,
  24. PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP,
  25. PTHREAD_RWLOCK_PREFER_WRITER_NP,
  26. };
  27. static void *
  28. tf (void *arg)
  29. {
  30. pthread_rwlock_t *r = arg;
  31. /* Timeout: 0.3 secs. */
  32. struct timeval tv;
  33. (void) gettimeofday (&tv, NULL);
  34. struct timespec ts;
  35. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  36. ts.tv_nsec += 300000000;
  37. if (ts.tv_nsec >= 1000000000)
  38. {
  39. ts.tv_nsec -= 1000000000;
  40. ++ts.tv_sec;
  41. }
  42. puts ("child calling timedrdlock");
  43. int err = pthread_rwlock_timedrdlock (r, &ts);
  44. if (err == 0)
  45. {
  46. puts ("rwlock_timedrdlock returned");
  47. pthread_exit ((void *) 1l);
  48. }
  49. if (err != ETIMEDOUT)
  50. {
  51. printf ("err = %s (%d), expected %s (%d)\n",
  52. strerror (err), err, strerror (ETIMEDOUT), ETIMEDOUT);
  53. pthread_exit ((void *) 1l);
  54. }
  55. puts ("1st child timedrdlock done");
  56. struct timeval tv2;
  57. (void) gettimeofday (&tv2, NULL);
  58. timersub (&tv2, &tv, &tv);
  59. if (tv.tv_usec < 200000)
  60. {
  61. puts ("timeout too short");
  62. pthread_exit ((void *) 1l);
  63. }
  64. (void) gettimeofday (&tv, NULL);
  65. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  66. ts.tv_sec += 10;
  67. /* Note that the following operation makes ts invalid. */
  68. ts.tv_nsec += 1000000000;
  69. err = pthread_rwlock_timedrdlock (r, &ts);
  70. if (err == 0)
  71. {
  72. puts ("2nd timedrdlock succeeded");
  73. pthread_exit ((void *) 1l);
  74. }
  75. if (err != EINVAL)
  76. {
  77. puts ("2nd timedrdlock did not return EINVAL");
  78. pthread_exit ((void *) 1l);
  79. }
  80. puts ("2nd child timedrdlock done");
  81. return NULL;
  82. }
  83. static int
  84. do_test (void)
  85. {
  86. size_t cnt;
  87. for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
  88. {
  89. pthread_rwlock_t r;
  90. pthread_rwlockattr_t a;
  91. if (pthread_rwlockattr_init (&a) != 0)
  92. {
  93. printf ("round %Zu: rwlockattr_t failed\n", cnt);
  94. exit (1);
  95. }
  96. if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
  97. {
  98. printf ("round %Zu: rwlockattr_setkind failed\n", cnt);
  99. exit (1);
  100. }
  101. if (pthread_rwlock_init (&r, &a) != 0)
  102. {
  103. printf ("round %Zu: rwlock_init failed\n", cnt);
  104. exit (1);
  105. }
  106. if (pthread_rwlockattr_destroy (&a) != 0)
  107. {
  108. printf ("round %Zu: rwlockattr_destroy failed\n", cnt);
  109. exit (1);
  110. }
  111. struct timeval tv;
  112. (void) gettimeofday (&tv, NULL);
  113. struct timespec ts;
  114. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  115. ++ts.tv_sec;
  116. /* Get a write lock. */
  117. int e = pthread_rwlock_timedwrlock (&r, &ts);
  118. if (e != 0)
  119. {
  120. printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt, e);
  121. exit (1);
  122. }
  123. puts ("1st timedwrlock done");
  124. (void) gettimeofday (&tv, NULL);
  125. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  126. ++ts.tv_sec;
  127. e = pthread_rwlock_timedrdlock (&r, &ts);
  128. if (e == 0)
  129. {
  130. puts ("timedrdlock succeeded");
  131. exit (1);
  132. }
  133. if (e != EDEADLK)
  134. {
  135. puts ("timedrdlock did not return EDEADLK");
  136. exit (1);
  137. }
  138. puts ("1st timedrdlock done");
  139. (void) gettimeofday (&tv, NULL);
  140. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  141. ++ts.tv_sec;
  142. e = pthread_rwlock_timedwrlock (&r, &ts);
  143. if (e == 0)
  144. {
  145. puts ("2nd timedwrlock succeeded");
  146. exit (1);
  147. }
  148. if (e != EDEADLK)
  149. {
  150. puts ("2nd timedwrlock did not return EDEADLK");
  151. exit (1);
  152. }
  153. puts ("2nd timedwrlock done");
  154. pthread_t th;
  155. if (pthread_create (&th, NULL, tf, &r) != 0)
  156. {
  157. printf ("round %Zu: create failed\n", cnt);
  158. exit (1);
  159. }
  160. puts ("started thread");
  161. void *status;
  162. if (pthread_join (th, &status) != 0)
  163. {
  164. printf ("round %Zu: join failed\n", cnt);
  165. exit (1);
  166. }
  167. if (status != NULL)
  168. {
  169. printf ("failure in round %Zu\n", cnt);
  170. exit (1);
  171. }
  172. puts ("joined thread");
  173. if (pthread_rwlock_destroy (&r) != 0)
  174. {
  175. printf ("round %Zu: rwlock_destroy failed\n", cnt);
  176. exit (1);
  177. }
  178. }
  179. return 0;
  180. }
  181. #define TEST_FUNCTION do_test ()
  182. #include "../test-skeleton.c"