pthread_cancel.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "pthread_impl.h"
  2. #include "syscall.h"
  3. #include "libc.h"
  4. long __cancel()
  5. {
  6. pthread_t self = __pthread_self();
  7. if (self->canceldisable == PTHREAD_CANCEL_ENABLE || self->cancelasync)
  8. pthread_exit(PTHREAD_CANCELED);
  9. self->canceldisable = PTHREAD_CANCEL_DISABLE;
  10. return -ECANCELED;
  11. }
  12. /* If __syscall_cp_asm has adjusted the stack pointer, it must provide a
  13. * definition of __cp_cancel to undo those adjustments and call __cancel.
  14. * Otherwise, __cancel provides a definition for __cp_cancel. */
  15. weak_alias(__cancel, __cp_cancel);
  16. long __syscall_cp_asm(volatile void *, syscall_arg_t,
  17. syscall_arg_t, syscall_arg_t, syscall_arg_t,
  18. syscall_arg_t, syscall_arg_t, syscall_arg_t);
  19. long __syscall_cp_c(syscall_arg_t nr,
  20. syscall_arg_t u, syscall_arg_t v, syscall_arg_t w,
  21. syscall_arg_t x, syscall_arg_t y, syscall_arg_t z)
  22. {
  23. pthread_t self;
  24. long r;
  25. int st;
  26. if (!libc.has_thread_pointer || (st=(self=__pthread_self())->canceldisable)
  27. && (st==PTHREAD_CANCEL_DISABLE || nr==SYS_close))
  28. return __syscall(nr, u, v, w, x, y, z);
  29. r = __syscall_cp_asm(&self->cancel, nr, u, v, w, x, y, z);
  30. if (r==-EINTR && nr!=SYS_close && self->cancel &&
  31. self->canceldisable != PTHREAD_CANCEL_DISABLE)
  32. r = __cancel();
  33. return r;
  34. }
  35. static void _sigaddset(sigset_t *set, int sig)
  36. {
  37. unsigned s = sig-1;
  38. set->__bits[s/8/sizeof *set->__bits] |= 1UL<<(s&8*sizeof *set->__bits-1);
  39. }
  40. static void cancel_handler(int sig, siginfo_t *si, void *ctx)
  41. {
  42. pthread_t self = __pthread_self();
  43. ucontext_t *uc = ctx;
  44. const char *ip = ((char **)&uc->uc_mcontext)[CANCEL_REG_IP];
  45. extern const char __cp_begin[1], __cp_end[1];
  46. a_barrier();
  47. if (!self->cancel || self->canceldisable == PTHREAD_CANCEL_DISABLE) return;
  48. _sigaddset(&uc->uc_sigmask, SIGCANCEL);
  49. if (self->cancelasync || ip >= __cp_begin && ip < __cp_end) {
  50. ((char **)&uc->uc_mcontext)[CANCEL_REG_IP] = (char *)__cp_cancel;
  51. return;
  52. }
  53. __syscall(SYS_tkill, self->tid, SIGCANCEL);
  54. }
  55. void __testcancel()
  56. {
  57. if (!libc.has_thread_pointer) return;
  58. pthread_t self = __pthread_self();
  59. if (self->cancel && !self->canceldisable)
  60. __cancel();
  61. }
  62. static void init_cancellation()
  63. {
  64. struct sigaction sa = {
  65. .sa_flags = SA_SIGINFO | SA_RESTART,
  66. .sa_sigaction = cancel_handler
  67. };
  68. sigfillset(&sa.sa_mask);
  69. __libc_sigaction(SIGCANCEL, &sa, 0);
  70. }
  71. int pthread_cancel(pthread_t t)
  72. {
  73. static int init;
  74. if (!init) {
  75. init_cancellation();
  76. init = 1;
  77. }
  78. a_store(&t->cancel, 1);
  79. return pthread_kill(t, SIGCANCEL);
  80. }