robust4.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. * robust4.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 in different orderings
  38. * Thread A terminates with thread waiting on mutexes
  39. * Thread B awakes and inherits each mutex in turn, sets consistent and unlocks
  40. * Main acquires mutexes with recovered state.
  41. *
  42. * Depends on API functions:
  43. * pthread_create()
  44. * pthread_join()
  45. * pthread_mutex_init()
  46. * pthread_mutex_lock()
  47. * pthread_mutex_unlock()
  48. * pthread_mutex_destroy()
  49. * pthread_mutexattr_init()
  50. * pthread_mutexattr_setrobust()
  51. * pthread_mutexattr_settype()
  52. * pthread_mutexattr_destroy()
  53. */
  54. #include "test.h"
  55. static int lockCount;
  56. static pthread_mutex_t mutex[3];
  57. void * owner(void * arg)
  58. {
  59. assert(pthread_mutex_lock(&mutex[0]) == 0);
  60. lockCount++;
  61. assert(pthread_mutex_lock(&mutex[1]) == 0);
  62. lockCount++;
  63. assert(pthread_mutex_lock(&mutex[2]) == 0);
  64. lockCount++;
  65. Sleep(200);
  66. return 0;
  67. }
  68. void * inheritor(void * arg)
  69. {
  70. int* o = (int*)arg;
  71. assert(pthread_mutex_lock(&mutex[o[0]]) == EOWNERDEAD);
  72. lockCount++;
  73. assert(pthread_mutex_lock(&mutex[o[1]]) == EOWNERDEAD);
  74. lockCount++;
  75. assert(pthread_mutex_lock(&mutex[o[2]]) == EOWNERDEAD);
  76. lockCount++;
  77. assert(pthread_mutex_consistent(&mutex[o[2]]) == 0);
  78. assert(pthread_mutex_consistent(&mutex[o[1]]) == 0);
  79. assert(pthread_mutex_consistent(&mutex[o[0]]) == 0);
  80. assert(pthread_mutex_unlock(&mutex[o[2]]) == 0);
  81. assert(pthread_mutex_unlock(&mutex[o[1]]) == 0);
  82. assert(pthread_mutex_unlock(&mutex[o[0]]) == 0);
  83. return 0;
  84. }
  85. int
  86. main()
  87. {
  88. pthread_t to, ti;
  89. pthread_mutexattr_t ma;
  90. int order[3];
  91. assert(pthread_mutexattr_init(&ma) == 0);
  92. assert(pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST) == 0);
  93. order[0]=0;
  94. order[1]=1;
  95. order[2]=2;
  96. lockCount = 0;
  97. assert(pthread_mutex_init(&mutex[0], &ma) == 0);
  98. assert(pthread_mutex_init(&mutex[1], &ma) == 0);
  99. assert(pthread_mutex_init(&mutex[2], &ma) == 0);
  100. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  101. Sleep(100);
  102. assert(pthread_create(&ti, NULL, inheritor, (void *)order) == 0);
  103. assert(pthread_join(to, NULL) == 0);
  104. assert(pthread_join(ti, NULL) == 0);
  105. assert(lockCount == 6);
  106. assert(pthread_mutex_lock(&mutex[0]) == 0);
  107. assert(pthread_mutex_unlock(&mutex[0]) == 0);
  108. assert(pthread_mutex_destroy(&mutex[0]) == 0);
  109. assert(pthread_mutex_lock(&mutex[1]) == 0);
  110. assert(pthread_mutex_unlock(&mutex[1]) == 0);
  111. assert(pthread_mutex_destroy(&mutex[1]) == 0);
  112. assert(pthread_mutex_lock(&mutex[2]) == 0);
  113. assert(pthread_mutex_unlock(&mutex[2]) == 0);
  114. assert(pthread_mutex_destroy(&mutex[2]) == 0);
  115. order[0]=1;
  116. order[1]=0;
  117. order[2]=2;
  118. lockCount = 0;
  119. assert(pthread_mutex_init(&mutex[0], &ma) == 0);
  120. assert(pthread_mutex_init(&mutex[1], &ma) == 0);
  121. assert(pthread_mutex_init(&mutex[2], &ma) == 0);
  122. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  123. Sleep(100);
  124. assert(pthread_create(&ti, NULL, inheritor, (void *)order) == 0);
  125. assert(pthread_join(to, NULL) == 0);
  126. assert(pthread_join(ti, NULL) == 0);
  127. assert(lockCount == 6);
  128. assert(pthread_mutex_lock(&mutex[0]) == 0);
  129. assert(pthread_mutex_unlock(&mutex[0]) == 0);
  130. assert(pthread_mutex_destroy(&mutex[0]) == 0);
  131. assert(pthread_mutex_lock(&mutex[1]) == 0);
  132. assert(pthread_mutex_unlock(&mutex[1]) == 0);
  133. assert(pthread_mutex_destroy(&mutex[1]) == 0);
  134. assert(pthread_mutex_lock(&mutex[2]) == 0);
  135. assert(pthread_mutex_unlock(&mutex[2]) == 0);
  136. assert(pthread_mutex_destroy(&mutex[2]) == 0);
  137. order[0]=0;
  138. order[1]=2;
  139. order[2]=1;
  140. lockCount = 0;
  141. assert(pthread_mutex_init(&mutex[0], &ma) == 0);
  142. assert(pthread_mutex_init(&mutex[1], &ma) == 0);
  143. assert(pthread_mutex_init(&mutex[2], &ma) == 0);
  144. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  145. Sleep(100);
  146. assert(pthread_create(&ti, NULL, inheritor, (void *)order) == 0);
  147. assert(pthread_join(to, NULL) == 0);
  148. assert(pthread_join(ti, NULL) == 0);
  149. assert(lockCount == 6);
  150. assert(pthread_mutex_lock(&mutex[0]) == 0);
  151. assert(pthread_mutex_unlock(&mutex[0]) == 0);
  152. assert(pthread_mutex_destroy(&mutex[0]) == 0);
  153. assert(pthread_mutex_lock(&mutex[1]) == 0);
  154. assert(pthread_mutex_unlock(&mutex[1]) == 0);
  155. assert(pthread_mutex_destroy(&mutex[1]) == 0);
  156. assert(pthread_mutex_lock(&mutex[2]) == 0);
  157. assert(pthread_mutex_unlock(&mutex[2]) == 0);
  158. assert(pthread_mutex_destroy(&mutex[2]) == 0);
  159. order[0]=2;
  160. order[1]=1;
  161. order[2]=0;
  162. lockCount = 0;
  163. assert(pthread_mutex_init(&mutex[0], &ma) == 0);
  164. assert(pthread_mutex_init(&mutex[1], &ma) == 0);
  165. assert(pthread_mutex_init(&mutex[2], &ma) == 0);
  166. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  167. Sleep(100);
  168. assert(pthread_create(&ti, NULL, inheritor, (void *)order) == 0);
  169. assert(pthread_join(to, NULL) == 0);
  170. assert(pthread_join(ti, NULL) == 0);
  171. assert(lockCount == 6);
  172. assert(pthread_mutex_lock(&mutex[0]) == 0);
  173. assert(pthread_mutex_unlock(&mutex[0]) == 0);
  174. assert(pthread_mutex_destroy(&mutex[0]) == 0);
  175. assert(pthread_mutex_lock(&mutex[1]) == 0);
  176. assert(pthread_mutex_unlock(&mutex[1]) == 0);
  177. assert(pthread_mutex_destroy(&mutex[1]) == 0);
  178. assert(pthread_mutex_lock(&mutex[2]) == 0);
  179. assert(pthread_mutex_unlock(&mutex[2]) == 0);
  180. assert(pthread_mutex_destroy(&mutex[2]) == 0);
  181. assert(pthread_mutexattr_destroy(&ma) == 0);
  182. return 0;
  183. }