tst-rwlock2.c 3.6 KB

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