cancel.c 7.1 KB

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