cancel.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Thread cancellation */
  15. #define __FORCE_GLIBC
  16. #include <features.h>
  17. #include <errno.h>
  18. #include "pthread.h"
  19. #include "internals.h"
  20. #include "spinlock.h"
  21. #include "restart.h"
  22. #ifdef __UCLIBC_HAS_RPC__
  23. #include <rpc/rpc.h>
  24. extern void __rpc_thread_destroy(void);
  25. #endif
  26. int pthread_setcancelstate(int state, int * oldstate)
  27. {
  28. pthread_descr self = thread_self();
  29. if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
  30. return EINVAL;
  31. if (oldstate != NULL) *oldstate = THREAD_GETMEM(self, p_cancelstate);
  32. THREAD_SETMEM(self, p_cancelstate, state);
  33. if (THREAD_GETMEM(self, p_canceled) &&
  34. THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
  35. THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  36. pthread_exit(PTHREAD_CANCELED);
  37. return 0;
  38. }
  39. int pthread_setcanceltype(int type, int * oldtype)
  40. {
  41. pthread_descr self = thread_self();
  42. if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS)
  43. return EINVAL;
  44. if (oldtype != NULL) *oldtype = THREAD_GETMEM(self, p_canceltype);
  45. THREAD_SETMEM(self, p_canceltype, type);
  46. if (THREAD_GETMEM(self, p_canceled) &&
  47. THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
  48. THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  49. pthread_exit(PTHREAD_CANCELED);
  50. return 0;
  51. }
  52. int pthread_cancel(pthread_t thread)
  53. {
  54. pthread_handle handle = thread_handle(thread);
  55. int pid;
  56. int dorestart = 0;
  57. pthread_descr th;
  58. pthread_extricate_if *pextricate;
  59. __pthread_lock(&handle->h_lock, NULL);
  60. if (invalid_handle(handle, thread)) {
  61. __pthread_unlock(&handle->h_lock);
  62. return ESRCH;
  63. }
  64. th = handle->h_descr;
  65. if (th->p_canceled) {
  66. __pthread_unlock(&handle->h_lock);
  67. return 0;
  68. }
  69. pextricate = th->p_extricate;
  70. th->p_canceled = 1;
  71. pid = th->p_pid;
  72. /* If the thread has registered an extrication interface, then
  73. invoke the interface. If it returns 1, then we succeeded in
  74. dequeuing the thread from whatever waiting object it was enqueued
  75. with. In that case, it is our responsibility to wake it up.
  76. And also to set the p_woken_by_cancel flag so the woken thread
  77. can tell that it was woken by cancellation. */
  78. if (pextricate != NULL) {
  79. dorestart = pextricate->pu_extricate_func(pextricate->pu_object, th);
  80. th->p_woken_by_cancel = dorestart;
  81. }
  82. __pthread_unlock(&handle->h_lock);
  83. /* If the thread has suspended or is about to, then we unblock it by
  84. issuing a restart, instead of a cancel signal. Otherwise we send
  85. the cancel signal to unblock the thread from a cancellation point,
  86. or to initiate asynchronous cancellation. The restart is needed so
  87. we have proper accounting of restarts; suspend decrements the thread's
  88. resume count, and restart() increments it. This also means that suspend's
  89. handling of the cancel signal is obsolete. */
  90. if (dorestart)
  91. restart(th);
  92. else
  93. kill(pid, __pthread_sig_cancel);
  94. return 0;
  95. }
  96. void pthread_testcancel(void)
  97. {
  98. pthread_descr self = thread_self();
  99. if (THREAD_GETMEM(self, p_canceled)
  100. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
  101. pthread_exit(PTHREAD_CANCELED);
  102. }
  103. void _pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer,
  104. void (*routine)(void *), void * arg)
  105. {
  106. pthread_descr self = thread_self();
  107. buffer->__routine = routine;
  108. buffer->__arg = arg;
  109. buffer->__prev = THREAD_GETMEM(self, p_cleanup);
  110. THREAD_SETMEM(self, p_cleanup, buffer);
  111. }
  112. void _pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer,
  113. int execute)
  114. {
  115. pthread_descr self = thread_self();
  116. if (execute) buffer->__routine(buffer->__arg);
  117. THREAD_SETMEM(self, p_cleanup, buffer->__prev);
  118. }
  119. void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer,
  120. void (*routine)(void *), void * arg)
  121. {
  122. pthread_descr self = thread_self();
  123. buffer->__routine = routine;
  124. buffer->__arg = arg;
  125. buffer->__canceltype = THREAD_GETMEM(self, p_canceltype);
  126. buffer->__prev = THREAD_GETMEM(self, p_cleanup);
  127. THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED);
  128. THREAD_SETMEM(self, p_cleanup, buffer);
  129. }
  130. void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer,
  131. int execute)
  132. {
  133. pthread_descr self = thread_self();
  134. if (execute) buffer->__routine(buffer->__arg);
  135. THREAD_SETMEM(self, p_cleanup, buffer->__prev);
  136. THREAD_SETMEM(self, p_canceltype, buffer->__canceltype);
  137. if (THREAD_GETMEM(self, p_canceled) &&
  138. THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
  139. THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  140. pthread_exit(PTHREAD_CANCELED);
  141. }
  142. void __pthread_perform_cleanup(void)
  143. {
  144. pthread_descr self = thread_self();
  145. struct _pthread_cleanup_buffer * c;
  146. for (c = THREAD_GETMEM(self, p_cleanup); c != NULL; c = c->__prev)
  147. c->__routine(c->__arg);
  148. #ifdef __UCLIBC_HAS_RPC__
  149. /* And the TSD which needs special help. */
  150. if (THREAD_GETMEM(self, p_libc_specific[_LIBC_TSD_KEY_RPC_VARS]) != NULL)
  151. __rpc_thread_destroy ();
  152. #endif
  153. }
  154. #ifndef __PIC__
  155. /* We need a hook to force the cancelation wrappers to be linked in when
  156. static libpthread is used. */
  157. extern const int __pthread_provide_wrappers;
  158. static const int * const __pthread_require_wrappers =
  159. &__pthread_provide_wrappers;
  160. #endif