tst-rwlock9.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* Test program for timedout read/write lock functions.
  2. Copyright (C) 2000, 2003 Free Software Foundation, Inc.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2000.
  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 License as
  6. published by the Free Software Foundation; either version 2.1 of the
  7. 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; see the file COPYING.LIB. If
  14. not, see <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <error.h>
  17. #include <pthread.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <unistd.h>
  22. #include <sys/time.h>
  23. #if defined(__GLIBC__) || defined(__UCLIBC__)
  24. #define NWRITERS 15
  25. #define WRITETRIES 10
  26. #define NREADERS 15
  27. #define READTRIES 15
  28. #define TIMEOUT 1000000
  29. #define DELAY 1000000
  30. #ifndef INIT
  31. # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
  32. #endif
  33. static pthread_rwlock_t lock = INIT;
  34. static void *
  35. writer_thread (void *nr)
  36. {
  37. struct timespec ts;
  38. struct timespec delay;
  39. int n;
  40. delay.tv_sec = 0;
  41. delay.tv_nsec = DELAY;
  42. for (n = 0; n < WRITETRIES; ++n)
  43. {
  44. int e;
  45. do
  46. {
  47. struct timeval tv;
  48. (void) gettimeofday (&tv, NULL);
  49. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  50. ts.tv_nsec += 2 * TIMEOUT;
  51. if (ts.tv_nsec >= 1000000000)
  52. {
  53. ts.tv_nsec -= 1000000000;
  54. ++ts.tv_sec;
  55. }
  56. printf ("writer thread %ld tries again\n", (long int) nr);
  57. e = pthread_rwlock_timedwrlock (&lock, &ts);
  58. if (e != 0 && e != ETIMEDOUT)
  59. {
  60. puts ("timedwrlock failed");
  61. exit (1);
  62. }
  63. }
  64. while (e == ETIMEDOUT);
  65. printf ("writer thread %ld succeeded\n", (long int) nr);
  66. nanosleep (&delay, NULL);
  67. if (pthread_rwlock_unlock (&lock) != 0)
  68. {
  69. puts ("unlock for writer failed");
  70. exit (1);
  71. }
  72. printf ("writer thread %ld released\n", (long int) nr);
  73. }
  74. return NULL;
  75. }
  76. static void *
  77. reader_thread (void *nr)
  78. {
  79. struct timespec ts;
  80. struct timespec delay;
  81. int n;
  82. delay.tv_sec = 0;
  83. delay.tv_nsec = DELAY;
  84. for (n = 0; n < READTRIES; ++n)
  85. {
  86. int e;
  87. do
  88. {
  89. struct timeval tv;
  90. (void) gettimeofday (&tv, NULL);
  91. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  92. ts.tv_nsec += TIMEOUT;
  93. if (ts.tv_nsec >= 1000000000)
  94. {
  95. ts.tv_nsec -= 1000000000;
  96. ++ts.tv_sec;
  97. }
  98. printf ("reader thread %ld tries again\n", (long int) nr);
  99. e = pthread_rwlock_timedrdlock (&lock, &ts);
  100. if (e != 0 && e != ETIMEDOUT)
  101. {
  102. puts ("timedrdlock failed");
  103. exit (1);
  104. }
  105. }
  106. while (e == ETIMEDOUT);
  107. printf ("reader thread %ld succeeded\n", (long int) nr);
  108. nanosleep (&delay, NULL);
  109. if (pthread_rwlock_unlock (&lock) != 0)
  110. {
  111. puts ("unlock for reader failed");
  112. exit (1);
  113. }
  114. printf ("reader thread %ld released\n", (long int) nr);
  115. }
  116. return NULL;
  117. }
  118. #endif
  119. static int
  120. do_test (void)
  121. {
  122. #if defined(__GLIBC__) || defined(__UCLIBC__)
  123. pthread_t thwr[NWRITERS];
  124. pthread_t thrd[NREADERS];
  125. int n;
  126. void *res;
  127. /* Make standard error the same as standard output. */
  128. dup2 (1, 2);
  129. /* Make sure we see all message, even those on stdout. */
  130. setvbuf (stdout, NULL, _IONBF, 0);
  131. for (n = 0; n < NWRITERS; ++n)
  132. if (pthread_create (&thwr[n], NULL, writer_thread,
  133. (void *) (long int) n) != 0)
  134. {
  135. puts ("writer create failed");
  136. exit (1);
  137. }
  138. for (n = 0; n < NREADERS; ++n)
  139. if (pthread_create (&thrd[n], NULL, reader_thread,
  140. (void *) (long int) n) != 0)
  141. {
  142. puts ("reader create failed");
  143. exit (1);
  144. }
  145. /* Wait for all the threads. */
  146. for (n = 0; n < NWRITERS; ++n)
  147. if (pthread_join (thwr[n], &res) != 0)
  148. {
  149. puts ("writer join failed");
  150. exit (1);
  151. }
  152. for (n = 0; n < NREADERS; ++n)
  153. if (pthread_join (thrd[n], &res) != 0)
  154. {
  155. puts ("reader join failed");
  156. exit (1);
  157. }
  158. return 0;
  159. #else
  160. return 23;
  161. #endif
  162. }
  163. #undef TIMEOUT
  164. #define TIMEOUT 30
  165. #define TEST_FUNCTION do_test ()
  166. #include "../test-skeleton.c"