tst-rwlock6.c 4.9 KB

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