tst-rwlock9.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 not,
  14. write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  15. Boston, MA 02111-1307, USA. */
  16. #include <errno.h>
  17. #include <error.h>
  18. #include <pthread.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <time.h>
  22. #include <unistd.h>
  23. #include <sys/time.h>
  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. static int
  119. do_test (void)
  120. {
  121. pthread_t thwr[NWRITERS];
  122. pthread_t thrd[NREADERS];
  123. int n;
  124. void *res;
  125. /* Make standard error the same as standard output. */
  126. dup2 (1, 2);
  127. /* Make sure we see all message, even those on stdout. */
  128. setvbuf (stdout, NULL, _IONBF, 0);
  129. for (n = 0; n < NWRITERS; ++n)
  130. if (pthread_create (&thwr[n], NULL, writer_thread,
  131. (void *) (long int) n) != 0)
  132. {
  133. puts ("writer create failed");
  134. exit (1);
  135. }
  136. for (n = 0; n < NREADERS; ++n)
  137. if (pthread_create (&thrd[n], NULL, reader_thread,
  138. (void *) (long int) n) != 0)
  139. {
  140. puts ("reader create failed");
  141. exit (1);
  142. }
  143. /* Wait for all the threads. */
  144. for (n = 0; n < NWRITERS; ++n)
  145. if (pthread_join (thwr[n], &res) != 0)
  146. {
  147. puts ("writer join failed");
  148. exit (1);
  149. }
  150. for (n = 0; n < NREADERS; ++n)
  151. if (pthread_join (thrd[n], &res) != 0)
  152. {
  153. puts ("reader join failed");
  154. exit (1);
  155. }
  156. return 0;
  157. }
  158. #undef TIMEOUT
  159. #define TIMEOUT 30
  160. #define TEST_FUNCTION do_test ()
  161. #include "../test-skeleton.c"