test-mutexattr-printers.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* Helper program for testing the pthread_mutex_t and pthread_mutexattr_t
  2. pretty printers.
  3. Copyright (C) 2016-2019 Free Software Foundation, Inc.
  4. This file is part of the GNU C Library.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. /* Keep the calls to the pthread_* functions on separate lines to make it easy
  17. to advance through the program using the gdb 'next' command. */
  18. #include <pthread.h>
  19. #define PASS 0
  20. #define FAIL 1
  21. #define PRIOCEILING 42
  22. /* Need these so we don't have lines longer than 79 chars. */
  23. #define SET_TYPE(attr, type) pthread_mutexattr_settype (attr, type)
  24. #define SET_ROBUST(attr, robust) pthread_mutexattr_setrobust (attr, robust)
  25. #define SET_SHARED(attr, shared) pthread_mutexattr_setpshared (attr, shared)
  26. #define SET_PROTOCOL(attr, protocol) \
  27. pthread_mutexattr_setprotocol (attr, protocol)
  28. #define SET_PRIOCEILING(mutex, prioceiling, old_ceiling) \
  29. pthread_mutex_setprioceiling (mutex, prioceiling, old_ceiling)
  30. static int mutex_reinit (pthread_mutex_t *mutex,
  31. const pthread_mutexattr_t *attr);
  32. static int test_settype (pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
  33. static int test_setrobust (pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
  34. static int test_setpshared (pthread_mutex_t *mutex, pthread_mutexattr_t *attr);
  35. static int test_setprotocol (pthread_mutex_t *mutex,
  36. pthread_mutexattr_t *attr);
  37. int
  38. main (void)
  39. {
  40. pthread_mutex_t mutex;
  41. pthread_mutexattr_t attr;
  42. int result = FAIL;
  43. if (pthread_mutexattr_init (&attr) == 0
  44. && pthread_mutex_init (&mutex, NULL) == 0
  45. && test_settype (&mutex, &attr) == PASS
  46. && test_setrobust (&mutex, &attr) == PASS
  47. && test_setpshared (&mutex, &attr) == PASS
  48. && test_setprotocol (&mutex, &attr) == PASS)
  49. result = PASS;
  50. /* Else, one of the pthread_mutex* functions failed. */
  51. return result;
  52. }
  53. /* Destroys MUTEX and re-initializes it using ATTR. */
  54. static int
  55. mutex_reinit (pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
  56. {
  57. int result = FAIL;
  58. if (pthread_mutex_destroy (mutex) == 0
  59. && pthread_mutex_init (mutex, attr) == 0)
  60. result = PASS;
  61. return result;
  62. }
  63. /* Tests setting the mutex type. */
  64. static int
  65. test_settype (pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
  66. {
  67. int result = FAIL;
  68. if (SET_TYPE (attr, PTHREAD_MUTEX_ERRORCHECK) == 0 /* Set type. */
  69. && mutex_reinit (mutex, attr) == 0
  70. && SET_TYPE (attr, PTHREAD_MUTEX_RECURSIVE) == 0
  71. && mutex_reinit (mutex, attr) == 0
  72. && SET_TYPE (attr, PTHREAD_MUTEX_NORMAL) == 0
  73. && mutex_reinit (mutex, attr) == 0)
  74. result = PASS;
  75. return result;
  76. }
  77. /* Tests setting whether the mutex is robust. */
  78. static int
  79. test_setrobust (pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
  80. {
  81. int result = FAIL;
  82. if (SET_ROBUST (attr, PTHREAD_MUTEX_ROBUST) == 0 /* Set robust. */
  83. && mutex_reinit (mutex, attr) == 0
  84. && SET_ROBUST (attr, PTHREAD_MUTEX_STALLED) == 0
  85. && mutex_reinit (mutex, attr) == 0)
  86. result = PASS;
  87. return result;
  88. }
  89. /* Tests setting whether the mutex can be shared between processes. */
  90. static int
  91. test_setpshared (pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
  92. {
  93. int result = FAIL;
  94. if (SET_SHARED (attr, PTHREAD_PROCESS_SHARED) == 0 /* Set shared. */
  95. && mutex_reinit (mutex, attr) == 0
  96. && SET_SHARED (attr, PTHREAD_PROCESS_PRIVATE) == 0
  97. && mutex_reinit (mutex, attr) == 0)
  98. result = PASS;
  99. return result;
  100. }
  101. /* Tests setting the mutex protocol and, for Priority Protect, the Priority
  102. Ceiling. */
  103. static int
  104. test_setprotocol (pthread_mutex_t *mutex, pthread_mutexattr_t *attr)
  105. {
  106. int result = FAIL;
  107. int old_prioceiling;
  108. if (SET_PROTOCOL (attr, PTHREAD_PRIO_INHERIT) == 0 /* Set protocol. */
  109. && mutex_reinit (mutex, attr) == 0
  110. && SET_PROTOCOL (attr, PTHREAD_PRIO_PROTECT) == 0
  111. && mutex_reinit (mutex, attr) == 0
  112. && SET_PRIOCEILING(mutex, PRIOCEILING, &old_prioceiling) == 0
  113. && SET_PROTOCOL (attr, PTHREAD_PRIO_NONE) == 0
  114. && mutex_reinit (mutex, attr) == 0)
  115. result = PASS;
  116. return result;
  117. }