cancel.c 7.7 KB

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