robust5.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * robust5.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Thread A locks multiple robust mutexes
  37. * Thread B blocks on same mutexes
  38. * Thread A terminates with thread waiting on mutexes
  39. * Thread B awakes and inherits each mutex in turn
  40. * Thread B terminates leaving orphaned mutexes
  41. * Main inherits mutexes, sets consistent and unlocks.
  42. *
  43. * Depends on API functions:
  44. * pthread_create()
  45. * pthread_join()
  46. * pthread_mutex_init()
  47. * pthread_mutex_lock()
  48. * pthread_mutex_unlock()
  49. * pthread_mutex_destroy()
  50. * pthread_mutexattr_init()
  51. * pthread_mutexattr_setrobust()
  52. * pthread_mutexattr_settype()
  53. * pthread_mutexattr_destroy()
  54. */
  55. #include "test.h"
  56. static int lockCount;
  57. static pthread_mutex_t mutex[3];
  58. void * owner(void * arg)
  59. {
  60. assert(pthread_mutex_lock(&mutex[0]) == 0);
  61. lockCount++;
  62. assert(pthread_mutex_lock(&mutex[1]) == 0);
  63. lockCount++;
  64. assert(pthread_mutex_lock(&mutex[2]) == 0);
  65. lockCount++;
  66. return 0;
  67. }
  68. void * inheritor(void * arg)
  69. {
  70. assert(pthread_mutex_lock(&mutex[0]) == EOWNERDEAD);
  71. lockCount++;
  72. assert(pthread_mutex_lock(&mutex[1]) == EOWNERDEAD);
  73. lockCount++;
  74. assert(pthread_mutex_lock(&mutex[2]) == EOWNERDEAD);
  75. lockCount++;
  76. return 0;
  77. }
  78. int
  79. main()
  80. {
  81. pthread_t to, ti;
  82. pthread_mutexattr_t ma;
  83. assert(pthread_mutexattr_init(&ma) == 0);
  84. assert(pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST) == 0);
  85. lockCount = 0;
  86. assert(pthread_mutex_init(&mutex[0], &ma) == 0);
  87. assert(pthread_mutex_init(&mutex[1], &ma) == 0);
  88. assert(pthread_mutex_init(&mutex[2], &ma) == 0);
  89. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  90. assert(pthread_join(to, NULL) == 0);
  91. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  92. assert(pthread_join(ti, NULL) == 0);
  93. assert(lockCount == 6);
  94. assert(pthread_mutex_lock(&mutex[0]) == EOWNERDEAD);
  95. assert(pthread_mutex_consistent(&mutex[0]) == 0);
  96. assert(pthread_mutex_unlock(&mutex[0]) == 0);
  97. assert(pthread_mutex_destroy(&mutex[0]) == 0);
  98. assert(pthread_mutex_lock(&mutex[1]) == EOWNERDEAD);
  99. assert(pthread_mutex_consistent(&mutex[1]) == 0);
  100. assert(pthread_mutex_unlock(&mutex[1]) == 0);
  101. assert(pthread_mutex_destroy(&mutex[1]) == 0);
  102. assert(pthread_mutex_lock(&mutex[2]) == EOWNERDEAD);
  103. assert(pthread_mutex_consistent(&mutex[2]) == 0);
  104. assert(pthread_mutex_unlock(&mutex[2]) == 0);
  105. assert(pthread_mutex_destroy(&mutex[2]) == 0);
  106. assert(pthread_mutexattr_destroy(&ma) == 0);
  107. return 0;
  108. }