robust3.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * robust3.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. * For all robust mutex types.
  37. * Thread A locks mutex
  38. * Thread B blocks on mutex
  39. * Thread A terminates with threads waiting on robust mutex
  40. * Thread B awakes and inherits mutex, sets consistent and unlocks
  41. * Main acquires mutex with recovered state.
  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_consistent()
  50. * pthread_mutex_destroy()
  51. * pthread_mutexattr_init()
  52. * pthread_mutexattr_setrobust()
  53. * pthread_mutexattr_settype()
  54. * pthread_mutexattr_destroy()
  55. */
  56. #include "test.h"
  57. static int lockCount;
  58. static pthread_mutex_t mutex;
  59. void * owner(void * arg)
  60. {
  61. assert(pthread_mutex_lock(&mutex) == 0);
  62. lockCount++;
  63. Sleep(200);
  64. return 0;
  65. }
  66. void * inheritor(void * arg)
  67. {
  68. assert(pthread_mutex_lock(&mutex) == EOWNERDEAD);
  69. lockCount++;
  70. assert(pthread_mutex_consistent(&mutex) == 0);
  71. assert(pthread_mutex_unlock(&mutex) == 0);
  72. return 0;
  73. }
  74. int
  75. main()
  76. {
  77. pthread_t to, ti;
  78. pthread_mutexattr_t ma;
  79. assert(pthread_mutexattr_init(&ma) == 0);
  80. assert(pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST) == 0);
  81. /* Default (NORMAL) type */
  82. lockCount = 0;
  83. assert(pthread_mutex_init(&mutex, &ma) == 0);
  84. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  85. Sleep(100);
  86. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  87. assert(pthread_join(to, NULL) == 0);
  88. assert(pthread_join(ti, NULL) == 0);
  89. assert(lockCount == 2);
  90. assert(pthread_mutex_lock(&mutex) == 0);
  91. assert(pthread_mutex_unlock(&mutex) == 0);
  92. assert(pthread_mutex_destroy(&mutex) == 0);
  93. /* NORMAL type */
  94. lockCount = 0;
  95. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_NORMAL) == 0);
  96. assert(pthread_mutex_init(&mutex, &ma) == 0);
  97. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  98. Sleep(100);
  99. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  100. assert(pthread_join(to, NULL) == 0);
  101. assert(pthread_join(ti, NULL) == 0);
  102. assert(lockCount == 2);
  103. assert(pthread_mutex_lock(&mutex) == 0);
  104. assert(pthread_mutex_unlock(&mutex) == 0);
  105. assert(pthread_mutex_destroy(&mutex) == 0);
  106. /* ERRORCHECK type */
  107. lockCount = 0;
  108. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK) == 0);
  109. assert(pthread_mutex_init(&mutex, &ma) == 0);
  110. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  111. Sleep(100);
  112. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  113. assert(pthread_join(to, NULL) == 0);
  114. assert(pthread_join(ti, NULL) == 0);
  115. assert(lockCount == 2);
  116. assert(pthread_mutex_lock(&mutex) == 0);
  117. assert(pthread_mutex_unlock(&mutex) == 0);
  118. assert(pthread_mutex_destroy(&mutex) == 0);
  119. /* RECURSIVE type */
  120. lockCount = 0;
  121. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE) == 0);
  122. assert(pthread_mutex_init(&mutex, &ma) == 0);
  123. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  124. Sleep(100);
  125. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  126. assert(pthread_join(to, NULL) == 0);
  127. assert(pthread_join(ti, NULL) == 0);
  128. assert(lockCount == 2);
  129. assert(pthread_mutex_lock(&mutex) == 0);
  130. assert(pthread_mutex_unlock(&mutex) == 0);
  131. assert(pthread_mutex_destroy(&mutex) == 0);
  132. assert(pthread_mutexattr_destroy(&ma) == 0);
  133. return 0;
  134. }