tst-rwlock2.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright (C) 2002 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
  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. static int
  19. do_test (void)
  20. {
  21. pthread_rwlock_t r;
  22. int e;
  23. if (pthread_rwlock_init (&r, NULL) != 0)
  24. {
  25. puts ("rwlock_init failed");
  26. return 1;
  27. }
  28. puts ("rwlock_init succeeded");
  29. if (pthread_rwlock_wrlock (&r) != 0)
  30. {
  31. puts ("1st rwlock_wrlock failed");
  32. return 1;
  33. }
  34. puts ("1st rwlock_wrlock succeeded");
  35. e = pthread_rwlock_tryrdlock (&r);
  36. if (e == 0)
  37. {
  38. puts ("rwlock_tryrdlock on rwlock with writer succeeded");
  39. return 1;
  40. }
  41. if (e != EBUSY)
  42. {
  43. puts ("rwlock_tryrdlock on rwlock with writer return value != EBUSY");
  44. return 1;
  45. }
  46. puts ("rwlock_tryrdlock on rwlock with writer failed with EBUSY");
  47. e = pthread_rwlock_trywrlock (&r);
  48. if (e == 0)
  49. {
  50. puts ("rwlock_trywrlock on rwlock with writer succeeded");
  51. return 1;
  52. }
  53. if (e != EBUSY)
  54. {
  55. puts ("rwlock_trywrlock on rwlock with writer return value != EBUSY");
  56. return 1;
  57. }
  58. puts ("rwlock_trywrlock on rwlock with writer failed with EBUSY");
  59. if (pthread_rwlock_unlock (&r) != 0)
  60. {
  61. puts ("1st rwlock_unlock failed");
  62. return 1;
  63. }
  64. puts ("1st rwlock_unlock succeeded");
  65. if (pthread_rwlock_tryrdlock (&r) != 0)
  66. {
  67. puts ("rwlock_tryrdlock on unlocked rwlock failed");
  68. return 1;
  69. }
  70. puts ("rwlock_tryrdlock on unlocked rwlock succeeded");
  71. e = pthread_rwlock_trywrlock (&r);
  72. if (e == 0)
  73. {
  74. puts ("rwlock_trywrlock on rwlock with reader succeeded");
  75. return 1;
  76. }
  77. if (e != EBUSY)
  78. {
  79. puts ("rwlock_trywrlock on rwlock with reader return value != EBUSY");
  80. return 1;
  81. }
  82. puts ("rwlock_trywrlock on rwlock with reader failed with EBUSY");
  83. if (pthread_rwlock_unlock (&r) != 0)
  84. {
  85. puts ("2nd rwlock_unlock failed");
  86. return 1;
  87. }
  88. puts ("2nd rwlock_unlock succeeded");
  89. if (pthread_rwlock_trywrlock (&r) != 0)
  90. {
  91. puts ("rwlock_trywrlock on unlocked rwlock failed");
  92. return 1;
  93. }
  94. puts ("rwlock_trywrlock on unlocked rwlock succeeded");
  95. e = pthread_rwlock_tryrdlock (&r);
  96. if (e == 0)
  97. {
  98. puts ("rwlock_tryrdlock on rwlock with writer succeeded");
  99. return 1;
  100. }
  101. if (e != EBUSY)
  102. {
  103. puts ("rwlock_tryrdlock on rwlock with writer return value != EBUSY");
  104. return 1;
  105. }
  106. puts ("rwlock_tryrdlock on rwlock with writer failed with EBUSY");
  107. if (pthread_rwlock_unlock (&r) != 0)
  108. {
  109. puts ("3rd rwlock_unlock failed");
  110. return 1;
  111. }
  112. puts ("3rd rwlock_unlock succeeded");
  113. if (pthread_rwlock_destroy (&r) != 0)
  114. {
  115. puts ("rwlock_destroy failed");
  116. return 1;
  117. }
  118. puts ("rwlock_destroy succeeded");
  119. return 0;
  120. }
  121. #define TEST_FUNCTION do_test ()
  122. #include "../test-skeleton.c"