tst-rwlock7.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. int err = pthread_rwlock_timedwrlock (r, &ts);
  44. if (err == 0)
  45. {
  46. puts ("rwlock_timedwrlock 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. struct timeval tv2;
  56. (void) gettimeofday (&tv2, NULL);
  57. timersub (&tv2, &tv, &tv);
  58. if (tv.tv_usec < 200000)
  59. {
  60. puts ("timeout too short");
  61. pthread_exit ((void *) 1l);
  62. }
  63. (void) gettimeofday (&tv, NULL);
  64. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  65. ts.tv_sec += 10;
  66. /* Note that the following operation makes ts invalid. */
  67. ts.tv_nsec += 1000000000;
  68. err = pthread_rwlock_timedwrlock (r, &ts);
  69. if (err == 0)
  70. {
  71. puts ("2nd timedwrlock succeeded");
  72. pthread_exit ((void *) 1l);
  73. }
  74. if (err != EINVAL)
  75. {
  76. puts ("2nd timedwrlock did not return EINVAL");
  77. pthread_exit ((void *) 1l);
  78. }
  79. return NULL;
  80. }
  81. static int
  82. do_test (void)
  83. {
  84. size_t cnt;
  85. for (cnt = 0; cnt < sizeof (kind) / sizeof (kind[0]); ++cnt)
  86. {
  87. pthread_rwlock_t r;
  88. pthread_rwlockattr_t a;
  89. if (pthread_rwlockattr_init (&a) != 0)
  90. {
  91. printf ("round %Zu: rwlockattr_t failed\n", cnt);
  92. exit (1);
  93. }
  94. if (pthread_rwlockattr_setkind_np (&a, kind[cnt]) != 0)
  95. {
  96. printf ("round %Zu: rwlockattr_setkind failed\n", cnt);
  97. exit (1);
  98. }
  99. if (pthread_rwlock_init (&r, &a) != 0)
  100. {
  101. printf ("round %Zu: rwlock_init failed\n", cnt);
  102. exit (1);
  103. }
  104. if (pthread_rwlockattr_destroy (&a) != 0)
  105. {
  106. printf ("round %Zu: rwlockattr_destroy failed\n", cnt);
  107. exit (1);
  108. }
  109. struct timeval tv;
  110. (void) gettimeofday (&tv, NULL);
  111. struct timespec ts;
  112. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  113. ++ts.tv_sec;
  114. /* Get a read lock. */
  115. if (pthread_rwlock_timedrdlock (&r, &ts) != 0)
  116. {
  117. printf ("round %Zu: rwlock_timedrdlock failed\n", cnt);
  118. exit (1);
  119. }
  120. pthread_t th;
  121. if (pthread_create (&th, NULL, tf, &r) != 0)
  122. {
  123. printf ("round %Zu: create failed\n", cnt);
  124. exit (1);
  125. }
  126. void *status;
  127. if (pthread_join (th, &status) != 0)
  128. {
  129. printf ("round %Zu: join failed\n", cnt);
  130. exit (1);
  131. }
  132. if (status != NULL)
  133. {
  134. printf ("failure in round %Zu\n", cnt);
  135. exit (1);
  136. }
  137. if (pthread_rwlock_destroy (&r) != 0)
  138. {
  139. printf ("round %Zu: rwlock_destroy failed\n", cnt);
  140. exit (1);
  141. }
  142. }
  143. return 0;
  144. }
  145. #define TEST_FUNCTION do_test ()
  146. #include "../test-skeleton.c"