pthread_mutex_consistent.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /*
  2. * pthread_mutex_consistent.c
  3. *
  4. * Description:
  5. * This translation unit implements mutual exclusion (mutex) primitives.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  35. */
  36. #if !defined(_UWIN)
  37. /*# include <process.h> */
  38. #endif
  39. #include "pthread.h"
  40. #include "implement.h"
  41. INLINE
  42. int
  43. ptw32_robust_mutex_inherit(pthread_mutex_t * mutex)
  44. {
  45. int result;
  46. pthread_mutex_t mx = *mutex;
  47. ptw32_robust_node_t* robust = mx->robustNode;
  48. switch ((LONG)PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(
  49. (PTW32_INTERLOCKED_LONGPTR)&robust->stateInconsistent,
  50. (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_INCONSISTENT,
  51. (PTW32_INTERLOCKED_LONG)-1 /* The terminating thread sets this */))
  52. {
  53. case -1L:
  54. result = EOWNERDEAD;
  55. break;
  56. case (LONG)PTW32_ROBUST_NOTRECOVERABLE:
  57. result = ENOTRECOVERABLE;
  58. break;
  59. default:
  60. result = 0;
  61. break;
  62. }
  63. return result;
  64. }
  65. /*
  66. * The next two internal support functions depend on only being
  67. * called by the thread that owns the robust mutex. This enables
  68. * us to avoid additional locks.
  69. * Any mutex currently in the thread's robust mutex list is held
  70. * by the thread, again eliminating the need for locks.
  71. * The forward/backward links allow the thread to unlock mutexes
  72. * in any order, not necessarily the reverse locking order.
  73. * This is all possible because it is an error if a thread that
  74. * does not own the [robust] mutex attempts to unlock it.
  75. */
  76. INLINE
  77. void
  78. ptw32_robust_mutex_add(pthread_mutex_t* mutex, pthread_t self)
  79. {
  80. ptw32_robust_node_t** list;
  81. pthread_mutex_t mx = *mutex;
  82. ptw32_thread_t* tp = (ptw32_thread_t*)self.p;
  83. ptw32_robust_node_t* robust = mx->robustNode;
  84. list = &tp->robustMxList;
  85. mx->ownerThread = self;
  86. if (NULL == *list)
  87. {
  88. robust->prev = NULL;
  89. robust->next = NULL;
  90. *list = robust;
  91. }
  92. else
  93. {
  94. robust->prev = NULL;
  95. robust->next = *list;
  96. (*list)->prev = robust;
  97. *list = robust;
  98. }
  99. }
  100. INLINE
  101. void
  102. ptw32_robust_mutex_remove(pthread_mutex_t* mutex, ptw32_thread_t* otp)
  103. {
  104. ptw32_robust_node_t** list;
  105. pthread_mutex_t mx = *mutex;
  106. ptw32_robust_node_t* robust = mx->robustNode;
  107. list = &(((ptw32_thread_t*)mx->ownerThread.p)->robustMxList);
  108. mx->ownerThread.p = otp;
  109. if (robust->next != NULL)
  110. {
  111. robust->next->prev = robust->prev;
  112. }
  113. if (robust->prev != NULL)
  114. {
  115. robust->prev->next = robust->next;
  116. }
  117. if (*list == robust)
  118. {
  119. *list = robust->next;
  120. }
  121. }
  122. int
  123. pthread_mutex_consistent (pthread_mutex_t* mutex)
  124. {
  125. pthread_mutex_t mx = *mutex;
  126. int result = 0;
  127. /*
  128. * Let the system deal with invalid pointers.
  129. */
  130. if (mx == NULL)
  131. {
  132. return EINVAL;
  133. }
  134. if (mx->kind >= 0
  135. || (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_INCONSISTENT != PTW32_INTERLOCKED_COMPARE_EXCHANGE_LONG(
  136. (PTW32_INTERLOCKED_LONGPTR)&mx->robustNode->stateInconsistent,
  137. (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_CONSISTENT,
  138. (PTW32_INTERLOCKED_LONG)PTW32_ROBUST_INCONSISTENT))
  139. {
  140. result = EINVAL;
  141. }
  142. return (result);
  143. }