tst-rwlock6.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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, 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 <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <sys/time.h>
  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. static int
  85. do_test (void)
  86. {
  87. size_t cnt;
  88. for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
  89. {
  90. pthread_rwlock_t r;
  91. pthread_rwlockattr_t a;
  92. if (pthread_rwlockattr_init (&a) != 0)
  93. {
  94. printf ("round %Zu: rwlockattr_t failed\n", cnt);
  95. exit (1);
  96. }
  97. if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
  98. {
  99. printf ("round %Zu: rwlockattr_setkind failed\n", cnt);
  100. exit (1);
  101. }
  102. if (pthread_rwlock_init (&r, &a) != 0)
  103. {
  104. printf ("round %Zu: rwlock_init failed\n", cnt);
  105. exit (1);
  106. }
  107. if (pthread_rwlockattr_destroy (&a) != 0)
  108. {
  109. printf ("round %Zu: rwlockattr_destroy failed\n", cnt);
  110. exit (1);
  111. }
  112. struct timeval tv;
  113. (void) gettimeofday (&tv, NULL);
  114. struct timespec ts;
  115. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  116. ++ts.tv_sec;
  117. /* Get a write lock. */
  118. int e = pthread_rwlock_timedwrlock (&r, &ts);
  119. if (e != 0)
  120. {
  121. printf ("round %Zu: rwlock_timedwrlock failed (%d)\n", cnt, e);
  122. exit (1);
  123. }
  124. puts ("1st timedwrlock done");
  125. (void) gettimeofday (&tv, NULL);
  126. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  127. ++ts.tv_sec;
  128. e = pthread_rwlock_timedrdlock (&r, &ts);
  129. if (e == 0)
  130. {
  131. puts ("timedrdlock succeeded");
  132. exit (1);
  133. }
  134. if (e != EDEADLK)
  135. {
  136. puts ("timedrdlock did not return EDEADLK");
  137. exit (1);
  138. }
  139. puts ("1st timedrdlock done");
  140. (void) gettimeofday (&tv, NULL);
  141. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  142. ++ts.tv_sec;
  143. e = pthread_rwlock_timedwrlock (&r, &ts);
  144. if (e == 0)
  145. {
  146. puts ("2nd timedwrlock succeeded");
  147. exit (1);
  148. }
  149. if (e != EDEADLK)
  150. {
  151. puts ("2nd timedwrlock did not return EDEADLK");
  152. exit (1);
  153. }
  154. puts ("2nd timedwrlock done");
  155. pthread_t th;
  156. if (pthread_create (&th, NULL, tf, &r) != 0)
  157. {
  158. printf ("round %Zu: create failed\n", cnt);
  159. exit (1);
  160. }
  161. puts ("started thread");
  162. void *status;
  163. if (pthread_join (th, &status) != 0)
  164. {
  165. printf ("round %Zu: join failed\n", cnt);
  166. exit (1);
  167. }
  168. if (status != NULL)
  169. {
  170. printf ("failure in round %Zu\n", cnt);
  171. exit (1);
  172. }
  173. puts ("joined thread");
  174. if (pthread_rwlock_destroy (&r) != 0)
  175. {
  176. printf ("round %Zu: rwlock_destroy failed\n", cnt);
  177. exit (1);
  178. }
  179. }
  180. return 0;
  181. }
  182. #define TEST_FUNCTION do_test ()
  183. #include "../test-skeleton.c"