w32_CancelableWait.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * w32_CancelableWait.c
  3. *
  4. * Description:
  5. * This translation unit implements miscellaneous thread functions.
  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. #include "pthread.h"
  37. #include "implement.h"
  38. static INLINE int
  39. ptw32_cancelable_wait (HANDLE waitHandle, DWORD timeout)
  40. /*
  41. * -------------------------------------------------------------------
  42. * This provides an extra hook into the pthread_cancel
  43. * mechanism that will allow you to wait on a Windows handle and make it a
  44. * cancellation point. This function blocks until the given WIN32 handle is
  45. * signaled or pthread_cancel has been called. It is implemented using
  46. * WaitForMultipleObjects on 'waitHandle' and a manually reset WIN32
  47. * event used to implement pthread_cancel.
  48. *
  49. * Given this hook it would be possible to implement more of the cancellation
  50. * points.
  51. * -------------------------------------------------------------------
  52. */
  53. {
  54. int result;
  55. pthread_t self;
  56. ptw32_thread_t * sp;
  57. HANDLE handles[2];
  58. DWORD nHandles = 1;
  59. DWORD status;
  60. handles[0] = waitHandle;
  61. self = pthread_self();
  62. sp = (ptw32_thread_t *) self.p;
  63. if (sp != NULL)
  64. {
  65. /*
  66. * Get cancelEvent handle
  67. */
  68. if (sp->cancelState == PTHREAD_CANCEL_ENABLE)
  69. {
  70. if ((handles[1] = sp->cancelEvent) != NULL)
  71. {
  72. nHandles++;
  73. }
  74. }
  75. }
  76. else
  77. {
  78. handles[1] = NULL;
  79. }
  80. status = WaitForMultipleObjects (nHandles, handles, PTW32_FALSE, timeout);
  81. switch (status - WAIT_OBJECT_0)
  82. {
  83. case 0:
  84. /*
  85. * Got the handle.
  86. * In the event that both handles are signalled, the smallest index
  87. * value (us) is returned. As it has been arranged, this ensures that
  88. * we don't drop a signal that we should act on (i.e. semaphore,
  89. * mutex, or condition variable etc).
  90. */
  91. result = 0;
  92. break;
  93. case 1:
  94. /*
  95. * Got cancel request.
  96. * In the event that both handles are signaled, the cancel will
  97. * be ignored (see case 0 comment).
  98. */
  99. ResetEvent (handles[1]);
  100. if (sp != NULL)
  101. {
  102. ptw32_mcs_local_node_t stateLock;
  103. /*
  104. * Should handle POSIX and implicit POSIX threads..
  105. * Make sure we haven't been async-canceled in the meantime.
  106. */
  107. ptw32_mcs_lock_acquire (&sp->stateLock, &stateLock);
  108. if (sp->state < PThreadStateCanceling)
  109. {
  110. sp->state = PThreadStateCanceling;
  111. sp->cancelState = PTHREAD_CANCEL_DISABLE;
  112. ptw32_mcs_lock_release (&stateLock);
  113. ptw32_throw (PTW32_EPS_CANCEL);
  114. /* Never reached */
  115. }
  116. ptw32_mcs_lock_release (&stateLock);
  117. }
  118. /* Should never get to here. */
  119. result = EINVAL;
  120. break;
  121. default:
  122. if (status == WAIT_TIMEOUT)
  123. {
  124. result = ETIMEDOUT;
  125. }
  126. else
  127. {
  128. result = EINVAL;
  129. }
  130. break;
  131. }
  132. return (result);
  133. } /* CancelableWait */
  134. int
  135. pthreadCancelableWait (HANDLE waitHandle)
  136. {
  137. return (ptw32_cancelable_wait (waitHandle, INFINITE));
  138. }
  139. int
  140. pthreadCancelableTimedWait (HANDLE waitHandle, DWORD timeout)
  141. {
  142. return (ptw32_cancelable_wait (waitHandle, timeout));
  143. }