pthread_cancel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * pthread_cancel.c
  3. *
  4. * Description:
  5. * POSIX thread functions related to thread cancellation.
  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. #include "context.h"
  39. static void
  40. ptw32_cancel_self (void)
  41. {
  42. ptw32_throw (PTW32_EPS_CANCEL);
  43. /* Never reached */
  44. }
  45. static void CALLBACK
  46. ptw32_cancel_callback (ULONG_PTR unused)
  47. {
  48. ptw32_throw (PTW32_EPS_CANCEL);
  49. /* Never reached */
  50. }
  51. /*
  52. * ptw32_RegisterCancelation() -
  53. * Must have args of same type as QueueUserAPCEx because this function
  54. * is a substitute for QueueUserAPCEx if it's not available.
  55. */
  56. DWORD
  57. ptw32_RegisterCancelation (PAPCFUNC unused1, HANDLE threadH, DWORD unused2)
  58. {
  59. CONTEXT context;
  60. context.ContextFlags = CONTEXT_CONTROL;
  61. GetThreadContext (threadH, &context);
  62. PTW32_PROGCTR (context) = (DWORD_PTR) ptw32_cancel_self;
  63. SetThreadContext (threadH, &context);
  64. return 0;
  65. }
  66. int
  67. pthread_cancel (pthread_t thread)
  68. /*
  69. * ------------------------------------------------------
  70. * DOCPUBLIC
  71. * This function requests cancellation of 'thread'.
  72. *
  73. * PARAMETERS
  74. * thread
  75. * reference to an instance of pthread_t
  76. *
  77. *
  78. * DESCRIPTION
  79. * This function requests cancellation of 'thread'.
  80. * NOTE: cancellation is asynchronous; use pthread_join to
  81. * wait for termination of 'thread' if necessary.
  82. *
  83. * RESULTS
  84. * 0 successfully requested cancellation,
  85. * ESRCH no thread found corresponding to 'thread',
  86. * ENOMEM implicit self thread create failed.
  87. * ------------------------------------------------------
  88. */
  89. {
  90. int result;
  91. int cancel_self;
  92. pthread_t self;
  93. ptw32_thread_t * tp;
  94. ptw32_mcs_local_node_t stateLock;
  95. result = pthread_kill (thread, 0);
  96. if (0 != result)
  97. {
  98. return result;
  99. }
  100. if ((self = pthread_self ()).p == NULL)
  101. {
  102. return ENOMEM;
  103. };
  104. /*
  105. * For self cancellation we need to ensure that a thread can't
  106. * deadlock itself trying to cancel itself asynchronously
  107. * (pthread_cancel is required to be an async-cancel
  108. * safe function).
  109. */
  110. cancel_self = pthread_equal (thread, self);
  111. tp = (ptw32_thread_t *) thread.p;
  112. /*
  113. * Lock for async-cancel safety.
  114. */
  115. ptw32_mcs_lock_acquire (&tp->stateLock, &stateLock);
  116. if (tp->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS
  117. && tp->cancelState == PTHREAD_CANCEL_ENABLE
  118. && tp->state < PThreadStateCanceling)
  119. {
  120. if (cancel_self)
  121. {
  122. tp->state = PThreadStateCanceling;
  123. tp->cancelState = PTHREAD_CANCEL_DISABLE;
  124. ptw32_mcs_lock_release (&stateLock);
  125. ptw32_throw (PTW32_EPS_CANCEL);
  126. /* Never reached */
  127. }
  128. else
  129. {
  130. HANDLE threadH = tp->threadH;
  131. SuspendThread (threadH);
  132. if (WaitForSingleObject (threadH, 0) == WAIT_TIMEOUT)
  133. {
  134. tp->state = PThreadStateCanceling;
  135. tp->cancelState = PTHREAD_CANCEL_DISABLE;
  136. /*
  137. * If alertdrv and QueueUserAPCEx is available then the following
  138. * will result in a call to QueueUserAPCEx with the args given, otherwise
  139. * this will result in a call to ptw32_RegisterCancelation and only
  140. * the threadH arg will be used.
  141. */
  142. ptw32_register_cancelation ((PAPCFUNC)ptw32_cancel_callback, threadH, 0);
  143. ptw32_mcs_lock_release (&stateLock);
  144. ResumeThread (threadH);
  145. }
  146. }
  147. }
  148. else
  149. {
  150. /*
  151. * Set for deferred cancellation.
  152. */
  153. if (tp->state < PThreadStateCancelPending)
  154. {
  155. tp->state = PThreadStateCancelPending;
  156. if (!SetEvent (tp->cancelEvent))
  157. {
  158. result = ESRCH;
  159. }
  160. }
  161. else if (tp->state >= PThreadStateCanceling)
  162. {
  163. result = ESRCH;
  164. }
  165. ptw32_mcs_lock_release (&stateLock);
  166. }
  167. return (result);
  168. }