cancel.c 7.7 KB

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