tst-rwlock8.c 3.4 KB

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