cancel.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include <bits/stackinfo.h>
  27. #include <stdio.h>
  28. #ifdef _STACK_GROWS_DOWN
  29. # define FRAME_LEFT(frame, other) ((char *) frame >= (char *) other)
  30. #elif _STACK_GROWS_UP
  31. # define FRAME_LEFT(frame, other) ((char *) frame <= (char *) other)
  32. #else
  33. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  34. #endif
  35. libpthread_hidden_proto(pthread_setcancelstate)
  36. libpthread_hidden_proto(pthread_setcanceltype)
  37. int pthread_setcancelstate(int state, int * oldstate)
  38. {
  39. pthread_descr self = thread_self();
  40. if (state < PTHREAD_CANCEL_ENABLE || state > PTHREAD_CANCEL_DISABLE)
  41. return EINVAL;
  42. if (oldstate != NULL) *oldstate = THREAD_GETMEM(self, p_cancelstate);
  43. THREAD_SETMEM(self, p_cancelstate, state);
  44. if (THREAD_GETMEM(self, p_canceled) &&
  45. THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
  46. THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  47. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  48. return 0;
  49. }
  50. libpthread_hidden_def(pthread_setcancelstate)
  51. int pthread_setcanceltype(int type, int * oldtype)
  52. {
  53. pthread_descr self = thread_self();
  54. if (type < PTHREAD_CANCEL_DEFERRED || type > PTHREAD_CANCEL_ASYNCHRONOUS)
  55. return EINVAL;
  56. if (oldtype != NULL) *oldtype = THREAD_GETMEM(self, p_canceltype);
  57. THREAD_SETMEM(self, p_canceltype, type);
  58. if (THREAD_GETMEM(self, p_canceled) &&
  59. THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
  60. THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  61. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  62. return 0;
  63. }
  64. libpthread_hidden_def(pthread_setcanceltype)
  65. int pthread_cancel(pthread_t thread)
  66. {
  67. pthread_handle handle = thread_handle(thread);
  68. int pid;
  69. int dorestart = 0;
  70. pthread_descr th;
  71. pthread_extricate_if *pextricate;
  72. int already_canceled;
  73. __pthread_lock(&handle->h_lock, NULL);
  74. if (invalid_handle(handle, thread)) {
  75. __pthread_unlock(&handle->h_lock);
  76. return ESRCH;
  77. }
  78. th = handle->h_descr;
  79. already_canceled = th->p_canceled;
  80. th->p_canceled = 1;
  81. if (th->p_cancelstate == PTHREAD_CANCEL_DISABLE || already_canceled) {
  82. __pthread_unlock(&handle->h_lock);
  83. return 0;
  84. }
  85. pextricate = th->p_extricate;
  86. pid = th->p_pid;
  87. /* If the thread has registered an extrication interface, then
  88. invoke the interface. If it returns 1, then we succeeded in
  89. dequeuing the thread from whatever waiting object it was enqueued
  90. with. In that case, it is our responsibility to wake it up.
  91. And also to set the p_woken_by_cancel flag so the woken thread
  92. can tell that it was woken by cancellation. */
  93. if (pextricate != NULL) {
  94. dorestart = pextricate->pu_extricate_func(pextricate->pu_object, th);
  95. th->p_woken_by_cancel = dorestart;
  96. }
  97. __pthread_unlock(&handle->h_lock);
  98. /* If the thread has suspended or is about to, then we unblock it by
  99. issuing a restart, instead of a cancel signal. Otherwise we send
  100. the cancel signal to unblock the thread from a cancellation point,
  101. or to initiate asynchronous cancellation. The restart is needed so
  102. we have proper accounting of restarts; suspend decrements the thread's
  103. resume count, and restart() increments it. This also means that suspend's
  104. handling of the cancel signal is obsolete. */
  105. if (dorestart)
  106. restart(th);
  107. else
  108. kill(pid, __pthread_sig_cancel);
  109. return 0;
  110. }
  111. void pthread_testcancel(void)
  112. {
  113. pthread_descr self = thread_self();
  114. if (THREAD_GETMEM(self, p_canceled)
  115. && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE)
  116. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  117. }
  118. void _pthread_cleanup_push(struct _pthread_cleanup_buffer * buffer,
  119. void (*routine)(void *), void * arg)
  120. {
  121. pthread_descr self = thread_self();
  122. buffer->__routine = routine;
  123. buffer->__arg = arg;
  124. buffer->__prev = THREAD_GETMEM(self, p_cleanup);
  125. if (buffer->__prev != NULL && FRAME_LEFT (buffer, buffer->__prev))
  126. buffer->__prev = NULL;
  127. THREAD_SETMEM(self, p_cleanup, buffer);
  128. }
  129. void _pthread_cleanup_pop(struct _pthread_cleanup_buffer * buffer,
  130. int execute)
  131. {
  132. pthread_descr self = thread_self();
  133. if (execute) buffer->__routine(buffer->__arg);
  134. THREAD_SETMEM(self, p_cleanup, buffer->__prev);
  135. }
  136. void _pthread_cleanup_push_defer(struct _pthread_cleanup_buffer * buffer,
  137. void (*routine)(void *), void * arg)
  138. {
  139. pthread_descr self = thread_self();
  140. buffer->__routine = routine;
  141. buffer->__arg = arg;
  142. buffer->__canceltype = THREAD_GETMEM(self, p_canceltype);
  143. buffer->__prev = THREAD_GETMEM(self, p_cleanup);
  144. if (buffer->__prev != NULL && FRAME_LEFT (buffer, buffer->__prev))
  145. buffer->__prev = NULL;
  146. THREAD_SETMEM(self, p_canceltype, PTHREAD_CANCEL_DEFERRED);
  147. THREAD_SETMEM(self, p_cleanup, buffer);
  148. }
  149. strong_alias(_pthread_cleanup_push_defer,__pthread_cleanup_push_defer)
  150. void _pthread_cleanup_pop_restore(struct _pthread_cleanup_buffer * buffer,
  151. int execute)
  152. {
  153. pthread_descr self = thread_self();
  154. if (execute) buffer->__routine(buffer->__arg);
  155. THREAD_SETMEM(self, p_cleanup, buffer->__prev);
  156. THREAD_SETMEM(self, p_canceltype, buffer->__canceltype);
  157. if (THREAD_GETMEM(self, p_canceled) &&
  158. THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE &&
  159. THREAD_GETMEM(self, p_canceltype) == PTHREAD_CANCEL_ASYNCHRONOUS)
  160. __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
  161. }
  162. strong_alias(_pthread_cleanup_pop_restore,__pthread_cleanup_pop_restore)
  163. void __pthread_perform_cleanup(char *currentframe)
  164. {
  165. pthread_descr self = thread_self();
  166. struct _pthread_cleanup_buffer * c;
  167. for (c = THREAD_GETMEM(self, p_cleanup); c != NULL; c = c->__prev)
  168. {
  169. #if _STACK_GROWS_DOWN
  170. if ((char *) c <= currentframe)
  171. break;
  172. #elif _STACK_GROWS_UP
  173. if ((char *) c >= currentframe)
  174. break;
  175. #else
  176. # error "Define either _STACK_GROWS_DOWN or _STACK_GROWS_UP"
  177. #endif
  178. c->__routine(c->__arg);
  179. }
  180. #ifdef __UCLIBC_HAS_RPC__
  181. /* And the TSD which needs special help. */
  182. if (THREAD_GETMEM(self, p_libc_specific[_LIBC_TSD_KEY_RPC_VARS]) != NULL)
  183. __rpc_thread_destroy ();
  184. #endif
  185. }
  186. #ifndef __PIC__
  187. /* We need a hook to force the cancelation wrappers to be linked in when
  188. static libpthread is used. */
  189. extern const char __pthread_provide_wrappers;
  190. static const char *const __pthread_require_wrappers =
  191. &__pthread_provide_wrappers;
  192. #endif