tst-rwlock8.c 3.6 KB

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