tst-rwlock14.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* Copyright (C) 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2004.
  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
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the 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; if not, see
  14. <http://www.gnu.org/licenses/>. */
  15. #include <errno.h>
  16. #include <pthread.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. static pthread_barrier_t b;
  21. static pthread_rwlock_t r = PTHREAD_RWLOCK_INITIALIZER;
  22. static void *
  23. tf (void *arg)
  24. {
  25. /* Lock the read-write lock. */
  26. if (pthread_rwlock_wrlock (&r) != 0)
  27. {
  28. puts ("tf: cannot lock rwlock");
  29. exit (EXIT_FAILURE);
  30. }
  31. pthread_t mt = *(pthread_t *) arg;
  32. pthread_barrier_wait (&b);
  33. /* This call will never return. */
  34. pthread_join (mt, NULL);
  35. return NULL;
  36. }
  37. static int
  38. do_test (void)
  39. {
  40. int result = 0;
  41. struct timespec ts;
  42. if (clock_gettime (CLOCK_REALTIME, &ts) != 0)
  43. {
  44. puts ("clock_gettime failed");
  45. return 1;
  46. }
  47. if (pthread_barrier_init (&b, NULL, 2) != 0)
  48. {
  49. puts ("barrier_init failed");
  50. return 1;
  51. }
  52. pthread_t me = pthread_self ();
  53. pthread_t th;
  54. if (pthread_create (&th, NULL, tf, &me) != 0)
  55. {
  56. puts ("create failed");
  57. return 1;
  58. }
  59. /* Wait until the rwlock is locked. */
  60. pthread_barrier_wait (&b);
  61. ts.tv_nsec = -1;
  62. int e = pthread_rwlock_timedrdlock (&r, &ts);
  63. if (e == 0)
  64. {
  65. puts ("first rwlock_timedrdlock did not fail");
  66. result = 1;
  67. }
  68. else if (e != EINVAL)
  69. {
  70. puts ("first rwlock_timedrdlock did not return EINVAL");
  71. result = 1;
  72. }
  73. e = pthread_rwlock_timedwrlock (&r, &ts);
  74. if (e == 0)
  75. {
  76. puts ("first rwlock_timedwrlock did not fail");
  77. result = 1;
  78. }
  79. else if (e != EINVAL)
  80. {
  81. puts ("first rwlock_timedwrlock did not return EINVAL");
  82. result = 1;
  83. }
  84. ts.tv_nsec = 1000000000;
  85. e = pthread_rwlock_timedrdlock (&r, &ts);
  86. if (e == 0)
  87. {
  88. puts ("second rwlock_timedrdlock did not fail");
  89. result = 1;
  90. }
  91. else if (e != EINVAL)
  92. {
  93. puts ("second rwlock_timedrdlock did not return EINVAL");
  94. result = 1;
  95. }
  96. e = pthread_rwlock_timedrdlock (&r, &ts);
  97. if (e == 0)
  98. {
  99. puts ("second rwlock_timedrdlock did not fail");
  100. result = 1;
  101. }
  102. else if (e != EINVAL)
  103. {
  104. puts ("second rwlock_timedrdlock did not return EINVAL");
  105. result = 1;
  106. }
  107. ts.tv_nsec = 0x100001000LL;
  108. if (ts.tv_nsec != 0x100001000LL)
  109. ts.tv_nsec = 2000000000;
  110. e = pthread_rwlock_timedrdlock (&r, &ts);
  111. if (e == 0)
  112. {
  113. puts ("third rwlock_timedrdlock did not fail");
  114. result = 1;
  115. }
  116. else if (e != EINVAL)
  117. {
  118. puts ("third rwlock_timedrdlock did not return EINVAL");
  119. result = 1;
  120. }
  121. e = pthread_rwlock_timedrdlock (&r, &ts);
  122. if (e == 0)
  123. {
  124. puts ("third rwlock_timedrdlock did not fail");
  125. result = 1;
  126. }
  127. else if (e != EINVAL)
  128. {
  129. puts ("third rwlock_timedrdlock did not return EINVAL");
  130. result = 1;
  131. }
  132. if (result == 0)
  133. puts ("no bugs");
  134. return result;
  135. }
  136. #define TEST_FUNCTION do_test ()
  137. #include "../test-skeleton.c"