tst-rwlock9.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #define NWRITERS 15
  24. #define WRITETRIES 10
  25. #define NREADERS 15
  26. #define READTRIES 15
  27. #define TIMEOUT 1000000
  28. #define DELAY 1000000
  29. #ifndef INIT
  30. # define INIT PTHREAD_RWLOCK_WRITER_NONRECURSIVE_INITIALIZER_NP
  31. #endif
  32. static pthread_rwlock_t lock = INIT;
  33. static void *
  34. writer_thread (void *nr)
  35. {
  36. struct timespec ts;
  37. struct timespec delay;
  38. int n;
  39. delay.tv_sec = 0;
  40. delay.tv_nsec = DELAY;
  41. for (n = 0; n < WRITETRIES; ++n)
  42. {
  43. int e;
  44. do
  45. {
  46. struct timeval tv;
  47. (void) gettimeofday (&tv, NULL);
  48. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  49. ts.tv_nsec += 2 * TIMEOUT;
  50. if (ts.tv_nsec >= 1000000000)
  51. {
  52. ts.tv_nsec -= 1000000000;
  53. ++ts.tv_sec;
  54. }
  55. printf ("writer thread %ld tries again\n", (long int) nr);
  56. e = pthread_rwlock_timedwrlock (&lock, &ts);
  57. if (e != 0 && e != ETIMEDOUT)
  58. {
  59. puts ("timedwrlock failed");
  60. exit (1);
  61. }
  62. }
  63. while (e == ETIMEDOUT);
  64. printf ("writer thread %ld succeeded\n", (long int) nr);
  65. nanosleep (&delay, NULL);
  66. if (pthread_rwlock_unlock (&lock) != 0)
  67. {
  68. puts ("unlock for writer failed");
  69. exit (1);
  70. }
  71. printf ("writer thread %ld released\n", (long int) nr);
  72. }
  73. return NULL;
  74. }
  75. static void *
  76. reader_thread (void *nr)
  77. {
  78. struct timespec ts;
  79. struct timespec delay;
  80. int n;
  81. delay.tv_sec = 0;
  82. delay.tv_nsec = DELAY;
  83. for (n = 0; n < READTRIES; ++n)
  84. {
  85. int e;
  86. do
  87. {
  88. struct timeval tv;
  89. (void) gettimeofday (&tv, NULL);
  90. TIMEVAL_TO_TIMESPEC (&tv, &ts);
  91. ts.tv_nsec += TIMEOUT;
  92. if (ts.tv_nsec >= 1000000000)
  93. {
  94. ts.tv_nsec -= 1000000000;
  95. ++ts.tv_sec;
  96. }
  97. printf ("reader thread %ld tries again\n", (long int) nr);
  98. e = pthread_rwlock_timedrdlock (&lock, &ts);
  99. if (e != 0 && e != ETIMEDOUT)
  100. {
  101. puts ("timedrdlock failed");
  102. exit (1);
  103. }
  104. }
  105. while (e == ETIMEDOUT);
  106. printf ("reader thread %ld succeeded\n", (long int) nr);
  107. nanosleep (&delay, NULL);
  108. if (pthread_rwlock_unlock (&lock) != 0)
  109. {
  110. puts ("unlock for reader failed");
  111. exit (1);
  112. }
  113. printf ("reader thread %ld released\n", (long int) nr);
  114. }
  115. return NULL;
  116. }
  117. static int
  118. do_test (void)
  119. {
  120. pthread_t thwr[NWRITERS];
  121. pthread_t thrd[NREADERS];
  122. int n;
  123. void *res;
  124. /* Make standard error the same as standard output. */
  125. dup2 (1, 2);
  126. /* Make sure we see all message, even those on stdout. */
  127. setvbuf (stdout, NULL, _IONBF, 0);
  128. for (n = 0; n < NWRITERS; ++n)
  129. if (pthread_create (&thwr[n], NULL, writer_thread,
  130. (void *) (long int) n) != 0)
  131. {
  132. puts ("writer create failed");
  133. exit (1);
  134. }
  135. for (n = 0; n < NREADERS; ++n)
  136. if (pthread_create (&thrd[n], NULL, reader_thread,
  137. (void *) (long int) n) != 0)
  138. {
  139. puts ("reader create failed");
  140. exit (1);
  141. }
  142. /* Wait for all the threads. */
  143. for (n = 0; n < NWRITERS; ++n)
  144. if (pthread_join (thwr[n], &res) != 0)
  145. {
  146. puts ("writer join failed");
  147. exit (1);
  148. }
  149. for (n = 0; n < NREADERS; ++n)
  150. if (pthread_join (thrd[n], &res) != 0)
  151. {
  152. puts ("reader join failed");
  153. exit (1);
  154. }
  155. return 0;
  156. }
  157. #undef TIMEOUT
  158. #define TIMEOUT 30
  159. #define TEST_FUNCTION do_test ()
  160. #include "../test-skeleton.c"