tst-rwlock14.c 3.7 KB

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