robust2.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * robust2.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 and unlocks
  41. * Main attempts to lock mutex with unrecovered 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_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;
  58. void * owner(void * arg)
  59. {
  60. assert(pthread_mutex_lock(&mutex) == 0);
  61. lockCount++;
  62. Sleep(200);
  63. return 0;
  64. }
  65. void * inheritor(void * arg)
  66. {
  67. assert(pthread_mutex_lock(&mutex) == EOWNERDEAD);
  68. lockCount++;
  69. assert(pthread_mutex_unlock(&mutex) == 0);
  70. return 0;
  71. }
  72. int
  73. main()
  74. {
  75. pthread_t to, ti;
  76. pthread_mutexattr_t ma;
  77. assert(pthread_mutexattr_init(&ma) == 0);
  78. assert(pthread_mutexattr_setrobust(&ma, PTHREAD_MUTEX_ROBUST) == 0);
  79. /* Default (NORMAL) type */
  80. lockCount = 0;
  81. assert(pthread_mutex_init(&mutex, &ma) == 0);
  82. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  83. Sleep(100);
  84. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  85. assert(pthread_join(to, NULL) == 0);
  86. assert(pthread_join(ti, NULL) == 0);
  87. assert(lockCount == 2);
  88. assert(pthread_mutex_lock(&mutex) == ENOTRECOVERABLE);
  89. assert(pthread_mutex_destroy(&mutex) == 0);
  90. /* NORMAL type */
  91. lockCount = 0;
  92. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_NORMAL) == 0);
  93. assert(pthread_mutex_init(&mutex, &ma) == 0);
  94. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  95. Sleep(100);
  96. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  97. assert(pthread_join(to, NULL) == 0);
  98. assert(pthread_join(ti, NULL) == 0);
  99. assert(lockCount == 2);
  100. assert(pthread_mutex_lock(&mutex) == ENOTRECOVERABLE);
  101. assert(pthread_mutex_destroy(&mutex) == 0);
  102. /* ERRORCHECK type */
  103. lockCount = 0;
  104. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK) == 0);
  105. assert(pthread_mutex_init(&mutex, &ma) == 0);
  106. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  107. Sleep(100);
  108. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  109. assert(pthread_join(to, NULL) == 0);
  110. assert(pthread_join(ti, NULL) == 0);
  111. assert(lockCount == 2);
  112. assert(pthread_mutex_lock(&mutex) == ENOTRECOVERABLE);
  113. assert(pthread_mutex_destroy(&mutex) == 0);
  114. /* RECURSIVE type */
  115. lockCount = 0;
  116. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE) == 0);
  117. assert(pthread_mutex_init(&mutex, &ma) == 0);
  118. assert(pthread_create(&to, NULL, owner, NULL) == 0);
  119. Sleep(100);
  120. assert(pthread_create(&ti, NULL, inheritor, NULL) == 0);
  121. assert(pthread_join(to, NULL) == 0);
  122. assert(pthread_join(ti, NULL) == 0);
  123. assert(lockCount == 2);
  124. assert(pthread_mutex_lock(&mutex) == ENOTRECOVERABLE);
  125. assert(pthread_mutex_destroy(&mutex) == 0);
  126. assert(pthread_mutexattr_destroy(&ma) == 0);
  127. return 0;
  128. }