test-rwlock-printers.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Helper program for testing the pthread_rwlock_t pretty printer.
  2. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  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. /* Keep the calls to the pthread_* functions on separate lines to make it easy
  16. to advance through the program using the gdb 'next' command. */
  17. #include <pthread.h>
  18. #define PASS 0
  19. #define FAIL 1
  20. static int test_locking_reader (pthread_rwlock_t *rwlock);
  21. static int test_locking_writer (pthread_rwlock_t *rwlock);
  22. int
  23. main (void)
  24. {
  25. pthread_rwlock_t rwlock;
  26. int result = FAIL;
  27. if (test_locking_reader (&rwlock) == PASS
  28. && test_locking_writer (&rwlock) == PASS)
  29. result = PASS;
  30. /* Else, one of the pthread_rwlock* functions failed. */
  31. return result;
  32. }
  33. /* Tests locking the rwlock multiple times as a reader. */
  34. static int
  35. test_locking_reader (pthread_rwlock_t *rwlock)
  36. {
  37. int result = FAIL;
  38. if (pthread_rwlock_init (rwlock, NULL) == 0
  39. && pthread_rwlock_rdlock (rwlock) == 0 /* Test locking (reader). */
  40. && pthread_rwlock_rdlock (rwlock) == 0
  41. && pthread_rwlock_rdlock (rwlock) == 0
  42. && pthread_rwlock_unlock (rwlock) == 0
  43. && pthread_rwlock_unlock (rwlock) == 0
  44. && pthread_rwlock_unlock (rwlock) == 0
  45. && pthread_rwlock_destroy (rwlock) == 0)
  46. result = PASS;
  47. return result;
  48. }
  49. /* Tests locking the rwlock as a writer. */
  50. static int
  51. test_locking_writer (pthread_rwlock_t *rwlock)
  52. {
  53. int result = FAIL;
  54. if (pthread_rwlock_init (rwlock, NULL) == 0
  55. && pthread_rwlock_wrlock (rwlock) == 0 /* Test locking (writer). */
  56. && pthread_rwlock_unlock (rwlock) == 0
  57. && pthread_rwlock_destroy (rwlock) == 0)
  58. result = PASS;
  59. return result;
  60. }