pthread_cancel.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * pthread_cancel.c
  3. *
  4. * Description:
  5. * POSIX thread functions related to thread cancellation.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-embedded (PTE) - POSIX Threads Library for embedded systems
  10. * Copyright(C) 2008 Jason Schmidlapp
  11. *
  12. * Contact Email: [email protected]
  13. *
  14. *
  15. * Based upon Pthreads-win32 - POSIX Threads Library for Win32
  16. * Copyright(C) 1998 John E. Bossom
  17. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  18. *
  19. * Contact Email: [email protected]
  20. *
  21. * The original list of contributors to the Pthreads-win32 project
  22. * is contained in the file CONTRIBUTORS.ptw32 included with the
  23. * source code distribution. The list can also be seen at the
  24. * following World Wide Web location:
  25. * http://sources.redhat.com/pthreads-win32/contributors.html
  26. *
  27. * This library is free software; you can redistribute it and/or
  28. * modify it under the terms of the GNU Lesser General Public
  29. * License as published by the Free Software Foundation; either
  30. * version 2 of the License, or (at your option) any later version.
  31. *
  32. * This library is distributed in the hope that it will be useful,
  33. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  34. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  35. * Lesser General Public License for more details.
  36. *
  37. * You should have received a copy of the GNU Lesser General Public
  38. * License along with this library in the file COPYING.LIB;
  39. * if not, write to the Free Software Foundation, Inc.,
  40. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  41. */
  42. #include "pthread.h"
  43. #include "implement.h"
  44. #include <stdio.h>
  45. int
  46. pthread_cancel (pthread_t thread)
  47. /*
  48. * ------------------------------------------------------
  49. * DOCPUBLIC
  50. * This function requests cancellation of 'thread'.
  51. *
  52. * PARAMETERS
  53. * thread
  54. * reference to an instance of pthread_t
  55. *
  56. *
  57. * DESCRIPTION
  58. * This function requests cancellation of 'thread'.
  59. * NOTE: cancellation is asynchronous; use pthread_join to
  60. * wait for termination of 'thread' if necessary.
  61. *
  62. * RESULTS
  63. * 0 successfully requested cancellation,
  64. * ESRCH no thread found corresponding to 'thread',
  65. * ENOMEM implicit self thread create failed.
  66. * ------------------------------------------------------
  67. */
  68. {
  69. int result;
  70. int cancel_self;
  71. pthread_t self;
  72. pte_thread_t * tp;
  73. result = pthread_kill (thread, 0);
  74. if (0 != result)
  75. {
  76. return result;
  77. }
  78. if ((self = pthread_self ()) == NULL)
  79. {
  80. return ENOMEM;
  81. };
  82. /*
  83. * FIXME!!
  84. *
  85. * Can a thread cancel itself?
  86. *
  87. * The standard doesn't
  88. * specify an error to be returned if the target
  89. * thread is itself.
  90. *
  91. * If it may, then we need to ensure that a thread can't
  92. * deadlock itself trying to cancel itself asyncronously
  93. * (pthread_cancel is required to be an async-cancel
  94. * safe function).
  95. */
  96. cancel_self = pthread_equal (thread, self);
  97. tp = (pte_thread_t *) thread;
  98. /*
  99. * Lock for async-cancel safety.
  100. */
  101. (void) pthread_mutex_lock (&tp->cancelLock);
  102. if (tp->cancelType == PTHREAD_CANCEL_ASYNCHRONOUS
  103. && tp->cancelState == PTHREAD_CANCEL_ENABLE
  104. && tp->state < PThreadStateCanceling)
  105. {
  106. if (cancel_self)
  107. {
  108. tp->state = PThreadStateCanceling;
  109. tp->cancelState = PTHREAD_CANCEL_DISABLE;
  110. (void) pthread_mutex_unlock (&tp->cancelLock);
  111. pte_throw (PTE_EPS_CANCEL);
  112. /* Never reached */
  113. }
  114. else
  115. {
  116. /*
  117. * We don't support asynchronous cancellation for thread other than ourselves.
  118. * as it requires significant platform and OS specific functionality (see below).
  119. *
  120. * We should never get here, as we don't allow the cancellability type to be
  121. * sent to async.
  122. *
  123. * If you really wanted to implement async cancellation, you would probably need to
  124. * do something like the Win32 implement did, which is:
  125. * 1. Suspend the target thread.
  126. * 2. Replace the PC for the target thread to a routine that throws an exception
  127. * or does a longjmp, depending on cleanup method.
  128. * 3. Resume the target thread.
  129. *
  130. * Note that most of the async cancellation code is still in here if anyone
  131. * wanted to add the OS/platform specific stuff.
  132. */
  133. (void) pthread_mutex_unlock (&tp->cancelLock);
  134. result = EPERM;
  135. }
  136. }
  137. else
  138. {
  139. /*
  140. * Set for deferred cancellation.
  141. */
  142. if (tp->state < PThreadStateCancelPending)
  143. {
  144. tp->state = PThreadStateCancelPending;
  145. if (pte_osThreadCancel(tp->threadId) != PTE_OS_OK)
  146. {
  147. result = ESRCH;
  148. }
  149. }
  150. else if (tp->state >= PThreadStateCanceling)
  151. {
  152. result = ESRCH;
  153. }
  154. (void) pthread_mutex_unlock (&tp->cancelLock);
  155. }
  156. return (result);
  157. }